data-structure-typed 2.4.5 → 2.5.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.
- package/CHANGELOG.md +1 -1
- package/README.md +15 -5
- package/dist/cjs/index.cjs +10240 -2079
- package/dist/cjs-legacy/index.cjs +10305 -2135
- package/dist/esm/index.mjs +10241 -2078
- package/dist/esm-legacy/index.mjs +10306 -2134
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -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 +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -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 +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/data-structure-typed.js +10308 -2133
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/package.json +5 -4
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
- package/src/data-structures/binary-tree/binary-tree.ts +454 -79
- package/src/data-structures/binary-tree/bst.ts +359 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1403 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
- package/src/data-structures/binary-tree/tree-set.ts +1250 -9
- package/src/data-structures/graph/directed-graph.ts +229 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +213 -59
- package/src/data-structures/hash/hash-map.ts +241 -77
- package/src/data-structures/heap/heap.ts +301 -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 +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +424 -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 +287 -65
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +225 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -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,20 @@ 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
|
+
* @example
|
|
185
|
+
* // Check if empty
|
|
186
|
+
* const map = new HashMap();
|
|
187
|
+
* console.log(map.isEmpty()); // true;
|
|
238
188
|
*/
|
|
239
189
|
isEmpty(): boolean {
|
|
240
190
|
return this._size === 0;
|
|
@@ -244,6 +194,21 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
244
194
|
* Remove all entries and reset counters.
|
|
245
195
|
* @remarks Time O(N), Space O(1)
|
|
246
196
|
* @returns void
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
* @example
|
|
208
|
+
* // Remove all entries
|
|
209
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
210
|
+
* map.clear();
|
|
211
|
+
* console.log(map.isEmpty()); // true;
|
|
247
212
|
*/
|
|
248
213
|
clear(): void {
|
|
249
214
|
this._store = {};
|
|
@@ -266,6 +231,48 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
266
231
|
* @param key - Key.
|
|
267
232
|
* @param value - Value.
|
|
268
233
|
* @returns True when the operation succeeds.
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
* @example
|
|
259
|
+
* // basic HashMap creation and set operation
|
|
260
|
+
* // Create a simple HashMap with key-value pairs
|
|
261
|
+
* const map = new HashMap<number, string>([
|
|
262
|
+
* [1, 'one'],
|
|
263
|
+
* [2, 'two'],
|
|
264
|
+
* [3, 'three']
|
|
265
|
+
* ]);
|
|
266
|
+
*
|
|
267
|
+
* // Verify size
|
|
268
|
+
* console.log(map.size); // 3;
|
|
269
|
+
*
|
|
270
|
+
* // Set a new key-value pair
|
|
271
|
+
* map.set(4, 'four');
|
|
272
|
+
* console.log(map.size); // 4;
|
|
273
|
+
*
|
|
274
|
+
* // Verify entries
|
|
275
|
+
* console.log([...map.entries()]); // length: 4;
|
|
269
276
|
*/
|
|
270
277
|
set(key: K, value: V): boolean {
|
|
271
278
|
if (this._isObjKey(key)) {
|
|
@@ -284,6 +291,21 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
284
291
|
* @remarks Time O(N), Space O(N)
|
|
285
292
|
* @param entryOrRawElements - Iterable of entries or raw elements to insert.
|
|
286
293
|
* @returns Array of per-entry results.
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
* @example
|
|
305
|
+
* // Add multiple entries
|
|
306
|
+
* const map = new HashMap<string, number>();
|
|
307
|
+
* map.setMany([['a', 1], ['b', 2], ['c', 3]]);
|
|
308
|
+
* console.log(map.size); // 3;
|
|
287
309
|
*/
|
|
288
310
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
|
|
289
311
|
const results: boolean[] = [];
|
|
@@ -301,6 +323,39 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
301
323
|
* @remarks Time O(1), Space O(1)
|
|
302
324
|
* @param key - Key to look up.
|
|
303
325
|
* @returns Value or undefined.
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
* @example
|
|
339
|
+
* // HashMap get and has operations
|
|
340
|
+
* const map = new HashMap<string, number>([
|
|
341
|
+
* ['apple', 1],
|
|
342
|
+
* ['banana', 2],
|
|
343
|
+
* ['cherry', 3]
|
|
344
|
+
* ]);
|
|
345
|
+
*
|
|
346
|
+
* // Check if key exists
|
|
347
|
+
* console.log(map.has('apple')); // true;
|
|
348
|
+
* console.log(map.has('date')); // false;
|
|
349
|
+
*
|
|
350
|
+
* // Get value by key
|
|
351
|
+
* console.log(map.get('banana')); // 2;
|
|
352
|
+
* console.log(map.get('grape')); // undefined;
|
|
353
|
+
*
|
|
354
|
+
* // Get all keys and values
|
|
355
|
+
* const keys = [...map.keys()];
|
|
356
|
+
* const values = [...map.values()];
|
|
357
|
+
* console.log(keys); // contains 'apple';
|
|
358
|
+
* console.log(values); // contains 3;
|
|
304
359
|
*/
|
|
305
360
|
override get(key: K): V | undefined {
|
|
306
361
|
if (this._isObjKey(key)) return this.objMap.get(key);
|
|
@@ -313,6 +368,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
313
368
|
* @remarks Time O(1), Space O(1)
|
|
314
369
|
* @param key - Key to test.
|
|
315
370
|
* @returns True if present.
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
* @example
|
|
384
|
+
* // Check key existence
|
|
385
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
386
|
+
*
|
|
387
|
+
* console.log(map.has('a')); // true;
|
|
388
|
+
* console.log(map.has('z')); // false;
|
|
316
389
|
*/
|
|
317
390
|
override has(key: K): boolean {
|
|
318
391
|
if (this._isObjKey(key)) return this.objMap.has(key);
|
|
@@ -325,6 +398,25 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
325
398
|
* @remarks Time O(1), Space O(1)
|
|
326
399
|
* @param key - Key to delete.
|
|
327
400
|
* @returns True if the key was found and removed.
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
* @example
|
|
414
|
+
* // Remove entries by key
|
|
415
|
+
* const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
|
|
416
|
+
*
|
|
417
|
+
* console.log(map.delete('y')); // true;
|
|
418
|
+
* console.log(map.has('y')); // false;
|
|
419
|
+
* console.log(map.size); // 2;
|
|
328
420
|
*/
|
|
329
421
|
delete(key: K): boolean {
|
|
330
422
|
if (this._isObjKey(key)) {
|
|
@@ -357,6 +449,22 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
357
449
|
* Deep clone this map, preserving hashing behavior.
|
|
358
450
|
* @remarks Time O(N), Space O(N)
|
|
359
451
|
* @returns A new map with the same content.
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
* @example
|
|
463
|
+
* // Create independent copy
|
|
464
|
+
* const map = new HashMap<string, number>([['a', 1]]);
|
|
465
|
+
* const copy = map.clone();
|
|
466
|
+
* copy.set('a', 99);
|
|
467
|
+
* console.log(map.get('a')); // 1;
|
|
360
468
|
*/
|
|
361
469
|
clone(): this {
|
|
362
470
|
const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
|
|
@@ -370,6 +478,25 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
370
478
|
* @param callbackfn - Mapping function (key, value, index, map) → newValue.
|
|
371
479
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
372
480
|
* @returns A new map with transformed values.
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
* @example
|
|
494
|
+
* // Transform all values
|
|
495
|
+
* const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
|
|
496
|
+
*
|
|
497
|
+
* const doubled = prices.map(v => (v ?? 0) * 2);
|
|
498
|
+
* console.log(doubled.get('apple')); // 2;
|
|
499
|
+
* console.log(doubled.get('banana')); // 4;
|
|
373
500
|
*/
|
|
374
501
|
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): any {
|
|
375
502
|
const out = this._createLike<K, VM, [K, VM]>();
|
|
@@ -384,6 +511,43 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
384
511
|
* @param predicate - Predicate (key, value, index, map) → boolean.
|
|
385
512
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
386
513
|
* @returns A new map containing entries that satisfied the predicate.
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
* @example
|
|
527
|
+
* // HashMap iteration and filter operations
|
|
528
|
+
* const map = new HashMap<number, string>([
|
|
529
|
+
* [1, 'Alice'],
|
|
530
|
+
* [2, 'Bob'],
|
|
531
|
+
* [3, 'Charlie'],
|
|
532
|
+
* [4, 'Diana'],
|
|
533
|
+
* [5, 'Eve']
|
|
534
|
+
* ]);
|
|
535
|
+
*
|
|
536
|
+
* // Iterate through entries
|
|
537
|
+
* const entries: [number, string][] = [];
|
|
538
|
+
* for (const [key, value] of map) {
|
|
539
|
+
* entries.push([key, value]);
|
|
540
|
+
* }
|
|
541
|
+
* console.log(entries); // length: 5;
|
|
542
|
+
*
|
|
543
|
+
* // Filter operation (for iteration with collection methods)
|
|
544
|
+
* const filtered = [...map].filter(([key]) => key > 2);
|
|
545
|
+
* console.log(filtered.length); // 3;
|
|
546
|
+
*
|
|
547
|
+
* // Map operation
|
|
548
|
+
* const values = [...map.values()].map(v => v.length);
|
|
549
|
+
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
550
|
+
* console.log(values); // contains 7;
|
|
387
551
|
*/
|
|
388
552
|
|
|
389
553
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): any {
|