This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit f9683ae67c73c6f30ab5c89aed8f003c206f53f1 Author: jsorel <[email protected]> AuthorDate: Mon Nov 30 14:07:52 2026 +0100 feat(Math): add Array.toArrayLong() --- .../main/org/apache/sis/maths/Array.java | 26 +++++++++++++++ .../org/apache/sis/maths/ArrayFactoryJava.java | 39 +++++++++++++++++----- .../org/apache/sis/maths/ArrayUnmodifiable.java | 10 ++++++ .../main/org/apache/sis/maths/NDArrays.java | 2 +- .../org/apache/sis/maths/AbstractArrayTest.java | 20 +++++++++++ .../test/org/apache/sis/maths/ArrayNlTest.java | 32 ++++++++++++++++++ .../test/org/apache/sis/maths/ArrayNuiTest.java | 22 ++++++++++++ .../test/org/apache/sis/maths/NDArraysTest.java | 11 ++++++ 8 files changed, 153 insertions(+), 9 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Array.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Array.java index 606c17b6b6..4d18633a2b 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Array.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Array.java @@ -173,6 +173,14 @@ public interface Array extends NDArray { return toArrayInt(0, Math.toIntExact(getLength())); } + /** + * @return this array tuples as an interleaved array cast to longs. + * Example : [x1,x2,x3, y1,y2,y3, ... z1, z2,z3] + */ + default long[] toArrayLong() { + return toArrayLong(0, Math.toIntExact(getLength())); + } + /** * @return this array tuples as an interleaved array cast to floats. * Example : [x1,x2,x3, y1,y2,y3, ... z1, z2,z3] @@ -243,6 +251,24 @@ public interface Array extends NDArray { return array; } + /** + * The default implementation casts the {@code double} values, which is exact for all data types + * except 64 bits integers whose magnitude exceeds 2<sup>53</sup>. + * + * @param offset index of the first tuple to copy + * @param nbTuple number of tuples to copy + * @return this array tuples as an interleaved array cast to longs. + * Example : [x1,x2,x3, y1,y2,y3, ... z1, z2,z3] + */ + default long[] toArrayLong(long offset, int nbTuple) { + final double[] values = toArrayDouble(offset, nbTuple); + final long[] array = new long[values.length]; + for (int i = 0; i < values.length; i++) { + array[i] = (long) values[i]; + } + return array; + } + /** * @param offset index of the first tuple to copy * @param nbTuple number of tuples to copy diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayFactoryJava.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayFactoryJava.java index d368614c99..fdee327f5d 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayFactoryJava.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayFactoryJava.java @@ -882,7 +882,7 @@ public final class ArrayFactoryJava implements ArrayFactory { public void set(long index, Vector<?> tuple) { final int offset = Math.toIntExact(index * dimension); for (int i=0;i<dimension;i++) { - array[offset+i] = (int) tuple.get(i); + array[offset+i] = (int) (long) tuple.get(i); } } @@ -959,10 +959,18 @@ public final class ArrayFactoryJava implements ArrayFactory { @Override public void set(long index, Array array, long offset, long nb) { ArgumentChecks.ensureCountBetween("dimension", true, dimension, dimension, array.getDimension()); - final int[] arr = array.toArrayInt(offset, Math.toIntExact(nb)); - System.arraycopy(arr, 0, this.array, - Math.toIntExact(index*dimension), - Math.toIntExact(nb*dimension)); + final int start = Math.toIntExact(index*dimension); + final int length = Math.toIntExact(nb*dimension); + final DataType source = array.getDataType(); + if (source == DataType.INT || source == DataType.UINT) { + System.arraycopy(array.toArrayInt(offset, Math.toIntExact(nb)), 0, this.array, start, length); + } else { + // Values of 2^31 and more do not fit in an int: narrow from long to keep the unsigned bit pattern. + final long[] arr = array.toArrayLong(offset, Math.toIntExact(nb)); + for (int i = 0; i < length; i++) { + this.array[start + i] = (int) arr[i]; + } + } } @Override @@ -985,7 +993,7 @@ public final class ArrayFactoryJava implements ArrayFactory { @Override public void set(long tupleIndex, int sampleIndex, double value) { - array[Math.toIntExact(tupleIndex*dimension + sampleIndex)] = (int) value; + array[Math.toIntExact(tupleIndex*dimension + sampleIndex)] = (int) (long) value; } }; } @@ -1024,7 +1032,7 @@ public final class ArrayFactoryJava implements ArrayFactory { public void set(long index, Vector<?> tuple) { final int offset = Math.toIntExact(index * dimension); for (int i=0;i<dimension;i++) { - array[offset+i] = (int) tuple.get(i); + array[offset+i] = (long) tuple.get(i); } } @@ -1095,6 +1103,21 @@ public final class ArrayFactoryJava implements ArrayFactory { return result; } + @Override + public void set(long index, Array array, long offset, long nb) { + ArgumentChecks.ensureCountBetween("dimension", true, dimension, dimension, array.getDimension()); + final long[] arr = array.toArrayLong(offset, Math.toIntExact(nb)); + System.arraycopy(arr, 0, this.array, + Math.toIntExact(index*dimension), + Math.toIntExact(nb*dimension)); + } + + @Override + public long[] toArrayLong(long offset, int nbTuple) { + final int start = Math.toIntExact(offset * dimension); + return Arrays.copyOfRange(array, start, start + nbTuple * dimension); + } + @Override public Long resize(long newSize) { return new Long(type, Arrays.copyOf(array, Math.toIntExact(newSize*dimension))); @@ -1115,7 +1138,7 @@ public final class ArrayFactoryJava implements ArrayFactory { @Override public void set(long tupleIndex, int sampleIndex, double value) { - array[Math.toIntExact(tupleIndex*dimension + sampleIndex)] = (int) value; + array[Math.toIntExact(tupleIndex*dimension + sampleIndex)] = (long) value; } }; } diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayUnmodifiable.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayUnmodifiable.java index 64b77b1ba2..62293d8fad 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayUnmodifiable.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/ArrayUnmodifiable.java @@ -112,6 +112,11 @@ final class ArrayUnmodifiable extends AbstractArray { return parent.toArrayInt(); } + @Override + public long[] toArrayLong() { + return parent.toArrayLong(); + } + @Override public float[] toArrayFloat() { return parent.toArrayFloat(); @@ -137,6 +142,11 @@ final class ArrayUnmodifiable extends AbstractArray { return parent.toArrayInt(offset, nbTuple); } + @Override + public long[] toArrayLong(long offset, int nbTuple) { + return parent.toArrayLong(offset, nbTuple); + } + @Override public float[] toArrayFloat(long offset, int nbTuple) { return parent.toArrayFloat(offset, nbTuple); diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/NDArrays.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/NDArrays.java index 1b01493814..5807800323 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/NDArrays.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/NDArrays.java @@ -311,7 +311,7 @@ public final class NDArrays { public static Array group(Array ... arrays) throws FactoryException { ArgumentChecks.ensureNonEmpty("arrays", arrays); final long length = arrays[0].getLength(); - if (length == 1) return arrays[0].copy(); + if (arrays.length == 1) return arrays[0].copy(); final DataType dataType = arrays[0].getDataType(); final CoordinateReferenceSystem[] crs = new CoordinateReferenceSystem[arrays.length]; final List<SampleDimension> allDims = new ArrayList<>(); diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractArrayTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractArrayTest.java index 395b0a777a..e5f40e2b50 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractArrayTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractArrayTest.java @@ -169,6 +169,16 @@ public abstract class AbstractArrayTest { } } + {//to long array + final long[] table = array.toArrayLong(); + inc = 0; + for (long i = 0, n = length; i < n; i++) { + for (int d = 0; d < dimension; d++) { + assertEquals(inc++, table[(int)(i * dimension + d)]); + } + } + } + {//to float array final float[] table = array.toArrayFloat(); inc = 0; @@ -232,6 +242,16 @@ public abstract class AbstractArrayTest { } } + {//to long array + final long[] table = array.toArrayLong(rangeStart, rangeLength); + inc = rangeStart*dimension; + for (int i = 0; i < rangeLength; i++) { + for (int d = 0; d < dimension; d++) { + assertEquals(inc++, table[i * dimension + d]); + } + } + } + {//to float array final float[] table = array.toArrayFloat(rangeStart, rangeLength); inc = rangeStart*dimension; diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNlTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNlTest.java index 607df155e2..5ee1a3de9e 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNlTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNlTest.java @@ -16,6 +16,10 @@ */ package org.apache.sis.maths; +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + /** * @@ -33,4 +37,32 @@ public class ArrayNlTest extends AbstractArrayTest { return new ArrayFactoryJava.Long(SampleSystem.ofSize(dim), new long[length*dim]); } + /** + * Tests that values outside the 32 bits range are stored, through a vector, a cursor and another array. + */ + @Test + public void testSetLargeValues() { + final Array array = create(1, 2); + array.set(0, Vector.of(1099511627776.0)); + final Cursor cursor = array.cursor(); + cursor.moveTo(1); + cursor.setSample(0, -1099511627776.0); + assertArrayEquals(new long[] {1099511627776L, -1099511627776L}, array.toArrayLong()); + + final Array copy = create(1, 3); + copy.set(1, new ArrayFactoryJava.Long(SampleSystem.ofSize(1), new long[] {9007199254740993L, Long.MIN_VALUE}), 0, 2); + assertArrayEquals(new long[] {0, 9007199254740993L, Long.MIN_VALUE}, copy.toArrayLong()); + } + + /** + * Tests that {@link Array#toArrayLong()} keeps the values which a {@code double} cannot represent. + */ + @Test + public void testToArrayLongExact() { + final Array array = new ArrayFactoryJava.Long(SampleSystem.ofSize(2), + new long[] {9007199254740993L, -9007199254740993L, Long.MAX_VALUE, Long.MIN_VALUE}); + assertArrayEquals(new long[] {9007199254740993L, -9007199254740993L, Long.MAX_VALUE, Long.MIN_VALUE}, array.toArrayLong()); + assertArrayEquals(new long[] {Long.MAX_VALUE, Long.MIN_VALUE}, array.toArrayLong(1, 1)); + } + } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNuiTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNuiTest.java index a9eca54f3c..9b191d3e86 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNuiTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/ArrayNuiTest.java @@ -16,6 +16,10 @@ */ package org.apache.sis.maths; +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + /** * @@ -33,4 +37,22 @@ public class ArrayNuiTest extends AbstractArrayTest { return new ArrayFactoryJava.UInt(SampleSystem.ofSize(dim), new int[length*dim]); } + /** + * Tests that values of 2<sup>31</sup> and more are stored, through a vector, a cursor and another array. + */ + @Test + public void testSetLargeValues() { + final Array array = create(1, 2); + array.set(0, Vector.of(4294967295.0)); + final Cursor cursor = array.cursor(); + cursor.moveTo(1); + cursor.setSample(0, 2147483648.0); + assertArrayEquals(new double[] {4294967295.0, 2147483648.0}, array.toArrayDouble()); + + final Array copy = create(1, 2); + copy.set(0, NDArrays.of(1, 2147483648.0, 4294967295.0), 0, 2); + assertArrayEquals(new double[] {2147483648.0, 4294967295.0}, copy.toArrayDouble()); + assertEquals(array, array.retype(DataType.DOUBLE).retype(DataType.UINT)); + } + } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/NDArraysTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/NDArraysTest.java index d69d95c150..a3c2209606 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/NDArraysTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/NDArraysTest.java @@ -103,6 +103,17 @@ public class NDArraysTest { group.toArrayDouble(), 0.0); } + /** + * Tests the grouping of arrays of one tuple each. + */ + @Test + public void testGroupSingleTuple() throws FactoryException { + final Array group = NDArrays.group(NDArrays.of(1, 5.0), NDArrays.of(2, 6.0, 7.0)); + assertEquals(1, group.getLength()); + assertEquals(3, group.getSampleSystem().getSize()); + assertArrayEquals(new double[] {5, 6, 7}, group.toArrayDouble()); + } + /** * Test array group size different */
