コード例 #1
0
        /// <summary>
        /// Determines if a given key exists in the hashtable
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Contains(string key)
        {
            //get the hash value of the key
            int index = GetHash(key);
            //go straight to that index
            HTNode current = HashArray[index];

            if (current == null)
            {
                return(false);
            }
            //travel through the linkedlist at an index, if there is one
            //to check if the key is there
            while (current.Next != null)
            {
                if (current.Key == key)
                {
                    return(true);
                }
                current = current.Next;
            }
            //check the last node for the key
            if (current.Key == key)
            {
                return(true);
            }
            //return false if the key is not found
            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Finds the value at the given key
        /// </summary>
        /// <param name="key"></param>
        /// <returns>value of a given key</returns>
        public int Find(string key)
        {
            //get the hash value of the key
            int index = GetHash(key);
            //go straight to that index in the array
            HTNode current = HashArray[index];

            //look through linkedlist, if there is one at that index, for the key
            while (current.Next != null)
            {   //return the value if the key is found
                if (current.Key == key)
                {
                    return(current.Value);
                }
                //reset counter to continue traversal
                current = current.Next;
            }
            //otherwise, return the value of the last node
            if (current.Key == key)
            {
                return(current.Value);
            }
            //If the key doesn't exist, return 0;
            return(0);
        }
コード例 #3
0
        /// <summary>
        /// Create a new node, find a hash for the key, and check if the index is occupied
        /// If it is occupied, chain the nodes together
        /// </summary>
        /// <param name="key">string</param>
        /// <param name="value">int</param>
        public void Add(string key, int value)
        {
            HTNode node  = new HTNode(key, value);
            int    index = GetHash(key);

            //if there is a collision, chain the new node to the one already there
            if (HashArray[index] != null)
            {
                node.Next = HashArray[index];
            }
            //if that index is unoccupied, add the node
            HashArray[index] = node;
        }