Guest User

Untitled

a guest
Mar 10th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. internal class Program
  2. {
  3. class Dict<TKey,TValue>
  4. {
  5. private struct Entry
  6. {
  7. public int hashCode; // Lower 31 bits of hash code, -1 if unused
  8. public int next; // Index of next entry, -1 if last
  9. public TKey key; // Key of entry
  10. public TValue value; // Value of entry
  11. }
  12.  
  13. private int[] buckets;
  14. private Entry[] entries;
  15. private int count;
  16. private int freeList;
  17. private int freeCount;
  18. private IEqualityComparer<TKey> comparer;
  19.  
  20. public Dict()
  21. {
  22. this.comparer = EqualityComparer<TKey>.Default;
  23. Initialize(2);
  24. }
  25.  
  26. public Dict(IEqualityComparer<TKey> cmp)
  27. {
  28. this.comparer = cmp;
  29. Initialize(2);
  30. }
  31.  
  32. public int Count
  33. {
  34. get { return count; }
  35. }
  36.  
  37. private void Initialize(int size)
  38. {
  39. buckets = new int[size];
  40. for(int i = 0; i < buckets.Length; i++)
  41. buckets[i] = -1;
  42. entries = new Entry[size];
  43. freeList = -1;
  44. }
  45. public void Add(TKey key, TValue value)
  46. {
  47. Insert(key, value);
  48. }
  49. private void Insert(TKey key, TValue value)
  50. {
  51. int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
  52. int targetBucket = hashCode % buckets.Length;
  53.  
  54. for(int i = buckets[targetBucket]; i >= 0; i = entries[i].next)
  55. {
  56. if(entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key))
  57. {
  58. entries[i].value = value;
  59. return;
  60. }
  61. }
  62. int index;
  63. if(freeCount > 0)
  64. {
  65. index = freeList;
  66. freeList = entries[index].next;
  67. freeCount--;
  68. }
  69. else
  70. {
  71. if(count == entries.Length)
  72. {
  73. Resize();
  74. targetBucket = hashCode % buckets.Length;
  75. }
  76. index = count;
  77. count++;
  78. }
  79.  
  80. entries[index].hashCode = hashCode;
  81. entries[index].next = buckets[targetBucket];
  82. entries[index].key = key;
  83. entries[index].value = value;
  84. buckets[targetBucket] = index;
  85. }
  86.  
  87. public bool ContainsKey(TKey key)
  88. {
  89. return FindEntry(key) >= 0;
  90. }
  91.  
  92. private int FindEntry(TKey key)
  93. {
  94. if(key == null)
  95. return -1;
  96.  
  97. if(buckets != null)
  98. {
  99. int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
  100. for(int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next)
  101. {
  102. if(entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) return i;
  103. }
  104. }
  105. return -1;
  106. }
  107.  
  108. public TValue this[TKey key]
  109. {
  110. get {
  111. int i = FindEntry(key);
  112. if(i >= 0)
  113. return entries[i].value;
  114. return
  115. default(TValue);
  116. }
  117. set {
  118. Insert(key, value);
  119. }
  120. }
  121.  
  122. private void Resize()
  123. {
  124. int newSize = count * 2;
  125. int[] newBuckets = new int[newSize];
  126. for(int i = 0; i < newBuckets.Length; i++) newBuckets[i] = -1;
  127. Entry[] newEntries = new Entry[newSize];
  128. Array.Copy(entries, 0, newEntries, 0, count);
  129. for(int i = 0; i < count; i++)
  130. {
  131. if(newEntries[i].hashCode >= 0)
  132. {
  133. int bucket = newEntries[i].hashCode % newSize;
  134. newEntries[i].next = newBuckets[bucket];
  135. newBuckets[bucket] = i;
  136. }
  137. }
  138. buckets = newBuckets;
  139. entries = newEntries;
  140. }
  141. }
  142.  
  143. public static int hash(string name)
  144. {
  145. int sum = 0;
  146. foreach(char ch in name)
  147. //sum *= 13 + 7*ch;
  148. sum += ch;
  149. return sum;
  150. }
  151.  
  152. public class SimpleSumComparer : IEqualityComparer<string>
  153. {
  154. public bool Equals(string a, string b)
  155. {
  156. return a == b;
  157. }
  158.  
  159. public int GetHashCode(string name)
  160. {
  161. int hash = 0;
  162. foreach(char ch in name)
  163. hash = (hash * 31) + ch;
  164. return hash;
  165. }
  166. }
  167.  
  168. public static void Main(string[] args)
  169. {
  170. string input_text = System.IO.File.ReadAllText(@"engwiki_ascii.txt");//bigone
  171. var watch = System.Diagnostics.Stopwatch.StartNew();
  172. long elapsedMs;
  173.  
  174. //var obj = new Dictionary<string, int>(new SimpleSumComparer());
  175. var obj = new Dict<string, int>(new SimpleSumComparer());
  176.  
  177. string str = "";
  178. foreach(var i in input_text)
  179. {
  180. if(i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z' || i == '\'')
  181. str += i;
  182. else if(str.Length > 0)
  183. {
  184. if(obj.ContainsKey(str))
  185. ++obj[str];
  186. else
  187. obj.Add(str, 1);
  188. str = "";
  189. }
  190. }
  191. Console.WriteLine("wiki: " + obj["wiki"]);
  192. Console.WriteLine("size: " + obj.Count);
  193. watch.Stop();
  194. elapsedMs = watch.ElapsedMilliseconds;
  195. System.Console.WriteLine("time: " + elapsedMs/1000.0);
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment