max-priority-queue-typed 2.4.5 → 2.5.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.
- package/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -29,83 +29,6 @@ import { ERR } from '../../common';
|
|
|
29
29
|
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
30
30
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
31
31
|
* @example
|
|
32
|
-
* // should maintain insertion order
|
|
33
|
-
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
34
|
-
* linkedHashMap.set(1, 'A');
|
|
35
|
-
* linkedHashMap.set(2, 'B');
|
|
36
|
-
* linkedHashMap.set(3, 'C');
|
|
37
|
-
*
|
|
38
|
-
* const result = Array.from(linkedHashMap);
|
|
39
|
-
* console.log(result); // [
|
|
40
|
-
* // [1, 'A'],
|
|
41
|
-
* // [2, 'B'],
|
|
42
|
-
* // [3, 'C']
|
|
43
|
-
* // ];
|
|
44
|
-
* @example
|
|
45
|
-
* // basic HashMap creation and set operation
|
|
46
|
-
* // Create a simple HashMap with key-value pairs
|
|
47
|
-
* const map = new HashMap<number, string>([
|
|
48
|
-
* [1, 'one'],
|
|
49
|
-
* [2, 'two'],
|
|
50
|
-
* [3, 'three']
|
|
51
|
-
* ]);
|
|
52
|
-
*
|
|
53
|
-
* // Verify size
|
|
54
|
-
* console.log(map.size); // 3;
|
|
55
|
-
*
|
|
56
|
-
* // Set a new key-value pair
|
|
57
|
-
* map.set(4, 'four');
|
|
58
|
-
* console.log(map.size); // 4;
|
|
59
|
-
*
|
|
60
|
-
* // Verify entries
|
|
61
|
-
* console.log([...map.entries()]); // length: 4;
|
|
62
|
-
* @example
|
|
63
|
-
* // HashMap get and has operations
|
|
64
|
-
* const map = new HashMap<string, number>([
|
|
65
|
-
* ['apple', 1],
|
|
66
|
-
* ['banana', 2],
|
|
67
|
-
* ['cherry', 3]
|
|
68
|
-
* ]);
|
|
69
|
-
*
|
|
70
|
-
* // Check if key exists
|
|
71
|
-
* console.log(map.has('apple')); // true;
|
|
72
|
-
* console.log(map.has('date')); // false;
|
|
73
|
-
*
|
|
74
|
-
* // Get value by key
|
|
75
|
-
* console.log(map.get('banana')); // 2;
|
|
76
|
-
* console.log(map.get('grape')); // undefined;
|
|
77
|
-
*
|
|
78
|
-
* // Get all keys and values
|
|
79
|
-
* const keys = [...map.keys()];
|
|
80
|
-
* const values = [...map.values()];
|
|
81
|
-
* console.log(keys); // contains 'apple';
|
|
82
|
-
* console.log(values); // contains 3;
|
|
83
|
-
* @example
|
|
84
|
-
* // HashMap iteration and filter operations
|
|
85
|
-
* const map = new HashMap<number, string>([
|
|
86
|
-
* [1, 'Alice'],
|
|
87
|
-
* [2, 'Bob'],
|
|
88
|
-
* [3, 'Charlie'],
|
|
89
|
-
* [4, 'Diana'],
|
|
90
|
-
* [5, 'Eve']
|
|
91
|
-
* ]);
|
|
92
|
-
*
|
|
93
|
-
* // Iterate through entries
|
|
94
|
-
* const entries: [number, string][] = [];
|
|
95
|
-
* for (const [key, value] of map) {
|
|
96
|
-
* entries.push([key, value]);
|
|
97
|
-
* }
|
|
98
|
-
* console.log(entries); // length: 5;
|
|
99
|
-
*
|
|
100
|
-
* // Filter operation (for iteration with collection methods)
|
|
101
|
-
* const filtered = [...map].filter(([key]) => key > 2);
|
|
102
|
-
* console.log(filtered.length); // 3;
|
|
103
|
-
*
|
|
104
|
-
* // Map operation
|
|
105
|
-
* const values = [...map.values()].map(v => v.length);
|
|
106
|
-
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
107
|
-
* console.log(values); // contains 7;
|
|
108
|
-
* @example
|
|
109
32
|
* // HashMap for user session caching O(1) performance
|
|
110
33
|
* interface UserSession {
|
|
111
34
|
* userId: number;
|
|
@@ -157,6 +80,19 @@ import { ERR } from '../../common';
|
|
|
157
80
|
* // Get all active sessions
|
|
158
81
|
* const activeCount = [...sessionCache.values()].length;
|
|
159
82
|
* console.log(activeCount); // 2;
|
|
83
|
+
* @example
|
|
84
|
+
* // Aggregate values
|
|
85
|
+
* const counts = new HashMap<string, number>([['a', 5], ['b', 3], ['c', 8]]);
|
|
86
|
+
*
|
|
87
|
+
* const total = counts.reduce((sum, v) => sum + (v ?? 0), 0);
|
|
88
|
+
* console.log(total); // 16;
|
|
89
|
+
* @example
|
|
90
|
+
* // Iterate over entries
|
|
91
|
+
* const map = new HashMap<string, number>([['x', 1], ['y', 2]]);
|
|
92
|
+
* const keys: string[] = [];
|
|
93
|
+
*
|
|
94
|
+
* map.forEach((v, k) => keys.push(k));
|
|
95
|
+
* console.log(keys.sort()); // ['x', 'y'];
|
|
160
96
|
*/
|
|
161
97
|
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
162
98
|
/**
|
|
@@ -235,6 +171,40 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
235
171
|
* Check whether the map is empty.
|
|
236
172
|
* @remarks Time O(1), Space O(1)
|
|
237
173
|
* @returns True if size is 0.
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
* @example
|
|
205
|
+
* // Check if empty
|
|
206
|
+
* const map = new HashMap();
|
|
207
|
+
* console.log(map.isEmpty()); // true;
|
|
238
208
|
*/
|
|
239
209
|
isEmpty(): boolean {
|
|
240
210
|
return this._size === 0;
|
|
@@ -244,6 +214,41 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
244
214
|
* Remove all entries and reset counters.
|
|
245
215
|
* @remarks Time O(N), Space O(1)
|
|
246
216
|
* @returns void
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
* @example
|
|
248
|
+
* // Remove all entries
|
|
249
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
250
|
+
* map.clear();
|
|
251
|
+
* console.log(map.isEmpty()); // true;
|
|
247
252
|
*/
|
|
248
253
|
clear(): void {
|
|
249
254
|
this._store = {};
|
|
@@ -256,7 +261,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
256
261
|
* @remarks Time O(1), Space O(1)
|
|
257
262
|
* @returns True if the value is a 2-tuple.
|
|
258
263
|
*/
|
|
259
|
-
isEntry(rawElement:
|
|
264
|
+
isEntry(rawElement: unknown): rawElement is [K, V] {
|
|
260
265
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
261
266
|
}
|
|
262
267
|
|
|
@@ -266,6 +271,88 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
266
271
|
* @param key - Key.
|
|
267
272
|
* @param value - Value.
|
|
268
273
|
* @returns True when the operation succeeds.
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
* @example
|
|
339
|
+
* // basic HashMap creation and set operation
|
|
340
|
+
* // Create a simple HashMap with key-value pairs
|
|
341
|
+
* const map = new HashMap<number, string>([
|
|
342
|
+
* [1, 'one'],
|
|
343
|
+
* [2, 'two'],
|
|
344
|
+
* [3, 'three']
|
|
345
|
+
* ]);
|
|
346
|
+
*
|
|
347
|
+
* // Verify size
|
|
348
|
+
* console.log(map.size); // 3;
|
|
349
|
+
*
|
|
350
|
+
* // Set a new key-value pair
|
|
351
|
+
* map.set(4, 'four');
|
|
352
|
+
* console.log(map.size); // 4;
|
|
353
|
+
*
|
|
354
|
+
* // Verify entries
|
|
355
|
+
* console.log([...map.entries()]); // length: 4;
|
|
269
356
|
*/
|
|
270
357
|
set(key: K, value: V): boolean {
|
|
271
358
|
if (this._isObjKey(key)) {
|
|
@@ -284,6 +371,41 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
284
371
|
* @remarks Time O(N), Space O(N)
|
|
285
372
|
* @param entryOrRawElements - Iterable of entries or raw elements to insert.
|
|
286
373
|
* @returns Array of per-entry results.
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
* @example
|
|
405
|
+
* // Add multiple entries
|
|
406
|
+
* const map = new HashMap<string, number>();
|
|
407
|
+
* map.setMany([['a', 1], ['b', 2], ['c', 3]]);
|
|
408
|
+
* console.log(map.size); // 3;
|
|
287
409
|
*/
|
|
288
410
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
|
|
289
411
|
const results: boolean[] = [];
|
|
@@ -301,6 +423,59 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
301
423
|
* @remarks Time O(1), Space O(1)
|
|
302
424
|
* @param key - Key to look up.
|
|
303
425
|
* @returns Value or undefined.
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
* @example
|
|
459
|
+
* // HashMap get and has operations
|
|
460
|
+
* const map = new HashMap<string, number>([
|
|
461
|
+
* ['apple', 1],
|
|
462
|
+
* ['banana', 2],
|
|
463
|
+
* ['cherry', 3]
|
|
464
|
+
* ]);
|
|
465
|
+
*
|
|
466
|
+
* // Check if key exists
|
|
467
|
+
* console.log(map.has('apple')); // true;
|
|
468
|
+
* console.log(map.has('date')); // false;
|
|
469
|
+
*
|
|
470
|
+
* // Get value by key
|
|
471
|
+
* console.log(map.get('banana')); // 2;
|
|
472
|
+
* console.log(map.get('grape')); // undefined;
|
|
473
|
+
*
|
|
474
|
+
* // Get all keys and values
|
|
475
|
+
* const keys = [...map.keys()];
|
|
476
|
+
* const values = [...map.values()];
|
|
477
|
+
* console.log(keys); // contains 'apple';
|
|
478
|
+
* console.log(values); // contains 3;
|
|
304
479
|
*/
|
|
305
480
|
override get(key: K): V | undefined {
|
|
306
481
|
if (this._isObjKey(key)) return this.objMap.get(key);
|
|
@@ -313,6 +488,44 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
313
488
|
* @remarks Time O(1), Space O(1)
|
|
314
489
|
* @param key - Key to test.
|
|
315
490
|
* @returns True if present.
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
* @example
|
|
524
|
+
* // Check key existence
|
|
525
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
526
|
+
*
|
|
527
|
+
* console.log(map.has('a')); // true;
|
|
528
|
+
* console.log(map.has('z')); // false;
|
|
316
529
|
*/
|
|
317
530
|
override has(key: K): boolean {
|
|
318
531
|
if (this._isObjKey(key)) return this.objMap.has(key);
|
|
@@ -325,6 +538,45 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
325
538
|
* @remarks Time O(1), Space O(1)
|
|
326
539
|
* @param key - Key to delete.
|
|
327
540
|
* @returns True if the key was found and removed.
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
* @example
|
|
574
|
+
* // Remove entries by key
|
|
575
|
+
* const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
|
|
576
|
+
*
|
|
577
|
+
* console.log(map.delete('y')); // true;
|
|
578
|
+
* console.log(map.has('y')); // false;
|
|
579
|
+
* console.log(map.size); // 2;
|
|
328
580
|
*/
|
|
329
581
|
delete(key: K): boolean {
|
|
330
582
|
if (this._isObjKey(key)) {
|
|
@@ -357,6 +609,42 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
357
609
|
* Deep clone this map, preserving hashing behavior.
|
|
358
610
|
* @remarks Time O(N), Space O(N)
|
|
359
611
|
* @returns A new map with the same content.
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
* @example
|
|
643
|
+
* // Create independent copy
|
|
644
|
+
* const map = new HashMap<string, number>([['a', 1]]);
|
|
645
|
+
* const copy = map.clone();
|
|
646
|
+
* copy.set('a', 99);
|
|
647
|
+
* console.log(map.get('a')); // 1;
|
|
360
648
|
*/
|
|
361
649
|
clone(): this {
|
|
362
650
|
const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
|
|
@@ -370,9 +658,48 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
370
658
|
* @param callbackfn - Mapping function (key, value, index, map) → newValue.
|
|
371
659
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
372
660
|
* @returns A new map with transformed values.
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
* @example
|
|
694
|
+
* // Transform all values
|
|
695
|
+
* const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
|
|
696
|
+
*
|
|
697
|
+
* const doubled = prices.map(v => (v ?? 0) * 2);
|
|
698
|
+
* console.log(doubled.get('apple')); // 2;
|
|
699
|
+
* console.log(doubled.get('banana')); // 4;
|
|
373
700
|
*/
|
|
374
|
-
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?:
|
|
375
|
-
const out = this._createLike<K, VM, [K, VM]>()
|
|
701
|
+
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: unknown): HashMap<K, VM> {
|
|
702
|
+
const out = this._createLike<K, VM, [K, VM]>() as unknown as HashMap<K, VM>;
|
|
376
703
|
let index = 0;
|
|
377
704
|
for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
378
705
|
return out;
|
|
@@ -384,9 +711,66 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
384
711
|
* @param predicate - Predicate (key, value, index, map) → boolean.
|
|
385
712
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
386
713
|
* @returns A new map containing entries that satisfied the predicate.
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
* @example
|
|
747
|
+
* // HashMap iteration and filter operations
|
|
748
|
+
* const map = new HashMap<number, string>([
|
|
749
|
+
* [1, 'Alice'],
|
|
750
|
+
* [2, 'Bob'],
|
|
751
|
+
* [3, 'Charlie'],
|
|
752
|
+
* [4, 'Diana'],
|
|
753
|
+
* [5, 'Eve']
|
|
754
|
+
* ]);
|
|
755
|
+
*
|
|
756
|
+
* // Iterate through entries
|
|
757
|
+
* const entries: [number, string][] = [];
|
|
758
|
+
* for (const [key, value] of map) {
|
|
759
|
+
* entries.push([key, value]);
|
|
760
|
+
* }
|
|
761
|
+
* console.log(entries); // length: 5;
|
|
762
|
+
*
|
|
763
|
+
* // Filter operation (for iteration with collection methods)
|
|
764
|
+
* const filtered = [...map].filter(([key]) => key > 2);
|
|
765
|
+
* console.log(filtered.length); // 3;
|
|
766
|
+
*
|
|
767
|
+
* // Map operation
|
|
768
|
+
* const values = [...map.values()].map(v => v.length);
|
|
769
|
+
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
770
|
+
* console.log(values); // contains 7;
|
|
387
771
|
*/
|
|
388
772
|
|
|
389
|
-
filter(predicate: EntryCallback<K, V, boolean>, thisArg?:
|
|
773
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): any {
|
|
390
774
|
const out = this._createLike<K, V, [K, V]>();
|
|
391
775
|
let index = 0;
|
|
392
776
|
for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
|
|
@@ -403,8 +787,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
403
787
|
* @param [options] - Options forwarded to the constructor.
|
|
404
788
|
* @returns A like-kind map instance.
|
|
405
789
|
*/
|
|
406
|
-
protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?:
|
|
407
|
-
const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?:
|
|
790
|
+
protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?: Record<string, unknown>): this {
|
|
791
|
+
const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?: Record<string, unknown>) => this;
|
|
408
792
|
return new Ctor(entries, options);
|
|
409
793
|
}
|
|
410
794
|
|
|
@@ -422,7 +806,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
422
806
|
for (const node of this.objMap) yield node as [K, V];
|
|
423
807
|
}
|
|
424
808
|
|
|
425
|
-
protected _isObjKey(key:
|
|
809
|
+
protected _isObjKey(key: unknown): key is object | ((...args: unknown[]) => unknown) {
|
|
426
810
|
const keyType = typeof key;
|
|
427
811
|
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
428
812
|
}
|
|
@@ -744,7 +1128,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
744
1128
|
return this._size === 0;
|
|
745
1129
|
}
|
|
746
1130
|
|
|
747
|
-
isEntry(rawElement:
|
|
1131
|
+
isEntry(rawElement: unknown): rawElement is [K, V] {
|
|
748
1132
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
749
1133
|
}
|
|
750
1134
|
|
|
@@ -754,12 +1138,12 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
754
1138
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
755
1139
|
}
|
|
756
1140
|
|
|
757
|
-
clone():
|
|
1141
|
+
clone(): this {
|
|
758
1142
|
const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
|
|
759
|
-
return this._createLike<
|
|
1143
|
+
return this._createLike<K, V, [K, V]>(this, opts);
|
|
760
1144
|
}
|
|
761
1145
|
|
|
762
|
-
filter(predicate: EntryCallback<K, V, boolean>, thisArg?:
|
|
1146
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): this {
|
|
763
1147
|
const out = this._createLike<K, V, [K, V]>();
|
|
764
1148
|
let index = 0;
|
|
765
1149
|
for (const [key, value] of this) {
|
|
@@ -778,8 +1162,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
778
1162
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
779
1163
|
* @returns A new map of the same class with transformed entries.
|
|
780
1164
|
*/
|
|
781
|
-
map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?:
|
|
782
|
-
const out = this._createLike<MK, MV, [MK, MV]>()
|
|
1165
|
+
map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: unknown): LinkedHashMap<MK, MV> {
|
|
1166
|
+
const out = this._createLike<MK, MV, [MK, MV]>() as unknown as LinkedHashMap<MK, MV>;
|
|
783
1167
|
let index = 0;
|
|
784
1168
|
for (const [key, value] of this) {
|
|
785
1169
|
const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
|
|
@@ -819,8 +1203,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
819
1203
|
return true;
|
|
820
1204
|
}
|
|
821
1205
|
|
|
822
|
-
protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?:
|
|
823
|
-
const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?:
|
|
1206
|
+
protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?: Record<string, unknown>): this {
|
|
1207
|
+
const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?: Record<string, unknown>) => this;
|
|
824
1208
|
return new Ctor(entries, options);
|
|
825
1209
|
}
|
|
826
1210
|
}
|