This data structure comes from my personal game. It is unmanaged so capable for burst-compile. It is simple and easy to modify but not necessarily the most efficient.
- Burst-Compilable.
- Shares capacity space among keys.
public NativeMappedArray(int keyCapacity, int arrayCapacity, Allocator allocator)
keyCapacity
How many keys are expected to contain.
arrayCapacity
'Array capacity' for each key.
public bool AddKey(TKey key)
If the key doesn't exist, add a key with an empty chunk and return true .
public bool Add(TKey key, TValue value)
Add value to the last chunk of the key. If a new chunk is added, return true.
public bool RemoveKey(TKey key)
Remove a key and all its chunck.
public bool Remove(TKey key, TValue value)
Remove the first item in key's chunks that Equals(value)
.
(In the worst case, this will traverse all items in chunks that belongs to key.)
public void RemoveAt(TKey key, int valueIndex)
Remove the valueIndex % chunkSize
item at the valueIndex / chunkSize
chunk of the key. Use this instead of Remove()
if possible.
NativeMappedArray<K, V> map;
/* The best way to get all values */
int valueCount = map.ValueCount(k)
for (int i = 0; i < valueCount; i++)
{
V value = map.GetValueAt(k, i);
// Do something.
}
The chunk is implemented as List6. It's size is fixed as 6. It's better to make chunk type as a generic type for the structure.