Esempio n. 1
0
 public void AddRange <TValue, TNode>(TValue *copySource, int length, out TNode *page, out int start, Allocator allocator)
     where TValue : unmanaged
     where TNode : unmanaged
 {
     AddRange(copySource, length, out var _page, out start, allocator);
     page = (TNode *)_page;
 }
Esempio n. 2
0
        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;
            }
        }
Esempio n. 3
0
        // 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);
                }
            }
        }
Esempio n. 4
0
        public unsafe bool TryGetPtr(TKey key, out TValue *ptr)
        {
            unsafe
            {
                if (Capacity == 0)
                {
                    ptr = default; return(false);
                }

                ptr = default;
                var hash = Hash(key);

                int i = 0;
                while (true)
                {
                    var pos = hash + i;
                    if (pos >= m_Capacity)
                    {
                        return(false);
                    }

                    if (IsSlotOccupied(pos) == true)
                    {
                        var posKey = m_Keys[pos];
                        if (posKey.Equals(key))
                        {
                            ptr = m_Values + pos;
                            return(true);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                    i++;
                }
            }
        }