sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/05: Add a Vector.fill(…) method as a convenient way to initialize the content of an array of unknown type.
Date Tue, 06 Jul 2021 15:39:55 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch GeoTIFF
in repository https://gitbox.apache.org/repos/asf/sis.git

commit ecbad762aca3bd5e0a718ab06b8980baae69263f
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue May 18 00:45:49 2021 +0200

    Add a Vector.fill(…) method as a convenient way to initialize the content of an array
of unknown type.
---
 .../main/java/org/apache/sis/math/ArrayVector.java | 52 ++++++++++++++++++++++
 .../org/apache/sis/math/ConcatenatedVector.java    | 13 ++++++
 .../org/apache/sis/math/LinearlyDerivedVector.java | 31 ++++++++++---
 .../java/org/apache/sis/math/PackedVector.java     |  2 +-
 .../src/main/java/org/apache/sis/math/Vector.java  | 16 +++++++
 .../test/java/org/apache/sis/math/VectorTest.java  | 12 ++++-
 6 files changed, 118 insertions(+), 8 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
index f7d021e..25c2aea 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
@@ -254,6 +254,12 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             return old;
         }
 
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            Arrays.fill(array, fromIndex, toIndex, value.doubleValue());
+            modCount++;
+        }
+
         /** Finds index of a match or mismatch (depending on {@code equality}). */
         @Override int indexOf(final int toSearch, int index, final boolean equality) {
             final long first = Double.doubleToLongBits(array[toSearch]);
@@ -368,6 +374,12 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             return old;
         }
 
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            Arrays.fill(array, fromIndex, toIndex, value.floatValue());
+            modCount++;
+        }
+
         /** Finds index of a match or mismatch (depending on {@code equality}). */
         @Override final int indexOf(final int toSearch, int index, final boolean equality)
{
             final int first = Float.floatToIntBits(array[toSearch]);
@@ -529,6 +541,13 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             return old;
         }
 
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            verifyType(value, Numbers.LONG);
+            Arrays.fill(array, fromIndex, toIndex, value.longValue());
+            modCount++;
+        }
+
         /** Finds index of a match or mismatch (depending on {@code equality}). */
         @Override final int indexOf(final int toSearch, int index, final boolean equality)
{
             final long first = array[toSearch];
@@ -652,6 +671,13 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             return old;
         }
 
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            verifyType(value, Numbers.INTEGER);
+            Arrays.fill(array, fromIndex, toIndex, value.intValue());
+            modCount++;
+        }
+
         /** Finds index of a match or mismatch (depending on {@code equality}). */
         @Override final int indexOf(final int toSearch, int index, final boolean equality)
{
             final int first = array[toSearch];
@@ -779,6 +805,13 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             return old;
         }
 
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            verifyType(value, Numbers.SHORT);
+            Arrays.fill(array, fromIndex, toIndex, value.shortValue());
+            modCount++;
+        }
+
         /** Finds index of a match or mismatch (depending on {@code equality}). */
         @Override final int indexOf(final int toSearch, int index, final boolean equality)
{
             final short first = array[toSearch];
@@ -881,6 +914,13 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             return old;
         }
 
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            verifyType(value, Numbers.BYTE);
+            Arrays.fill(array, fromIndex, toIndex, value.byteValue());
+            modCount++;
+        }
+
         /** Finds index of a match or mismatch (depending on {@code equality}). */
         @Override final int indexOf(final int toSearch, int index, final boolean equality)
{
             final byte first = array[toSearch];
@@ -1205,6 +1245,12 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             modCount++;
             return old;
         }
