Guest User

Untitled

a guest
Dec 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. public class MyDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey,TValue>>
  2. {
  3. private int _count;
  4.  
  5. private Node<TKey, TValue>[] _storage;
  6.  
  7. private readonly int _currentSize;
  8.  
  9. private const int DefaultSize = 100;
  10.  
  11. public MyDictionary(int size)
  12. {
  13. _currentSize = size;
  14. _storage = new Node<TKey, TValue>[size];
  15. }
  16.  
  17. public MyDictionary()
  18. {
  19. _currentSize = DefaultSize;
  20. _storage = new Node<TKey, TValue>[DefaultSize];
  21. }
  22.  
  23. public int Count()
  24. {
  25. return _count;
  26. }
  27.  
  28. public void Add(TKey key, TValue value)
  29. {
  30. if (ContainsKey(key))
  31. {
  32. throw new ArgumentException($"Dictionary already contain this key: {key}");
  33. }
  34.  
  35. var pos = GetPosition(key);
  36.  
  37. Add(pos, key, value);
  38.  
  39. _count++;
  40. }
  41.  
  42. public bool ContainsKey(TKey key)
  43. {
  44. foreach (var node in _storage)
  45. {
  46. if ( node != null && node.Next != null)
  47. {
  48. var tmp = node;
  49.  
  50. while (tmp != null)
  51. {
  52. if (tmp.Key.Equals(key))
  53. {
  54. return true;
  55. }
  56.  
  57. tmp = tmp.Next;
  58. }
  59. }
  60.  
  61. if (node != null && node.Key.Equals(key))
  62. {
  63. return true;
  64. }
  65.  
  66. }
  67.  
  68. return false;
  69. }
  70.  
  71. public bool ContainsValue(TValue value)
  72. {
  73. foreach (var node in _storage)
  74. {
  75. if (node?.Next != null)
  76. {
  77. var tmp = node;
  78.  
  79.  
  80. while (tmp != null)
  81. {
  82. if (tmp.Value.Equals(value))
  83. {
  84. return true;
  85. }
  86.  
  87. tmp = tmp.Next;
  88. }
  89. }
  90. if (node != null && node.Value.Equals(value))
  91. {
  92. return true;
  93. }
  94. }
  95.  
  96. return false;
  97. }
  98.  
  99. public void Remove(TKey key)
  100. {
  101. if (!ContainsKey(key))
  102. {
  103. throw new ArgumentException($"Dictionary not contain the value with key: {key}");
  104. }
  105.  
  106. var pos = GetPosition(key);
  107.  
  108. if (_storage[pos].Next == null)
  109. {
  110. _storage[pos] = null;
  111. _count--;
  112. return;
  113. }
  114.  
  115. var tmp = _storage[pos];
  116.  
  117. _storage[pos] = null;
  118.  
  119. while (tmp != null)
  120. {
  121. if (!tmp.Key.Equals(key))
  122. {
  123. Add(pos, tmp.Key, tmp.Value);
  124. }
  125.  
  126. tmp = tmp.Next;
  127. }
  128.  
  129. _count--;
  130. }
  131.  
  132. public void Clear()
  133. {
  134. _storage = new Node<TKey, TValue>[_currentSize];
  135. _count = 0;
  136. }
  137.  
  138. private void Add(int pos, TKey key, TValue value)
  139. {
  140. if (_storage[pos] != null)
  141. {
  142. var tmp = _storage[pos];
  143.  
  144. while (tmp.Next != null)
  145. {
  146. tmp = tmp.Next;
  147. }
  148.  
  149. tmp.Next = new Node<TKey, TValue>() { Key = key, Value = value };
  150. }
  151. else
  152. {
  153. _storage[pos] = new Node<TKey, TValue>() { Key = key, Value = value };
  154. }
  155. }
  156.  
  157. private int GetPosition(TKey key)
  158. {
  159. return Math.Abs(key.GetHashCode() % _currentSize);
  160. }
  161.  
  162. public IEnumerator<KeyValuePair<TKey, TValue>> Enumerator()
  163. {
  164. foreach (var node in _storage)
  165. {
  166. if (node == null) continue;
  167.  
  168. if (node.Next != null)
  169. {
  170. var current = node;
  171.  
  172. while (current != null)
  173. {
  174. yield return new KeyValuePair<TKey, TValue>(current.Key, current.Value);
  175.  
  176. current = current.Next;
  177. }
  178. }
  179. else
  180. {
  181. yield return new KeyValuePair<TKey, TValue>(node.Key, node.Value);
  182. }
  183. }
  184. }
  185.  
  186. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
  187. {
  188. return Enumerator();
  189. }
  190.  
  191. public IEnumerator GetEnumerator()
  192. {
  193. return Enumerator();
  194. }
  195. }
  196.  
  197. internal class Node<TKey, TValue>
  198. {
  199. public TValue Value { get; set; }
  200.  
  201. public TKey Key { get; set; }
  202.  
  203. public Node<TKey, TValue> Next;
  204. }
Add Comment
Please, Sign In to add comment