Skip to content

Commit 9875c66

Browse files
committed
feat iter : add direct access field array
1 parent c1566de commit 9875c66

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

  • src/main/java/com/github/elebras1/flecs

src/main/java/com/github/elebras1/flecs/Iter.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,61 @@ public <T> String fieldString(Class<T> componentClass, int index, String fieldNa
180180
return this.cachedColumns[index].asSlice(offset, capacity).getString(0);
181181
}
182182

183+
public <T> int[] fieldIntArray(Class<T> componentClass, int index, String fieldName, int i) {
184+
long offset = fieldPtr(componentClass, index, fieldName, i);
185+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
186+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
187+
return this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_INT);
188+
}
189+
190+
public <T> long[] fieldLongArray(Class<T> componentClass, int index, String fieldName, int i) {
191+
long offset = fieldPtr(componentClass, index, fieldName, i);
192+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
193+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
194+
return this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_LONG);
195+
}
196+
197+
public <T> float[] fieldFloatArray(Class<T> componentClass, int index, String fieldName, int i) {
198+
long offset = fieldPtr(componentClass, index, fieldName, i);
199+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
200+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
201+
return this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_FLOAT);
202+
}
203+
204+
public <T> double[] fieldDoubleArray(Class<T> componentClass, int index, String fieldName, int i) {
205+
long offset = fieldPtr(componentClass, index, fieldName, i);
206+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
207+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
208+
return this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_DOUBLE);
209+
}
210+
211+
public <T> byte[] fieldByteArray(Class<T> componentClass, int index, String fieldName, int i) {
212+
long offset = fieldPtr(componentClass, index, fieldName, i);
213+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
214+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
215+
return this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_BYTE);
216+
}
217+
218+
public <T> short[] fieldShortArray(Class<T> componentClass, int index, String fieldName, int i) {
219+
long offset = fieldPtr(componentClass, index, fieldName, i);
220+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
221+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
222+
return this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_SHORT);
223+
}
224+
225+
public <T> boolean[] fieldBooleanArray(Class<T> componentClass, int index, String fieldName, int i) {
226+
long offset = fieldPtr(componentClass, index, fieldName, i);
227+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
228+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
229+
230+
byte[] bytes = this.cachedColumns[index].asSlice(offset, arrayByteSize).toArray(ValueLayout.JAVA_BYTE);
231+
boolean[] result = new boolean[bytes.length];
232+
for (int j = 0; j < bytes.length; j++) {
233+
result[j] = bytes[j] != 0;
234+
}
235+
return result;
236+
}
237+
183238
public <T> void setFieldInt(Class<T> componentClass, int index, String fieldName, int i, int value) {
184239
long offset = fieldPtr(componentClass, index, fieldName, i);
185240
this.cachedColumns[index].set(ValueLayout.JAVA_INT, offset, value);
@@ -230,6 +285,71 @@ public <T> void setFieldString(Class<T> componentClass, int index, String fieldN
230285
slice.set(ValueLayout.JAVA_BYTE, len, (byte) 0);
231286
}
232287

288+
public <T> void setFieldIntArray(Class<T> componentClass, int index, String fieldName, int i, int[] value) {
289+
long offset = fieldPtr(componentClass, index, fieldName, i);
290+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
291+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
292+
int capacity = (int) (arrayByteSize / ValueLayout.JAVA_INT.byteSize());
293+
int len = Math.min(value.length, capacity);
294+
MemorySegment.copy(value, 0, this.cachedColumns[index], ValueLayout.JAVA_INT, offset, len);
295+
}
296+
297+
public <T> void setFieldLongArray(Class<T> componentClass, int index, String fieldName, int i, long[] value) {
298+
long offset = fieldPtr(componentClass, index, fieldName, i);
299+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
300+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
301+
int capacity = (int) (arrayByteSize / ValueLayout.JAVA_LONG.byteSize());
302+
int len = Math.min(value.length, capacity);
303+
MemorySegment.copy(value, 0, this.cachedColumns[index], ValueLayout.JAVA_LONG, offset, len);
304+
}
305+
306+
public <T> void setFieldFloatArray(Class<T> componentClass, int index, String fieldName, int i, float[] value) {
307+
long offset = fieldPtr(componentClass, index, fieldName, i);
308+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
309+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
310+
int capacity = (int) (arrayByteSize / ValueLayout.JAVA_FLOAT.byteSize());
311+
int len = Math.min(value.length, capacity);
312+
MemorySegment.copy(value, 0, this.cachedColumns[index], ValueLayout.JAVA_FLOAT, offset, len);
313+
}
314+
315+
public <T> void setFieldDoubleArray(Class<T> componentClass, int index, String fieldName, int i, double[] value) {
316+
long offset = fieldPtr(componentClass, index, fieldName, i);
317+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
318+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
319+
int capacity = (int) (arrayByteSize / ValueLayout.JAVA_DOUBLE.byteSize());
320+
int len = Math.min(value.length, capacity);
321+
MemorySegment.copy(value, 0, this.cachedColumns[index], ValueLayout.JAVA_DOUBLE, offset, len);
322+
}
323+
324+
public <T> void setFieldByteArray(Class<T> componentClass, int index, String fieldName, int i, byte[] value) {
325+
long offset = fieldPtr(componentClass, index, fieldName, i);
326+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
327+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
328+
int capacity = (int) (arrayByteSize / ValueLayout.JAVA_BYTE.byteSize());
329+
int len = Math.min(value.length, capacity);
330+
MemorySegment.copy(value, 0, this.cachedColumns[index], ValueLayout.JAVA_BYTE, offset, len);
331+
}
332+
333+
public <T> void setFieldShortArray(Class<T> componentClass, int index, String fieldName, int i, short[] value) {
334+
long offset = fieldPtr(componentClass, index, fieldName, i);
335+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
336+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
337+
int capacity = (int) (arrayByteSize / ValueLayout.JAVA_SHORT.byteSize());
338+
int len = Math.min(value.length, capacity);
339+
MemorySegment.copy(value, 0, this.cachedColumns[index], ValueLayout.JAVA_SHORT, offset, len);
340+
}
341+
342+
public <T> void setFieldBooleanArray(Class<T> componentClass, int index, String fieldName, int i, boolean[] value) {
343+
long offset = fieldPtr(componentClass, index, fieldName, i);
344+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
345+
long arrayByteSize = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
346+
int capacity = (int) arrayByteSize;
347+
int len = Math.min(value.length, capacity);
348+
for (int j = 0; j < len; j++) {
349+
this.cachedColumns[index].set(ValueLayout.JAVA_BYTE, offset + j, (byte) (value[j] ? 1 : 0));
350+
}
351+
}
352+
233353
public long event() {
234354
return ecs_iter_t.event(this.nativeIter);
235355
}

0 commit comments

Comments
 (0)