+
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            Arrays.fill(array, fromIndex, toIndex, value.toString());
+            modCount++;
+        }
     }
 
     /**
@@ -1253,5 +1299,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
             modCount++;
             return old;
         }
+
+        /** Sets the value of all elements in the given range. */
+        @Override public void fill(final int fromIndex, final int toIndex, final Number value)
{
+            Arrays.fill(array, fromIndex, toIndex, value);
+            modCount++;
+        }
     }
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
index e23cc5f..2d5cdbe 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
@@ -253,6 +253,19 @@ final class ConcatenatedVector extends Vector implements Serializable
{
     }
 
     /**
+     * Sets a range of elements to the given number.
+     */
+    @Override
+    public void fill(final int fromIndex, final int toIndex, final Number value) {
+        if (fromIndex < limit) {
+            first.fill(fromIndex, Math.min(toIndex, limit), value);
+        }
+        if ((toIndex > limit)) {
+            second.fill(Math.max(0, fromIndex - limit), toIndex - limit, value);
+        }
+    }
+
+    /**
      * Returns the increment between all consecutive values if this increment is constant,
or {@code null} otherwise.
      */
     @Override
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
b/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
index 383087e..16241b3 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
@@ -162,11 +162,17 @@ final class LinearlyDerivedVector extends Vector implements Serializable
{
      * if the {@linkplain #base} vector stores values using integer primitive type.
      */
     @Override
-    public Number set(final int index, Number value) {
-        if (value != null) {
-            value = (value.doubleValue() - offset) / scale;
-        }
-        return convert(base.set(index, value));
+    public Number set(final int index, final Number value) {
+        return convert(base.set(index, inverse(value)));
+    }
+
+    /**
+     * Sets a range of elements to the given number. This action is likely to loose precision
+     * if the {@linkplain #base} vector stores values using integer primitive type.
+     */
+    @Override
+    public void fill(final int fromIndex, final int toIndex, final Number value) {
+        base.fill(fromIndex, toIndex, inverse(value));
     }
 
     /**
@@ -211,7 +217,20 @@ final class LinearlyDerivedVector extends Vector implements Serializable
{
      */
     private Number convert(Number value) {
         if (value != null) {
-            value = value.doubleValue() * scale + offset;       // TODO: use Math.fml in
JDK9.
+            value = value.doubleValue() * scale + offset;       // TODO: use Math.fma in
JDK9.
+        }
+        return value;
+    }
+
+    /**
+     * Applies the inverse linear function on the given value.
+     *
+     * @param  value  the number to inverse convert, or {@code null}.
+     * @return the inverse converted number (may be {@code null}).
+     */
+    private Number inverse(Number value) {
+        if (value != null) {
+            value = (value.doubleValue() - offset) / scale;
         }
         return value;
     }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/PackedVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/PackedVector.java
index a91ee2e..9f57038 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/PackedVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/PackedVector.java
@@ -204,7 +204,7 @@ final class PackedVector extends ArrayVector<Long> {
     }
 
     /**
-     * Sets the value at the given index at returns the previous value.
+     * Sets the value at the given index and returns the previous value.
      */
     @Override
     public Number set(final int index, final Number value) {
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
index c0cf85c..43b445c 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
@@ -547,6 +547,22 @@ public abstract class Vector extends AbstractList<Number> implements
RandomAcces
     public abstract Number set(int index, Number value);
 
     /**
+     * Sets a range of elements to the given number. Invoking this method is equivalent
+     * to invoking {@link #set(int, Number)} in a loop, but potentially much more efficient.
+     *
+     * @param  fromIndex  index of the first element (inclusive) to be filled with the specified
value.
+     * @param  toIndex    index of the last element (exclusive) to be filled with the specified
value.
+     * @param  value      the value to be stored in elements of the vector.
+     *
+     * @since 1.1
+     */
+    public void fill(int fromIndex, final int toIndex, final Number value) {
+        // Subclasses override with more efficient implementations.
+        ArgumentChecks.ensureValidIndexRange(size(), fromIndex, toIndex);
+        while (fromIndex < toIndex) set(fromIndex++, value);
+    }
+
+    /**
      * Returns the index of the first value which is equal (if {@code equality} is true)
      * or different (if {@code equality} is false) to the value at the {@code toSearch} index.
      * Subclasses should override if they can provide a more efficient implementation.
diff --git a/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java b/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java
index 275ea03..f2ed27c 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java
@@ -28,7 +28,7 @@ import static org.opengis.test.Assert.*;
  * Tests the {@link Vector} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   0.8
  * @module
  */
@@ -263,6 +263,16 @@ public final strictfp class VectorTest extends TestCase {
     }
 
     /**
+     * Tests {@link Vector#fill(int, int, Number)}.
+     */
+    @Test
+    public void testFill() {
+        vector = Vector.create(new int[] {2, 5, 3}, false).concatenate(Vector.create(new
int[] {7, 2, 8}, false));
+        vector.fill(2, 5, -1);
+        assertContentEquals(Vector.create(new int[] {2, 5, -1, -1, -1, 8}, false), vector);
+    }
+
+    /**
      * Tests a vector backed by an array of strings.
      * This is not recommended, but happen in GDAL extensions of GeoTIFF.
      * See {@code org.apache.sis.storage.geotiff.Type.ASCII}.

Mime
View raw message