Esempio n. 1
0
        private HashtableItem <T, K> GetEntry(T value)
        {
            // Compute the hashcode of the object.
            int hashcode = this.hashcode_proc(value);

            // Compute the index into the indice table.
            int index = hashcode % this.indice_count;

            // Check if there is a HashtableItem at this index.
            if (this.indices[index] == -1)
            {
                return(null);
            }

            // Loop through the bucket and search for an entry with matching value.
            for (HashtableItem <T, K> item = this.nodes[this.indices[index]];
                 item != null; item = item.Next)
            {
                // Check if the hashcode matches.
                if (item.Hashcode != hashcode)
                {
                    continue;
                }

                // Check if the objects matching using the compare proc.
                if (this.compare_proc(value, item.Value) == true)
                {
                    return(item);
                }
            }

            // An entry with matching value was not found, return null;
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Clears all the items from the hashtable.
        /// </summary>
        public void Clear()
        {
            // Clear the indice array.
            for (int i = 0; i < this.indice_count; i++)
            {
                this.indices[i] = -1;
            }

            // Set the head node pointer to null.
            this.items = null;

            // Initialize the linked list of HashtableItem nodes.
            for (int i = 0; i < this.Capacity; i++)
            {
                // Initialize the node.
                this.nodes[i]          = new HashtableItem <T, K>();
                this.nodes[i].Value    = default(T);
                this.nodes[i].Key      = default(K);
                this.nodes[i].Hashcode = 0;
                this.nodes[i].Next     = this.items;

                // Set the new head node to this HashtableItem structure.
                this.items = this.nodes[i];
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a value-key pair to the hashtable.
        /// </summary>
        /// <param name="value">Object to add to the hashtable.</param>
        /// <param name="key">Unique key object that corresponds with <paramref name="value"/></param>
        /// <returns>True if the item was added, false otherwise.</returns>
        public bool Add(T value, K key)
        {
            // Check if there are any empty items in the hashtable left.
            if (this.items == null)
            {
                return(false);
            }

            // Compute the hashcode of the item.
            int hashcode = this.hashcode_proc(value);

            // Get the next unused HashtableItem structure.
            HashtableItem <T, K> item = this.items;

            item.Value    = value;
            item.Key      = key;
            item.Hashcode = hashcode;

            // Update the head node pointer.
            this.items = item.Next;

            // Compute the index into the indice table.
            int index = hashcode % this.indice_count;

            // Update the next pointer for the new HashtableItem.
            item.Next = this.indices[index] == -1 ? null : this.nodes[this.indices[index]];

            // Update the indice table to point to our new HashtableItem.
            this.indices[index] = Array.IndexOf(this.nodes, item);

            // The item was successfully added.
            return(true);
        }
Esempio n. 4
0
        public T ContainsValue(T value, ref K key)
        {
            // Try to find a HashtableItem with matching value.
            HashtableItem <T, K> item = GetEntry(value);

            if (item == null)
            {
                return(default(T));
            }

            // Copy the key object.
            key = item.Key;

            // Return the item value.
            return(item.Value);
        }
Esempio n. 5
0
        public bool GetKey(T value, ref K key)
        {
            // Satisfy the compiler.
            //key = default(K);

            // Try to find a HashtableItem with matching value.
            HashtableItem <T, K> item = GetEntry(value);

            if (item == null)
            {
                return(false);
            }

            // Copy the key object and return true.
            key = item.Key;
            return(true);
        }