/// <summary> /// Constructs a new hash table array with the specified capacity /// </summary> /// <param name="capacity">The capacity of the array</param> public HashTableArray(int capacity) { _array = new HashTableArrayNode <TKey, TValue> [capacity]; for (int i = 0; i < capacity; i++) { _array[i] = new HashTableArrayNode <TKey, TValue>(); } }
/// <summary> /// Constructs a new hash table array with the specified capacity /// </summary> /// <param name="capacity">The capacity of the array</param> public HashTableArray(int capacity) { _array = new HashTableArrayNode <TKey, TValue> [capacity]; // This will also initialize all the nodes for the HashTableArrayNode, // we should not have done this in a production code because we should Lazy these // we should only instantate these nodes when needed. for (int i = 0; i < capacity; i++) { _array[i] = new HashTableArrayNode <TKey, TValue>(); } }
public void Add(TKey key, TValue value) { 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); }