PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/UÅūduotis NR. 1/1Lab_Algoritmai/DoubleHasSearchSuAtmintim/DoubleHashTable.cs

https://bitbucket.org/minsma/algoritmu-sudarymas-ir-analize
C# | 304 lines | 248 code | 56 blank | 0 comment | 39 complexity | 4df302ccfb8d5de4e0d0ffd5a9880ba0 MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace DoubleHashSearch
  5. {
  6. class DoubleHashTable<K, V>
  7. {
  8. public const int DEFAULT_INITIAL_CAPACITY = 16;
  9. public const float DEFAULT_LOAD_FACTOR = 0.75f;
  10. Entry<K, V>[] table;
  11. int size = 0;
  12. float loadFactor;
  13. int index = 0;
  14. public DoubleHashTable() : this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR)
  15. {
  16. }
  17. public DoubleHashTable(int initialCapacity, float loadFactor)
  18. {
  19. if (initialCapacity <= 0)
  20. {
  21. throw new ArgumentException("Illegal initial capacity: "
  22. + initialCapacity);
  23. }
  24. if ((loadFactor <= 0.0) || (loadFactor > 1.0))
  25. {
  26. throw new ArgumentException("Illegal load factor: "
  27. + loadFactor);
  28. }
  29. this.table = new Entry<K, V>[initialCapacity];
  30. this.loadFactor = loadFactor;
  31. }
  32. private void fillBytesArray(byte[][] bytes)
  33. {
  34. int i = 0;
  35. foreach (var item in table)
  36. {
  37. if (item != null)
  38. {
  39. string el = item.ToString().Split('=')[0];
  40. bytes[i] = new byte[el.Length];
  41. Encoding.ASCII.GetBytes(el).CopyTo(bytes[i], 0);
  42. i++;
  43. }
  44. else
  45. {
  46. bytes[i] = new byte[15];
  47. Encoding.ASCII.GetBytes("null ").CopyTo(bytes[i], 0);
  48. i++;
  49. }
  50. }
  51. }
  52. public void WriteToFile(string filename, int n)
  53. {
  54. byte[][] bytes = new byte[table.Length][];
  55. if (File.Exists(filename))
  56. File.Delete(filename);
  57. fillBytesArray(bytes);
  58. try
  59. {
  60. using (BinaryWriter writer = new BinaryWriter(File.Open(filename,
  61. FileMode.Create)))
  62. {
  63. foreach (var item in bytes)
  64. {
  65. for (int j = 0; j < item.Length; j++)
  66. writer.Write(item[j]);
  67. }
  68. }
  69. }
  70. catch (IOException ex)
  71. {
  72. Console.WriteLine(ex.ToString());
  73. }
  74. }
  75. public bool FileContains(FileStream fs, string x, int k)
  76. {
  77. return FileContains(fs, x, 0, k);
  78. }
  79. private bool FileContains(FileStream fs, string x, int t, int k)
  80. {
  81. byte[] data = new byte[k];
  82. fs.Seek(t, SeekOrigin.Begin);
  83. while (fs.Read(data, t, k) > 0)
  84. {
  85. string element = Encoding.ASCII.GetString(data, t, k);
  86. if (x.Equals(element))
  87. return true;
  88. }
  89. return false;
  90. }
  91. public bool isEmpty()
  92. {
  93. return size == 0;
  94. }
  95. public int tableSize()
  96. {
  97. return size;
  98. }
  99. public void clear()
  100. {
  101. Array.Fill(table, null);
  102. size = 0;
  103. index = 0;
  104. }
  105. public string[] toArray()
  106. {
  107. string[] result = new string[table.Length];
  108. int count = 0;
  109. foreach (Entry<K, V> n in table)
  110. {
  111. if (n != null)
  112. {
  113. result[count] = n.ToString();
  114. count++;
  115. }
  116. else
  117. {
  118. result[count] = "";
  119. count++;
  120. }
  121. }
  122. return result;
  123. }
  124. public V put(K key, V value)
  125. {
  126. if (key == null || value == null)
  127. {
  128. throw new NullReferenceException("Key or value is null in "
  129. + "put(Key key, Value value)");
  130. }
  131. int index = findPosition(key);
  132. if (index == -1)
  133. {
  134. rehash();
  135. put(key, value);
  136. return value;
  137. }
  138. if (table[index] == null)
  139. {
  140. table[index] = new Entry<K, V>(key, value);
  141. size++;
  142. if (size > table.Length * loadFactor)
  143. {
  144. rehash();
  145. }
  146. }
  147. else
  148. {
  149. table[index].value = value;
  150. }
  151. return value;
  152. }
  153. public V get(K key)
  154. {
  155. if (key == null)
  156. {
  157. throw new NullReferenceException("Key is null in get(Key key)");
  158. }
  159. int index = findPosition(key);
  160. return (index != -1 && table[index] != null) ? table[index].value : default(V);
  161. }
  162. public V remove(K key)
  163. {
  164. if (key == null)
  165. {
  166. throw new NullReferenceException("Key is null in remove(Key key)");
  167. }
  168. int index = findPosition(key);
  169. if (index > -1)
  170. {
  171. table[index] = null;
  172. size--;
  173. }
  174. return default(V);
  175. }
  176. public bool contains(K key)
  177. {
  178. return get(key) != null;
  179. }
  180. public String toString()
  181. {
  182. StringBuilder result = new StringBuilder();
  183. foreach (Entry<K, V> entry in table)
  184. {
  185. if (entry != null)
  186. {
  187. result.Append(entry.ToString()).Append('\n');
  188. }
  189. }
  190. return result.ToString();
  191. }
  192. private int findPosition(K key)
  193. {
  194. int index = hash(key);
  195. int index0 = index;
  196. int i = 0;
  197. for (int j = 0; j < table.Length; j++)
  198. {
  199. if (table[index] == null || table[index].key.Equals(key))
  200. {
  201. return index;
  202. }
  203. i++;
  204. index = (index0 + index * hash2(key)) % table.Length;
  205. }
  206. return -1;
  207. }
  208. private int hash(K key)
  209. {
  210. int h = key.GetHashCode();
  211. return Math.Abs(h) % table.Length;
  212. }
  213. private int hash2(K key)
  214. {
  215. return 7 - (Math.Abs(key.GetHashCode()) % 7);
  216. }
  217. private void rehash()
  218. {
  219. DoubleHashTable<K, V> mapKTUOA
  220. = new DoubleHashTable<K, V>(table.Length * 2, loadFactor);
  221. for (int i = 0; i < table.Length; i++)
  222. {
  223. if (table[i] != null)
  224. {
  225. mapKTUOA.put(table[i].key, table[i].value);
  226. }
  227. }
  228. table = mapKTUOA.table;
  229. }
  230. class Entry<K, V>
  231. {
  232. public K key;
  233. public V value;
  234. public Entry()
  235. {
  236. }
  237. public Entry(K key, V value)
  238. {
  239. this.key = key;
  240. this.value = value;
  241. }
  242. public override String ToString()
  243. {
  244. return key + "=" + value;
  245. }
  246. }
  247. }
  248. }