private static void checkIterator(Iterator<Vector.Element> nzIter, double[] values) { while (nzIter.hasNext()) { Vector.Element elt = nzIter.next(); assertEquals( (elt.index()) + " Value: " + values[elt.index()] + " does not equal: " + elt.get(), values[elt.index()], elt.get(), 0.0); } }
public static void writeVector(DataOutput out, Vector vector, boolean laxPrecision) throws IOException { boolean dense = vector.isDense(); boolean sequential = vector.isSequentialAccess(); boolean named = vector instanceof NamedVector; out.writeByte( (dense ? FLAG_DENSE : 0) | (sequential ? FLAG_SEQUENTIAL : 0) | (named ? FLAG_NAMED : 0) | (laxPrecision ? FLAG_LAX_PRECISION : 0)); Varint.writeUnsignedVarInt(vector.size(), out); if (dense) { for (Vector.Element element : vector) { if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } else { Varint.writeUnsignedVarInt(vector.getNumNondefaultElements(), out); Iterator<Vector.Element> iter = vector.iterateNonZero(); if (sequential) { int lastIndex = 0; while (iter.hasNext()) { Vector.Element element = iter.next(); int thisIndex = element.index(); // Delta-code indices: Varint.writeUnsignedVarInt(thisIndex - lastIndex, out); lastIndex = thisIndex; if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } else { while (iter.hasNext()) { Vector.Element element = iter.next(); Varint.writeUnsignedVarInt(element.index(), out); if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } } if (named) { String name = ((NamedVector) vector).getName(); out.writeUTF(name == null ? "" : name); } }
public static VectorWritable merge(Iterator<VectorWritable> vectors) { Vector accumulator = vectors.next().get(); while (vectors.hasNext()) { VectorWritable v = vectors.next(); if (v != null) { Iterator<Vector.Element> nonZeroElements = v.get().iterateNonZero(); while (nonZeroElements.hasNext()) { Vector.Element nonZeroElement = nonZeroElements.next(); accumulator.setQuick(nonZeroElement.index(), nonZeroElement.get()); } } } return new VectorWritable(accumulator); }
public void testIteratorSet() throws CloneNotSupportedException { Vector clone = test.clone(); Iterator<Vector.Element> it = clone.iterateNonZero(); while (it.hasNext()) { Vector.Element e = it.next(); e.set(e.get() * 2.0); } it = clone.iterateNonZero(); while (it.hasNext()) { Vector.Element e = it.next(); assertEquals(test.get(e.index()) * 2.0, e.get()); } clone = test.clone(); it = clone.iterator(); while (it.hasNext()) { Vector.Element e = it.next(); e.set(e.get() * 2.0); } it = clone.iterator(); while (it.hasNext()) { Vector.Element e = it.next(); assertEquals(test.get(e.index()) * 2.0, e.get()); } }