예제 #1
0
        /// <summary>
        /// Updates the value of the existing key/value pair in the node array.
        /// If the Key does not exist, an ArgumentException is thrown.
        /// </summary>
        /// <param name="key">The key of the item being updated.</param>
        /// <param name="value">The updated value</param>
        public void Update(TKey key, TValue value) // O(n) where n is the number of items in the HashTableNodeArray (i.e. where a collision has occured); typically O(1)
        {
            HashTableArrayNode <TKey, TValue> nodes = _array[GetIndex(key)];

            if (nodes == null)
            {
                throw new ArgumentException("The key does not exist in the hash table.");
            }
            nodes.Update(key, value);
        }
예제 #2
0
        /// <summary>
        /// Removes the item from the node array whose key matches
        /// the specified key.
        /// </summary>
        /// <param name="key">The key of the item to remove.</param>
        /// <returns>True if the item was removed; false otherwise.</returns>
        public bool Remove(TKey key) //O(n) where n is the number of items in the HashTableNodeArray (i.e. where a collision has occured); typically O(1)
        {
            HashTableArrayNode <TKey, TValue> nodes = _array[GetIndex(key)];

            if (nodes != null)
            {
                return(nodes.Remove(key));
            }
            return(false);
        }
예제 #3
0
        /// <summary>
        /// Finds and returns the value for the specified key.
        /// </summary>
        /// <param name="key">The key whose value is sought.</param>
        /// <param name="value">The value associated with the specified key.</param>
        /// <returns>True if the value is found; false otherwise.</returns>
        public bool TryGetValue(TKey key, out TValue value) // O(n) where n is the number of items in the HashTableNodeArray (i.e. where a collision has occured); typically O(1)
        {
            HashTableArrayNode <TKey, TValue> nodes = _array[GetIndex(key)];

            if (nodes != null)
            {
                return(nodes.TryGetValue(key, out value));
            }
            value = default(TValue);
            return(false);
        }
예제 #4
0
        public void Add(TKey key, TValue value) // O(1)
        {
            // Lazily allocates HashTableArrayNode instance so that only
            // hash table entries that hold a value allocate an instance.
            int index = GetIndex(key);
            HashTableArrayNode <TKey, TValue> nodes = _array[index];

            if (nodes == null)
            {
                nodes         = new HashTableArrayNode <TKey, TValue>();
                _array[index] = nodes;
            }
            nodes.Add(key, value);
        }