Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. class HashMap {
  2. constructor(initialCapacity=8) {
  3. this.length = 0;
  4. this._slots = [];
  5. this._capacity = initialCapacity;
  6. this._deleted = 0;
  7. }
  8.  
  9. get(key) {
  10. const index = this._findSlot(key);
  11. if (this._slots[index] === undefined) {
  12. throw new Error('Key error');
  13. }
  14. return this._slots[index].value;
  15. }
  16.  
  17. set(key, value) {
  18. const loadRatio = (this.length + this._deleted + 1) / this._capacity;
  19. if (loadRatio > HashMap.MAX_LOAD_RATIO) {
  20. this._resize(this._capacity * HashMap.SIZE_RATIO);
  21. }
  22.  
  23. const index = this._findSlot(key);
  24. var linkedList = new LinkedList();
  25. this.length++;
  26. }
  27.  
  28. remove(key) {
  29. const index = this._findSlot(key);
  30. const slot = this._slots[index];
  31. if (slot === undefined) {
  32. throw new Error('Key error');
  33. }
  34. slot.deleted = true;
  35. this.length--;
  36. this._deleted++;
  37. }
  38.  
  39. _findSlot(key) {
  40. const hash = HashMap._hashString(key);
  41. const start = hash % this._capacity;
  42.  
  43. for (let i=start; i<start + this._capacity; i++) {
  44. const index = i % this._capacity;
  45. const slot = this._slots[index];
  46. if (slot === undefined || (slot.key == key && !slot.deleted)) {
  47. return index;
  48. }
  49. }
  50. }
  51.  
  52. _resize(size) {
  53. const oldSlots = this._slots;
  54. this._capacity = size;
  55. // Reset the length - it will get rebuilt as you add the items back
  56. this.length = 0;
  57. this._deleted = 0;
  58. this._slots = [];
  59.  
  60. for (const slot of oldSlots) {
  61. if (slot !== undefined && !slot.deleted) {
  62. this.set(slot.key, slot.value);
  63. }
  64. }
  65. }
  66.  
  67. static _hashString(string) {
  68. let hash = 5381;
  69. for (let i=0; i<string.length; i++) {
  70. hash = (hash << 5) + hash + string.charCodeAt(i);
  71. hash = hash & hash;
  72. }
  73. return hash >>> 0;
  74. }
  75. }
  76.  
  77. HashMap.MAX_LOAD_RATIO = 0.9;
  78. HashMap.SIZE_RATIO = 3;
  79.  
  80. class LinkedList {
  81. constructor() {
  82. this.length = 0;
  83. this.head = null;
  84. }
  85. insert(index, value) {
  86. if (index < 0 || index > this.length) {
  87. throw new Error('Index error');
  88. }
  89.  
  90. const newNode = {
  91. value
  92. };
  93.  
  94. if (index == 0) {
  95. newNode.next = this.head;
  96. this.head = newNode;
  97. }
  98. else {
  99. // Find the node which we want to insert after
  100. const node = this._find(index - 1);
  101. newNode.next = node.next;
  102. node.next = newNode;
  103. }
  104.  
  105. this.length++;
  106. }
  107.  
  108. _find(index) {
  109. let node = this.head;
  110. for (let i=0; i<index; i++) {
  111. node = node.next;
  112. }
  113. return node;
  114. }
  115. get(index) {
  116. if (index < 0 || index >= this.length) {
  117. throw new Error('Index error');
  118. }
  119.  
  120. return this._find(index).value;
  121. }
  122. remove(index) {
  123. if (index < 0 || index >= this.length) {
  124. throw new Error('Index error');
  125. }
  126.  
  127. if (index == 0) {
  128. this.head = this.head.next;
  129. }
  130. else {
  131. // Find the node before the one we want to remove
  132. const node = this._find(index - 1);
  133. node.next = node.next.next;
  134. }
  135.  
  136. this.length--;
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement