Advertisement
StreetKatya

MyHashTable

Mar 27th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MyHashTable
  9. {
  10. public class OpenAddressHashTable<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IHashTable<TKey, TValue> where TKey : IEquatable<TKey>
  11. {
  12. Pair<TKey, TValue>[] _table;
  13. private int _capacity;
  14. HashMaker<TKey> _hashMaker1, _hashMaker2;
  15. public int Count { get; private set; }
  16. private const double FillFactor = 0.71;
  17. private readonly GetPrimeNumber _primeNumber = new GetPrimeNumber();
  18.  
  19. public OpenAddressHashTable()
  20. {
  21. _capacity = _primeNumber.GetMin();
  22. _table = new Pair<TKey, TValue>[_capacity];
  23. _hashMaker1 = new HashMaker<TKey>(_capacity);
  24. _hashMaker2 = new HashMaker<TKey>(_capacity - 1);
  25. Count = 0;
  26. }
  27. public OpenAddressHashTable(int m)
  28. {
  29. _table = new Pair<TKey, TValue>[m];
  30. _capacity = m;
  31. _hashMaker1 = new HashMaker<TKey>(_capacity);
  32. _hashMaker2 = new HashMaker<TKey>(_capacity - 1);
  33. Count = 0;
  34. }
  35. public void Add(TKey key, TValue value)
  36. {
  37. var hash = _hashMaker1.ReturnHash(key);
  38.  
  39. if (!TryToPut(hash, key, value)) // ячейка занята
  40. {
  41. int iterationNumber = 1;
  42. while (true)
  43. {
  44. var place = (hash + iterationNumber * (1 + _hashMaker2.ReturnHash(key))) % _capacity;//(1 + _hashMaker2.ReturnHash(key))
  45. if (TryToPut(place, key, value))
  46. break;
  47. iterationNumber++;
  48. if (iterationNumber >= _capacity)
  49. throw new ApplicationException("HashTable full!!!");
  50. }
  51. }
  52. if ((double)Count / _capacity >= FillFactor)
  53. {
  54. IncreaseTable();
  55. }
  56. }
  57. public void Delete(TKey key)
  58. {
  59. Pair<TKey, TValue> pair = Find(key);
  60. if(pair != null)
  61. {
  62. pair.DeletePair();
  63. }
  64. }
  65. private bool TryToPut(int place, TKey key, TValue value)
  66. {
  67. if (_table[place] == null || _table[place].IsDeleted())
  68. {
  69. _table[place] = new Pair<TKey, TValue>(key, value);
  70. Count++;
  71. return true;
  72. }
  73. if (_table[place].Key.Equals(key))
  74. {
  75. throw new ArgumentException();
  76. }
  77. return false;
  78. }
  79.  
  80. private Pair<TKey, TValue> Find(TKey x)
  81. {
  82. var hash = _hashMaker1.ReturnHash(x);
  83. if (_table[hash] == null)
  84. return null;
  85. if (!_table[hash].IsDeleted() && _table[hash].Key.Equals(x))
  86. {
  87. return _table[hash];
  88. }
  89. int iterationNumber = 1;
  90. while (true)
  91. {
  92. var place = (hash + iterationNumber * (1 + _hashMaker2.ReturnHash(x))) % _capacity;
  93. if (_table[place] == null)
  94. return null;
  95. if (!_table[place].IsDeleted() && _table[place].Key.Equals(x))
  96. {
  97. return _table[place];
  98. }
  99. iterationNumber++;
  100. if (iterationNumber >= _capacity)
  101. return null;
  102. }
  103. }
  104. public TValue this[TKey key]
  105. {
  106. get
  107. {
  108. var pair = Find(key);
  109. if (pair == null)
  110. throw new KeyNotFoundException();
  111. return pair.Value;
  112. }
  113.  
  114. set
  115. {
  116. var pair = Find(key);
  117. if (pair == null)
  118. throw new KeyNotFoundException();
  119. pair.Value = value;
  120. }
  121. }
  122.  
  123. private void IncreaseTable()
  124. {
  125. //получить число и увеличить таблицу
  126. int m = _primeNumber.Next();
  127. Pair<TKey, TValue>[] newTable = _table;
  128. _table = new Pair<TKey, TValue>[m];
  129. _capacity = m;
  130. _hashMaker1.SimpleNumber = _capacity;
  131. _hashMaker2.SimpleNumber = _capacity - 1;
  132. Count = 0;
  133. foreach (var pair in newTable)
  134. {
  135. if (pair != null)
  136. Add(pair.Key, pair.Value);
  137. }
  138. //int size = _primeNumber.Next();
  139. //_hashMaker1.SimpleNumber = size;
  140. //_hashMaker2.SimpleNumber = size - 1;
  141. //_capacity = size;
  142. //var tableItem = _table;
  143. //_table = new Pair<TKey, TValue>[size];
  144. //Count = 0;
  145. //foreach(var pair in tableItem)
  146. //{
  147. // if(pair != null)
  148. // Add(pair.Key, pair.Value);
  149. //}
  150. }
  151.  
  152. public bool ContainsKey(TKey key)
  153. {
  154. return Find(key) != null;
  155. }
  156.  
  157. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  158. {
  159. return (from pair in _table where pair != null && !pair.IsDeleted() select new KeyValuePair<TKey, TValue>(pair.Key, pair.Value)).GetEnumerator();
  160. }
  161.  
  162. IEnumerator IEnumerable.GetEnumerator()
  163. {
  164. return GetEnumerator();
  165. }
  166. }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement