From c1a119466dee6cb93c2e99d17ccb6cbe8af36e07 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Mon, 15 Jul 2024 16:32:07 -0500 Subject: [PATCH] Make shm the third, not first, NDArray attribute So that it matches the Python implementation of Appose. The reason it's third in Python is so that it can have the default parameter value of None, to provide feature parity with the Java version's two-argument NDAray constructor that creates the SharedMemory. --- src/main/java/org/apposed/appose/NDArray.java | 31 ++++++++++--------- src/main/java/org/apposed/appose/Types.java | 4 +-- .../apposed/appose/NDArrayExampleGroovy.java | 3 +- .../apposed/appose/NDArrayExamplePython.java | 3 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/apposed/appose/NDArray.java b/src/main/java/org/apposed/appose/NDArray.java index 7d3efe9..ab6c0e3 100644 --- a/src/main/java/org/apposed/appose/NDArray.java +++ b/src/main/java/org/apposed/appose/NDArray.java @@ -43,7 +43,7 @@ public class NDArray implements AutoCloseable { /** * shared memory containing the flattened array data. */ - private final SharedMemory sharedMemory; + private final SharedMemory shm; /** * data type of the array elements. @@ -56,14 +56,15 @@ public class NDArray implements AutoCloseable { private final Shape shape; /** - * Constructs an {@code NDArray} with the specified {@code SharedMemory}. + * Constructs an {@code NDArray} with the specified data type, shape, + * and {@code SharedMemory}. * - * @param sharedMemory the flattened array data. * @param dType element data type * @param shape array shape + * @param shm the flattened array data. */ - public NDArray(final SharedMemory sharedMemory, final DType dType, final Shape shape) { - this.sharedMemory = sharedMemory; + public NDArray(final DType dType, final Shape shape, final SharedMemory shm) { + this.shm = shm; this.dType = dType; this.shape = shape; } @@ -76,8 +77,8 @@ public NDArray(final SharedMemory sharedMemory, final DType dType, final Shape s * @param shape array shape */ public NDArray(final DType dType, final Shape shape) { - this(SharedMemory.create(null, - safeInt(shape.numElements() * dType.bytesPerElement())), dType, shape); + this(dType, shape, SharedMemory.create(null, + safeInt(shape.numElements() * dType.bytesPerElement()))); } /** @@ -98,7 +99,7 @@ public Shape shape() { * @return The shared memory block containing the array data. */ public SharedMemory shm() { - return sharedMemory; + return shm; } /** @@ -108,7 +109,7 @@ public SharedMemory shm() { */ public ByteBuffer buffer() { final long length = shape.numElements() * dType.bytesPerElement(); - return sharedMemory.pointer().getByteBuffer(0, length); + return shm.pointer().getByteBuffer(0, length); } /** @@ -116,16 +117,16 @@ public ByteBuffer buffer() { */ @Override public void close() { - sharedMemory.close(); + shm.close(); } @Override public String toString() { - return "NDArray{" + - "sharedMemory=" + sharedMemory + - ", dType=" + dType + - ", shape=" + shape + - '}'; + return "NDArray(" + + "dType=" + dType + + ", shape=" + shape + + ", shm=" + shm + + ")"; } /** diff --git a/src/main/java/org/apposed/appose/Types.java b/src/main/java/org/apposed/appose/Types.java index 811ff0c..7180bb0 100644 --- a/src/main/java/org/apposed/appose/Types.java +++ b/src/main/java/org/apposed/appose/Types.java @@ -163,10 +163,10 @@ private static Object processValue(Object value) { final int size = (int) map.get("size"); return SharedMemory.attach(name, size); case "ndarray": - final SharedMemory shm = (SharedMemory) map.get("shm"); final NDArray.DType dType = toDType((String) map.get("dtype")); final NDArray.Shape shape = toShape((List) map.get("shape")); - return new NDArray(shm, dType, shape); + final SharedMemory shm = (SharedMemory) map.get("shm"); + return new NDArray(dType, shape, shm); default: System.err.println("unknown appose_type \"" + appose_type + "\""); } diff --git a/src/test/java/org/apposed/appose/NDArrayExampleGroovy.java b/src/test/java/org/apposed/appose/NDArrayExampleGroovy.java index 1467656..0948605 100644 --- a/src/test/java/org/apposed/appose/NDArrayExampleGroovy.java +++ b/src/test/java/org/apposed/appose/NDArrayExampleGroovy.java @@ -13,7 +13,8 @@ public static void main(String[] args) throws Exception { // create a FLOAT32 NDArray with shape (4,3,2) in F_ORDER // respectively (2,3,4) in C_ORDER final NDArray.DType dType = NDArray.DType.FLOAT32; - final NDArray ndArray = new NDArray(dType, new NDArray.Shape(F_ORDER, 4, 3, 2)); + final NDArray.Shape shape = new NDArray.Shape(F_ORDER, 4, 3, 2); + final NDArray ndArray = new NDArray(dType, shape); // fill with values 0..23 in flat iteration order final FloatBuffer buf = ndArray.buffer().asFloatBuffer(); diff --git a/src/test/java/org/apposed/appose/NDArrayExamplePython.java b/src/test/java/org/apposed/appose/NDArrayExamplePython.java index 4a80b39..80b7942 100644 --- a/src/test/java/org/apposed/appose/NDArrayExamplePython.java +++ b/src/test/java/org/apposed/appose/NDArrayExamplePython.java @@ -13,7 +13,8 @@ public static void main(String[] args) throws Exception { // create a FLOAT32 NDArray with shape (4,3,2) in F_ORDER // respectively (2,3,4) in C_ORDER final NDArray.DType dType = NDArray.DType.FLOAT32; - final NDArray ndArray = new NDArray(dType, new NDArray.Shape(F_ORDER, 4, 3, 2)); + final NDArray.Shape = new NDArray.Shape(F_ORDER, 4, 3, 2); + final NDArray ndArray = new NDArray(dType, shape); // fill with values 0..23 in flat iteration order final FloatBuffer buf = ndArray.buffer().asFloatBuffer();