sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1522970 - in /sis/branches/JDK7: core/sis-referencing/src/main/java/org/apache/sis/geometry/ core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/ core/sis-utility/src/main/java/org/apache/sis/internal/converter/ ...
Date Fri, 13 Sep 2013 15:15:22 GMT
Author: desruisseaux
Date: Fri Sep 13 15:15:21 2013
New Revision: 1522970

URL: http://svn.apache.org/r1522970
Log:
Replaced some inline values swapping by an explit 'swap' method for making the code easier
to read.
This helps also in Matrix.inverse() implementation. We rely on Hotspot optimization for producing
the same performance than when the swapping was done inline.

Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/converter/FallbackConverter.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
[UTF-8] Fri Sep 13 15:15:21 2013
@@ -190,9 +190,9 @@ class ArrayEnvelope extends AbstractEnve
             box.getNorthBoundLatitude()
         };
         if (Boolean.FALSE.equals(box.getInclusion())) {
-            swap(0);
+            ArraysExt.swap(ordinates, 0, ordinates.length >>> 1);
             if (!isPoleToPole(ordinates[1], ordinates[3])) {
-                swap(1);
+                ArraysExt.swap(ordinates, 1, (ordinates.length >>> 1) + 1);
             }
         }
         try {
@@ -321,16 +321,6 @@ scanNumber: while ((i += Character.charC
     }
 
     /**
-     * Swaps two ordinate values.
-     */
-    private void swap(final int i) {
-        final int m = i + (ordinates.length >>> 1);
-        final double t = ordinates[i];
-        ordinates[i] = ordinates[m];
-        ordinates[m] = t;
-    }
-
-    /**
      * Returns the index of the first valid ordinate value of the lower corner in the {@link
#ordinates} array.
      * This is always 0, unless this envelope is a {@link SubEnvelope}.
      *

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
[UTF-8] Fri Sep 13 15:15:21 2013
@@ -19,6 +19,7 @@ package org.apache.sis.referencing.opera
 import java.util.Arrays;
 import org.opengis.referencing.operation.Matrix;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.math.MathFunctions;
 
 
@@ -248,11 +249,7 @@ class GeneralMatrix extends MatrixSIS {
         final int numCol = this.numCol;
         for (int j=0; j<numRow; j++) {
             for (int i=0; i<j; i++) {
-                final int lowerLeft  = j*numCol + i;
-                final int upperRight = i*numCol + j;
-                final double swap = elements[lowerLeft];
-                elements[lowerLeft] = elements[upperRight];
-                elements[upperRight] = swap;
+                ArraysExt.swap(elements, j*numCol + i, i*numCol + j);
             }
         }
     }

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/converter/FallbackConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/converter/FallbackConverter.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/converter/FallbackConverter.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/converter/FallbackConverter.java
[UTF-8] Fri Sep 13 15:15:21 2013
@@ -93,7 +93,7 @@ final class FallbackConverter<S,T> exten
                               final ObjectConverter<S, ? extends T> fallback)
     {
         super(sourceClass, targetClass);
-        if (swap(primary, fallback.getClass())) {
+        if (needSwap(primary, fallback.getClass())) {
             this.primary  = fallback;
             this.fallback = primary;
         } else {
@@ -110,11 +110,11 @@ final class FallbackConverter<S,T> exten
      * @param  fallbackClass The target class of the fallback converter to test.
      * @return {@code true} if the given primary and fallback converters should be interchanged.
      */
-    private static <S> boolean swap(final ObjectConverter<S,?> primary, final
Class<?> fallbackClass) {
+    private static <S> boolean needSwap(final ObjectConverter<S,?> primary, final
Class<?> fallbackClass) {
         if (primary instanceof FallbackConverter<?,?>) {
             final FallbackConverter<S,?> candidate = (FallbackConverter<S,?>)
primary;
-            return swap(candidate.primary,  fallbackClass) &&
-                   swap(candidate.fallback, fallbackClass);
+            return needSwap(candidate.primary,  fallbackClass) &&
+                   needSwap(candidate.fallback, fallbackClass);
         } else {
             final Class<?> targetClass = primary.getTargetClass();
             return fallbackClass.isAssignableFrom(targetClass) && // This condition
is more likely to fail first.

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
[UTF-8] Fri Sep 13 15:15:21 2013
@@ -90,6 +90,39 @@ public final class Numerics extends Stat
     }
 
     /**
+     * Returns a copy of the given array where each value has been casted to the {@code float}
type.
+     *
+     * @param  data The array to copy, or {@code null}.
+     * @return A copy of the given array with values casted to the {@code float} type, or
+     *         {@code null} if the given array was null.
+     */
+    public static float[] copyAsFloats(final double[] data) {
+        if (data == null) return null;
+        final float[] result = new float[data.length];
+        for (int i=0; i<data.length; i++) {
+            result[i] = (float) data[i];
+        }
+        return result;
+    }
+
+    /**
+     * Returns a copy of the given array where each value has been
+     * {@linkplain Math#round(double) rounded} to the {@code int} type.
+     *
+     * @param  data The array to copy, or {@code null}.
+     * @return A copy of the given array with values rounded to the {@code int} type, or
+     *         {@code null} if the given array was null.
+     */
+    public static int[] copyAsInts(final double[] data) {
+        if (data == null) return null;
+        final int[] result = new int[data.length];
+        for (int i=0; i<data.length; i++) {
+            result[i] = (int) Math.round(data[i]);
+        }
+        return result;
+    }
+
+    /**
      * Returns {@code true} if the given floats are equals. Positive and negative zero are
      * considered different, while a NaN value is considered equal to all other NaN values.
      *

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java [UTF-8]
(original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java [UTF-8]
Fri Sep 13 15:15:21 2013
@@ -69,7 +69,7 @@ import java.util.Objects;
  *
  * @author Martin Desruisseaux (IRD, Geomatys)
  * @since   0.3 (derived from geotk-2.0)
- * @version 0.3
+ * @version 0.4
  * @module
  *
  * @see Arrays
@@ -1395,14 +1395,13 @@ public final class ArraysExt extends Sta
      * @param  data The array to copy, or {@code null}.
      * @return A copy of the given array with values casted to the {@code float} type, or
      *         {@code null} if the given array was null.
+     *
+     * @deprecated Will be removed since we do not plan to provide static converter methods
for every possible
+     *             type combination, and user should probably control himself the rounding
behavior.
      */
+    @Deprecated
     public static float[] copyAsFloats(final double[] data) {
-        if (data == null) return null;
-        final float[] result = new float[data.length];
-        for (int i=0; i<data.length; i++) {
-            result[i] = (float) data[i];
-        }
-        return result;
+        return org.apache.sis.internal.util.Numerics.copyAsFloats(data);
     }
 
     /**
@@ -1412,14 +1411,13 @@ public final class ArraysExt extends Sta
      * @param  data The array to copy, or {@code null}.
      * @return A copy of the given array with values rounded to the {@code int} type, or
      *         {@code null} if the given array was null.
+     *
+     * @deprecated Will be removed since we do not plan to provide static converter methods
for every possible
+     *             type combination, and user should probably control himself the rounding
behavior.
      */
+    @Deprecated
     public static int[] copyAsInts(final double[] data) {
-        if (data == null) return null;
-        final int[] result = new int[data.length];
-        for (int i=0; i<data.length; i++) {
-            result[i] = (int) Math.round(data[i]);
-        }
-        return result;
+        return org.apache.sis.internal.util.Numerics.copyAsInts(data);
     }
 
     /**
@@ -1717,6 +1715,117 @@ public final class ArraysExt extends Sta
     }
 
     /**
+     * Swaps the elements at the given indices in the given array of {@code double} values.
+     *
+     * {@note While trivial, this method is provided because its need occurs relatively often
+     *        and the availability of a <code>swap</code> method makes the code
easier to read.}
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final double[] data, final int i0, final int i1) {
+        final double t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
+     * Swaps the elements at the given indices in the given array of {@code float} values.
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final float[] data, final int i0, final int i1) {
+        final float t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
+     * Swaps the elements at the given indices in the given array of {@code long} values.
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final long[] data, final int i0, final int i1) {
+        final long t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
+     * Swaps the elements at the given indices in the given array of {@code int} values.
+     *
+     * {@note While trivial, this method is provided because its need occurs relatively often
+     *        and the availability of a <code>swap</code> method makes the code
easier to read.}
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final int[] data, final int i0, final int i1) {
+        final int t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
+     * Swaps the elements at the given indices in the given array of {@code short} values.
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final short[] data, final int i0, final int i1) {
+        final short t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
+     * Swaps the elements at the given indices in the given array of {@code byte} values.
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final byte[] data, final int i0, final int i1) {
+        final byte t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
+     * Swaps the elements at the given indices in the given array of {@code char} values.
+     *
+     * @param data The array in which to swap elements.
+     * @param i0   Index of one element to be swapped.
+     * @param i1   Index of the other element to be swapped.
+     *
+     * @since 0.4
+     */
+    public static void swap(final char[] data, final int i0, final int i1) {
+        final char t = data[i0];
+        data[i0] = data[i1];
+        data[i1] = t;
+    }
+
+    /**
      * Returns {@code true} if all values in the specified array are equal to the specified
      * value, which may be {@link Double#NaN}.
      *

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
[UTF-8] Fri Sep 13 15:15:21 2013
@@ -28,7 +28,7 @@ import static org.junit.Assert.*;
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Johann Sorel (Geomatys)
  * @since   0.3 (derived from geotk-3.04)
- * @version 0.3
+ * @version 0.4
  * @module
  */
 public final strictfp class ArraysExtTest extends TestCase {
@@ -179,4 +179,74 @@ public final strictfp class ArraysExtTes
         assertFalse(ArraysExt.isSorted(array, false));
         assertFalse(ArraysExt.isSorted(array, true));
     }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapDouble() {
+        final double[] array = new double[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new double[] {4, 15, 12, 8, 18}, array, 0.0);
+    }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapFloat() {
+        final float[] array = new float[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new float[] {4, 15, 12, 8, 18}, array, 0f);
+    }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapLong() {
+        final long[] array = new long[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new long[] {4, 15, 12, 8, 18}, array);
+    }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapInteger() {
+        final int[] array = new int[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new int[] {4, 15, 12, 8, 18}, array);
+    }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapShort() {
+        final short[] array = new short[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new short[] {4, 15, 12, 8, 18}, array);
+    }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapByte() {
+        final byte[] array = new byte[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new byte[] {4, 15, 12, 8, 18}, array);
+    }
+
+    /**
+     * Tests the {@link ArraysExt#swap(double[], int, int)} method.
+     */
+    @Test
+    public void testSwapChar() {
+        final char[] array = new char[] {4, 8, 12, 15, 18};
+        ArraysExt.swap(array, 1, 3);
+        assertArrayEquals(new char[] {4, 15, 12, 8, 18}, array);
+    }
 }

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java?rev=1522970&r1=1522969&r2=1522970&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
[UTF-8] Fri Sep 13 15:15:21 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.internal.netcdf;
 
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.storage.netcdf.AttributeNames;
 
 
@@ -60,7 +61,7 @@ public final class Axis {
     /**
      * Constructs a new axis associated to an arbitrary number of grid dimension.
      * In the particular case where the number of dimensions is equals to 2, this
-     * his constructor will detects by itself which grid dimension varies fastest.
+     * constructor will detects by itself which grid dimension varies fastest.
      *
      * @param owner            Provides callback for the conversion from grid coordinates
to geodetic coordinates.
      * @param attributeNames   The attributes to use for fetching dimension information,
or {@code null} if unknown.
@@ -83,9 +84,7 @@ public final class Axis {
             if (Math.abs(d2) > Math.abs(d1)) {
                 sourceSizes[0] = up1;
                 sourceSizes[1] = up0;
-                final int swap = sourceDimensions[0];
-                sourceDimensions[0] = sourceDimensions[1];
-                sourceDimensions[1] = swap;
+                ArraysExt.swap(sourceDimensions, 0, 1);
             }
         }
     }



Mime
View raw message