Hello, I am looking to understand ContiguousArray Better.
I am working on a game and I need to store thousands of entities of the same Type ( Reference type ) contiguous and ordered in memory in order to traverse and update the entities in this collection in a cache friendly manner.
The problem is that these entities can die randomly and so I need to remove them randomly from the Contiguous Array, and this is where I have difficulty understanding if Contiguous Array keeps its elements contiguous and in order in memory after certain operations.
Lets say I have a Contiguous Array named entities.
var entities: ContiguousArray
Where Type1 is a class ( Reference type )
As the game goes, I append 1000 entities in the array using the method append( Element ) Are they contiguous in memory after these operations ? Also are they ordered in memory according to their position in the array ?
Then lets say entities from index 10 to 100 die and also entities from index 300 to 500 die and I remove them form the array using the method remove( at: Int ), are the remaining entities still contiguous in memory after these operations ? Also are they ordered in memory according to their new positions in the array ?
Lets say these entities die but I dont remove them from the array and instead I directly shift all the entities inside the array, shifting live entities to a position where an entity died using the subscript entities[ deadEntityIndex ] = entities[ liveEntityIndex ] repeatedly, is the array still contiguous after all these operations ? Also are they ordered in memory according to their new positions in the array ?
Basically I am looking to understand if the contigupus array keeps being contiguous in memory and also ordered in memory according to their position in the array after these operations:
- Appending Entities
- Removing entities at random places
- Shifting entities inside the array from one place to another
- Changing entities position inside the array
If there is a precise description of the locality of entities in ContunuousArray after these operations please refer me to it.
Finally just out of curiosity lets say we have two contiguous arrays arr1 and arr2 of elements of the same type ( Reference Type ) and they both share a common element.
arr1[ 10 ] = element1
arr2[ 100 ] = element1
Obviously both cant be contiguous because the shared element cant be duplicated in memory, so wich of these arrays is contiguous, the last one in wich an operation was performed ?
Thanks a lot for reading my question and helping.
Powered by WPeMatico