Beispiel #1
0
        // Adds a key value pair to hash
        public void add(K key, V value)
        {
            // Find head of chain for given key
            int             bucketIndex = getBucketIndex(key);
            HashNode <K, V> head        = bucketArray[bucketIndex];

            // Check if key is already present
            while (head != null)
            {
                if (head.key.Equals(key))
                {
                    head.value = value;
                    return;
                }
                head = head.next;
            }

            // Insert key in chain
            size++;
            head = bucketArray[bucketIndex];
            HashNode <K, V> newNode = new HashNode <K, V>(key, value);

            newNode.next             = head;
            bucketArray[bucketIndex] = newNode;

            // If load factor goes beyond threshold, then
            // double hash table size
            if ((1.0 * size) / numBuckets >= 0.7)
            {
                var temp = bucketArray;
                bucketArray = new List <HashNode <K, V> >();
                numBuckets  = 2 * numBuckets;
                size        = 0;
                for (int i = 0; i < numBuckets; i++)
                {
                    bucketArray.Add(null);
                }

                foreach (HashNode <K, V> x in temp)
                {
                    var headNode = x;
                    while (headNode != null)
                    {
                        add(headNode.key, headNode.value);
                        headNode = headNode.next;
                    }
                }
            }
        }
Beispiel #2
0
        // Method to remove a given key
        public V remove(K key)
        {
            // Apply hash function to find index for given key
            int bucketIndex = getBucketIndex(key);

            // Get head of chain
            HashNode <K, V> head = bucketArray[bucketIndex];

            // Search for key in its chain
            HashNode <K, V> prev = null;

            while (head != null)
            {
                // If Key found
                if (head.key.Equals(key))
                {
                    break;
                }

                // Else keep moving in chain
                prev = head;
                head = head.next;
            }

            // If key was not there
            if (head == null)
            {
                return(default(V));
            }

            // Reduce size
            size--;

            // Remove key
            if (prev != null)
            {
                prev.next = head.next;
            }
            else
            {
                bucketArray[bucketIndex] = head.next;
            }

            return(head.value);
        }
Beispiel #3
0
        // Returns value for a key
        public V get(K key)
        {
            // Find head of chain for given key
            int             bucketIndex = getBucketIndex(key);
            HashNode <K, V> head        = bucketArray[bucketIndex];

            // Search key in chain
            while (head != null)
            {
                if (head.key.Equals(key))
                {
                    return(head.value);
                }
                head = head.next;
            }

            // If key not found
            return(default(V));
        }