private void Rehashing() { sizeOfArray = 1 + sizeOfArray * growthCoefficient; LinkedList[] newArray = new LinkedList[sizeOfArray]; for (int i = 0; i < _array.Length; i++) { LinkedList newLinkedList = _array[i]; if (newLinkedList != null) { for (int j = 0; j < _array[i].GetLength(); j++) { DictionaryBucketNode currentDictionaryBucketNode = newLinkedList.GetDictionaryBucketNode(j); int keyHash = currentDictionaryBucketNode.key.GetHashCode(); int newIndex = Math.Abs(keyHash % sizeOfArray); if (newArray[newIndex] == null) { LinkedList linkedList = new LinkedList(); linkedList.Add(currentDictionaryBucketNode); newArray[newIndex] = linkedList; } else { newArray[newIndex].Add(currentDictionaryBucketNode); } } } } _array = newArray; }
public DictionaryBucketNode GetDictionaryBucketNode(int newIndex) { DictionaryBucketNode dictionaryBucketNode = new DictionaryBucketNode(); Node nodeAtIndex = rootNode; for (int i = 0; i < newIndex; i++) { nodeAtIndex = nodeAtIndex.NextNode; } dictionaryBucketNode = nodeAtIndex.Value; return(dictionaryBucketNode); }
private bool IsLinkedListFull(int index, DictionaryBucketNode dictionaryBucketNode) { bool isLinkedListFull = false; if (_array[index] != null) { int length = _array[index].GetLength(); if (length == sizeOfList) { isLinkedListFull = true; } } return(isLinkedListFull); }
public void Add(DictionaryBucketNode dictionaryBucketNode) { Node newNode = new Node(); newNode.Value = dictionaryBucketNode; if (rootNode == null) { rootNode = newNode; } else { Node lastNode = FindLastNode(rootNode); lastNode.NextNode = newNode; } }
public void Add(string key, string value) { DictionaryBucketNode dictionaryBucketNode = new DictionaryBucketNode(); dictionaryBucketNode.key = key; dictionaryBucketNode.value = value; GetIndex(key); bool isItFull = IsLinkedListFull(index, dictionaryBucketNode); if (isItFull) { Rehashing(); GetIndex(key); if (_array[index] == null) { LinkedList linkedList = new LinkedList(); linkedList.Add(dictionaryBucketNode); _array[index] = linkedList; } else { _array[index].Add(dictionaryBucketNode); } } else { GetIndex(key); if (_array[index] == null) { LinkedList linkedList = new LinkedList(); linkedList.Add(dictionaryBucketNode); _array[index] = linkedList; } else { _array[index].Add(dictionaryBucketNode); } } }
public string Get(string key) { int keyHash = key.GetHashCode(); int newIndex = Math.Abs(keyHash % sizeOfArray); LinkedList linkedList = new LinkedList(); linkedList = _array[newIndex]; if (linkedList != null) { int sizeOfLinkedList = linkedList.GetLength(); for (int i = 0; i < sizeOfLinkedList; i++) { DictionaryBucketNode currentDictionaryBucketNode = linkedList.GetDictionaryBucketNode(i); if (currentDictionaryBucketNode.key == key) { return(currentDictionaryBucketNode.value); } } } throw new KeyNotFoundException(); }