using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hash_dictionary { public class Dict:IEnumerable> { private const int INI_Size = 16; LinkedList>[] values; public Dict() { this.values = new LinkedList>[INI_Size]; } public int Count { get; private set; } public int Capacity { get { return this.values.Length; } } public void Add(K key, V value) { var hash = this.HashKey(key); if(this.values[hash]== null) { this.values[hash] = new LinkedList>(); } var keyExistsAlready = this.values[hash].Any(p => p.Key.Equals(key)); if (keyExistsAlready) { throw new InvalidOperationException("key already exist"); } var pair = new KeyValuePair(key, value); this.values[hash].AddLast(pair); this.Count++; if (this.Count >= this.Capacity * 0.75) { this.ResizeAndReAddValues(); } } public V Find (K key) { var hash = this.HashKey(key); if(this.values[hash] == null) { return default(V); } var collection = this.values[hash]; return collection.First(p => p.Key.Equals(key)).Value; } public bool ContainsKey(K key) { var hash = HashKey(key); if (this.values[hash] == null) { return false; } var collection = this.values[hash]; return collection.Any(pair => pair.Key.Equals(key)); } private int HashKey(K key) { var hash = Math.Abs(key.GetHashCode()) % this.Capacity; return hash; } private void ResizeAndReAddValues() { var cachedValues = this.values; this.values = new LinkedList>[2 * this.Capacity]; this.Count = 0; foreach (var collection in cachedValues) { if (collection != null) { foreach (var value in collection) { this.Add(value.Key, value.Value); } } } } public IEnumerator> GetEnumerator() { foreach(var collection in this.values) { if (collection != null) { foreach (var value in collection) { yield return value; } } } } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }