Skip to content

Commit 8424e60

Browse files
committed
Update Native Arrays
1 parent d5ef07a commit 8424e60

8 files changed

Lines changed: 95 additions & 28 deletions

File tree

src/main/java/volucris/bindings/core/NativeByteArray.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public NativeByteArray(Arena arena, int count) {
1919
public NativeByteArray(Arena arena, String string) {
2020
segment = arena.allocateFrom(string);
2121
}
22-
22+
2323
public NativeByteArray(MemorySegment segment) {
2424
this.segment = segment;
2525
}
26-
26+
2727
public byte get(int index) {
2828
return segment.getAtIndex(ValueLayout.JAVA_BYTE, index);
2929
}
@@ -34,17 +34,22 @@ public NativeByteArray set(int index, byte value) {
3434
}
3535

3636
public NativeByteArray set(int dstIndex, int srcIndex, int count, NativeByteArray values) {
37-
for (int i = 0; i < count; i++)
38-
set(dstIndex + i, values.get(srcIndex + i));
37+
MemorySegment.copy(
38+
values.memorySegment(), ValueLayout.JAVA_BYTE,
39+
srcIndex,
40+
segment, ValueLayout.JAVA_BYTE,
41+
dstIndex,
42+
count
43+
);
3944
return this;
4045
}
41-
46+
4247
public NativeByteArray fill(int index, int count, byte value) {
4348
for (int i = 0; i < count; i++)
4449
set(index + i, value);
4550
return this;
4651
}
47-
52+
4853
public NativeByteArray set(int dstIndex, int srcIndex, int count, byte... values) {
4954
long dstOffset = dstIndex * ValueLayout.JAVA_BYTE.byteSize();
5055
MemorySegment.copy(values, srcIndex, segment, ValueLayout.JAVA_BYTE, dstOffset, count);
@@ -64,7 +69,7 @@ public byte[] toArray() {
6469
public String getString() {
6570
return segment.getString(0);
6671
}
67-
72+
6873
public MemorySegment memorySegment() {
6974
return segment;
7075
}

src/main/java/volucris/bindings/core/NativeDoubleArray.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ public NativeDoubleArray set(int index, double value) {
3030
}
3131

3232
public NativeDoubleArray set(int dstIndex, int srcIndex, int count, NativeDoubleArray values) {
33-
for (int i = 0; i < count; i++)
34-
set(dstIndex + i, values.get(srcIndex + i));
33+
MemorySegment.copy(
34+
values.memorySegment(), ValueLayout.JAVA_DOUBLE,
35+
srcIndex * ValueLayout.JAVA_DOUBLE.byteSize(),
36+
segment, ValueLayout.JAVA_DOUBLE,
37+
dstIndex * ValueLayout.JAVA_DOUBLE.byteSize(),
38+
count
39+
);
3540
return this;
3641
}
3742

src/main/java/volucris/bindings/core/NativeFloatArray.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ public NativeFloatArray set(int index, float value) {
3030
}
3131

3232
public NativeFloatArray set(int dstIndex, int srcIndex, int count, NativeFloatArray values) {
33-
for (int i = 0; i < count; i++)
34-
set(dstIndex + i, values.get(srcIndex + i));
33+
MemorySegment.copy(
34+
values.memorySegment(), ValueLayout.JAVA_FLOAT,
35+
srcIndex * ValueLayout.JAVA_FLOAT.byteSize(),
36+
segment, ValueLayout.JAVA_FLOAT,
37+
dstIndex * ValueLayout.JAVA_FLOAT.byteSize(),
38+
count
39+
);
3540
return this;
3641
}
3742

src/main/java/volucris/bindings/core/NativeIntArray.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ public NativeIntArray set(int index, int value) {
3030
}
3131

3232
public NativeIntArray set(int dstIndex, int srcIndex, int count, NativeIntArray values) {
33-
for (int i = 0; i < count; i++)
34-
set(dstIndex + i, values.get(srcIndex + i));
33+
MemorySegment.copy(
34+
values.memorySegment(), ValueLayout.JAVA_INT,
35+
srcIndex * ValueLayout.JAVA_INT.byteSize(),
36+
segment, ValueLayout.JAVA_INT,
37+
dstIndex * ValueLayout.JAVA_INT.byteSize(),
38+
count
39+
);
3540
return this;
3641
}
3742

src/main/java/volucris/bindings/core/NativeLongArray.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ public NativeLongArray set(int index, long value) {
3030
}
3131

3232
public NativeLongArray set(int dstIndex, int srcIndex, int count, NativeLongArray values) {
33-
for (int i = 0; i < count; i++)
34-
set(dstIndex + i, values.get(srcIndex + i));
33+
MemorySegment.copy(
34+
values.memorySegment(), ValueLayout.JAVA_LONG,
35+
srcIndex * ValueLayout.JAVA_LONG.byteSize(),
36+
segment, ValueLayout.JAVA_LONG,
37+
dstIndex * ValueLayout.JAVA_LONG.byteSize(),
38+
count
39+
);
3540
return this;
3641
}
3742

src/main/java/volucris/bindings/core/NativePointerArray.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.lang.foreign.Arena;
44
import java.lang.foreign.MemorySegment;
5+
import java.util.ArrayList;
56
import java.util.Collection;
7+
import java.util.List;
68

79
import static volucris.bindings.core.FFMUtils.UNBOUNDED_ADDRESS;
810

@@ -14,6 +16,17 @@ public NativePointerArray(Arena arena, int count) {
1416
segment = arena.allocate(UNBOUNDED_ADDRESS, count);
1517
}
1618

19+
public NativePointerArray(Arena arena, MemorySegment pointer) {
20+
segment = arena.allocateFrom(UNBOUNDED_ADDRESS, pointer);
21+
}
22+
23+
public NativePointerArray(Arena arena, MemorySegment... pointers) {
24+
segment = arena.allocate(UNBOUNDED_ADDRESS, pointers.length);
25+
26+
for (int i = 0; i < pointers.length; i++)
27+
set(i, pointers[i]);
28+
}
29+
1730
public MemorySegment get(int index) {
1831
return segment.getAtIndex(UNBOUNDED_ADDRESS, index);
1932
}
@@ -28,8 +41,13 @@ public NativePointerArray set(int index, MemorySegment value) {
2841
}
2942

3043
public NativePointerArray set(int dstIndex, int srcIndex, int count, NativePointerArray pointers) {
31-
for (int i = 0; i < count; i++)
32-
set(dstIndex + i, pointers.get(srcIndex + i));
44+
MemorySegment.copy(
45+
pointers.memorySegment(), UNBOUNDED_ADDRESS,
46+
srcIndex * UNBOUNDED_ADDRESS.byteSize(),
47+
segment, UNBOUNDED_ADDRESS,
48+
dstIndex * UNBOUNDED_ADDRESS.byteSize(),
49+
count
50+
);
3351
return this;
3452
}
3553

@@ -44,6 +62,14 @@ public NativePointerArray setString(Arena arena, int index, String value) {
4462
return this;
4563
}
4664

65+
public Collection<String> getStrings(int index, int count) {
66+
List<String> list = new ArrayList<String>();
67+
for (int i = 0; i < count; i++) {
68+
list.add(getString(index + i));
69+
}
70+
return list;
71+
}
72+
4773
public String getString(int index) {
4874
return segment.getAtIndex(UNBOUNDED_ADDRESS, index).getString(0);
4975
}

src/main/java/volucris/bindings/core/NativeShortArray.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@ public NativeShortArray set(int index, short value) {
3030
}
3131

3232
public NativeShortArray set(int dstIndex, int srcIndex, int count, NativeShortArray values) {
33-
for (int i = 0; i < count; i++)
34-
set(dstIndex + i, values.get(srcIndex + i));
33+
MemorySegment.copy(
34+
values.memorySegment(), ValueLayout.JAVA_SHORT,
35+
srcIndex * ValueLayout.JAVA_SHORT.byteSize(),
36+
segment, ValueLayout.JAVA_SHORT,
37+
dstIndex * ValueLayout.JAVA_SHORT.byteSize(),
38+
count
39+
);
3540
return this;
3641
}
37-
42+
3843
public NativeShortArray fill(int index, int count, short value) {
3944
for (int i = 0; i < count; i++)
4045
set(index + i, value);
4146
return this;
4247
}
43-
48+
4449
public NativeShortArray set(int dstIndex, int srcIndex, int count, short... values) {
4550
long dstOffset = dstIndex * ValueLayout.JAVA_SHORT.byteSize();
4651
MemorySegment.copy(values, srcIndex, segment, ValueLayout.JAVA_SHORT, dstOffset, count);

src/main/java/volucris/bindings/core/NativeStructArray.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.lang.foreign.Arena;
44
import java.lang.foreign.MemorySegment;
5-
import java.lang.foreign.StructLayout;
5+
import java.lang.foreign.GroupLayout;
66
import java.util.function.Consumer;
77
import java.util.function.Function;
88

99
public class NativeStructArray<T extends Struct<T>> {
1010

11-
private final StructLayout layout;
11+
private final GroupLayout layout;
1212

1313
private final MemorySegment segment;
1414

@@ -17,7 +17,7 @@ public class NativeStructArray<T extends Struct<T>> {
1717
private final T[] cache;
1818

1919
@SuppressWarnings("unchecked")
20-
public NativeStructArray(Arena arena, StructLayout layout, Function<MemorySegment, T> factory, int count) {
20+
public NativeStructArray(Arena arena, GroupLayout layout, Function<MemorySegment, T> factory, int count) {
2121
this.segment = arena.allocate(layout, count);
2222
this.layout = layout;
2323
this.factory = factory;
@@ -26,7 +26,7 @@ public NativeStructArray(Arena arena, StructLayout layout, Function<MemorySegmen
2626
}
2727

2828
@SuppressWarnings("unchecked")
29-
public NativeStructArray(MemorySegment segment, StructLayout layout, Function<MemorySegment, T> factory) {
29+
public NativeStructArray(MemorySegment segment, GroupLayout layout, Function<MemorySegment, T> factory) {
3030
this.segment = segment;
3131
this.layout = layout;
3232
this.factory = factory;
@@ -59,16 +59,27 @@ public NativeStructArray<T> set(int index, T other) {
5959
}
6060

6161
public NativeStructArray<T> set(int dstIndex, int srcIndex, int count, NativeStructArray<T> values) {
62-
for (int i = 0; i < count; i++)
63-
set(dstIndex + i, values.get(srcIndex + i));
62+
MemorySegment.copy(
63+
values.memorySegment(),
64+
srcIndex * layout.byteSize(),
65+
segment, dstIndex * layout.byteSize(),
66+
count * layout.byteSize()
67+
);
6468
return this;
6569
}
66-
70+
6771
public NativeStructArray<T> forElement(int index, Consumer<T> consumer) {
6872
consumer.accept(get(index));
6973
return this;
7074
}
7175

76+
public NativeStructArray<T> forEach(int count, Consumer<T> consumer) {
77+
for (int i = 0; i < count; i++) {
78+
forElement(i, consumer);
79+
}
80+
return this;
81+
}
82+
7283
public MemorySegment memorySegment() {
7384
return segment;
7485
}

0 commit comments

Comments
 (0)