Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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;
            }
        }
Exemplo n.º 5
0
        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);
                }
            }
        }
Exemplo n.º 6
0
        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();
        }