Advertisement
nassss

Untitled

Mar 27th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. using DictionaryWorkshop.Implementation.Contracts;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace DictionaryWorkshop.Implementation
  9. {
  10. public class MyDictionary<TKey, TValue> : IMyDictionary<TKey, TValue>, IEnumerable<MyKeyValuePair<TKey, TValue>>
  11. {
  12. private const int initialCap = 16;
  13. private const float loadFactor = 0.75f;
  14. private MyLinkedList<TKey, TValue>[] buckets;
  15. public MyDictionary()
  16. {
  17. this.buckets = new MyLinkedList<TKey, TValue>[initialCap];
  18. }
  19.  
  20. public MyDictionary(int capacity)
  21. {
  22. this.buckets = new MyLinkedList<TKey, TValue>[capacity];
  23. this.Count = 0;
  24. }
  25. public int Count { get; private set; }
  26. public int Capacity { get => this.buckets.Length; }
  27. public TValue this[TKey key]
  28. {
  29. get
  30. {
  31. return this.Get(key);
  32. }
  33. set
  34. {
  35. if (value != null)
  36. {
  37. AddOrReplace(key, value);
  38. }
  39. }
  40. }
  41. public TValue Get (TKey key)
  42. {
  43. var item = Find(key);
  44. if (item==null)
  45. {
  46. throw new KeyNotFoundException();
  47. }
  48. return item.Value;
  49. }
  50.  
  51. public bool Add(TKey key, TValue value)
  52. {
  53. if ((float)(this.Count + 1) / this.Capacity > loadFactor)
  54. {
  55. this.Resize();
  56. }
  57. var index = FindHashKey(key);
  58.  
  59. if (this.buckets[index] == null)
  60. {
  61. this.buckets[index] = new MyLinkedList<TKey, TValue>(key, value);
  62. this.Count++;
  63. return true;
  64. }
  65. var ifKeyExist = this.buckets[index].Any(x => x.Key.Equals(key));
  66.  
  67. if (ifKeyExist)
  68. {
  69. throw new InvalidOperationException("Key already exists!");
  70. }
  71. this.buckets[index].Add(key,value);
  72. this.Count++;
  73. return true;
  74.  
  75. }
  76. private void Resize()
  77. {
  78. var newDictionary = new MyDictionary<TKey, TValue>(2*this.Capacity);
  79. foreach (var item in this)
  80. {
  81. newDictionary.Add(item.Key, item.Value);
  82. }
  83. this.buckets = newDictionary.buckets;
  84. this.Count = newDictionary.Count;
  85. }
  86.  
  87. public bool ContainsKey(TKey key)
  88. {
  89. var item = Find(key);
  90. return item != null;
  91. }
  92.  
  93. private int FindHashKey(TKey key)
  94. {
  95. return Math.Abs(key.GetHashCode() % this.buckets.Length);
  96. }
  97. public MyKeyValuePair<TKey, TValue> Find (TKey key)
  98. {
  99. int index = FindHashKey(key);
  100. var items = this.buckets[index];
  101. if (items !=null)
  102. {
  103. foreach (var item in items)
  104. {
  105. if (item.Key.Equals(key))
  106. {
  107. return item;
  108. }
  109. }
  110. }
  111. return null;
  112. }
  113. public bool AddOrReplace(TKey key, TValue value)
  114. {
  115. if ((float)(this.Count + 1) / this.Capacity > loadFactor)
  116. {
  117. this.Resize();
  118. }
  119. int slotNumber = this.FindHashKey(key);
  120. if (this.buckets[slotNumber] == null)
  121. {
  122. this.buckets[slotNumber] = new MyLinkedList<TKey, TValue>(key, value);
  123. }
  124. foreach (var item in this.buckets[slotNumber])
  125. {
  126. if (item.Key.Equals(key))
  127. {
  128. item.Value = value;
  129. return false;
  130. }
  131. }
  132. this.buckets[slotNumber].Add(key, value);
  133. this.Count++;
  134. return true;
  135.  
  136. }
  137.  
  138. public IEnumerable<TKey> GetKeys()
  139. {
  140. return this.buckets.Where(l => l != null)
  141. .SelectMany(x => x.Select(y => y.Key));
  142. }
  143. public IEnumerable<TValue> GetValues()
  144. {
  145. return this.buckets.Where(v => v != null)
  146. .SelectMany(x => x.Select(y => y.Value));
  147. }
  148. public bool Remove(TKey key)
  149. {
  150. var kvpItem = Find(key);
  151. if (kvpItem !=null)
  152. {
  153. int index = this.FindHashKey(key);
  154. buckets[index].Remove(key);
  155. this.Count--;
  156. return true;
  157. }
  158. return false;
  159. }
  160.  
  161. public bool TryGetValue(TKey key, out TValue value)
  162. {
  163. var item = this.Find(key);
  164. if (item!= null)
  165. {
  166. value = item.Value;
  167. return true;
  168. }
  169. value = default(TValue);
  170. return false;
  171. }
  172.  
  173. public IEnumerator<MyKeyValuePair<TKey, TValue>> GetEnumerator()
  174. {
  175. foreach (var items in buckets)
  176. {
  177. if (items !=null)
  178. {
  179. foreach (var item in items)
  180. {
  181. yield return item;
  182. }
  183. }
  184. }
  185. }
  186.  
  187. IEnumerator IEnumerable.GetEnumerator()
  188. {
  189. return this.GetEnumerator();
  190. }
  191. public override string ToString()
  192. {
  193. var sb = new StringBuilder();
  194. foreach (var item in buckets)
  195. {
  196. if (item !=null)
  197. {
  198. sb.AppendLine(item.ToString());
  199. }
  200. }
  201. return sb.ToString().Trim();
  202. }
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement