sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1763107 - in /sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math: ArrayVector.java ConcatenatedVector.java SequenceVector.java Vector.java
Date Sun, 02 Oct 2016 22:53:09 GMT
Author: desruisseaux
Date: Sun Oct  2 22:53:09 2016
New Revision: 1763107

URL: http://svn.apache.org/viewvc?rev=1763107&view=rev
Log:
Add a Vector.range() method, together with specialized implementations in sub-classes for
performance.

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/ConcatenatedVector.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=1763107&r1=1763106&r2=1763107&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] Sun Oct  2 22:53:09 2016
@@ -17,10 +17,12 @@
 package org.apache.sis.math;
 
 import java.io.Serializable;
+import java.util.function.IntSupplier;
 import org.apache.sis.util.Numbers;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.collection.CheckedContainer;
 import org.apache.sis.internal.util.Numerics;
+import org.apache.sis.measure.NumberRange;
 
 
 /**
@@ -182,6 +184,18 @@ abstract class ArrayVector<E extends Num
             modCount++;
             return old;
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<java.lang.Double> range(final IntSupplier indices, int
n) {
+            double min = java.lang.Double.POSITIVE_INFINITY;
+            double max = java.lang.Double.NEGATIVE_INFINITY;
+            while (--n >= 0) {
+                final double value = array[(indices != null) ? indices.getAsInt() : n];
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -231,6 +245,27 @@ abstract class ArrayVector<E extends Num
             modCount++;
             return old;
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override final NumberRange<?> range(final IntSupplier indices, int n) {
+            float min = java.lang.Float.POSITIVE_INFINITY;
+            float max = java.lang.Float.NEGATIVE_INFINITY;
+            while (--n >= 0) {
+                final float value = array[(indices != null) ? indices.getAsInt() : n];
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return createRange(min, max);
+        }
+
+        /**
+         * Creates a range from the given minimum and maximum values, inclusive.
+         * The default implementation creates a range of {@code float}, but this method is
+         * overridden by {@code ArrayVector.Decimal} which create a range of {@code double}.
+         */
+        NumberRange<?> createRange(final float min, final float max) {
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -251,6 +286,12 @@ abstract class ArrayVector<E extends Num
         @Override public double doubleValue(final int index) {
             return DecimalFunctions.floatToDouble(super.floatValue(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,
+                                      DecimalFunctions.floatToDouble(max), true);
+        }
     }
 
     /**
@@ -290,6 +331,18 @@ abstract class ArrayVector<E extends Num
             modCount++;
             return old;
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            long min = java.lang.Long.MAX_VALUE;
+            long max = java.lang.Long.MIN_VALUE;
+            while (--n >= 0) {
+                final long value = array[(indices != null) ? indices.getAsInt() : n];
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -330,6 +383,18 @@ abstract class ArrayVector<E extends Num
             modCount++;
             return old;
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            int min = java.lang.Integer.MAX_VALUE;
+            int max = java.lang.Integer.MIN_VALUE;
+            while (--n >= 0) {
+                final int value = array[(indices != null) ? indices.getAsInt() : n];
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -371,6 +436,18 @@ abstract class ArrayVector<E extends Num
             modCount++;
             return old;
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            short min = java.lang.Short.MAX_VALUE;
+            short max = java.lang.Short.MIN_VALUE;
+            while (--n >= 0) {
+                final short value = array[(indices != null) ? indices.getAsInt() : n];
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -413,6 +490,18 @@ abstract class ArrayVector<E extends Num
             modCount++;
             return old;
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            byte min = java.lang.Byte.MAX_VALUE;
+            byte max = java.lang.Byte.MIN_VALUE;
+            while (--n >= 0) {
+                final byte value = array[(indices != null) ? indices.getAsInt() : n];
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -451,6 +540,18 @@ abstract class ArrayVector<E extends Num
         @Override public String stringValue(final int index) {
             return java.lang.Long.toUnsignedString(super.longValue(index));
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<java.lang.Double> range(final IntSupplier indices, int
n) {
+            double min = java.lang.Double.POSITIVE_INFINITY;
+            double max = java.lang.Double.NEGATIVE_INFINITY;
+            while (--n >= 0) {
+                final double value = doubleValue((indices != null) ? indices.getAsInt() :
n);
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -480,6 +581,18 @@ abstract class ArrayVector<E extends Num
         @Override public String stringValue(final int index) {
             return java.lang.Integer.toUnsignedString(super.intValue(index));
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            long min = java.lang.Long.MAX_VALUE;
+            long max = java.lang.Long.MIN_VALUE;
+            while (--n >= 0) {
+                final long value = longValue((indices != null) ? indices.getAsInt() : n);
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -510,6 +623,18 @@ abstract class ArrayVector<E extends Num
         @Override public String stringValue(final int index) {
             return java.lang.Integer.toString(intValue(index));
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            int min = java.lang.Integer.MAX_VALUE;
+            int max = java.lang.Integer.MIN_VALUE;
+            while (--n >= 0) {
+                final int value = intValue((indices != null) ? indices.getAsInt() : n);
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**
@@ -541,6 +666,18 @@ abstract class ArrayVector<E extends Num
         @Override public String stringValue(final int index) {
             return java.lang.Integer.toString(intValue(index));
         }
+
+        /** Finds the minimum and maximum values in the array or in a subset of the array.
*/
+        @Override NumberRange<?> range(final IntSupplier indices, int n) {
+            short min = java.lang.Short.MAX_VALUE;
+            short max = java.lang.Short.MIN_VALUE;
+            while (--n >= 0) {
+                final short value = shortValue((indices != null) ? indices.getAsInt() : n);
+                if (value < min) min = value;
+                if (value > max) max = value;
+            }
+            return NumberRange.create(min, true, max, true);
+        }
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java?rev=1763107&r1=1763106&r2=1763107&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
[UTF-8] Sun Oct  2 22:53:09 2016
@@ -18,6 +18,7 @@ package org.apache.sis.math;
 
 import java.io.Serializable;
 import org.apache.sis.util.Classes;
+import org.apache.sis.measure.NumberRange;
 
 
 /**
@@ -235,6 +236,15 @@ final class ConcatenatedVector extends V
     }
 
     /**
+     * Computes the minimal and maximal values in this vector.
+     * This is the union of the range of the two concatenated vectors.
+     */
+    @Override
+    public NumberRange<?> range() {
+        return first.range().unionAny(second.range());
+    }
+
+    /**
      * Delegates to the backing vectors if possible.
      */
     @Override

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=1763107&r1=1763106&r2=1763107&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] Sun Oct  2 22:53:09 2016
@@ -17,6 +17,7 @@
 package org.apache.sis.math;
 
 import java.io.Serializable;
+import org.apache.sis.measure.NumberRange;
 import org.apache.sis.util.resources.Errors;
 
 import static org.apache.sis.util.Numbers.*;
@@ -49,6 +50,7 @@ final class SequenceVector extends Vecto
 
     /**
      * The difference between the values at two adjacent indexes.
+     * May be positive, negative or zero.
      */
     private final double increment;
 
@@ -60,9 +62,9 @@ final class SequenceVector extends Vecto
     /**
      * Creates a sequence of numbers in a given range of values using the given increment.
      *
-     * @param first     The first value, inclusive.
-     * @param increment The difference between the values at two adjacent indexes.
-     * @param length    The length of the vector.
+     * @param  first      the first value, inclusive.
+     * @param  increment  the difference between the values at two adjacent indexes.
+     * @param  length     the length of the vector.
      */
     public SequenceVector(final double first, final double increment, final int length) {
         if (length < 0) {
@@ -157,6 +159,21 @@ final class SequenceVector extends Vecto
     }
 
     /**
+     * Computes the minimal and maximal values in this vector.
+     * This is easily computed from the first value and the increment.
+     */
+    @Override
+    public NumberRange<Double> range() {
+        double min = first;
+        double max = first + increment * (length - 1);
+        if (max < min) {
+            min = max;
+            max = first;
+        }
+        return NumberRange.create(min, true, max, true);
+    }
+
+    /**
      * Creates a new sequence.
      */
     @Override

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=1763107&r1=1763106&r2=1763107&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]
Sun Oct  2 22:53:09 2016
@@ -20,6 +20,8 @@ import java.io.Serializable;
 import java.util.Arrays;
 import java.util.AbstractList;
 import java.util.RandomAccess;
+import java.util.function.IntSupplier;
+import org.apache.sis.measure.NumberRange;
 import org.apache.sis.util.resources.Errors;
 
 import static org.apache.sis.util.ArgumentChecks.ensureValidIndex;
