You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2017/05/22 15:45:56 UTC
svn commit: r1795826 - in
/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math:
ArrayVector.java SequenceVector.java Vector.java
Author: desruisseaux
Date: Mon May 22 15:45:56 2017
New Revision: 1795826
URL: http://svn.apache.org/viewvc?rev=1795826&view=rev
Log:
Add a Vector.doubleValues() method for inter-operability with APIs working with arrays.
Modified:
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java?rev=1795826&r1=1795825&r2=1795826&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java [UTF-8] Mon May 22 15:45:56 2017
@@ -153,7 +153,7 @@ abstract class ArrayVector<E extends Num
int i = 0;
double v;
do if (i >= length) {
- return new Floats(toFloatArray());
+ return new Floats(floatValues());
} while (!(Math.abs((v = doubleValue(i++)) - (float) v) > tolerance)); // Use '!' for accepting NaN.
/*
* Same try than above loop, but now using base 10 representation.
@@ -161,7 +161,7 @@ abstract class ArrayVector<E extends Num
*/
i = 0;
do if (i >= length) {
- return new Decimal(toFloatArray());
+ return new Decimal(floatValues());
} while (!(Math.abs((v = doubleValue(i++)) - DecimalFunctions.floatToDouble((float) v)) > tolerance));
}
}
@@ -169,17 +169,6 @@ abstract class ArrayVector<E extends Num
}
/**
- * Returns a copy of current data as a floating point array.
- */
- float[] toFloatArray() {
- final float[] copy = new float[size()];
- for (int i=0; i<copy.length; i++) {
- copy[i] = (float) doubleValue(i);
- }
- return copy;
- }
-
- /**
* Default implementation for the convenience of direct sub-types.
*/
@Override
@@ -269,11 +258,6 @@ abstract class ArrayVector<E extends Num
return old;
}
- /** Returns a copy of current data as a floating point array. */
- @Override float[] toFloatArray() {
- return Numerics.copyAsFloats(array);
- }
-
/** Finds the minimum and maximum values in the array or in a subset of the array. */
@Override NumberRange<Double> range(final IntSupplier indices, int n) {
double min = Double.POSITIVE_INFINITY;
@@ -285,6 +269,16 @@ abstract class ArrayVector<E extends Num
}
return NumberRange.create(min, true, max, true);
}
+
+ /** Returns a copy of current data as a floating point array. */
+ @Override public double[] doubleValues() {
+ return array.clone();
+ }
+
+ /** Returns a copy of current data as a floating point array. */
+ @Override public float[] floatValues() {
+ return Numerics.copyAsFloats(array);
+ }
}
/**
@@ -358,6 +352,11 @@ abstract class ArrayVector<E extends Num
NumberRange<?> createRange(final float min, final float max) {
return NumberRange.create(min, true, max, true);
}
+
+ /** Returns a copy of current data as a floating point array. */
+ @Override public final float[] floatValues() {
+ return array.clone();
+ }
}
/**
Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java?rev=1795826&r1=1795825&r2=1795826&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java [UTF-8] Mon May 22 15:45:56 2017
@@ -16,6 +16,7 @@
*/
package org.apache.sis.math;
+import java.util.Arrays;
import java.io.Serializable;
import org.apache.sis.measure.NumberRange;
import org.apache.sis.util.ArgumentChecks;
@@ -86,6 +87,32 @@ abstract class SequenceVector extends Ve
return this;
}
+ /**
+ * Creates the sequence as a floating point array.
+ */
+ @Override
+ public double[] doubleValues() {
+ if (increment(0).doubleValue() == 0) {
+ final double[] array = new double[size()];
+ Arrays.fill(array, doubleValue(0));
+ return array;
+ }
+ return super.doubleValues();
+ }
+
+ /**
+ * Creates the sequence as a floating point array.
+ */
+ @Override
+ public float[] floatValues() {
+ if (increment(0).doubleValue() == 0) {
+ final float[] array = new float[size()];
+ Arrays.fill(array, floatValue(0));
+ return array;
+ }
+ return super.floatValues();
+ }
+
/**
* A vector which is a sequence of increasing or decreasing {@code double} values.
Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java?rev=1795826&r1=1795825&r2=1795826&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java [UTF-8] Mon May 22 15:45:56 2017
@@ -262,6 +262,8 @@ public abstract class Vector extends Abs
* @throws IndexOutOfBoundsException if the given index is out of bounds.
* @throws NullPointerException if the value is {@code null} (never happen if this vector wraps an array of primitive type).
* @throws NumberFormatException if the value is stored as a {@code String} and can not be parsed.
+ *
+ * @see #doubleValues()
*/
public abstract double doubleValue(int index);
@@ -275,6 +277,8 @@ public abstract class Vector extends Abs
* @throws IndexOutOfBoundsException if the given index is out of bounds.
* @throws NullPointerException if the value is {@code null} (never happen if this vector wraps an array of primitive type).
* @throws NumberFormatException if the value is stored as a {@code String} and can not be parsed.
+ *
+ * @see #floatValues()
*/
public abstract float floatValue(int index);
@@ -388,6 +392,8 @@ public abstract class Vector extends Abs
* @param index the index in the [0 … {@linkplain #size() size}-1] range.
* @return a string representation of the value at the given index (may be {@code null}).
* @throws IndexOutOfBoundsException if the given index is out of bounds.
+ *
+ * @see #toString()
*/
public abstract String stringValue(int index);
@@ -989,9 +995,49 @@ public abstract class Vector extends Abs
}
/**
+ * Copies all values in an array of double precision floating point numbers.
+ * This method is for inter-operability with APIs requiring an array of primitive type.
+ *
+ * <p>The default implementation invokes {@link #doubleValue(int)} for all indices from 0 inclusive
+ * to {@link #size()} exclusive. Subclasses may override with more efficient implementation.</p>
+ *
+ * @return a copy of all floating point values in this vector.
+ *
+ * @see #doubleValue(int)
+ */
+ public double[] doubleValues() {
+ final double[] array = new double[size()];
+ for (int i=0; i<array.length; i++) {
+ array[i] = doubleValue(i);
+ }
+ return array;
+ }
+
+ /**
+ * Copies all values in an array of single precision floating point numbers.
+ * This method is for inter-operability with APIs requiring an array of primitive type.
+ *
+ * <p>The default implementation invokes {@link #floatValue(int)} for all indices from 0 inclusive
+ * to {@link #size()} exclusive. Subclasses may override with more efficient implementation.</p>
+ *
+ * @return a copy of all floating point values in this vector.
+ *
+ * @see #floatValue(int)
+ */
+ public float[] floatValues() {
+ final float[] copy = new float[size()];
+ for (int i=0; i<copy.length; i++) {
+ copy[i] = (float) doubleValue(i);
+ }
+ return copy;
+ }
+
+ /**
* Returns a string representation of this vector.
*
* @return a string representation of this vector.
+ *
+ * @see #stringValue(int)
*/
@Override
public String toString() {