sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/03: Add ArraysExt.sequence(int, int) for a common operation.
Date Fri, 14 Dec 2018 13:29:13 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 c60e904ff8de22ff380f24a59f6f70bc07244a69
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Dec 14 09:18:34 2018 +0100

    Add ArraysExt.sequence(int, int) for a common operation.
---
 .../org/apache/sis/coverage/grid/GridExtent.java   |  2 +-
 .../org/apache/sis/coverage/grid/GridGeometry.java |  3 ++-
 .../apache/sis/internal/raster/RasterFactory.java  |  6 ++----
 .../org/apache/sis/image/DefaultIteratorTest.java  |  3 ++-
 .../java/org/apache/sis/image/TiledImageMock.java  | 22 ++++++----------------
 .../sis/referencing/operation/matrix/Solver.java   |  5 +----
 .../operation/transform/PassThroughTransform.java  |  7 ++-----
 .../operation/transform/TransformSeparator.java    | 19 ++++---------------
 .../main/java/org/apache/sis/util/ArraysExt.java   | 21 ++++++++++++++++++++-
 9 files changed, 40 insertions(+), 48 deletions(-)

diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
index c133bf4..ab553c8 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
@@ -124,7 +124,7 @@ public class GridExtent implements Serializable {
     /**
      * Minimum and maximum grid coordinates. The first half contains minimum coordinates
(inclusive),
      * while the last half contains maximum coordinates (<strong>inclusive</strong>).
Note that the
-     * later is the opposite of Java2D usage but conform to ISO specification.
+     * later is the opposite of Java2D usage but conforms to ISO specification.
      */
     private final long[] coordinates;
 
diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
index 9a11914..b3f3c44 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
@@ -551,7 +551,8 @@ public class GridGeometry implements Serializable {
      * The given envelope can be expressed in any coordinate reference system (CRS).
      * That envelope CRS may have fewer dimensions than this grid geometry CRS,
      * in which case grid dimensions not mapped to envelope dimensions will be returned unchanged.
-     * In other words, the dimensions not found in the given {@code areaOfInterest} will
be unfiltered (not discarded).
+     * In other words, the {@code GridGeometry} dimensions not found in the given {@code
areaOfInterest}
+     * will be unfiltered (not discarded).
      *
      * <p>If the envelope CRS is not specified, then it is assumed the same than the
CRS of this grid geometry.
      * In such case the envelope needs to contain all dimensions.</p>
diff --git a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
index 009c1a5..15e7f08 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
@@ -21,6 +21,7 @@ import java.awt.image.DataBuffer;
 import java.awt.image.SampleModel;
 import java.awt.image.BandedSampleModel;
 import java.awt.image.WritableRaster;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.Static;
 
 
@@ -58,10 +59,7 @@ public final class RasterFactory extends Static {
             int[] bankIndices, int[] bandOffsets, final Point location)
     {
         if (bankIndices == null) {
-            bankIndices = new int[buffer.getNumBanks()];
-            for (int i=1; i<bankIndices.length; i++) {
-                bankIndices[i] = i;
-            }
+            bankIndices = ArraysExt.sequence(0, buffer.getNumBanks());
         }
         if (bandOffsets == null) {
             bandOffsets = new int[bankIndices.length];
diff --git a/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java b/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
index 863a3d9..8c4832f 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
@@ -26,6 +26,7 @@ import java.awt.image.WritableRaster;
 import java.awt.image.WritableRenderedImage;
 import java.nio.FloatBuffer;
 import org.opengis.coverage.grid.SequenceType;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.TestCase;
 import org.junit.After;
@@ -154,7 +155,7 @@ public strictfp class DefaultIteratorTest extends TestCase {
         }
         expected = new float[StrictMath.max(subMaxX - subMinX, 0) * StrictMath.max(subMaxY
- subMinY, 0) * numBands];
         final WritableRaster raster = Raster.createWritableRaster(new PixelInterleavedSampleModel(dataType,
-                width, height, numBands, width * numBands, TiledImageMock.bandOffsets(numBands)),
new Point(xmin, ymin));
+                width, height, numBands, width * numBands, ArraysExt.sequence(0, numBands)),
new Point(xmin, ymin));
         /*
          * At this point, all data structures have been created an initialized to zero sample
values.
          * Now fill the data structures with arbitrary values.
diff --git a/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java b/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java
index 1744867..3697ada 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java
@@ -28,6 +28,7 @@ import java.awt.image.TileObserver;
 import java.awt.image.WritableRaster;
 import java.awt.image.WritableRenderedImage;
 import java.util.Vector;
+import org.apache.sis.util.ArraysExt;
 
 import static org.junit.Assert.*;
 
@@ -97,10 +98,10 @@ final class TiledImageMock implements WritableRenderedImage {
      * @param numBands  number of bands in the sample model to create.
      */
     TiledImageMock(final int dataType,  final int numBands,
-               final int minX,      final int minY,
-               final int width,     final int height,
-               final int tileWidth, final int tileHeight,
-               final int minTileX,  final int minTileY)
+                   final int minX,      final int minY,
+                   final int width,     final int height,
+                   final int tileWidth, final int tileHeight,
+                   final int minTileX,  final int minTileY)
     {
         this.minX        = minX;
         this.minY        = minY;
@@ -114,18 +115,7 @@ final class TiledImageMock implements WritableRenderedImage {
         this.numYTiles   = (height + tileHeight - 1) / tileHeight;
         this.tiles       = new WritableRaster[numXTiles * numYTiles];
         this.sampleModel = new PixelInterleavedSampleModel(dataType, tileWidth, tileHeight,
-                                numBands, tileWidth * numBands, bandOffsets(numBands));
-    }
-
-    /**
-     * Returns arbitrary band offsets for the given number of bands.
-     */
-    static int[] bandOffsets(final int numBands) {
-        final int[] bandOffsets = new int[numBands];
-        for (int i=1; i<numBands; i++) {
-            bandOffsets[i] = i;
-        }
-        return bandOffsets;
+                                numBands, tileWidth * numBands, ArraysExt.sequence(0, numBands));
     }
 
     /*
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
index 249ee0b..95a5400 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
@@ -323,10 +323,7 @@ searchNaN:  for (int flatIndex = (size - 1) * size; --flatIndex >=
0;) {
     {
         final int errorLU = size * size;
         assert errorLU == GeneralMatrix.indexOfErrors(size, size, LU);
-        final int[] pivot = new int[size];
-        for (int j=0; j<size; j++) {
-           pivot[j] = j;
-        }
+        final int[] pivot = ArraysExt.sequence(0, size);
         final double[]  column = new double[size * 2];  // [0 … size-1] : column values;
[size … 2*size-1] : error terms.
         final DoubleDouble acc = new DoubleDouble();    // Temporary variable for sum ("accumulator")
and subtraction.
         final DoubleDouble rat = new DoubleDouble();    // Temporary variable for products
and ratios.
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
index bae1e8a..f0020ae 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
@@ -34,6 +34,7 @@ import org.apache.sis.geometry.GeneralDirectPosition;
 import org.apache.sis.io.wkt.Formatter;
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.Utilities;
+import org.apache.sis.util.ArraysExt;
 
 import static org.apache.sis.util.ArgumentChecks.*;
 
@@ -224,11 +225,7 @@ public class PassThroughTransform extends AbstractMathTransform implements
Seria
      * @see org.apache.sis.referencing.operation.DefaultPassThroughOperation#getModifiedCoordinates()
      */
     public final int[] getModifiedCoordinates() {
-        final int[] index = new int[subTransform.getSourceDimensions()];
-        for (int i=0; i<index.length; i++) {
-            index[i] = i + firstAffectedOrdinate;
-        }
-        return index;
+        return ArraysExt.sequence(firstAffectedOrdinate, subTransform.getSourceDimensions());
     }
 
     /**
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
index 8c416a3..7aac9aa 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
@@ -223,7 +223,7 @@ public class TransformSeparator {
                     isOutOfRange ? "upper" : "lower", min, max-1, isOutOfRange ? upper :
lower));
         }
         if (offset == 0) {
-            sequence = series(lower, upper);
+            sequence = ArraysExt.sequence(lower, upper - lower);
         } else {
             sequence = Arrays.copyOf(sequence, (offset -= lower) + upper);
             for (int i=lower; i<upper; i++) {
@@ -235,17 +235,6 @@ public class TransformSeparator {
     }
 
     /**
-     * Returns a series of increasing values starting at {@code lower}.
-     */
-    private static int[] series(final int lower, final int upper) throws IllegalArgumentException
{
-        final int[] sequence = new int[upper - lower];
-        for (int i = 0; i < sequence.length; i++) {
-            sequence[i] = i + lower;
-        }
-        return sequence;
-    }
-
-    /**
      * Adds input dimensions to keep in the separated transform.
      * The given values are <em>source</em> dimension indices of the transform
given to the constructor.
      *
@@ -423,10 +412,10 @@ public class TransformSeparator {
                 tr = filterTargetDimensions(tr, targetDimensions);
             }
             if (sourceDimensions == null) {
-                sourceDimensions = series(0, transform.getSourceDimensions());
+                sourceDimensions = ArraysExt.sequence(0, transform.getSourceDimensions());
             }
             if (targetDimensions == null) {
-                targetDimensions = series(0, transform.getTargetDimensions());
+                targetDimensions = ArraysExt.sequence(0, transform.getTargetDimensions());
             }
         } else {
             /*
@@ -513,7 +502,7 @@ public class TransformSeparator {
         final int lower  = dimensions[0];
         final int upper  = dimensions[dimensions.length - 1] + 1;
         if (lower == 0 && upper == numSrc && dimensions.length == numSrc)
{
-            targetDimensions = series(0, numTgt);
+            targetDimensions = ArraysExt.sequence(0, numTgt);
             return step;
         }
         if (step.isIdentity()) {
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java b/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
index 5afbff7..3d91f73 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
@@ -67,7 +67,7 @@ import java.lang.reflect.Array;
  * objects.
  *
  * @author Martin Desruisseaux (IRD, Geomatys)
- * @version 0.8
+ * @version 1.0
  *
  * @see Arrays
  *
@@ -1258,6 +1258,25 @@ public final class ArraysExt extends Static {
     }
 
     /**
+     * Returns a sequence of increasing values of the given length. Each value is increased
by 1.
+     * For example {@code sequence(-1, 4)} returns {@code {-1, 0, 1, 2}}. This method is
a convenience for
+     * enumerating a subset of dimensions in a coordinate reference system or a subset of
bands in an image.
+     *
+     * @param  start   first value in the array to return.
+     * @param  length  number of values to return.
+     * @return a sequence of increasing integers starting at {@code start} and having {@code
length} values.
+     *
+     * @since 1.0
+     */
+    public static int[] sequence(final int start, final int length) {
+        final int[] array = new int[length];
+        for (int i=0; i<length; i++) {
+            array[i] = start + i;
+        }
+        return array;
+    }
+
+    /**
      * Returns {@code true} if all elements in the specified array are in increasing order.
      * Special cases:
      *


Mime
View raw message