Advertisement
sirjordan

HashTable<K, T> - beta+

Sep 30th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. /// <summary>
  6. /// Contains a collection of KeyValuePairs
  7. /// </summary>
  8. /// <typeparam name="K">Key for add/search</typeparam>
  9. /// <typeparam name="T">Value for add</typeparam>
  10. /// <remarks>Make better hash function.</remarks>
  11. public class HashTable<K, T> : IEnumerable<KeyValuePair<K, T>>
  12.     where K : IComparable
  13. {
  14.     const int INITIAL_CAPACITY = 16;
  15.     const double LOAD_FACTOR = 0.75d;
  16.     private List<KeyValuePair<K, T>>[] elements;
  17.     private int elementsCount;
  18.  
  19.     public int Count
  20.     {
  21.         get { return this.elementsCount; }
  22.     }
  23.  
  24.     public HashTable()
  25.         : this(INITIAL_CAPACITY)
  26.     {
  27.     }
  28.  
  29.     private HashTable(int capacity)
  30.     {
  31.         this.elementsCount = 0;
  32.         this.elements = new List<KeyValuePair<K, T>>[capacity];
  33.     }
  34.  
  35.     public List<KeyValuePair<K, T>> Find(K key)
  36.     {
  37.         // index = hash(key) % capacity
  38.         int index = GetIndex(key, this.elements.Length);
  39.         if (this.elements[index] != null)
  40.         {
  41.             return this.elements[index];
  42.         }
  43.  
  44.         throw new KeyNotFoundException(string.Format("Key <{0}> not found", key.ToString()));
  45.     }
  46.  
  47.     public void Add(K key, T value)
  48.     {
  49.         // If not reches the load factor
  50.         if (this.elementsCount + 1 < this.elements.Length * LOAD_FACTOR)
  51.         {
  52.             int index = GetIndex(key, this.elements.Length);
  53.             if (this.elements[index] == null)
  54.             {
  55.                 this.elements[index] = new List<KeyValuePair<K, T>>();
  56.                 // The elementCounter increases only if add a new List()
  57.                 elementsCount++;
  58.             }
  59.  
  60.             KeyValuePair<K, T> newValue = new KeyValuePair<K, T>(key, value);
  61.             this.elements[index].Add(newValue);
  62.         }
  63.         else
  64.         {
  65.             // Make dual lengh the current capacity, copy all elements in the new, add the new element.
  66.             Expand();
  67.             Add(key, value);
  68.         }
  69.     }
  70.  
  71.     private void Expand()
  72.     {
  73.         int newCapacity = this.elements.Length * 2;
  74.         List<KeyValuePair<K, T>>[] newContainer = new List<KeyValuePair<K, T>>[newCapacity];
  75.         foreach (List<KeyValuePair<K, T>> element in this.elements)
  76.         {
  77.             if (element != null)
  78.             {
  79.                 // Get new hash key, based on the new array capacity
  80.                 int newIndex = GetIndex(element[0].Key, newCapacity);
  81.                 newContainer[newIndex] = element;
  82.             }
  83.         }
  84.  
  85.         this.elements = newContainer;
  86.     }
  87.  
  88.     private int GetIndex(K key, int arrayCapacity)
  89.     {
  90.         // TODO:
  91.         int hash = Math.Abs(key.GetHashCode());
  92.         int index = hash % arrayCapacity;
  93.         return index;
  94.     }
  95.  
  96.     public IEnumerator<KeyValuePair<K, T>> GetEnumerator()
  97.     {
  98.         foreach (List<KeyValuePair<K, T>> listOfElements in this.elements)
  99.         {
  100.             if (listOfElements != null)
  101.             {
  102.                 foreach (KeyValuePair<K, T> entry in listOfElements)
  103.                 {
  104.                     yield return entry;
  105.                 }
  106.             }
  107.         }
  108.     }
  109.  
  110.     IEnumerator IEnumerable.GetEnumerator()
  111.     {
  112.         return ((IEnumerable<KeyValuePair<K, T>>)this).GetEnumerator();
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement