Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. class HashMap {
  2. constructor() {
  3. this.array = [];
  4. this.maxSize = HashMap.INIT_MAXSIZE;
  5. this.size = 0;
  6. }
  7.  
  8. /**
  9. * Associate key with value, if key has already been entered the associated
  10. * value will will be changed. If value is omitted the key is used for the
  11. * value.
  12. * @param {type} key
  13. * @param {type} value
  14. * @returns {Object} The previous value associated with the key.
  15. */
  16. put(key, value) {
  17. if (typeof value === "undefined") {
  18. value = key;
  19. }
  20. let hashCode = HashMap.hash(key) % this.maxSize;
  21.  
  22. while (typeof this.array[hashCode] !== "undefined") {
  23. if (HashMap.equals(key, this.array[hashCode].key)) {
  24. break;
  25. }
  26. hashCode++;
  27. if (hashCode >= this.maxSize)
  28. hashCode = 0;
  29. }
  30.  
  31. let rvalue = null;
  32. if (typeof this.array[hashCode] === "undefined") {
  33. this.size++;
  34. this.array[hashCode] = {};
  35. } else {
  36. rvalue = this.array[hashCode].value;
  37. }
  38.  
  39. this.array[hashCode].key = key;
  40. this.array[hashCode].value = value;
  41.  
  42. if (this.size / this.maxSize > HashMap.MAX_SATURATION) {
  43. this.grow();
  44. }
  45.  
  46. return rvalue;
  47. }
  48.  
  49. /**
  50. * Retrieve the value attributed to a key.
  51. * @param {type} key
  52. * @returns {unresolved}
  53. */
  54. getValue(key) {
  55. if (this.isEmpty())
  56. return null;
  57.  
  58. let hashCode = HashMap.hash(key) % this.maxSize;
  59. while (typeof this.array[hashCode] !== "undefined") {
  60. if (HashMap.equals(key, this.array[hashCode].key)) {
  61. return this.array[hashCode].value;
  62. }
  63. hashCode++;
  64. if (hashCode === this.maxSize)
  65. hashCode = 0;
  66. }
  67. return null;
  68. }
  69.  
  70. /**
  71. * Determine if they hashmap contains a given key.
  72. * @param {type} key
  73. * @returns {unresolved}
  74. */
  75. hasKey(key) {
  76. if (this.isEmpty())
  77. return false;
  78.  
  79. let hashCode = HashMap.hash(key) % this.maxSize;
  80. while (typeof this.array[hashCode] !== "undefined") {
  81. if (HashMap.equals(key, this.array[hashCode].key)) {
  82. return true;
  83. }
  84. hashCode++;
  85. if (hashCode === this.maxSize)
  86. hashCode = 0;
  87. }
  88. return false;
  89. }
  90.  
  91. /**
  92. * Remove a value attributed to a key.
  93. * @param {type} key
  94. * @returns {value}
  95. */
  96. remove(key) {
  97. if (this.isEmpty())
  98. return null;
  99.  
  100. let hashCode = HashMap.hash(key) % this.maxSize;
  101. while (typeof this.array[hashCode] !== "undefined") {
  102. if (HashMap.equals(key, this.array[hashCode].key)) {
  103. let value = this.array[hashCode].value;
  104. delete this.array[hashCode];
  105. this.size--;
  106. return value;
  107. }
  108. hashCode++;
  109. if (hashCode === this.maxSize)
  110. hashCode = 0;
  111. }
  112. return null;
  113. }
  114.  
  115. /**
  116. * Determine if the map contains no key value pairs.
  117. * @returns {Boolean}
  118. */
  119. isEmpty() {
  120. return this.size === 0;
  121. }
  122.  
  123. /**
  124. * Remove all key-value pairs from the map.
  125. * @returns {HashMap} this hashmap
  126. */
  127. clear() {
  128. this.array = [];
  129. this.size = 0;
  130. return this;
  131. }
  132.  
  133. /**
  134. * Increase the size of the map. Called by 'put'.
  135. * @returns {undefined}
  136. */
  137. grow() {
  138. this.maxSize = this.maxSize + HashMap.INIT_MAXSIZE;
  139. let oldArray = this.array;
  140. this.array = [];
  141. this.size = 0;
  142.  
  143. for (var pair of oldArray) {
  144. if (typeof pair !== "undefined") {
  145. this.put(pair.key, pair.value);
  146. }
  147. }
  148. }
  149.  
  150. /**
  151. * Get the hash value of an object. Uses the 'hash' function if it is
  152. * present.
  153. * @param {type} object
  154. * @returns {Number|HASHMAP_FNV_OFFSET_BASIS|HASHMAP_FNV_MAX}
  155. */
  156. static hash(object) {
  157. if (this.isFunction(object.hash)) {
  158. return object.hash();
  159. }
  160.  
  161. let string = JSON.stringify(object);
  162. let x = HashMap.FNV_OFFSET_BASIS;
  163. for (var i = 0; i < string.length; i++) {
  164. x = x ^ string.charCodeAt(i);
  165. x = x * HashMap.FNV_PRIME;
  166. x = x % HashMap.FNV_MAX;
  167. x = x * x;
  168. }
  169. return x;
  170. }
  171.  
  172. /**
  173. * Determine if two objects are equal. Uses 'equals' method of object1 if
  174. * it is present.
  175. * @param {type} object1
  176. * * @param {type} object2
  177. * @returns {Number|HASHMAP_FNV_OFFSET_BASIS|HASHMAP_FNV_MAX}
  178. */
  179. static equals(object1, object2) {
  180. if (this.isFunction(object1.equals)) {
  181. return object1.equals(object2);
  182. }
  183.  
  184. return object1 === object2;
  185. }
  186.  
  187. /**
  188. * Determine if a field is a function.
  189. * @param {type} obj
  190. * @returns {Boolean}
  191. */
  192. static isFunction(obj) {
  193. return !!(obj && obj.constructor && obj.call && obj.apply);
  194. }
  195. }
  196.  
  197. HashMap.INIT_MAXSIZE = 27;
  198. HashMap.FNV_OFFSET_BASIS = 14695981039346656037;
  199. HashMap.FNV_PRIME = 37783;
  200. HashMap.FNV_MAX = Math.round(Math.sqrt(Number.MAX_SAFE_INTEGER));
  201. HashMap.MAX_SATURATION = 0.6;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement