Context:
Hello, I am making a Java game with terrain as chunks. (3d chunks but that's not crucial and I would like to focus on single dimension to make the questing simpler)
I was thinking about how to store chunks without a HashMap but some kind of array as this is performance sensitive code and HashMap is not the ideal thing for that. I came to the conclusion that an array with a start/offset would be ideal. Why? Chunks are usually in a group but this group could start on a really large or/and negative number.
Example of situation:
Now what I mean with start/offset is best described with an example.
Positions/Elements
100000 / foo
100001 / bar
100002 / foobar
Instead of creating an array with size of 100003 I could create one with size of 3 and have an offset of 100000. The get method would look like:
int offset=...; //set as items are added but in this example it would be 100000
T[] data;
<T> T get(int pos){
if(pos<offset || pos>=offset+data.length)return null;
return data[pos - offset];
}
Question:
I have experienced that the task of implementing this in an efficient way is quite hard. Now I am wondering if there is any way that this could be done with existing classes in java or a library.
To be honest I do not trust myself with this kind of performance sensitive code. (mainly handling array size and offset)