data-structure-typed 1.44.1 → 1.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/.eslintrc.js +6 -6
  2. package/CHANGELOG.md +1 -1
  3. package/README.md +15 -15
  4. package/benchmark/report.html +15 -15
  5. package/benchmark/report.json +121 -121
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  13. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  14. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  16. package/dist/cjs/data-structures/hash/hash-map.d.ts +230 -37
  17. package/dist/cjs/data-structures/hash/hash-map.js +427 -115
  18. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  19. package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
  20. package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
  21. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  22. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  23. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  24. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  25. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  26. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  27. package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
  28. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  29. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  30. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  31. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  32. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  33. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  34. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  35. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  36. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  37. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +15 -1
  38. package/dist/cjs/types/data-structures/hash/index.d.ts +6 -0
  39. package/dist/cjs/types/data-structures/hash/index.js +20 -0
  40. package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
  41. package/dist/cjs/utils/utils.d.ts +3 -0
  42. package/dist/cjs/utils/utils.js +15 -1
  43. package/dist/cjs/utils/utils.js.map +1 -1
  44. package/dist/mjs/data-structures/hash/hash-map.d.ts +230 -37
  45. package/dist/mjs/data-structures/hash/hash-map.js +433 -121
  46. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +15 -1
  47. package/dist/mjs/types/data-structures/hash/index.d.ts +6 -0
  48. package/dist/mjs/types/data-structures/hash/index.js +6 -1
  49. package/dist/mjs/utils/utils.d.ts +3 -0
  50. package/dist/mjs/utils/utils.js +11 -0
  51. package/dist/umd/data-structure-typed.js +533 -207
  52. package/dist/umd/data-structure-typed.min.js +1 -1
  53. package/dist/umd/data-structure-typed.min.js.map +1 -1
  54. package/package.json +2 -2
  55. package/src/data-structures/binary-tree/avl-tree.ts +7 -7
  56. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
  57. package/src/data-structures/binary-tree/binary-tree.ts +39 -31
  58. package/src/data-structures/binary-tree/bst.ts +12 -8
  59. package/src/data-structures/binary-tree/rb-tree.ts +17 -6
  60. package/src/data-structures/binary-tree/segment-tree.ts +1 -1
  61. package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
  62. package/src/data-structures/graph/abstract-graph.ts +46 -31
  63. package/src/data-structures/graph/directed-graph.ts +10 -5
  64. package/src/data-structures/graph/map-graph.ts +8 -8
  65. package/src/data-structures/graph/undirected-graph.ts +9 -9
  66. package/src/data-structures/hash/hash-map.ts +430 -123
  67. package/src/data-structures/hash/hash-table.ts +1 -1
  68. package/src/data-structures/hash/tree-map.ts +2 -1
  69. package/src/data-structures/hash/tree-set.ts +2 -1
  70. package/src/data-structures/heap/heap.ts +8 -5
  71. package/src/data-structures/heap/max-heap.ts +3 -3
  72. package/src/data-structures/heap/min-heap.ts +3 -3
  73. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  74. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  75. package/src/data-structures/matrix/matrix.ts +2 -2
  76. package/src/data-structures/matrix/matrix2d.ts +1 -1
  77. package/src/data-structures/matrix/navigator.ts +3 -3
  78. package/src/data-structures/matrix/vector2d.ts +2 -1
  79. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
  80. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
  81. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  82. package/src/data-structures/queue/deque.ts +5 -4
  83. package/src/data-structures/queue/queue.ts +2 -2
  84. package/src/data-structures/tree/tree.ts +1 -1
  85. package/src/data-structures/trie/trie.ts +1 -1
  86. package/src/interfaces/binary-tree.ts +2 -2
  87. package/src/interfaces/graph.ts +1 -1
  88. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  89. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  90. package/src/types/data-structures/binary-tree/bst.ts +2 -2
  91. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  92. package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
  93. package/src/types/data-structures/hash/hash-map.ts +17 -1
  94. package/src/types/data-structures/hash/index.ts +7 -0
  95. package/src/types/data-structures/matrix/navigator.ts +1 -1
  96. package/src/types/utils/utils.ts +1 -1
  97. package/src/types/utils/validate-type.ts +18 -4
  98. package/src/utils/utils.ts +16 -3
  99. package/test/config.ts +1 -1
  100. package/test/integration/all-in-one.ts +1 -1
  101. package/test/integration/avl-tree.test.ts +1 -1
  102. package/test/integration/bst.test.ts +19 -19
  103. package/test/integration/heap.test.js +1 -1
  104. package/test/integration/index.html +7 -7
  105. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -4
  106. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +4 -4
  107. package/test/performance/data-structures/binary-tree/bst.test.ts +4 -4
  108. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +6 -6
  109. package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
  110. package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
  111. package/test/performance/data-structures/heap/heap.test.ts +5 -5
  112. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
  113. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -4
  114. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +7 -5
  115. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +7 -7
  116. package/test/performance/data-structures/queue/deque.test.ts +5 -5
  117. package/test/performance/data-structures/queue/queue.test.ts +6 -6
  118. package/test/performance/data-structures/stack/stack.test.ts +6 -6
  119. package/test/performance/data-structures/trie/trie.test.ts +4 -4
  120. package/test/performance/reportor.ts +15 -13
  121. package/test/performance/types/reportor.ts +1 -1
  122. package/test/types/utils/json2html.ts +1 -1
  123. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +8 -8
  124. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +12 -12
  125. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -76
  126. package/test/unit/data-structures/binary-tree/bst.test.ts +50 -46
  127. package/test/unit/data-structures/binary-tree/overall.test.ts +18 -18
  128. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +21 -21
  129. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
  130. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -37
  131. package/test/unit/data-structures/graph/abstract-graph.test.ts +9 -8
  132. package/test/unit/data-structures/graph/directed-graph.test.ts +34 -14
  133. package/test/unit/data-structures/graph/map-graph.test.ts +1 -1
  134. package/test/unit/data-structures/graph/overall.test.ts +1 -1
  135. package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
  136. package/test/unit/data-structures/hash/coordinate-map.test.ts +1 -1
  137. package/test/unit/data-structures/hash/coordinate-set.test.ts +1 -1
  138. package/test/unit/data-structures/hash/hash-map.test.ts +150 -21
  139. package/test/unit/data-structures/hash/hash-table.test.ts +1 -1
  140. package/test/unit/data-structures/heap/heap.test.ts +41 -29
  141. package/test/unit/data-structures/heap/max-heap.test.ts +6 -6
  142. package/test/unit/data-structures/heap/min-heap.test.ts +6 -6
  143. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
  144. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
  145. package/test/unit/data-structures/linked-list/skip-list.test.ts +5 -5
  146. package/test/unit/data-structures/matrix/matrix.test.ts +5 -5
  147. package/test/unit/data-structures/matrix/matrix2d.test.ts +3 -3
  148. package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
  149. package/test/unit/data-structures/matrix/vector2d.test.ts +1 -1
  150. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +7 -7
  151. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
  152. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +19 -13
  153. package/test/unit/data-structures/queue/deque.test.ts +28 -28
  154. package/test/unit/data-structures/queue/queue.test.ts +3 -3
  155. package/test/unit/data-structures/stack/stack.test.ts +1 -1
  156. package/test/unit/data-structures/tree/tree.test.ts +1 -1
  157. package/test/unit/data-structures/trie/trie.test.ts +10 -10
  158. package/test/utils/array.ts +2 -2
  159. package/test/utils/big-o.ts +4 -4
  160. package/test/utils/json2html.ts +7 -3
  161. package/test/utils/number.ts +1 -1
