예제 #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;
 }
예제 #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;
  }
예제 #3
0
 /**
  * Copies values from a {@code SparseVector} into another vector
  *
  * @param destination The {@code Vector to copy values into.
  * @param source The {@code @SparseVector} to copy values from.
  */
 private static void copyFromSparseVector(IntegerVector destination, IntegerVector source) {
   int[] nonZeroIndices = ((SparseVector) source).getNonZeroIndices();
   for (int index : nonZeroIndices) destination.set(index, source.get(index));
 }