@@ -435,20 +437,20 @@ public abstract class Vector extends Abs
     }
 
     /**
-     * A view over an other vector in a range of index.
+     * A view over another vector in a range of index.
      */
     private final class SubSampling extends Vector implements Serializable {
         /** For cross-version compatibility. */
         private static final long serialVersionUID = 7641036842053528486L;
 
         /** Index of the first element in the enclosing vector. */
-        private final int first;
+        final int first;
 
         /** The index increment. May be negative but not zero. */
-        private final int step;
+        final int step;
 
         /** The length of this vector. */
-        private final int length;
+        final int length;
 
         /** Creates a new view over the given range. */
         protected SubSampling(final int first, final int step, final int length) {
@@ -464,7 +466,7 @@ public abstract class Vector extends Abs
         }
 
         /** Returns the index where to look for the value in the enclosing vector. */
-        private int toBacking(final int index) {
+        final int toBacking(final int index) {
             ensureValidIndex(length, index);
             return index*step + first;
         }
@@ -500,6 +502,7 @@ public abstract class Vector extends Abs
         @Override public String  stringValue(int index)     {return Vector.this.stringValue(toBacking(index));}
         @Override public Number  get        (int index)     {return Vector.this.get     
  (toBacking(index));}
 
+        /** Delegates to the enclosing vector. */
         @Override public Number set(final int index, final Number v) {
             final Number old = Vector.this.set(toBacking(index), v);
             modCount++;
@@ -523,6 +526,25 @@ public abstract class Vector extends Abs
             }
             return super.createConcatenate(toAppend);
         }
+
+        /** Delegates to the enclosing vector */
+        @Override NumberRange<?> range(final IntSupplier indices, final int n) {
+            if (indices != null) {
+                return Vector.this.range(() -> toBacking(indices.getAsInt()), n);
+            }
+            IntSupplier supplier = null;
+            if (first != 0 || step != 1) {
+                supplier = new IntSupplier() {
+                    private int index = first;
+                    @Override public int getAsInt() {
+                        final int i = index;
+                        index += step;
+                        return i;
+                    }
+                };
+            }
+            return Vector.this.range(supplier, n);
+        }
     }
 
     /**
@@ -649,6 +671,7 @@ public abstract class Vector extends Abs
         @Override public String   stringValue(int i)    {return Vector.this.stringValue(indices[i]);}
         @Override public Number   get        (int i)    {return Vector.this.get        (indices[i]);}
 
+        /** Delegates to the enclosing vector. */
         @Override public Number set(final int i, final Number v) {
             final Number old = Vector.this.set(indices[i], v);
             modCount++;
@@ -678,6 +701,20 @@ public abstract class Vector extends Abs
             }
             return super.createConcatenate(toAppend);
         }
+
+        /** Delegates to the enclosing vector. */
+        @Override NumberRange<?> range(final IntSupplier supplier, final int n) {
+            if (supplier != null) {
+                return Vector.this.range(() -> indices[supplier.getAsInt()], n);
+            } else {
+                return Vector.this.range(new IntSupplier() {
+                    private int index;
+                    @Override public int getAsInt() {
+                        return indices[index++];
+                    }
+                }, n);
+            }
+        }
     }
 
     /**
@@ -741,6 +778,36 @@ public abstract class Vector extends Abs
     }
 
     /**
+     * Returns the minimal and maximal values found in this vector.
+     *
+     * @return minimal and maximal values found in this vector.
+     */
+    public NumberRange<?> range() {
+        return range(null, size());
+    }
+
+    /**
+     * Computes the range of values at the indices provided by the given supplier.
+     * The default implementation iterates over all {@code double} values, but
+     * subclasses should override with a more efficient implementation if possible.
+     *
+     * @param  indices  supplier of indices of the values to examine for computing the range,
+     *                  or {@code null} for the 0, 1, 2, … <var>n</var>-1 sequence.
+     * @param  n        number of indices to get from the supplier.
+     * @return the range of all values at the given indices.
+     */
+    NumberRange<?> range(final IntSupplier indices, int n) {
+        double min = Double.POSITIVE_INFINITY;
+        double max = Double.NEGATIVE_INFINITY;
+        while (--n >= 0) {
+            final double value = doubleValue((indices != null) ? indices.getAsInt() : n);
+            if (value < min) min = value;
+            if (value > max) max = value;
+        }
+        return NumberRange.create(min, true, max, true);
+    }
+
+    /**
      * Returns a string representation of this vector.
      *
      * @return a string representation of this vector.



Mime
View raw message