Veikedo

hashTable

Jan 5th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace HashTableProject
  7. {
  8.   public class StringHashTable<T> : IDictionary<string, T>
  9.   {
  10.     private const double MaxLoadFactor = 0.75;
  11.     private readonly List<string> _keys;
  12.     private int _bucketCount;
  13.     private List<KeyValuePair<string, T>>[] _buckets;
  14.  
  15.     public StringHashTable() : this(500)
  16.     {
  17.     }
  18.  
  19.     private StringHashTable(int capacity)
  20.     {
  21.       _keys = new List<string>();
  22.       _buckets = new List<KeyValuePair<string, T>>[capacity];
  23.     }
  24.  
  25.     #region Implementation of IEnumerable
  26.  
  27.     public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
  28.     {
  29.       return _buckets.Where(list => list != null).SelectMany(list => list).GetEnumerator();
  30.     }
  31.  
  32.     IEnumerator IEnumerable.GetEnumerator()
  33.     {
  34.       return GetEnumerator();
  35.     }
  36.  
  37.     #endregion
  38.  
  39.     #region Implementation of ICollection<KeyValuePair<string,T>>
  40.  
  41.     public void Add(KeyValuePair<string, T> item)
  42.     {
  43.       Add(item.Key, item.Value);
  44.     }
  45.  
  46.     public void Clear()
  47.     {
  48.       throw new NotImplementedException();
  49.     }
  50.  
  51.     public bool Contains(KeyValuePair<string, T> item)
  52.     {
  53.       throw new NotImplementedException();
  54.     }
  55.  
  56.     public void CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
  57.     {
  58.       throw new NotImplementedException();
  59.     }
  60.  
  61.     public bool Remove(KeyValuePair<string, T> item)
  62.     {
  63.       throw new NotImplementedException();
  64.     }
  65.  
  66.     public int Count { get; private set; }
  67.  
  68.     public bool IsReadOnly
  69.     {
  70.       get { return false; }
  71.     }
  72.  
  73.     #endregion
  74.  
  75.     #region Implementation of IDictionary<string,T>
  76.  
  77.     public bool ContainsKey(string key)
  78.     {
  79.       return _keys.Contains(key);
  80.     }
  81.  
  82.     public void Add(string key, T value)
  83.     {
  84.       if (_keys.Contains(key))
  85.       {
  86.         throw new ArgumentException("Dublicate key", "key");
  87.       }
  88.  
  89.       if (LoadFactor >= MaxLoadFactor)
  90.       {
  91.         Resize();
  92.       }
  93.  
  94.       int index = GetKeyIndex(key);
  95.  
  96.       if (_buckets[index] == null)
  97.       {
  98.         _buckets[index] = new List<KeyValuePair<string, T>>();
  99.         _bucketCount++;
  100.       }
  101.  
  102.       _buckets[index].Add(new KeyValuePair<string, T>(key, value));
  103.       _keys.Add(key);
  104.  
  105.       Count++;
  106.     }
  107.  
  108.     public bool Remove(string key)
  109.     {
  110.       if (!ContainsKey(key))
  111.       {
  112.         return true;
  113.       }
  114.  
  115.       int index = GetKeyIndex(key);
  116.       int valueIndex = _buckets[index].FindIndex(pair => pair.Key == key);
  117.  
  118.       _buckets[index].RemoveAt(valueIndex);
  119.       _keys.Remove(key);
  120.  
  121.       return true;
  122.     }
  123.  
  124.     public bool TryGetValue(string key, out T value)
  125.     {
  126.       if (!ContainsKey(key))
  127.       {
  128.         value = default(T);
  129.         return false;
  130.       }
  131.  
  132.       int index = GetKeyIndex(key);
  133.       value = _buckets[index].First(pair => pair.Key == key).Value;
  134.  
  135.       return true;
  136.     }
  137.  
  138.     public T this[string key]
  139.     {
  140.       get
  141.       {
  142.         T value;
  143.         if (!TryGetValue(key, out value))
  144.         {
  145.           throw new KeyNotFoundException();
  146.         }
  147.  
  148.         return value;
  149.       }
  150.  
  151.       set { throw new NotImplementedException(); }
  152.     }
  153.  
  154.     public ICollection<string> Keys
  155.     {
  156.       get { return _keys; }
  157.     }
  158.  
  159.     public ICollection<T> Values
  160.     {
  161.       get
  162.       {
  163.         return _buckets.Where(list => list != null)
  164.                        .SelectMany(list => list.Select(pair => pair.Value))
  165.                        .ToList();
  166.       }
  167.     }
  168.  
  169.     #endregion
  170.  
  171.     private double LoadFactor
  172.     {
  173.       get { return (double) _bucketCount/Capacity; }
  174.     }
  175.  
  176.     private int Capacity
  177.     {
  178.       get { return _buckets.Length; }
  179.     }
  180.  
  181.     private void Resize()
  182.     {
  183.       var newSize = (int) (Capacity*1.5);
  184.       var newHashTable = new StringHashTable<T>(newSize);
  185.  
  186.       foreach (var pair in this)
  187.       {
  188.         newHashTable.Add(pair);
  189.       }
  190.  
  191.       _buckets = newHashTable._buckets;
  192.       _bucketCount = newHashTable._bucketCount;
  193.     }
  194.  
  195.     private int GetKeyIndex(string key)
  196.     {
  197.       ulong hash = key.Aggregate((ulong) 5381, (i, c) => (i << 5) + i + c);
  198.       return (int) (hash%(ulong) Capacity);
  199.     }
  200.   }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment