https://github.com/forax/struct-of-array
Two data structures with the API of an array of structs (AoS) and the internals of a struct of arrays (SoA)
https://github.com/forax/struct-of-array
data-structures game-development java struct-of-arrays
Last synced: 3 months ago
JSON representation
Two data structures with the API of an array of structs (AoS) and the internals of a struct of arrays (SoA)
- Host: GitHub
- URL: https://github.com/forax/struct-of-array
- Owner: forax
- License: apache-2.0
- Created: 2022-08-20T17:33:44.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-09-01T06:13:12.000Z (over 3 years ago)
- Last Synced: 2023-03-22T18:53:52.980Z (about 3 years ago)
- Topics: data-structures, game-development, java, struct-of-arrays
- Language: Java
- Homepage:
- Size: 81.1 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# struct-of-array
Two data structure with the API of an array of structs (AoS) and the internals of a
[struct of arrays (SoA)](https://en.wikipedia.org/wiki/AoS_and_SoA)
The API provides two data structure, a `StructOfArrayList` that have an API similar to an `ArrayList`
of records but stores the components of the record in different arrays and a `StructOfArrayMap`
that works like `HashMap` of integers (a sparse array) using the same destructuring.
```java
record Person(int age, String name) {}
var soaList = StructOfArrayList.of(MethodHandles.lookup(), Person.class);
soaList.add(new Person(36, "Ana"));
soaList.add(new Person(18, "Bob"));
System.out.println(soaList.get(0)); // Person[36, Ana]
```
Internally, the list stores an array of ints for the ages and an array of objects (the reference types are erased)
for the names.
And a similar example using `StructOfArrayMap`
```java
var soaMap = StructOfArrayMap.of(MethodHandles.lookup(), Person.class);
soaMap.put(10, new Person(36, "Ana"));
soaMap.put(6, new Person(18, "Bob"));
System.out.println(soaList.get(6)); // Person[18, Bob]
```
### How to build ?
Just use Maven with Java 17+
```bash
mvn package
```