graph-typed 1.44.1 → 1.45.0

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