Skip to content

Array-like structure nested in Unity.Collections.NativeHashMap

Notifications You must be signed in to change notification settings

zhanong/Native-Mapped-Array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Native Mapped Array

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.

FEATURE

  1. Burst-Compilable.
  2. Shares capacity space among keys.

IDEA

Basic Structure

drawing

Adding Item

drawing

Removing Item

drawing

HOW TO USE

Constructor

public NativeMappedArray(int keyCapacity, int arrayCapacity, Allocator allocator)

keyCapacity How many keys are expected to contain. arrayCapacity 'Array capacity' for each key.


Add

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.


Remove

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.


Access Data

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.
}

In the Future

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.

About

Array-like structure nested in Unity.Collections.NativeHashMap

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages