sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1763456 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/math/ main/java/org/apache/sis/util/collection/ test/java/org/apache/sis/math/
Date Wed, 05 Oct 2016 14:39:48 GMT
Author: desruisseaux
Date: Wed Oct  5 14:39:48 2016
New Revision: 1763456

URL: http://svn.apache.org/viewvc?rev=1763456&view=rev
Log:
Complete the Vector.compress() method and add tests.

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/Vector.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.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=1763456&r1=1763455&r2=1763456&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] Wed Oct  5 14:39:48 2016
@@ -83,9 +83,10 @@ abstract class ArrayVector<E extends Num
      * Returns a vector with the same data than this vector but encoded in a more compact
way,
      * or {@code this} if this method can not do better than current {@code Vector} instance.
      */
+    @Override
     public final Vector compress(final double tolerance) {
         final Vector vec = super.compress(tolerance);
-        if (vec == this) {
+        if (vec == this && !isEmpty()) {
             if (isInteger()) {
                 /*
                  * For integer values, verify if we can pack the data into a smaller type.
@@ -141,12 +142,44 @@ abstract class ArrayVector<E extends Num
                         return new PackedVector(this, min, Math.toIntExact(delta));
                     }
                 }
+            } else if (!Float.class.equals(getElementType())) {
+                /*
+                 * For floating point types, verify if values are equivalent to 'float' values.
+                 * There is two different ways to pad extra fraction digits in 'double' values:
+                 * with zero fraction digits in base 2 representation (the standard Java
cast),
+                 * or with zero fraction digits in base 10 representation.
+                 */
+                final int length = size();
+                int i = 0;
+                double v;
+                do if (i >= length) {
+                    return new Floats(toFloatArray());
+                } while (!(Math.abs((v = doubleValue(i++)) - (float) v) > tolerance));
   // Use '!' for accepting NaN.
+                /*
+                 * Same try than above loop, but now using base 10 representation.
+                 * This is a more costly computation.
+                 */
+                i = 0;
+                do if (i >= length) {
+                    return new Decimal(toFloatArray());
+                } while (!(Math.abs((v = doubleValue(i++)) - DecimalFunctions.floatToDouble((float)
v)) > tolerance));
             }
         }
         return vec;
     }
 
     /**
+     * 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
@@ -236,6 +269,11 @@ 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;
@@ -290,7 +328,7 @@ abstract class ArrayVector<E extends Num
         /** Returns the value at the given index. */
         @Override public       double doubleValue(int index) {return array[index];}
         @Override public final float   floatValue(int index) {return array[index];}
-        @Override public final Number         get(int index) {return array[index];}
+        @Override public       Number         get(int index) {return array[index];}
 
         /** Sets the value at the given index. */
         @Override public final Number set(final int index, final Number value) {
@@ -341,6 +379,11 @@ abstract class ArrayVector<E extends Num
             return DecimalFunctions.floatToDouble(super.floatValue(index));
         }
 
+        /** Returns the value at the given index. */
+        @Override public Number get(final int index) {
+            return doubleValue(index);
+        }
+
         /** Creates a range from the given minimum and maximum values. */
         @Override NumberRange<?> createRange(final float min, final float max) {
             return NumberRange.create(DecimalFunctions.floatToDouble(min), true,

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=1763456&r1=1763455&r2=1763456&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]
Wed Oct  5 14:39:48 2016
@@ -87,6 +87,8 @@ import static org.apache.sis.util.Argume
  * @since   0.8
  * @version 0.8
  * @module
+ *
+ * @see org.apache.sis.util.collection.IntegerList
  */
 public abstract class Vector extends AbstractList<Number> implements RandomAccess {
     /**
@@ -390,8 +392,13 @@ public abstract class Vector extends Abs
 
     /**
      * Returns the number at the given index, or {@code null} if none.
-     * If non-null, the object returned by this method will be an instance
-     * of the class returned by {@link #getElementType()} or a sub-class.
+     * The object returned by this method is usually an instance of the class returned by
{@link #getElementType()},
+     * but may also be an instance of a wider type if this is necessary for representing
the values.
+     *
+     * <div class="note"><b>Example:</b>
+     * if {@link #getElementType()} returns {@code Byte.class} but {@link #isUnsigned()}
returns {@code true},
+     * then this method may return instances of {@link Short} since that type is the smallest
Java primitive
+     * type capable to hold byte values in the [128 … 255] range.</div>
      *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index (may be {@code null}).

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java?rev=1763456&r1=1763455&r2=1763456&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
[UTF-8] Wed Oct  5 14:39:48 2016
@@ -37,6 +37,8 @@ import org.apache.sis.util.ArgumentCheck
  * @since   0.7
  * @version 0.7
  * @module
+ *
+ * @see org.apache.sis.math.Vector
  */
 public class IntegerList extends AbstractList<Integer> implements RandomAccess, Serializable,
Cloneable {
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java?rev=1763456&r1=1763455&r2=1763456&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java [UTF-8]
Wed Oct  5 14:39:48 2016
@@ -21,7 +21,7 @@ import org.apache.sis.test.DependsOnMeth
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.opengis.test.Assert.*;
 
 
 /**
@@ -189,6 +189,7 @@ public final strictfp class VectorTest e
      */
     @Test
     @DependsOnMethod("testFloatArray")
+    @SuppressWarnings("UnnecessaryBoxing")
     public void testConcatenate() {
         final float[] array = new float[40];
         for (int i=0; i<array.length; i++) {
@@ -350,4 +351,79 @@ public final strictfp class VectorTest e
             assertEquals(message, 9, range.getMaxDouble(), STRICT);
         }
     }
+
+    /**
+     * Tests {@link Vector#compress(double)}.
+     */
+    @Test
+    @DependsOnMethod({"testRange", "testIncrement"})
+    public void testCompress() {
+        /*
+         * Values that can be not be compressed further. We use a byte[] array
+         * with values different enough for requiring all 8 bits of byte type.
+         */
+        Vector vec =  Vector.create(new byte[] {30, 120, -50, -120}, false);
+        Vector compressed = vec.compress(0);
+        assertSame(vec, compressed);
+        /*
+         * Values that can be compressed as signed bytes.
+         */
+        vec =  Vector.create(new double[] {30, 120, -50, -120}, false);
+        assertNotSame(vec, compressed = vec.compress(0));
+        assertEquals("elementType", Byte.class, compressed.getElementType());
+        assertFalse("isUnsigned()", compressed.isUnsigned());
+        assertContentEquals(vec, compressed);
+        /*
+         * Values that can be compressed as unsigned signed bytes.
+         */
+        vec =  Vector.create(new float[] {30, 120, 250, 1}, false);
+        assertNotSame(vec, compressed = vec.compress(0));
+        assertEquals("elementType", Byte.class, compressed.getElementType());
+        assertTrue("isUnsigned()", compressed.isUnsigned());
+        assertContentEquals(vec, compressed);
+        /*
+         * Values that can be compressed as signed shorts.
+         */
+        vec =  Vector.create(new long[] {32000, 120, -25000, 14}, false);
+        assertNotSame(vec, compressed = vec.compress(0));
+        assertEquals("elementType", Short.class, compressed.getElementType());
+        assertFalse("isUnsigned()", compressed.isUnsigned());
+        assertContentEquals(vec, compressed);
+        /*
+         * Values that can be compressed as unsigned unsigned shorts.
+         */
+        vec =  Vector.create(new float[] {3, 60000, 25, 4}, false);
+        assertNotSame(vec, compressed = vec.compress(0));
+        assertEquals("elementType", Short.class, compressed.getElementType());
+        assertTrue("isUnsigned()", compressed.isUnsigned());
+        assertContentEquals(vec, compressed);
+        /*
+         * Values that can be compressed in a PackedVector.
+         * Values below require less bits than the 'byte' type.
+         */
+        vec =  Vector.create(new double[] {30, 27, 93, 72, -8}, false);
+        assertNotSame(vec, compressed = vec.compress(0));
+        assertInstanceOf("vector.compress(0)", PackedVector.class, compressed);
+        assertContentEquals(vec, compressed);
+        /*
+         * Values that can be compressed as float types.
+         */
+        vec =  Vector.create(new double[] {3.10, 60.59, -25.32, 4.78}, false);
+        assertNotSame(vec, compressed = vec.compress(0));
+        assertEquals("elementType", Float.class, compressed.getElementType());
+        assertFalse("isUnsigned()", compressed.isUnsigned());
+        assertContentEquals(vec, compressed);
+    }
+
+    /**
+     * Asserts that the content of the given vector are equal.
+     * The vectors do not need to use the same element type.
+     */
+    private static void assertContentEquals(final Vector expected, final Vector actual) {
+        final int length = expected.size();
+        assertEquals("size", length, actual.size());
+        for (int i=0; i<length; i++) {
+            assertEquals("value", expected.doubleValue(i), actual.doubleValue(i), STRICT);
+        }
+    }
 }



Mime
View raw message