Пример #1
0
  /**
   * Method size returns a new Tuple instance of the given size with the given Comparable as its
   * element values.
   *
   * @param size of type int
   * @param value of type Comparable
   * @return Tuple
   */
  public static Tuple size(int size, Comparable value) {
    Tuple result = new Tuple();

    for (int i = 0; i < size; i++) result.add(value);

    return result;
  }
Пример #2
0
  /**
   * Creates a new Tuple from the given positions, but sets the values in the current tuple to null.
   *
   * @param pos of type int[]
   * @return Tuple
   */
  Tuple extract(int[] pos) {
    Tuple results = new Tuple();

    for (int i : pos) results.add(elements.set(i, null));

    return results;
  }
Пример #3
0
  /**
   * Method get will return a new Tuple instace populated with element values from the given array
   * of positions.
   *
   * @param pos of type int[]
   * @return Tuple
   */
  public Tuple get(int[] pos) {
    if (pos == null || pos.length == 0) return new Tuple(this);

    Tuple results = new Tuple();

    for (int i : pos) results.add(elements.get(i));

    return results;
  }
Пример #4
0
  /**
   * Method parse will parse the {@link #print()} String representation of a Tuple instance and
   * return a new Tuple instance.
   *
   * @param string of type String
   * @return Tuple
   */
  public static Tuple parse(String string) {
    if (string == null || string.length() == 0) return null;

    string = string.replaceAll("^ *\\[*", "");
    string = string.replaceAll("\\]* *$", "");

    Scanner scanner = new Scanner(new StringReader(string));
    scanner.useDelimiter("(' *, *')|(^ *')|(' *$)");

    Tuple result = new Tuple();

    while (scanner.hasNext()) {
      if (scanner.hasNextInt()) result.add(scanner.nextInt());
      else if (scanner.hasNextDouble()) result.add(scanner.nextDouble());
      else result.add(scanner.next());
    }

    scanner.close();

    return result;
  }
Пример #5
0
  /**
   * Method remove removes the values specified by the given pos array and returns a new Tuple
   * containing the removed values.
   *
   * @param pos of type int[]
   * @return Tuple
   */
  public Tuple remove(int[] pos) {
    verifyModifiable();

    // calculate offsets to apply when removing values from elements
    int offset[] = new int[pos.length];

    for (int i = 0; i < pos.length; i++) {
      offset[i] = 0;

      for (int j = 0; j < i; j++) {
        if (pos[j] < pos[i]) offset[i]++;
      }
    }

    Tuple results = new Tuple();

    for (int i = 0; i < pos.length; i++) results.add(elements.remove(pos[i] - offset[i]));

    return results;
  }