public HashTableArray(int capacity) { _array = new HashTableArrayNode <TKey, TValue> [capacity]; for (int i = 0; i < capacity; i++) { _array[i] = new HashTableArrayNode <TKey, TValue>(); } }
/// <summary> /// Updates the value of existing key value pair in the node array /// </summary> /// <param name="key">The key value</param> /// <param name="value">The value to be added</param> /// <exception cref="ArgumentException">Thrown when key does not exists</exception> public void Update(TKey key, TValue value) { HashTableArrayNode <TKey, TValue> nodes = _array[GetIndex(key)]; if (nodes == null) { throw new ArgumentException("The key does not exists", "key"); } nodes.Update(key, value); }
/// <summary> /// Removes the key value pair that has the specified key /// </summary> /// <param name="key">The key of the key value pair to be removed</param> /// <returns>True if the key value pair is removed, false if it is not</returns> public bool Remove(TKey key) { HashTableArrayNode <TKey, TValue> nodes = _array[GetIndex(key)]; if (nodes != null) { return(nodes.Remove(key)); } return(false); }
/// <summary> /// Tries to get the value associated with the key and set the out value to the found value /// </summary> /// <param name="key">The key to look for in the collection</param> /// <param name="value">The out parameter the value will be assigned to if found</param> /// <returns>True if the key is found, false if it is not</returns> public bool TryGetValue(TKey key, out TValue value) { HashTableArrayNode <TKey, TValue> nodes = _array[GetIndex(key)]; if (nodes != null) { return(nodes.TryGetValue(key, out value)); } // Key does not exists, return default value for TValue value = default(TValue); return(false); }
/// <summary> /// Adds key value pair to the node. /// </summary> /// <param name="key">The key value</param> /// <param name="value">The value to be added</param> /// <exception cref="ArgumentException">Thrown when key already exists</exception> public void Add(TKey key, TValue value) { int index = GetIndex(key); HashTableArrayNode <TKey, TValue> nodes = _array[index]; if (nodes == null) { // Create the HashTableArrayNode and add it to the array nodes = new HashTableArrayNode <TKey, TValue>(); _array[index] = nodes; } // add the key value pair to the nodes nodes.Add(key, value); }