private void Resize(int targetSize) { K **newData = (K **)UnsafeUtility.Malloc(targetSize * 8, 16, data->alloc); UnsafeUtility.MemClear(newData, targetSize * 8); K **oldPtr = (K **)data->start; for (int i = 0; i < data->capacity; ++i) { K *currentPtr = oldPtr[i]; while (currentPtr != null) { AddTo(*currentPtr, *GetV(currentPtr), targetSize, newData); currentPtr = *GetNextPtr(currentPtr); } currentPtr = oldPtr[i]; while (currentPtr != null) { K *next = *GetNextPtr(currentPtr); UnsafeUtility.Free(currentPtr, data->alloc); currentPtr = next; } } UnsafeUtility.Free(data->start, data->alloc); data->start = newData; data->capacity = targetSize; }
private void AddTo(K key, V value, int capacity, K **origin) { int index = Mathf.Abs(key.GetHashCode()) % capacity; K **currentPos = origin + index; while ((*currentPos) != null) { currentPos = GetNextPtr(*currentPos); } (*currentPos) = (K *)MUnsafeUtility.Malloc(stride, data->alloc); (**currentPos) = key; (*GetV(*currentPos)) = value; (*GetNextPtr(*currentPos)) = null; }
public V this[K key] { get { int index = Mathf.Abs(key.GetHashCode()) % data->capacity; K **currentPos = GetK(index); while ((*currentPos) != null) { if (equalFunc.Run(ref **currentPos, ref key)) { return(*GetV(*currentPos)); } currentPos = GetNextPtr(*currentPos); } return(default);
public bool Contains(K key) { int index = Mathf.Abs(key.GetHashCode()) % data->capacity; K **currentPos = GetK(index); while ((*currentPos) != null) { if (equalFunc.Run(ref **currentPos, ref key)) { return(true); } currentPos = GetNextPtr(*currentPos); } return(false); }
public V this[K key] { get { Func <K, K, bool> equalsFunc = MUnsafeUtility.GetObject <Func <K, K, bool> >(this.equalsFunc); int index = Mathf.Abs(key.GetHashCode()) % data->capacity; K **currentPos = GetK(index); while ((*currentPos) != null) { if (equalsFunc(**currentPos, key)) { return(*GetV(*currentPos)); } currentPos = GetNextPtr(*currentPos); } return(default);
public bool Contains(K key) { Func <K, K, bool> equalsFunc = MUnsafeUtility.GetObject <Func <K, K, bool> >(this.equalsFunc); int index = Mathf.Abs(key.GetHashCode()) % data->capacity; K **currentPos = GetK(index); while ((*currentPos) != null) { if (equalsFunc(**currentPos, key)) { return(true); } currentPos = GetNextPtr(*currentPos); } return(false); }
public void Add(K key, V value) { //TODO //Resize int index = key.GetHashCode() % data->capacity; K **currentPos = GetK(index); while ((*currentPos) != null) { currentPos = GetNextPtr(*currentPos); } (*currentPos) = Malloc <K>(stride, data->alloc); (**currentPos) = key; (*GetV(*currentPos)) = value; (*GetNextPtr(*currentPos)) = null; }
public bool Get(K key, out V value) { int index = key.GetHashCode() % data->capacity; K **currentPos = GetK(index); while ((*currentPos) != null) { if (equalsFunc(**currentPos, key)) { value = *GetV(*currentPos); return(true); } currentPos = GetNextPtr(*currentPos); } value = default; return(false); }
public void Remove(K key) { int index = Mathf.Abs(key.GetHashCode()) % data->capacity; K **currentPtr = GetK(index); while ((*currentPtr) != null) { K **next = GetNextPtr(*currentPtr); if (equalFunc.Run(ref **currentPtr, ref key)) { K *prev = *currentPtr; *currentPtr = *next; UnsafeUtility.Free(prev, data->alloc); data->length--; return; } else { currentPtr = next; } } }
public void Remove(K key) { Func <K, K, bool> equalsFunc = MUnsafeUtility.GetObject <Func <K, K, bool> >(this.equalsFunc); int index = Mathf.Abs(key.GetHashCode()) % data->capacity; K **currentPtr = GetK(index); while ((*currentPtr) != null) { K **next = GetNextPtr(*currentPtr); if (equalsFunc(**currentPtr, key)) { K *prev = *currentPtr; *currentPtr = *next; UnsafeUtility.Free(prev, data->alloc); data->length--; return; } else { currentPtr = next; } } }
private void Resize(int targetSize) { K **newData = (K **)MUnsafeUtility.Malloc(targetSize * 8, data->alloc); UnsafeUtility.MemClear(newData, targetSize * 8); K **oldPtr = (K **)data->start; for (int i = 0; i < data->capacity; ++i) { K *currentPtr = oldPtr[i]; while (currentPtr != null) { K *nextPtr = *GetNextPtr(currentPtr); //AddTo(*currentPtr, *GetV(currentPtr), targetSize, newData); int hashCode = Mathf.Abs((*currentPtr).GetHashCode()) % targetSize; *GetNextPtr(currentPtr) = newData[hashCode]; newData[hashCode] = currentPtr; currentPtr = nextPtr; } } UnsafeUtility.Free(data->start, data->alloc); data->start = newData; data->capacity = targetSize; }