public MiniDict(TKey *keys, int size) { Count = size; keys_ = (TKey *)Marshal.AllocHGlobal(Count * sizeof(TKey)); Buffer.MemoryCopy(keys, (void *)keys_, Count * sizeof(TKey), Count * sizeof(TKey)); data_ = new TValue[Count]; }
public MiniDict(TKey[] keys, TValue[] values = null) { Count = keys.Length; keys_ = (TKey *)Marshal.AllocHGlobal(Count * sizeof(TKey)); fixed(void *k = &keys[0]) Buffer.MemoryCopy(k, (void *)keys_, Count * sizeof(TKey), Count * sizeof(TKey)); data_ = values ?? new TValue[Count]; }
public UnmanagedDictionary(uint defaultCapacity) { unsafe { if (defaultCapacity < 1) { defaultCapacity = 1; } m_KeyPresentBuffer = (Bitmask *)UnsafeUtility.Malloc(1 + defaultCapacity / 8, UnsafeUtility.AlignOf <byte>(), Allocator.Persistent); UnsafeUtility.MemSet((void *)m_KeyPresentBuffer, 0, 1 + defaultCapacity / 8); m_Keys = (TKey *)UnsafeUtility.Malloc(sizeof(TKey) * defaultCapacity, UnsafeUtility.AlignOf <TKey>(), Allocator.Persistent); m_Values = (TValue *)UnsafeUtility.Malloc(sizeof(TValue) * defaultCapacity, UnsafeUtility.AlignOf <TValue>(), Allocator.Persistent); m_Size = 0; m_Capacity = defaultCapacity; } }
// expands the thing uwu private void ExpandAndRehash() { unsafe { //UnityEngine.Debug.Log($"EXPAND AND REHASH FROM: {m_Capacity}, INTO: {2 * m_Capacity + 3}"); var oldPresence = m_KeyPresentBuffer; var oldKeys = m_Keys; var oldValues = m_Values; var oldCapacity = m_Capacity; var newCapacity = m_Capacity * 2 + 3; m_KeyPresentBuffer = (Bitmask *)UnsafeUtility.Malloc(1 + newCapacity / 8, UnsafeUtility.AlignOf <byte>(), Allocator.Persistent); UnsafeUtility.MemSet(m_KeyPresentBuffer, 0, 1 + newCapacity / 8); m_Keys = (TKey *)UnsafeUtility.Malloc(sizeof(TKey) * newCapacity, UnsafeUtility.AlignOf <TKey>(), Allocator.Persistent); m_Values = (TValue *)UnsafeUtility.Malloc(sizeof(TValue) * newCapacity, UnsafeUtility.AlignOf <TValue>(), Allocator.Persistent); m_Capacity = newCapacity; m_Size = 0; for (int i = 0; i < oldCapacity; i++) { if (IsSlotOccupiedOnBuffer(i, oldPresence) == true) { var k = oldKeys[i]; var v = oldValues[i]; Set(k, v); } } if (oldCapacity > 0) { //UnityEngine.Debug.Log($"Reshash clear: {((IntPtr)oldPresence).ToInt64()} {((IntPtr)oldKeys).ToInt64()} {((IntPtr)oldValues).ToInt64()}"); UnsafeUtility.Free((void *)oldPresence, Allocator.Persistent); UnsafeUtility.Free((void *)oldKeys, Allocator.Persistent); UnsafeUtility.Free((void *)oldValues, Allocator.Persistent); } } }