Struct NativeMultiHashMap<TKey, TValue>
An unordered, expandable associative array. Each key can have more than one associated value.
Implements
Namespace: Unity.Collections
Assembly: Unity.Collections.dll
Syntax
[NativeContainer]
public struct NativeMultiHashMap<TKey, TValue> : INativeDisposable where TKey : struct, IEquatable<TKey> where TValue : struct
Type Parameters
Name | Description |
---|---|
TKey | The type of the keys. |
TValue | The type of the values. |
Remarks
Unlike a regular NativeHashMap, a NativeMultiHashMap can store multiple key-value pairs with the same key.
The keys are not deduplicated: two key-value pairs with the same key are stored as fully separate key-value pairs.
Constructors
NativeMultiHashMap(int, AllocatorHandle)
Returns a newly allocated multi hash map.
Declaration
public NativeMultiHashMap(int capacity, AllocatorManager.AllocatorHandle allocator)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The number of key-value pairs that should fit in the initial allocation. |
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
Properties
Capacity
Returns the number of key-value pairs that fit in the current allocation.
Declaration
public int Capacity { get; set; }
Property Value
Type | Description |
---|---|
int | The number of key-value pairs that fit in the current allocation. |
Exceptions
Type | Condition |
---|---|
Exception | Thrown if |
IsCreated
Whether this hash map has been allocated (and not yet deallocated).
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool | True if this hash map has been allocated (and not yet deallocated). |
IsEmpty
Whether this hash map is empty.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
bool | True if this hash map is empty. |
Methods
Add(TKey, TValue)
Adds a new key-value pair.
Declaration
public void Add(TKey key, TValue item)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to add. |
TValue | item | The value to add. |
Remarks
If a key-value pair with this key is already present, an additional separate key-value pair is added.
AsParallelWriter()
Returns a parallel writer for this hash map.
Declaration
public NativeMultiHashMap<TKey, TValue>.ParallelWriter AsParallelWriter()
Returns
Type | Description |
---|---|
NativeMultiHashMap<TKey, TValue>.ParallelWriter | A parallel writer for this hash map. |
Clear()
Removes all key-value pairs.
Declaration
public void Clear()
Remarks
Does not change the capacity.
ContainsKey(TKey)
Returns true if a given key is present in this hash map.
Declaration
public bool ContainsKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to look up. |
Returns
Type | Description |
---|---|
bool | True if the key was present in this hash map. |
Count()
Returns the current number of key-value pairs in this hash map.
Declaration
public int Count()
Returns
Type | Description |
---|---|
int | The current number of key-value pairs in this hash map. |
Remarks
Key-value pairs with matching keys are counted as separate, individual pairs.
CountValuesForKey(TKey)
Returns the number of values associated with a given key.
Declaration
public int CountValuesForKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to look up. |
Returns
Type | Description |
---|---|
int | The number of values associated with the key. Returns 0 if the key was not present. |
Dispose()
Releases all resources (memory and safety handles).
Declaration
public void Dispose()
Dispose(JobHandle)
Creates and schedules a job that will dispose this hash map.
Declaration
public JobHandle Dispose(JobHandle inputDeps)
Parameters
Type | Name | Description |
---|---|---|
JobHandle | inputDeps | A job handle. The newly scheduled job will depend upon this handle. |
Returns
Type | Description |
---|---|
JobHandle | The handle of a new job that will dispose this hash map. |
GetEnumerator()
Returns an enumerator over the key-value pairs of this hash map.
Declaration
public NativeMultiHashMap<TKey, TValue>.KeyValueEnumerator GetEnumerator()
Returns
Type | Description |
---|---|
NativeMultiHashMap<TKey, TValue>.KeyValueEnumerator | An enumerator over the key-value pairs of this hash map. |
Remarks
A key with N values is visited by the enumerator N times.
GetKeyArray(AllocatorHandle)
Returns an array with a copy of all the keys (in no particular order).
Declaration
public NativeArray<TKey> GetKeyArray(AllocatorManager.AllocatorHandle allocator)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
Returns
Type | Description |
---|---|
NativeArray<TKey> | An array with a copy of all the keys (in no particular order). |
Remarks
A key with N values is included N times in the array.
Use `GetUniqueKeyArray` of <xref href="Unity.Collections.NativeHashMapExtensions" data-throw-if-not-resolved="false"></xref> instead if you only want one occurrence of each key.
GetKeyValueArrays(AllocatorHandle)
Returns a NativeKeyValueArrays with a copy of all the keys and values (in no particular order).
Declaration
public NativeKeyValueArrays<TKey, TValue> GetKeyValueArrays(AllocatorManager.AllocatorHandle allocator)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
Returns
Type | Description |
---|---|
NativeKeyValueArrays<TKey, TValue> | A NativeKeyValueArrays with a copy of all the keys and values (in no particular order). |
Remarks
A key with N values is included N times in the array.
GetValueArray(AllocatorHandle)
Returns an array with a copy of all the values (in no particular order).
Declaration
public NativeArray<TValue> GetValueArray(AllocatorManager.AllocatorHandle allocator)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
Returns
Type | Description |
---|---|
NativeArray<TValue> | An array with a copy of all the values (in no particular order). |
Remarks
The values are not deduplicated. If you sort the returned array, you can use Unique<T>(NativeArray<T>) to remove duplicate values.
GetValuesForKey(TKey)
Returns an enumerator over the values of an individual key.
Declaration
public NativeMultiHashMap<TKey, TValue>.Enumerator GetValuesForKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to get an enumerator for. |
Returns
Type | Description |
---|---|
NativeMultiHashMap<TKey, TValue>.Enumerator | An enumerator over the values of a key. |
Remove(NativeMultiHashMapIterator<TKey>)
Removes a single key-value pair.
Declaration
public void Remove(NativeMultiHashMapIterator<TKey> it)
Parameters
Type | Name | Description |
---|---|---|
NativeMultiHashMapIterator<TKey> | it | An iterator representing the key-value pair to remove. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if the iterator is invalid. |
Remove(TKey)
Removes a key and its associated value(s).
Declaration
public int Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to remove. |
Returns
Type | Description |
---|---|
int | The number of removed key-value pairs. If the key was not present, returns 0. |
SetValue(TValue, NativeMultiHashMapIterator<TKey>)
Sets a new value for an existing key-value pair.
Declaration
public bool SetValue(TValue item, NativeMultiHashMapIterator<TKey> it)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The new value. |
NativeMultiHashMapIterator<TKey> | it | The iterator representing a key-value pair. |
Returns
Type | Description |
---|---|
bool | True if a value was overwritten. |
TryGetFirstValue(TKey, out TValue, out NativeMultiHashMapIterator<TKey>)
Gets an iterator for a key.
Declaration
public bool TryGetFirstValue(TKey key, out TValue item, out NativeMultiHashMapIterator<TKey> it)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key. |
TValue | item | Outputs the associated value represented by the iterator. |
NativeMultiHashMapIterator<TKey> | it | Outputs an iterator. |
Returns
Type | Description |
---|---|
bool | True if the key was present. |
TryGetNextValue(out TValue, ref NativeMultiHashMapIterator<TKey>)
Advances an iterator to the next value associated with its key.
Declaration
public bool TryGetNextValue(out TValue item, ref NativeMultiHashMapIterator<TKey> it)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | Outputs the next value. |
NativeMultiHashMapIterator<TKey> | it | A reference to the iterator to advance. |
Returns
Type | Description |
---|---|
bool | True if the key was present and had another value. |