data-structure-typed 2.4.4 → 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.
Files changed (80) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +34 -1
  3. package/dist/cjs/index.cjs +10639 -2151
  4. package/dist/cjs-legacy/index.cjs +10694 -2195
  5. package/dist/esm/index.mjs +10639 -2150
  6. package/dist/esm-legacy/index.mjs +10694 -2194
  7. package/dist/types/common/error.d.ts +23 -0
  8. package/dist/types/common/index.d.ts +1 -0
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  11. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  15. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  16. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  18. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  19. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  20. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  42. package/dist/umd/data-structure-typed.js +10725 -2221
  43. package/dist/umd/data-structure-typed.min.js +4 -2
  44. package/package.json +5 -4
  45. package/src/common/error.ts +60 -0
  46. package/src/common/index.ts +2 -0
  47. package/src/data-structures/base/iterable-element-base.ts +2 -2
  48. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
  50. package/src/data-structures/binary-tree/binary-tree.ts +567 -121
  51. package/src/data-structures/binary-tree/bst.ts +370 -37
  52. package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
  53. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  54. package/src/data-structures/binary-tree/tree-map.ts +1411 -13
  55. package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
  56. package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
  57. package/src/data-structures/binary-tree/tree-set.ts +1257 -15
  58. package/src/data-structures/graph/abstract-graph.ts +106 -1
  59. package/src/data-structures/graph/directed-graph.ts +233 -47
  60. package/src/data-structures/graph/map-graph.ts +59 -1
  61. package/src/data-structures/graph/undirected-graph.ts +308 -59
  62. package/src/data-structures/hash/hash-map.ts +254 -79
  63. package/src/data-structures/heap/heap.ts +305 -102
  64. package/src/data-structures/heap/max-heap.ts +48 -3
  65. package/src/data-structures/heap/min-heap.ts +59 -0
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  67. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  68. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  69. package/src/data-structures/matrix/matrix.ts +433 -22
  70. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  71. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  72. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  73. package/src/data-structures/queue/deque.ts +358 -68
  74. package/src/data-structures/queue/queue.ts +223 -42
  75. package/src/data-structures/stack/stack.ts +184 -32
  76. package/src/data-structures/trie/trie.ts +227 -44
  77. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  78. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  79. package/src/types/data-structures/queue/deque.ts +7 -0
  80. package/src/utils/utils.ts +4 -2
@@ -15,6 +15,7 @@ import type {
15
15
  } from '../../types';
16
16
  import { IterableEntryBase } from '../base';
17
17
  import { isWeakKey, rangeCheck } from '../../utils';
18
+ import { ERR } from '../../common';
18
19
 
19
20
  /**
20
21
  * Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.
@@ -28,83 +29,6 @@ import { isWeakKey, rangeCheck } from '../../utils';
28
29
  * If you try to insert another entry with the same key, the new one will replace the old entry.
29
30
  * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
30
31
  * @example
31
- * // should maintain insertion order
32
- * const linkedHashMap = new LinkedHashMap<number, string>();
33
- * linkedHashMap.set(1, 'A');
34
- * linkedHashMap.set(2, 'B');
35
- * linkedHashMap.set(3, 'C');
36
- *
37
- * const result = Array.from(linkedHashMap);
38
- * console.log(result); // [
39
- * // [1, 'A'],
40
- * // [2, 'B'],
41
- * // [3, 'C']
42
- * // ];
43
- * @example
44
- * // basic HashMap creation and set operation
45
- * // Create a simple HashMap with key-value pairs
46
- * const map = new HashMap<number, string>([
47
- * [1, 'one'],
48
- * [2, 'two'],
49
- * [3, 'three']
50
- * ]);
51
- *
52
- * // Verify size
53
- * console.log(map.size); // 3;
54
- *
55
- * // Set a new key-value pair
56
- * map.set(4, 'four');
57
- * console.log(map.size); // 4;
58
- *
59
- * // Verify entries
60
- * console.log([...map.entries()]); // length: 4;
61
- * @example
62
- * // HashMap get and has operations
63
- * const map = new HashMap<string, number>([
64
- * ['apple', 1],
65
- * ['banana', 2],
66
- * ['cherry', 3]
67
- * ]);
68
- *
69
- * // Check if key exists
70
- * console.log(map.has('apple')); // true;
71
- * console.log(map.has('date')); // false;
72
- *
73
- * // Get value by key
74
- * console.log(map.get('banana')); // 2;
75
- * console.log(map.get('grape')); // undefined;
76
- *
77
- * // Get all keys and values
78
- * const keys = [...map.keys()];
79
- * const values = [...map.values()];
80
- * console.log(keys); // contains 'apple';
81
- * console.log(values); // contains 3;
82
- * @example
83
- * // HashMap iteration and filter operations
84
- * const map = new HashMap<number, string>([
85
- * [1, 'Alice'],
86
- * [2, 'Bob'],
87
- * [3, 'Charlie'],
88
- * [4, 'Diana'],
89
- * [5, 'Eve']
90
- * ]);
91
- *
92
- * // Iterate through entries
93
- * const entries: [number, string][] = [];
94
- * for (const [key, value] of map) {
95
- * entries.push([key, value]);
96
- * }
97
- * console.log(entries); // length: 5;
98
- *
99
- * // Filter operation (for iteration with collection methods)
100
- * const filtered = [...map].filter(([key]) => key > 2);
101
- * console.log(filtered.length); // 3;
102
- *
103
- * // Map operation
104
- * const values = [...map.values()].map(v => v.length);
105
- * console.log(values); // contains 3; // 'Bob', 'Eve'
106
- * console.log(values); // contains 7;
107
- * @example
108
32
  * // HashMap for user session caching O(1) performance
109
33
  * interface UserSession {
110
34
  * userId: number;
@@ -156,6 +80,19 @@ import { isWeakKey, rangeCheck } from '../../utils';
156
80
  * // Get all active sessions
157
81
  * const activeCount = [...sessionCache.values()].length;
158
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'];
159
96
  */
160
97
  export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
161
98
  /**
@@ -234,6 +171,20 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
234
171
  * Check whether the map is empty.
235
172
  * @remarks Time O(1), Space O(1)
236
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;
237
188
  */
238
189
  isEmpty(): boolean {
239
190
  return this._size === 0;
@@ -243,6 +194,21 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
243
194
  * Remove all entries and reset counters.
244
195
  * @remarks Time O(N), Space O(1)
245
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;
246
212
  */
247
213
  clear(): void {
248
214
  this._store = {};
@@ -265,6 +231,48 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
265
231
  * @param key - Key.
266
232
  * @param value - Value.
267
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;
268
276
  */
269
277
  set(key: K, value: V): boolean {
270
278
  if (this._isObjKey(key)) {
@@ -283,6 +291,21 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
283
291
  * @remarks Time O(N), Space O(N)
284
292
  * @param entryOrRawElements - Iterable of entries or raw elements to insert.
285
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;
286
309
  */
287
310
  setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
288
311
  const results: boolean[] = [];
@@ -300,6 +323,39 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
300
323
  * @remarks Time O(1), Space O(1)
301
324
  * @param key - Key to look up.
302
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;
303
359
  */
304
360
  override get(key: K): V | undefined {
305
361
  if (this._isObjKey(key)) return this.objMap.get(key);
@@ -312,6 +368,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
312
368
  * @remarks Time O(1), Space O(1)
313
369
  * @param key - Key to test.
314
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;
315
389
  */
316
390
  override has(key: K): boolean {
317
391
  if (this._isObjKey(key)) return this.objMap.has(key);
@@ -324,6 +398,25 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
324
398
  * @remarks Time O(1), Space O(1)
325
399
  * @param key - Key to delete.
326
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;
327
420
  */
328
421
  delete(key: K): boolean {
329
422
  if (this._isObjKey(key)) {
@@ -356,6 +449,22 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
356
449
  * Deep clone this map, preserving hashing behavior.
357
450
  * @remarks Time O(N), Space O(N)
358
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;
359
468
  */
360
469
  clone(): this {
361
470
  const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
@@ -369,6 +478,25 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
369
478
  * @param callbackfn - Mapping function (key, value, index, map) → newValue.
370
479
  * @param [thisArg] - Value for `this` inside the callback.
371
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;
372
500
  */
373
501
  map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): any {
374
502
  const out = this._createLike<K, VM, [K, VM]>();
@@ -383,6 +511,43 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
383
511
  * @param predicate - Predicate (key, value, index, map) → boolean.
384
512
  * @param [thisArg] - Value for `this` inside the predicate.
385
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;
386
551
  */
387
552
 
388
553
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): any {
@@ -534,8 +699,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
534
699
  if (this.isEntry(rawElement)) {
535
700
  return rawElement;
536
701
  }
537
- throw new Error(
538
- 'If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records.'
702
+ throw new TypeError(
703
+ ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap')
539
704
  );
540
705
  };
541
706
 
@@ -797,6 +962,16 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
797
962
  }
798
963
 
799
964
  protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean {
965
+ // Remove from hash table
966
+ const key: unknown = node.key;
967
+ if (isWeakKey(key)) {
968
+ this._objMap.delete(key);
969
+ } else {
970
+ const hash = this._hashFn(key as K);
971
+ delete this._noObjMap[hash];
972
+ }
973
+
974
+ // Remove from linked list
800
975
  const { prev, next } = node;
801
976
  prev.next = next;
802
977
  next.prev = prev;