sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/04: Replace loops by placeholders for JDK9 Arrays.equals(…) methods. Fix a wrong PackedVector optimization.
Date Mon, 19 Oct 2020 14:47:24 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 07454038cced9c9982eb60f79af4ffcb2efac8cc
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sun Oct 18 16:41:46 2020 +0200

    Replace loops by placeholders for JDK9 Arrays.equals(…) methods.
    Fix a wrong PackedVector optimization.
---
 .../sis/coverage/grid/GridCoordinatesView.java     |  13 +--
 .../java/org/apache/sis/internal/jdk9/JDK9.java    | 108 +++++++++++++++++++++
 .../main/java/org/apache/sis/math/ArrayVector.java |  71 ++++----------
 .../java/org/apache/sis/math/PackedVector.java     |   6 +-
 .../apache/sis/util/collection/IntegerList.java    |   8 +-
 5 files changed, 135 insertions(+), 71 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoordinatesView.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoordinatesView.java
index cc648b3..77afa56 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoordinatesView.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoordinatesView.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import org.opengis.coverage.grid.GridCoordinates;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.internal.jdk9.JDK9;
 
 
 /**
@@ -128,16 +129,8 @@ final class GridCoordinatesView implements GridCoordinates {
          */
         if (object instanceof GridCoordinatesView) {
             final GridCoordinatesView that = (GridCoordinatesView) object;
-            final int dimension = getDimension();
-            if (dimension == that.getDimension()) {
-                // TODO: use Arrays.equals(...) with JDK9 instead.
-                for (int i=0; i<dimension; i++) {
-                    if (coordinates[offset + i] != that.coordinates[that.offset + i]) {
-                        return false;
-                    }
-                }
-                return true;
-            }
+            return JDK9.equals(this.coordinates, this.offset, this.offset + this.getDimension(),
+                               that.coordinates, that.offset, that.offset + that.getDimension());
         }
         return false;
     }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java b/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
