List of usage examples for java.util Vector.Builder add
public void add(int index, E element)
From source file:org.briljantframework.data.vector.Vectors.java
/** * <p>/*from w w w . j a va2s .c om*/ * Split {@code vector} into {@code chunks}. Handles the case when {@code vector.size()} is not * evenly dividable by chunks by making some chunks larger. * </p> * * <p> * This implementation is lazy, i.e. chunking is done 'on-the-fly'. To get a list, {@code new * ArrayList<>(Vectors.split(vec, 10))} * </p> * * <p> * Ensures that {@code vector.getType()} is preserved. * </p> * * @param vector the vector * @param chunks the number of chunks * @return a collection of {@code chunk} chunks */ public static Collection<Vector> split(Vector vector, int chunks) { Check.argument(vector.size() >= chunks, "size must be shorter than chunks"); if (vector.size() == chunks) { return Collections.singleton(vector); } int bin = vector.size() / chunks; int remainder = vector.size() % chunks; return new AbstractCollection<Vector>() { @Override public Iterator<Vector> iterator() { return new Iterator<Vector>() { private int current = 0; private int remainders = 0; @Override public boolean hasNext() { return current < vector.size(); } @Override public Vector next() { int binSize = bin; if (remainders < remainder) { remainders++; binSize += 1; } Vector.Builder builder = vector.newBuilder(); for (int i = 0; i < binSize; i++) { builder.add(vector, current++); } return builder.build(); } }; } @Override public int size() { return chunks; } }; }
From source file:org.briljantframework.data.vector.Vectors.java
/** * <p>// ww w . j a v a 2 s. c o m * Returns a vector consisting of the unique values in {@code vectors} * * <p> * For example, given {@code a, b} and {@code c} * * <pre> * { * @code * Vector a = new IntVector(1, 2, 3, 4); * Vector b = new IntVector(2, 3, 4, 5); * Vector c = new IntVector(3, 4, 5, 6); * * Vector d = Vectors.unique(a, b, c); * // d == [1,2,3,4,5,6]; * } * </pre> */ public static Vector unique(Vector... vectors) { vectors = Objects.requireNonNull(vectors); Check.argument(vectors.length > 0); Vector.Builder builder = vectors[0].newBuilder(); Set<Object> taken = new HashSet<>(); for (Vector vector : vectors) { for (int i = 0; i < vector.size(); i++) { Object value = vector.loc().get(Object.class, i); if (!taken.contains(value)) { taken.add(value); builder.add(vector, i); } } } return builder.build(); }