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