@@ -5,151 +5,463 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
+ import { isObjOrFunc, rangeCheck, throwRangeError } from '../../utils';
9
+ /**
10
+ * Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
11
+ * these underlying structures have already dealt with dynamic expansion and hash collisions.
12
+ * Therefore, there is no need for additional logic to handle these issues.
13
+ */
14
+ export class HashMapIterator {
15
+ hashMap;
16
+ iterateDirection;
17
+ _node;
18
+ _sentinel;
19
+ /**
20
+ * This is a constructor function for a linked list iterator in a HashMap data structure.
21
+ * @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
22
+ * represents a node in a linked list used in a hash map data structure. It contains a key-value pair
23
+ * and references to the previous and next nodes in the linked list.
24
+ * @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It
25
+ * is used to mark the beginning or end of the list and is typically used in data structures like
26
+ * hash maps or linked lists to simplify operations and boundary checks.
27
+ * @param hashMap - A HashMap object that stores key-value pairs.
28
+ * @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional
29
+ * parameter that specifies the direction in which the iterator should iterate over the elements of
30
+ * the HashMap. It can take one of the following values:
31
+ * @returns The constructor does not return anything. It is used to initialize the properties and
32
+ * methods of the object being created.
33
+ */
34
+ constructor(node, sentinel, hashMap, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
35
+ this._node = node;
36
+ this._sentinel = sentinel;
37
+ this.iterateDirection = iterateDirection;
38
+ if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
39
+ this.prev = function () {
40
+ if (this._node.prev === this._sentinel) {
41
+ throwRangeError();
42
+ }
43
+ this._node = this._node.prev;
44
+ return this;
45
+ };
46
+ this.next = function () {
47
+ if (this._node === this._sentinel) {
48
+ throwRangeError();
49
+ }
50
+ this._node = this._node.next;
51
+ return this;
52
+ };
53
+ }
54
+ else {
55
+ this.prev = function () {
56
+ if (this._node.next === this._sentinel) {
57
+ throwRangeError();
58
+ }
59
+ this._node = this._node.next;
60
+ return this;
61
+ };
62
+ this.next = function () {
63
+ if (this._node === this._sentinel) {
64
+ throwRangeError();
65
+ }
66
+ this._node = this._node.prev;
67
+ return this;
68
+ };
69
+ }
70
+ this.hashMap = hashMap;
71
+ }
72
+ /**
73
+ * The above function returns a Proxy object that allows access to the key and value of a node in a
74
+ * data structure.
75
+ * @returns The code is returning a Proxy object.
76
+ */
77
+ get current() {
78
+ if (this._node === this._sentinel) {
79
+ throwRangeError();
80
+ }
81
+ return new Proxy([], {
82
+ get: (target, prop) => {
83
+ if (prop === '0')
84
+ return this._node.key;
85
+ else if (prop === '1')
86
+ return this._node.value;
87
+ target[0] = this._node.key;
88
+ target[1] = this._node.value;
89
+ return target[prop];
90
+ },
91
+ set: (_, prop, newValue) => {
92
+ if (prop !== '1') {
93
+ throw new TypeError(`prop should be string '1'`);
94
+ }
95
+ this._node.value = newValue;
96
+ return true;
97
+ }
98
+ });
99
+ }
100
+ /**
101
+ * The function checks if a node is accessible.
102
+ * @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`.
103
+ */
104
+ isAccessible() {
105
+ return this._node !== this._sentinel;
106
+ }
107
+ prev() {
108
+ return this;
109
+ }
110
+ next() {
111
+ return this;
112
+ }
113
+ }
8
114
  export class HashMap {
115
+ OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
116
+ _nodes = [];
117
+ _orgMap = {};
118
+ _head;
119
+ _tail;
120
+ _sentinel;
9
121
  /**
10
- * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
11
- * multiplier, size, table array, and hash function.
12
- * @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
13
- * buckets or slots available for storing key-value pairs. The default value is 16.
14
- * @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
15
- * a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
16
- * load factor is reached, the hash table will
17
- * @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
18
- * the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
19
- * default hash function converts the key to a string, calculates the sum of the
20
- */
21
- constructor(initialCapacity = 16, loadFactor = 0.75, hashFn) {
22
- this._initialCapacity = initialCapacity;
23
- this._loadFactor = loadFactor;
24
- this._capacityMultiplier = 2;
25
- this._size = 0;
26
- this._table = new Array(initialCapacity);
27
- this._hashFn =
28
- hashFn ||
29
- ((key) => {
30
- const strKey = String(key);
31
- let hash = 0;
32
- for (let i = 0; i < strKey.length; i++) {
33
- hash += strKey.charCodeAt(i);
34
- }
35
- return hash % this.table.length;
36
- });
37
- }
38
- _initialCapacity;
39
- get initialCapacity() {
40
- return this._initialCapacity;
41
- }
42
- _loadFactor;
43
- get loadFactor() {
44
- return this._loadFactor;
45
- }
46
- _capacityMultiplier;
47
- get capacityMultiplier() {
48
- return this._capacityMultiplier;
49
- }
50
- _size;
122
+ * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
123
+ * @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
124
+ * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
125
+ * `K` represents the type of the key and `V` represents the
126
+ */
127
+ constructor(hashMap = []) {
128
+ Object.setPrototypeOf(this._orgMap, null);
129
+ this._sentinel = {};
130
+ this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
131
+ hashMap.forEach(el => {
132
+ this.set(el[0], el[1]);
133
+ });
134
+ }
135
+ _size = 0;
51
136
  get size() {
52
137
  return this._size;
53
138
  }
54
- _table;
55
- get table() {
56
- return this._table;
139
+ /**
140
+ * Time Complexity: O(1)
141
+ * Space Complexity: O(1)
142
+ *
143
+ * The function returns a new iterator object for a HashMap.
144
+ * @returns A new instance of the HashMapIterator class is being returned.
145
+ */
146
+ get begin() {
147
+ return new HashMapIterator(this._head, this._sentinel, this);
57
148
  }
58
- _hashFn;
59
- get hashFn() {
60
- return this._hashFn;
149
+ /**
150
+ * Time Complexity: O(1)
151
+ * Space Complexity: O(1)
152
+ *
153
+ * The function returns a new HashMapIterator object with the _sentinel value as both the start and
154
+ * end values.
155
+ * @returns A new instance of the HashMapIterator class is being returned.
156
+ */
157
+ get end() {
158
+ return new HashMapIterator(this._sentinel, this._sentinel, this);
61
159
  }
62
- set(key, value) {
63
- const loadFactor = this.size / this.table.length;
64
- if (loadFactor >= this.loadFactor) {
65
- this.resizeTable(this.table.length * this.capacityMultiplier);
66
- }
67
- const index = this._hash(key);
68
- if (!this.table[index]) {
69
- this.table[index] = [];
160
+ /**
161
+ * Time Complexity: O(1)
162
+ * Space Complexity: O(1)
163
+ *
164
+ * The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
165
+ * a HashMap in reverse order.
166
+ * @returns A new instance of the HashMapIterator class is being returned.
167
+ */
168
+ get reverseBegin() {
169
+ return new HashMapIterator(this._tail, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
170
+ }
171
+ /**
172
+ * Time Complexity: O(1)
173
+ * Space Complexity: O(1)
174
+ *
175
+ * The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
176
+ * HashMap in reverse order.
177
+ * @returns A new instance of the HashMapIterator class is being returned.
178
+ */
179
+ get reverseEnd() {
180
+ return new HashMapIterator(this._sentinel, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
181
+ }
182
+ /**
183
+ * Time Complexity: O(1)
184
+ * Space Complexity: O(1)
185
+ *
186
+ * The function returns the key-value pair at the front of a data structure.
187
+ * @returns The front element of the data structure, represented as a tuple with a key (K) and a
188
+ * value (V).
189
+ */
190
+ get front() {
191
+ if (this._size === 0)
192
+ return;
193
+ return [this._head.key, this._head.value];
194
+ }
195
+ /**
196
+ * Time Complexity: O(1)
197
+ * Space Complexity: O(1)
198
+ *
199
+ * The function returns the key-value pair at the end of a data structure.
200
+ * @returns The method is returning an array containing the key-value pair of the tail element in the
201
+ * data structure.
202
+ */
203
+ get back() {
204
+ if (this._size === 0)
205
+ return;
206
+ return [this._tail.key, this._tail.value];
207
+ }
208
+ /**
209
+ * Time Complexity: O(1)
210
+ * Space Complexity: O(1)
211
+ *
212
+ * The `set` function adds a new key-value pair to a data structure, either using an object key or a
213
+ * string key.
214
+ * @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
215
+ * type, but typically it is a string or symbol.
216
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
217
+ * value associated with the key being set in the data structure.
218
+ * @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
219
+ * @returns the size of the data structure after the key-value pair has been set.
220
+ */
221
+ set(key, value, isObjectKey = isObjOrFunc(key)) {
222
+ let newTail;
223
+ if (isObjectKey) {
224
+ const index = key[this.OBJ_KEY_INDEX];
225
+ if (index !== undefined) {
226
+ this._nodes[index].value = value;
227
+ return this._size;
228
+ }
229
+ Object.defineProperty(key, this.OBJ_KEY_INDEX, {
230
+ value: this._nodes.length,
231
+ configurable: true
232
+ });
233
+ newTail = {
234
+ key: key,
235
+ value: value,
236
+ prev: this._tail,
237
+ next: this._sentinel
238
+ };
239
+ this._nodes.push(newTail);
70
240
  }
71
- // Check if the key already exists in the bucket
72
- for (let i = 0; i < this.table[index].length; i++) {
73
- if (this.table[index][i][0] === key) {
74
- this.table[index][i][1] = value;
75
- return;
241
+ else {
242
+ const node = this._orgMap[key];
243
+ if (node) {
244
+ node.value = value;
245
+ return this._size;
76
246
  }
247
+ this._orgMap[key] = newTail = {
248
+ key: key,
249
+ value: value,
250
+ prev: this._tail,
251
+ next: this._sentinel
252
+ };
253
+ }
254
+ if (this._size === 0) {
255
+ this._head = newTail;
256
+ this._sentinel.next = newTail;
257
+ }
258
+ else {
259
+ this._tail.next = newTail;
260
+ }
261
+ this._tail = newTail;
262
+ this._sentinel.prev = newTail;
263
+ return ++this._size;
264
+ }
265
+ /**
266
+ * Time Complexity: O(1)
267
+ * Space Complexity: O(1)
268
+ *
269
+ * The function `get` retrieves the value associated with a given key from a map, either by using the
270
+ * key directly or by using an index stored in the key object.
271
+ * @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
272
+ * of any type, but typically it is a string or symbol.
273
+ * @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
274
+ * whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
275
+ * `key` is an object key. If `isObjectKey` is `false`, it means that `key`
276
+ * @returns The value associated with the given key is being returned. If the key is an object key,
277
+ * the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
278
+ * property of the key. If the key is a string key, the value is retrieved from the `_orgMap` object
279
+ * using the key itself. If the key is not found, `undefined` is
280
+ */
281
+ get(key, isObjectKey = isObjOrFunc(key)) {
282
+ if (isObjectKey) {
283
+ const index = key[this.OBJ_KEY_INDEX];
284
+ return index !== undefined ? this._nodes[index].value : undefined;
77
285
  }
78
- this.table[index].push([key, value]);
79
- this._size++;
286
+ const node = this._orgMap[key];
287
+ return node ? node.value : undefined;
80
288
  }
81
- get(key) {
82
- const index = this._hash(key);
83
- if (!this.table[index]) {
84
- return undefined;
289
+ /**
290
+ * Time Complexity: O(n), where n is the index.
291
+ * Space Complexity: O(1)
292
+ *
293
+ * The function `getAt` retrieves the key-value pair at a specified index in a linked list.
294
+ * @param {number} index - The index parameter is a number that represents the position of the
295
+ * element we want to retrieve from the data structure.
296
+ * @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
297
+ * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
298
+ * where `K` is the key and `V` is the value.
299
+ */
300
+ getAt(index) {
301
+ rangeCheck(index, 0, this._size - 1);
302
+ let node = this._head;
303
+ while (index--) {
304
+ node = node.next;
85
305
  }
86
- for (const [k, v] of this.table[index]) {
87
- if (k === key) {
88
- return v;
306
+ return [node.key, node.value];
307
+ }
308
+ /**
309
+ * Time Complexity: O(1)
310
+ * Space Complexity: O(1)
311
+ *
312
+ * The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
313
+ * and whether it is an object key or not.
314
+ * @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
315
+ * can be of any type, depending on how the HashMap is implemented.
316
+ * @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
317
+ * indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
318
+ * the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
319
+ * @returns a new instance of the `HashMapIterator` class.
320
+ */
321
+ getIterator(key, isObjectKey) {
322
+ let node;
323
+ if (isObjectKey) {
324
+ const index = key[this.OBJ_KEY_INDEX];
325
+ if (index === undefined) {
326
+ node = this._sentinel;
327
+ }
328
+ else {
329
+ node = this._nodes[index];
89
330
  }
90
331
  }
91
- return undefined;
332
+ else {
333
+ node = this._orgMap[key] || this._sentinel;
334
+ }
335
+ return new HashMapIterator(node, this._sentinel, this);
92
336
  }
93
- delete(key) {
94
- const index = this._hash(key);
95
- if (!this.table[index]) {
96
- return;
337
+ /**
338
+ * Time Complexity: O(1)
339
+ * Space Complexity: O(1)
340
+ *
341
+ * The `delete` function removes a key-value pair from a map-like data structure.
342
+ * @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
343
+ * It can be of any type, but typically it is a string or an object.
344
+ * @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
345
+ * whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
346
+ * `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
347
+ * @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
348
+ * was not found.
349
+ */
350
+ delete(key, isObjectKey = isObjOrFunc(key)) {
351
+ let node;
352
+ if (isObjectKey) {
353
+ const index = key[this.OBJ_KEY_INDEX];
354
+ if (index === undefined)
355
+ return false;
356
+ delete key[this.OBJ_KEY_INDEX];
357
+ node = this._nodes[index];
358
+ delete this._nodes[index];
97
359
  }
98
- for (let i = 0; i < this.table[index].length; i++) {
99
- if (this.table[index][i][0] === key) {
100
- this.table[index].splice(i, 1);
101
- this._size--;
102
- // Check if the table needs to be resized down
103
- const loadFactor = this.size / this.table.length;
104
- if (loadFactor < this.loadFactor / this.capacityMultiplier) {
105
- this.resizeTable(this.table.length / this.capacityMultiplier);
106
- }
107
- return;
108
- }
360
+ else {
361
+ node = this._orgMap[key];
362
+ if (node === undefined)
363
+ return false;
364
+ delete this._orgMap[key];
109
365
  }
366
+ this._deleteNode(node);
367
+ return true;
110
368
  }
111
- *entries() {
112
- for (const bucket of this.table) {
113
- if (bucket) {
114
- for (const [key, value] of bucket) {
115
- yield [key, value];
116
- }
117
- }
369
+ /**
370
+ * Time Complexity: O(n), where n is the index.
371
+ * Space Complexity: O(1)
372
+ *
373
+ * The `deleteAt` function deletes a node at a specified index in a linked list.
374
+ * @param {number} index - The index parameter represents the position at which the node should be
375
+ * deleted in the linked list.
376
+ * @returns The size of the list after deleting the element at the specified index.
377
+ */
378
+ deleteAt(index) {
379
+ rangeCheck(index, 0, this._size - 1);
380
+ let node = this._head;
381
+ while (index--) {
382
+ node = node.next;
118
383
  }
384
+ this._deleteNode(node);
385
+ return this._size;
119
386
  }
120
- [Symbol.iterator]() {
121
- return this.entries();
387
+ /**
388
+ * Time Complexity: O(1)
389
+ * Space Complexity: O(1)
390
+ *
391
+ * The function checks if a data structure is empty by comparing its size to zero.
392
+ * @returns The method is returning a boolean value indicating whether the size of the object is 0 or
393
+ * not.
394
+ */
395
+ isEmpty() {
396
+ return this._size === 0;
122
397
  }
398
+ /**
399
+ * Time Complexity: O(1)
400
+ * Space Complexity: O(1)
401
+ *
402
+ * The `clear` function clears all the elements in a data structure and resets its properties.
403
+ */
123
404
  clear() {
405
+ // const OBJ_KEY_INDEX = this.OBJ_KEY_INDEX;
406
+ // this._nodes.forEach(el => {
407
+ // delete (<Record<symbol, number>><unknown>el.key)[OBJ_KEY_INDEX];
408
+ // });
409
+ this._nodes = [];
410
+ this._orgMap = {};
411
+ Object.setPrototypeOf(this._orgMap, null);
124
412
  this._size = 0;
125
- this._table = new Array(this.initialCapacity);
413
+ this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
126
414
  }
127
- isEmpty() {
128
- return this.size === 0;
129
- }
130
- _hash(key) {
131
- return this._hashFn(key);
132
- }
133
- /**
134
- * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
135
- * rehashing the key-value pairs from the old table into the new table.
136
- * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
137
- * the number of buckets that the new table should have.
138
- */
139
- resizeTable(newCapacity) {
140
- const newTable = new Array(newCapacity);
141
- for (const bucket of this._table) {
142
- // Note that this is this._table
143
- if (bucket) {
144
- for (const [key, value] of bucket) {
145
- const newIndex = this._hash(key) % newCapacity;
146
- if (!newTable[newIndex]) {
147
- newTable[newIndex] = [];
148
- }
149
- newTable[newIndex].push([key, value]);
150
- }
151
- }
415
+ /**
416
+ * Time Complexity: O(n), where n is the number of elements in the HashMap.
417
+ * Space Complexity: O(1)
418
+ *
419
+ * The `forEach` function iterates over each element in a HashMap and executes a callback function on
420
+ * each element.
421
+ * @param callback - The callback parameter is a function that will be called for each element in the
422
+ * HashMap. It takes three arguments:
423
+ */
424
+ forEach(callback) {
425
+ let index = 0;
426
+ let node = this._head;
427
+ while (node !== this._sentinel) {
428
+ callback([node.key, node.value], index++, this);
429
+ node = node.next;
430
+ }
431
+ }
432
+ /**
433
+ * Time Complexity: O(n), where n is the number of elements in the HashMap.
434
+ * Space Complexity: O(1)
435
+ *
436
+ * The above function is an iterator that yields key-value pairs from a linked list.
437
+ */
438
+ *[Symbol.iterator]() {
439
+ let node = this._head;
440
+ while (node !== this._sentinel) {
441
+ yield [node.key, node.value];
442
+ node = node.next;
443
+ }
444
+ }
445
+ /**
446
+ * Time Complexity: O(1)
447
+ * Space Complexity: O(1)
448
+ *
449
+ * The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail
450
+ * pointers if necessary.
451
+ * @param node - The `node` parameter is an instance of the `HashMapLinkedNode` class, which
452
+ * represents a node in a linked list. It contains a key-value pair and references to the previous
453
+ * and next nodes in the list.
454
+ */
455
+ _deleteNode(node) {
456
+ const { prev, next } = node;
457
+ prev.next = next;
458
+ next.prev = prev;
459
+ if (node === this._head) {
460
+ this._head = next;
461
+ }
462
+ if (node === this._tail) {
463
+ this._tail = prev;
152
464
  }
153
- this._table = newTable; // Again, here is this._table
465
+ this._size -= 1;
154
466
  }
155
467
  }
@@ -1 +1,15 @@
1
- export {};
1
+ export declare const enum IterateDirection {
2
+ DEFAULT = 0,
3
+ REVERSE = 1
4
+ }
5
+ export type HashMapOptions<T> = {
6
+ sizeFunction?: number | (() => number);
7
+ fixedLength?: number;
8
+ forEach: (callback: (el: T) => void) => void;
9
+ };
10
+ export type HashMapLinkedNode<K, V> = {
11
+ key: K;
12
+ value: V;
13
+ next: HashMapLinkedNode<K, V>;
14
+ prev: HashMapLinkedNode<K, V>;
15
+ };
@@ -1 +1,7 @@
1
+ export * from './coordinate-map';
2
+ export * from './coordinate-set';
3
+ export * from './hash-map';
4
+ export * from './hash-table';
5
+ export * from './tree-map';
6
+ export * from './tree-set';
1
7
  export type HashFunction<K> = (key: K) => number;
@@ -1 +1,6 @@
1
- export {};
1
+ export * from './coordinate-map';
2
+ export * from './coordinate-set';
3
+ export * from './hash-map';
4
+ export * from './hash-table';
5
+ export * from './tree-map';
6
+ export * from './tree-set';
@@ -18,3 +18,6 @@ export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Paramet
18
18
  cont: (...args: [...Parameters<TrlAsyncFn>]) => Thunk;
19
19
  };
20
20
  export declare const getMSB: (value: number) => number;
21
+ export declare const rangeCheck: (index: number, min: number, max: number, message?: string) => void;
22
+ export declare const throwRangeError: (message?: string) => void;
23
+ export declare const isObjOrFunc: (input: unknown) => input is Record<string, unknown> | ((...args: any[]) => any);
@@ -52,3 +52,14 @@ export const getMSB = (value) => {
52
52
  }
53
53
  return 1 << (31 - Math.clz32(value));
54
54
  };
55
+ export const rangeCheck = (index, min, max, message = 'Index out of bounds.') => {
56
+ if (index < min || index > max)
57
+ throw new RangeError(message);
58
+ };
59
+ export const throwRangeError = (message = 'The value is off-limits.') => {
60
+ throw new RangeError(message);
61
+ };
62
+ export const isObjOrFunc = (input) => {
63
+ const inputType = typeof input;
64
+ return (inputType === 'object' && input !== null) || inputType === 'function';
65
+ };