/// <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); }
/// <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); }
/// <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); }
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); }