Struct NativeHashMap<TKey, TValue>
Unordered associative array, a collection of keys and values.
Namespace: Unity.Collections
Syntax
public struct NativeHashMap<TKey, TValue> : IDisposable where TKey : struct, IEquatable<TKey> where TValue : struct
Type Parameters
Name | Description |
---|---|
TKey | The type of the keys in the container. |
TValue | The type of the values in the container. |
Constructors
NativeHashMap(Int32, Allocator)
Constructs a new container with the specified initial capacity and type of memory allocation.
Declaration
public NativeHashMap(int capacity, Allocator allocator)
Parameters
Type | Name | Description |
---|---|---|
Int32 | capacity | The initial capacity of the container. If the list grows larger than its capacity, the internal array is copied to a new, larger array. |
Allocator | allocator | A member of the Unity.Collections.Allocator enumeration. |
Properties
Capacity
The number of items that can fit in the container.
Declaration
public int Capacity { get; set; }
Property Value
Type | Description |
---|---|
Int32 | The number of items that the container can hold before it resizes its internal storage. |
Remarks
Capacity specifies the number of items the container can currently hold. You can change Capacity to fit more or fewer items. Changing Capacity creates a new array of the specified size, copies the old array to the new one, and then deallocates the original array memory.
IsCreated
Reports whether memory for the container is allocated.
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
Boolean | True if this container object's internal storage has been allocated. |
Remarks
Note that the container storage is not created if you use the default constructor. You must specify at least an allocation type to construct a usable container.
Item[TKey]
Retrieve a value by key.
Declaration
public TValue this[TKey key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
TKey | key |
Property Value
Type | Description |
---|---|
TValue |
Length
The current number of items in the container.
Declaration
public int Length { get; }
Property Value
Type | Description |
---|---|
Int32 | The item count. |
Methods
Add(TKey, TValue)
Add an element with the specified key and value into the container. If the key already exist an ArgumentException will be thrown.
Declaration
public void Add(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the element to add. |
TValue | value | The value of the element to add. |
AsParallelWriter()
Returns parallel writer instance.
Declaration
public NativeHashMap<TKey, TValue>.ParallelWriter AsParallelWriter()
Returns
Type | Description |
---|---|
NativeHashMap.ParallelWriter<> |
Clear()
Clears the container.
Declaration
public void Clear()
Remarks
Containers capacity remains unchanged.
ContainsKey(TKey)
Determines whether an key is in the container.
Declaration
public bool ContainsKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to locate in the container. |
Returns
Type | Description |
---|---|
Boolean | Returns true if the container contains the key. |
Dispose()
Disposes of this container and deallocates its memory immediately.
Declaration
public void Dispose()
Dispose(JobHandle)
Safely disposes of this container and deallocates its memory when the jobs that use it have completed.
Declaration
public JobHandle Dispose(JobHandle inputDeps)
Parameters
Type | Name | Description |
---|---|---|
JobHandle | inputDeps |
Returns
Type | Description |
---|---|
JobHandle | A new job handle containing the prior handles as well as the handle for the job that deletes the container. |
Remarks
You can call this function dispose of the container immediately after scheduling the job. Pass
the JobHandle returned by
the Job.Schedule
method using the jobHandle
parameter so the job scheduler can dispose the container after all jobs
using it have run.
GetKeyArray(Allocator)
Retrive array of key from the container.
Declaration
public NativeArray<TKey> GetKeyArray(Allocator allocator)
Parameters
Type | Name | Description |
---|---|---|
Allocator | allocator | A member of the Unity.Collections.Allocator enumeration. |
Returns
Type | Description |
---|---|
NativeArray<TKey> | Key array. |
GetKeyValueArrays(Allocator)
Retrieve key/value arrays.
Declaration
public NativeKeyValueArrays<TKey, TValue> GetKeyValueArrays(Allocator allocator)
Parameters
Type | Name | Description |
---|---|---|
Allocator | allocator | A member of the Unity.Collections.Allocator enumeration. |
Returns
Type | Description |
---|---|
NativeKeyValueArrays<TKey, TValue> | Key/value arrays. |
GetValueArray(Allocator)
Retreive array of values from the container.
Declaration
public NativeArray<TValue> GetValueArray(Allocator allocator)
Parameters
Type | Name | Description |
---|---|---|
Allocator | allocator | A member of the Unity.Collections.Allocator enumeration. |
Returns
Type | Description |
---|---|
NativeArray<TValue> | Value array. |
Remove(TKey)
Removes the element with the specified key from the container.
Declaration
public bool Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the element to remove. |
Returns
Type | Description |
---|---|
Boolean | Returns true if the key was removed from the container, otherwise returns false indicating key wasn't in the container. |
TryAdd(TKey, TValue)
Try adding an element with the specified key and value into the container. If the key already exist, the value won't be updated.
Declaration
public bool TryAdd(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the element to add. |
TValue | value | The value of the element to add. |
Returns
Type | Description |
---|---|
Boolean | Returns true if value is added into the container, otherwise returns false. |
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
Declaration
public bool TryGetValue(TKey key, out TValue item)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the value to get. |
TValue | item | If key is found item parameter will contain value |
Returns
Type | Description |
---|---|
Boolean | Returns true if key is found, otherwise returns false. |