Exemplo n.º 1
0
 /**
  * Returns {@code true} if the two integer vectors are equal to one another. Two {@code Vector}
  * insances are considered equal if they contain the same number of elements and all corresponding
  * pairs of {@code int} values are equal.
  */
 public static boolean equals(IntegerVector v1, IntegerVector v2) {
   if (v1.length() == v2.length()) {
     int length = v1.length();
     for (int i = 0; i < length; ++i) {
       if (v1.get(i) != v2.get(i)) return false;
     }
     return true;
   }
   return false;
 }
Exemplo n.º 2
0
  /**
   * Creates a copy of a given {@code IntegerVector} with the same type as the original.
   *
   * @param source The {@code Vector} to copy.
   * @return A copy of {@code source} with the same type.
   */
  public static IntegerVector copyOf(IntegerVector source) {
    IntegerVector result = null;

    if (source instanceof TernaryVector) {
      TernaryVector v = (TernaryVector) source;
      int[] pos = v.positiveDimensions();
      int[] neg = v.negativeDimensions();
      result =
          new TernaryVector(
              source.length(), Arrays.copyOf(pos, pos.length), Arrays.copyOf(neg, neg.length));
    } else if (source instanceof SparseVector) {
      result = new SparseHashIntegerVector(source.length());
      copyFromSparseVector(result, source);
    } else {
      result = new DenseIntVector(source.length());
      for (int i = 0; i < source.length(); ++i) result.set(i, source.get(i));
    }
    return result;
  }