Struct NativeHashMap<TKey, TValue>
An unordered, expandable associative array.
Implements
Namespace: Unity.Collections
Assembly: Unity.Collections.dll
Syntax
[NativeContainer]
public struct NativeHashMap<TKey, TValue> : INativeDisposable where TKey : unmanaged, IEquatable<TKey> where TValue : unmanaged
Type Parameters
Name | Description |
---|---|
TKey | The type of the keys. |
TValue | The type of the values. |
Remarks
Not suitable for parallel write access. Use NativeParallelHashMap<TKey, TValue> instead.
Constructors
NativeHashMap(int, AllocatorHandle)
Initializes and returns an instance of UnsafeHashMap.
Declaration
public NativeHashMap(int initialCapacity, AllocatorManager.AllocatorHandle allocator)
Parameters
Type | Name | Description |
---|---|---|
int | initialCapacity | The number of key-value pairs that should fit in the initial allocation. |
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
Properties
Capacity
The number of key-value pairs that fit in the current allocation.
Declaration
public int Capacity { readonly get; set; }
Property Value
Type | Description |
---|---|
int | The number of key-value pairs that fit in the current allocation. |
Count
The current number of key-value pairs in this hash map.
Declaration
public readonly int Count { get; }
Property Value
Type | Description |
---|---|
int | The current number of key-value pairs in this hash map. |
IsCreated
Whether this hash map has been allocated (and not yet deallocated).
Declaration
public readonly 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 readonly bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
bool | True if this hash map is empty or if the map has not been constructed. |
this[TKey]
Gets and sets values by key.
Declaration
public TValue this[TKey key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to look up. |
Property Value
Type | Description |
---|---|
TValue | The value associated with the key. |
Remarks
Getting a key that is not present will throw. Setting a key that is not already present will add the key.
Exceptions
Type | Condition |
---|---|
ArgumentException | For getting, thrown if the key was not present. |
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 the key is already present, this method throws without modifying the hash map.
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the key was already present. |
AsReadOnly()
Returns a readonly version of this NativeHashMap instance.
Declaration
public NativeHashMap<TKey, TValue>.ReadOnly AsReadOnly()
Returns
Type | Description |
---|---|
NativeHashMap<TKey, TValue>.ReadOnly | ReadOnly instance for this. |
Remarks
ReadOnly containers point to the same underlying data as the NativeHashMap it is made from.
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. |
Dispose()
Releases all resources (memory).
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 NativeHashMap<TKey, TValue>.Enumerator GetEnumerator()
Returns
Type | Description |
---|---|
NativeHashMap<TKey, TValue>.Enumerator | An enumerator over the key-value pairs of this hash map. |
GetKeyArray(AllocatorHandle)
Returns an array with a copy of all this hash map's 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 this hash map's keys (in no particular order). |
GetKeyValueArrays(AllocatorHandle)
Returns a NativeKeyValueArrays with a copy of all this hash map's keys and values.
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 this hash map's keys and values. |
Remarks
The key-value pairs are copied in no particular order. For all i
, Values[i]
will be the value associated with Keys[i]
.
GetValueArray(AllocatorHandle)
Returns an array with a copy of all this hash map's 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 this hash map's values (in no particular order). |
Remove(TKey)
Removes a key-value pair.
Declaration
public bool Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to remove. |
Returns
Type | Description |
---|---|
bool | True if a key-value pair was removed. |
TrimExcess()
Sets the capacity to match what it would be if it had been originally initialized with all its entries.
Declaration
public void TrimExcess()
TryAdd(TKey, TValue)
Adds a new key-value pair.
Declaration
public bool TryAdd(TKey key, TValue item)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to add. |
TValue | item | The value to add. |
Returns
Type | Description |
---|---|
bool | True if the key-value pair was added. |
Remarks
If the key is already present, this method returns false without modifying the hash map.
TryGetValue(TKey, out TValue)
Returns the value associated with a key.
Declaration
public bool TryGetValue(TKey key, out TValue item)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to look up. |
TValue | item | Outputs the value associated with the key. Outputs default if the key was not present. |
Returns
Type | Description |
---|---|
bool | True if the key was present. |