index 8f4b7f1..3ad93ce 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/jdk9/JDK9.java
@@ -159,4 +159,112 @@ public final class JDK9 {
     public static long multiplyFull​(int x, int y) {
         return ((long) x) * ((long) y);
     }
+
+    /**
+     * Place holder for {@link java.util.Arrays} method added in JDK9.
+     * This placeholder does not perform range check (JDK9 method does).
+     */
+    public static boolean equals(final byte[] a, int ai, final int aUp,
+                                 final byte[] b, int bi, final int bUp)
+    {
+        if (aUp - ai != bUp - bi) {
+            return false;
+        }
+        while (ai < aUp) {
+            if (a[ai++] != b[bi++]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Place holder for {@link java.util.Arrays} method added in JDK9.
+     * This placeholder does not perform range check (JDK9 method does).
+     */
+    public static boolean equals(final short[] a, int ai, final int aUp,
+                                 final short[] b, int bi, final int bUp)
+    {
+        if (aUp - ai != bUp - bi) {
+            return false;
+        }
+        while (ai < aUp) {
+            if (a[ai++] != b[bi++]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Place holder for {@link java.util.Arrays} method added in JDK9.
+     * This placeholder does not perform range check (JDK9 method does).
+     */
+    public static boolean equals(final int[] a, int ai, final int aUp,
+                                 final int[] b, int bi, final int bUp)
+    {
+        if (aUp - ai != bUp - bi) {
+            return false;
+        }
+        while (ai < aUp) {
+            if (a[ai++] != b[bi++]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Place holder for {@link java.util.Arrays} method added in JDK9.
+     * This placeholder does not perform range check (JDK9 method does).
+     */
+    public static boolean equals(final long[] a, int ai, final int aUp,
+                                 final long[] b, int bi, final int bUp)
+    {
+        if (aUp - ai != bUp - bi) {
+            return false;
+        }
+        while (ai < aUp) {
+            if (a[ai++] != b[bi++]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Place holder for {@link java.util.Arrays} method added in JDK9.
+     * This placeholder does not perform range check (JDK9 method does).
+     */
+    public static boolean equals(final float[] a, int ai, final int aUp,
+                                 final float[] b, int bi, final int bUp)
+    {
+        if (aUp - ai != bUp - bi) {
+            return false;
+        }
+        while (ai < aUp) {
+            if (Float.floatToIntBits(a[ai++]) != Float.floatToIntBits(b[bi++])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Place holder for {@link java.util.Arrays} method added in JDK9.
+     * This placeholder does not perform range check (JDK9 method does).
+     */
+    public static boolean equals(final double[] a, int ai, final int aUp,
+                                 final double[] b, int bi, final int bUp)
+    {
+        if (aUp - ai != bUp - bi) {
+            return false;
+        }
+        while (ai < aUp) {
+            if (Double.doubleToLongBits(a[ai++]) != Double.doubleToLongBits(b[bi++])) {
+                return false;
+            }
+        }
+        return true;
+    }
 }
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 8f66146..26d536c 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
@@ -33,6 +33,7 @@ import org.apache.sis.util.ArraysExt;
 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.internal.jdk9.JDK9;
 import org.apache.sis.measure.NumberRange;
 
 
@@ -256,16 +257,10 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Returns whether this vector in the given range is equals to the specified vector.
*/
-        @Override boolean equals(int lower, final int upper, final Vector other, int otherOffset)
{
+        @Override boolean equals(final int lower, final int upper, final Vector other, final
int otherOffset) {
             if (other instanceof Doubles) {
-                // TODO: replace by Arrays.equals(…) with JDK9.
-                final double[] cmp = ((Doubles) other).array;
-                while (lower < upper) {
-                    if (Double.doubleToLongBits(array[lower++]) != Double.doubleToLongBits(cmp[otherOffset++]))
{
-                        return false;
-                    }
-                }
-                return true;
+                return JDK9.equals(array, lower, upper,
+                        ((Doubles) other).array, otherOffset, otherOffset + (upper - lower));
             }
             return super.equals(lower, upper, other, otherOffset);
         }
@@ -363,16 +358,10 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Returns whether this vector in the given range is equals to the specified vector.
*/
-        @Override final boolean equals(int lower, final int upper, final Vector other, int
otherOffset) {
+        @Override final boolean equals(final int lower, final int upper, final Vector other,
final int otherOffset) {
             if (other.getClass() == getClass()) {
-                // TODO: replace by Arrays.equals(…) with JDK9.
-                final float[] cmp = ((Floats) other).array;
-                while (lower < upper) {
-                    if (Float.floatToIntBits(array[lower++]) != Float.floatToIntBits(cmp[otherOffset++]))
{
-                        return false;
-                    }
-                }
-                return true;
+                return JDK9.equals(array, lower, upper,
+                        ((Floats) other).array, otherOffset, otherOffset + (upper - lower));
             }
             return super.equals(lower, upper, other, otherOffset);
         }
@@ -507,16 +496,10 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Returns whether this vector in the given range is equals to the specified vector.
*/
-        @Override final boolean equals(int lower, final int upper, final Vector other, int
otherOffset) {
+        @Override final boolean equals(final int lower, final int upper, final Vector other,
final int otherOffset) {
             if (other.getClass() == getClass()) {
-                // TODO: replace by Arrays.equals(…) with JDK9.
-                final long[] cmp = ((Longs) other).array;
-                while (lower < upper) {
-                    if (array[lower++] != cmp[otherOffset++]) {
-                        return false;
-                    }
-                }
-                return true;
+                return JDK9.equals(array, lower, upper,
+                        ((Longs) other).array, otherOffset, otherOffset + (upper - lower));
             }
             return super.equals(lower, upper, other, otherOffset);
         }
@@ -626,16 +609,10 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Returns whether this vector in the given range is equals to the specified vector.
*/
-        @Override final boolean equals(int lower, final int upper, final Vector other, int
otherOffset) {
+        @Override final boolean equals(final int lower, final int upper, final Vector other,
final int otherOffset) {
             if (other.getClass() == getClass()) {
-                // TODO: replace by Arrays.equals(…) with JDK9.
-                final int[] cmp = ((Integers) other).array;
-                while (lower < upper) {
-                    if (array[lower++] != cmp[otherOffset++]) {
-                        return false;
-                    }
-                }
-                return true;
+                return JDK9.equals(array, lower, upper,
+                        ((Integers) other).array, otherOffset, otherOffset + (upper - lower));
             }
             return super.equals(lower, upper, other, otherOffset);
         }
@@ -749,16 +726,10 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Returns whether this vector in the given range is equals to the specified vector.
*/
-        @Override final boolean equals(int lower, final int upper, final Vector other, int
otherOffset) {
+        @Override final boolean equals(final int lower, final int upper, final Vector other,
final int otherOffset) {
             if (other.getClass() == getClass()) {
-                // TODO: replace by Arrays.equals(…) with JDK9.
-                final short[] cmp = ((Shorts) other).array;
-                while (lower < upper) {
-                    if (array[lower++] != cmp[otherOffset++]) {
-                        return false;
-                    }
-                }
-                return true;
+                return JDK9.equals(array, lower, upper,
+                        ((Shorts) other).array, otherOffset, otherOffset + (upper - lower));
             }
             return super.equals(lower, upper, other, otherOffset);
         }
@@ -849,14 +820,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         /** Returns whether this vector in the given range is equals to the specified vector.
*/
         @Override final boolean equals(int lower, final int upper, final Vector other, int
otherOffset) {
             if (other.getClass() == getClass()) {
-                // TODO: replace by Arrays.equals(…) with JDK9.
-                final byte[] cmp = ((Bytes) other).array;
-                while (lower < upper) {
-                    if (array[lower++] != cmp[otherOffset++]) {
-                        return false;
-                    }
-                }
-                return true;
+                return JDK9.equals(array, lower, upper,
+                        ((Bytes) other).array, otherOffset, otherOffset + (upper - lower));
             }
             return super.equals(lower, upper, other, otherOffset);
         }
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 65800d0..01339a8 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
@@ -218,15 +218,15 @@ final class PackedVector extends ArrayVector<Long> {
     }
 
     /**
-     * Optimization of {@code equals} method for the case where the other object
+     * Optimization of {@code equals(…)} method for the case where the other object
      * is another {@code PackedVector}.
      */
     @Override
-    boolean equals(int lower, final int upper, final Vector other, int otherOffset) {
+    public boolean equals(final Object other) {
         if (other instanceof PackedVector) {
             final PackedVector d = (PackedVector) other;
             return d.increment == increment && d.offset == offset && d.data.equals(data);
         }
-        return super.equals(lower, upper, other, otherOffset);
+        return super.equals(other);
     }
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
b/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
index 079c069..c4340f7 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/collection/IntegerList.java
@@ -32,6 +32,7 @@ import java.io.Serializable;
 import java.io.ObjectOutputStream;
 import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.internal.jdk9.JDK9;
 
 
 /**
@@ -699,11 +700,8 @@ public class IntegerList extends AbstractList<Integer> implements
RandomAccess,
                 int n = size * bitCount;
                 final int nr = n & OFFSET_MASK;             // Number of remaining values.
                 n >>>= BASE_SHIFT;
-                // TODO: use Arrays.equals(…) with JDK9.
-                for (int i=0; i<n; i++) {
-                    if (that.values[i] != values[i]) {
-                        return false;
-                    }
+                if (!JDK9.equals(values, 0, n, that.values, 0, n)) {
+                    return false;
                 }
                 if (nr == 0) return true;
                 return ((that.values[n] ^ values[n]) & ((1L << nr) - 1)) == 0;


Mime
View raw message