SparseFieldVector implements FieldVector<T>, Serializable { public T walkInDefaultOrder(final FieldVectorPreservingVisitor<T> visitor) { final int dim = getDimension(); visitor.start(dim, 0, dim - 1); for (int i = 0; i < dim; i++) { visitor.visit(i, getEntry(i)); } return visitor.end(); } SparseFieldVector(Field<T> field);  SparseFieldVector(Field<T> field, int dimension); protected  SparseFieldVector(SparseFieldVector<T> v, int resize);  SparseFieldVector(Field<T> field, int dimension, int expectedSize);  SparseFieldVector(Field<T> field, T[] values);  SparseFieldVector(SparseFieldVector<T> v); FieldVector<T> add(SparseFieldVector<T> v); FieldVector<T> append(SparseFieldVector<T> v); FieldVector<T> append(FieldVector<T> v); FieldVector<T> append(T d); FieldVector<T> copy(); T dotProduct(FieldVector<T> v); FieldVector<T> ebeDivide(FieldVector<T> v); FieldVector<T> ebeMultiply(FieldVector<T> v); @Deprecated T[] getData(); int getDimension(); T getEntry(int index); Field<T> getField(); FieldVector<T> getSubVector(int index, int n); FieldVector<T> mapAdd(T d); FieldVector<T> mapAddToSelf(T d); FieldVector<T> mapDivide(T d); FieldVector<T> mapDivideToSelf(T d); FieldVector<T> mapInv(); FieldVector<T> mapInvToSelf(); FieldVector<T> mapMultiply(T d); FieldVector<T> mapMultiplyToSelf(T d); FieldVector<T> mapSubtract(T d); FieldVector<T> mapSubtractToSelf(T d); FieldMatrix<T> outerProduct(SparseFieldVector<T> v); FieldMatrix<T> outerProduct(FieldVector<T> v); FieldVector<T> projection(FieldVector<T> v); void set(T value); void setEntry(int index, T value); void setSubVector(int index, FieldVector<T> v); SparseFieldVector<T> subtract(SparseFieldVector<T> v); FieldVector<T> subtract(FieldVector<T> v); T[] toArray(); FieldVector<T> add(FieldVector<T> v); T walkInDefaultOrder(final FieldVectorPreservingVisitor<T> visitor); T walkInDefaultOrder(final FieldVectorPreservingVisitor<T> visitor,
                                final int start, final int end); T walkInOptimizedOrder(final FieldVectorPreservingVisitor<T> visitor); T walkInOptimizedOrder(final FieldVectorPreservingVisitor<T> visitor,
                                  final int start, final int end); T walkInDefaultOrder(final FieldVectorChangingVisitor<T> visitor); T walkInDefaultOrder(final FieldVectorChangingVisitor<T> visitor,
                                final int start, final int end); T walkInOptimizedOrder(final FieldVectorChangingVisitor<T> visitor); T walkInOptimizedOrder(final FieldVectorChangingVisitor<T> visitor,
                                  final int start, final int end); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void testWalkInDefaultOrderPreservingVisitor1() { final Fraction[] data = new Fraction[] { Fraction.ZERO, Fraction.ONE, Fraction.ZERO, Fraction.ZERO, Fraction.TWO, Fraction.ZERO, Fraction.ZERO, Fraction.ZERO, new Fraction(3) }; final SparseFieldVector<Fraction> v = new SparseFieldVector<Fraction>(field, data); final FieldVectorPreservingVisitor<Fraction> visitor; visitor = new FieldVectorPreservingVisitor<Fraction>() { private int expectedIndex; public void visit(final int actualIndex, final Fraction actualValue) { Assert.assertEquals(expectedIndex, actualIndex); Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue); ++expectedIndex; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(0, actualStart); Assert.assertEquals(data.length - 1, actualEnd); expectedIndex = 0; } public Fraction end() { return Fraction.ZERO; } }; v.walkInDefaultOrder(visitor); }
@Test public void testWalkInDefaultOrderPreservingVisitor2() { final SparseFieldVector<Fraction> v = create(5); final FieldVectorPreservingVisitor<Fraction> visitor; visitor = new FieldVectorPreservingVisitor<Fraction>() { public void visit(int index, Fraction value) { } public void start(int dimension, int start, int end) { } public Fraction end() { return Fraction.ZERO; } }; try { v.walkInDefaultOrder(visitor, -1, 4); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 5, 4); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 0, -1); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 0, 5); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 4, 0); Assert.fail(); } catch (NumberIsTooSmallException e) { } }
@Test public void testWalkInDefaultOrderPreservingVisitor3() { final Fraction[] data = new Fraction[] { Fraction.ZERO, Fraction.ONE, Fraction.ZERO, Fraction.ZERO, Fraction.TWO, Fraction.ZERO, Fraction.ZERO, Fraction.ZERO, new Fraction(3) }; final SparseFieldVector<Fraction> v = new SparseFieldVector<Fraction>(field, data); final int expectedStart = 2; final int expectedEnd = 7; final FieldVectorPreservingVisitor<Fraction> visitor; visitor = new FieldVectorPreservingVisitor<Fraction>() { private int expectedIndex; public void visit(final int actualIndex, final Fraction actualValue) { Assert.assertEquals(expectedIndex, actualIndex); Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue); ++expectedIndex; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(expectedStart, actualStart); Assert.assertEquals(expectedEnd, actualEnd); expectedIndex = expectedStart; } public Fraction end() { return Fraction.ZERO; } }; v.walkInDefaultOrder(visitor, expectedStart, expectedEnd); }
@Test public void testWalkInDefaultOrderChangingVisitor2() { final SparseFieldVector<Fraction> v = create(5); final FieldVectorChangingVisitor<Fraction> visitor; visitor = new FieldVectorChangingVisitor<Fraction>() { public Fraction visit(int index, Fraction value) { return Fraction.ZERO; } public void start(int dimension, int start, int end) { } public Fraction end() { return Fraction.ZERO; } }; try { v.walkInDefaultOrder(visitor, -1, 4); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 5, 4); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 0, -1); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 0, 5); Assert.fail(); } catch (OutOfRangeException e) { } try { v.walkInDefaultOrder(visitor, 4, 0); Assert.fail(); } catch (NumberIsTooSmallException e) { } }