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.
Files changed (71) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +15 -5
  3. package/dist/cjs/index.cjs +10240 -2079
  4. package/dist/cjs-legacy/index.cjs +10305 -2135
  5. package/dist/esm/index.mjs +10241 -2078
  6. package/dist/esm-legacy/index.mjs +10306 -2134
  7. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  13. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  14. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  16. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  17. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  19. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  20. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  21. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  22. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  23. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  24. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  28. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  29. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  30. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  31. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  32. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  33. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  34. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  35. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  36. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  37. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  38. package/dist/umd/data-structure-typed.js +10308 -2133
  39. package/dist/umd/data-structure-typed.min.js +4 -4
  40. package/package.json +5 -4
  41. package/src/data-structures/base/iterable-element-base.ts +4 -5
  42. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
  44. package/src/data-structures/binary-tree/binary-tree.ts +454 -79
  45. package/src/data-structures/binary-tree/bst.ts +359 -34
  46. package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
  47. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  48. package/src/data-structures/binary-tree/tree-map.ts +1403 -6
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
  51. package/src/data-structures/binary-tree/tree-set.ts +1250 -9
  52. package/src/data-structures/graph/directed-graph.ts +229 -47
  53. package/src/data-structures/graph/map-graph.ts +59 -1
  54. package/src/data-structures/graph/undirected-graph.ts +213 -59
  55. package/src/data-structures/hash/hash-map.ts +241 -77
  56. package/src/data-structures/heap/heap.ts +301 -99
  57. package/src/data-structures/heap/max-heap.ts +46 -0
  58. package/src/data-structures/heap/min-heap.ts +59 -0
  59. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  60. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  61. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  62. package/src/data-structures/matrix/matrix.ts +424 -12
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  65. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  66. package/src/data-structures/queue/deque.ts +287 -65
  67. package/src/data-structures/queue/queue.ts +223 -42
  68. package/src/data-structures/stack/stack.ts +184 -32
  69. package/src/data-structures/trie/trie.ts +225 -43
  70. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  71. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
@@ -27,71 +27,6 @@ import { LinearBase } from '../base/linear-base';
27
27
  * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
28
28
  * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
29
29
  * @example
30
- * // basic Deque creation and push/pop operations
31
- * // Create a simple Deque with initial values
32
- * const deque = new Deque([1, 2, 3, 4, 5]);
33
- *
34
- * // Verify the deque maintains insertion order
35
- * console.log([...deque]); // [1, 2, 3, 4, 5];
36
- *
37
- * // Check length
38
- * console.log(deque.length); // 5;
39
- *
40
- * // Push to the end
41
- * deque.push(6);
42
- * console.log(deque.length); // 6;
43
- *
44
- * // Pop from the end
45
- * const last = deque.pop();
46
- * console.log(last); // 6;
47
- * @example
48
- * // Deque shift and unshift operations
49
- * const deque = new Deque<number>([20, 30, 40]);
50
- *
51
- * // Unshift adds to the front
52
- * deque.unshift(10);
53
- * console.log([...deque]); // [10, 20, 30, 40];
54
- *
55
- * // Shift removes from the front (O(1) complexity!)
56
- * const first = deque.shift();
57
- * console.log(first); // 10;
58
- *
59
- * // Verify remaining elements
60
- * console.log([...deque]); // [20, 30, 40];
61
- * console.log(deque.length); // 3;
62
- * @example
63
- * // Deque peek at both ends
64
- * const deque = new Deque<number>([10, 20, 30, 40, 50]);
65
- *
66
- * // Get first element without removing
67
- * const first = deque.at(0);
68
- * console.log(first); // 10;
69
- *
70
- * // Get last element without removing
71
- * const last = deque.at(deque.length - 1);
72
- * console.log(last); // 50;
73
- *
74
- * // Length unchanged
75
- * console.log(deque.length); // 5;
76
- * @example
77
- * // Deque for...of iteration and reverse
78
- * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
79
- *
80
- * // Iterate forward
81
- * const forward: string[] = [];
82
- * for (const item of deque) {
83
- * forward.push(item);
84
- * }
85
- * console.log(forward); // ['A', 'B', 'C', 'D'];
86
- *
87
- * // Reverse the deque
88
- * deque.reverse();
89
- * const backward: string[] = [];
90
- * for (const item of deque) {
91
- * backward.push(item);
92
- * }
93
- * console.log(backward); // ['D', 'C', 'B', 'A'];
94
- * @example
95
30
  * // Deque as sliding window for stream processing
96
31
  * interface DataPoint {
97
32
  * timestamp: number;
@@ -142,6 +77,10 @@ import { LinearBase } from '../base/linear-base';
142
77
  * console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
143
78
  * console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
144
79
  * console.log(dataWindow.length); // 3;
80
+ * @example
81
+ * // Convert deque to array
82
+ * const dq = new Deque<number>([10, 20, 30]);
83
+ * console.log(dq.toArray()); // [10, 20, 30];
145
84
  */
146
85
  export class Deque<E = any, R = any> extends LinearBase<E, R> {
147
86
  protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
@@ -310,6 +249,32 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
310
249
  * Get the first element without removing it.
311
250
  * @remarks Time O(1), Space O(1)
312
251
  * @returns First element or undefined.
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+ * @example
265
+ * // Deque peek at both ends
266
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
267
+ *
268
+ * // Get first element without removing
269
+ * const first = deque.at(0);
270
+ * console.log(first); // 10;
271
+ *
272
+ * // Get last element without removing
273
+ * const last = deque.at(deque.length - 1);
274
+ * console.log(last); // 50;
275
+ *
276
+ * // Length unchanged
277
+ * console.log(deque.length); // 5;
313
278
  */
314
279
 
315
280
  get first(): E | undefined {
@@ -321,6 +286,23 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
321
286
  * Get the last element without removing it.
322
287
  * @remarks Time O(1), Space O(1)
323
288
  * @returns Last element or undefined.
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+ * @example
302
+ * // Peek at the back element
303
+ * const dq = new Deque<string>(['a', 'b', 'c']);
304
+ * console.log(dq.last); // 'c';
305
+ * console.log(dq.first); // 'a';
324
306
  */
325
307
 
326
308
  get last(): E | undefined {
@@ -355,6 +337,36 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
355
337
  * @remarks Time O(1) amortized, Space O(1)
356
338
  * @param element - Element to append.
357
339
  * @returns True when appended.
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+ * @example
353
+ * // basic Deque creation and push/pop operations
354
+ * // Create a simple Deque with initial values
355
+ * const deque = new Deque([1, 2, 3, 4, 5]);
356
+ *
357
+ * // Verify the deque maintains insertion order
358
+ * console.log([...deque]); // [1, 2, 3, 4, 5];
359
+ *
360
+ * // Check length
361
+ * console.log(deque.length); // 5;
362
+ *
363
+ * // Push to the end
364
+ * deque.push(6);
365
+ * console.log(deque.length); // 6;
366
+ *
367
+ * // Pop from the end
368
+ * const last = deque.pop();
369
+ * console.log(last); // 6;
358
370
  */
359
371
 
360
372
  push(element: E): boolean {
@@ -380,6 +392,23 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
380
392
  * Remove and return the last element.
381
393
  * @remarks Time O(1), Space O(1)
382
394
  * @returns Removed element or undefined.
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+ * @example
408
+ * // Remove from the back
409
+ * const dq = new Deque<number>([1, 2, 3]);
410
+ * console.log(dq.pop()); // 3;
411
+ * console.log(dq.length); // 2;
383
412
  */
384
413
 
385
414
  pop(): E | undefined {
@@ -405,6 +434,23 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
405
434
  * Remove and return the first element.
406
435
  * @remarks Time O(1) amortized, Space O(1)
407
436
  * @returns Removed element or undefined.
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+ * @example
450
+ * // Remove from the front
451
+ * const dq = new Deque<number>([1, 2, 3]);
452
+ * console.log(dq.shift()); // 1;
453
+ * console.log(dq.length); // 2;
408
454
  */
409
455
 
410
456
  shift(): E | undefined {
@@ -431,6 +477,33 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
431
477
  * @remarks Time O(1) amortized, Space O(1)
432
478
  * @param element - Element to prepend.
433
479
  * @returns True when prepended.
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+
492
+ * @example
493
+ * // Deque shift and unshift operations
494
+ * const deque = new Deque<number>([20, 30, 40]);
495
+ *
496
+ * // Unshift adds to the front
497
+ * deque.unshift(10);
498
+ * console.log([...deque]); // [10, 20, 30, 40];
499
+ *
500
+ * // Shift removes from the front (O(1) complexity!)
501
+ * const first = deque.shift();
502
+ * console.log(first); // 10;
503
+ *
504
+ * // Verify remaining elements
505
+ * console.log([...deque]); // [20, 30, 40];
506
+ * console.log(deque.length); // 3;
434
507
  */
435
508
 
436
509
  unshift(element: E): boolean {
@@ -494,6 +567,20 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
494
567
  * Check whether the deque is empty.
495
568
  * @remarks Time O(1), Space O(1)
496
569
  * @returns True if length is 0.
570
+
571
+
572
+
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+ * @example
581
+ * // Check if empty
582
+ * const dq = new Deque();
583
+ * console.log(dq.isEmpty()); // true;
497
584
  */
498
585
 
499
586
  isEmpty(): boolean {
@@ -504,6 +591,21 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
504
591
  * Remove all elements and reset structure.
505
592
  * @remarks Time O(1), Space O(1)
506
593
  * @returns void
594
+
595
+
596
+
597
+
598
+
599
+
600
+
601
+
602
+
603
+
604
+ * @example
605
+ * // Remove all elements
606
+ * const dq = new Deque<number>([1, 2, 3]);
607
+ * dq.clear();
608
+ * console.log(dq.length); // 0;
507
609
  */
508
610
 
509
611
  clear(): void {
@@ -518,6 +620,20 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
518
620
  * @remarks Time O(1), Space O(1)
519
621
  * @param pos - Zero-based position from the front.
520
622
  * @returns Element or undefined.
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+
631
+
632
+ * @example
633
+ * // Access by index
634
+ * const dq = new Deque<string>(['a', 'b', 'c']);
635
+ * console.log(dq.at(0)); // 'a';
636
+ * console.log(dq.at(2)); // 'c';
521
637
  */
522
638
 
523
639
  at(pos: number): E | undefined {
@@ -705,6 +821,20 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
705
821
  * @remarks Time O(N), Space O(1)
706
822
  * @param element - Element to remove (using the configured equality).
707
823
  * @returns True if an element was removed.
824
+
825
+
826
+
827
+
828
+
829
+
830
+
831
+
832
+
833
+ * @example
834
+ * // Remove element
835
+ * const dq = new Deque<number>([1, 2, 3]);
836
+ * dq.delete(2);
837
+ * console.log(dq.length); // 2;
708
838
  */
709
839
 
710
840
  delete(element: E): boolean {
@@ -758,6 +888,36 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
758
888
  * Reverse the deque by reversing buckets and pointers.
759
889
  * @remarks Time O(N), Space O(N)
760
890
  * @returns This deque.
891
+
892
+
893
+
894
+
895
+
896
+
897
+
898
+
899
+
900
+
901
+
902
+
903
+ * @example
904
+ * // Deque for...of iteration and reverse
905
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
906
+ *
907
+ * // Iterate forward
908
+ * const forward: string[] = [];
909
+ * for (const item of deque) {
910
+ * forward.push(item);
911
+ * }
912
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
913
+ *
914
+ * // Reverse the deque
915
+ * deque.reverse();
916
+ * const backward: string[] = [];
917
+ * for (const item of deque) {
918
+ * backward.push(item);
919
+ * }
920
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
761
921
  */
762
922
 
763
923
  reverse(): this {
@@ -828,6 +988,22 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
828
988
  * Compact the deque by removing unused buckets.
829
989
  * @remarks Time O(N), Space O(1)
830
990
  * @returns True if compaction was performed (bucket count reduced).
991
+
992
+
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+ * @example
1001
+ * // Reclaim memory
1002
+ * const dq = new Deque<number>([1, 2, 3, 4, 5]);
1003
+ * dq.shift();
1004
+ * dq.shift();
1005
+ * dq.compact();
1006
+ * console.log(dq.length); // 3;
831
1007
  */
832
1008
  compact(): boolean {
833
1009
  const before = this._bucketCount;
@@ -861,6 +1037,23 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
861
1037
  * Deep clone this deque, preserving options.
862
1038
  * @remarks Time O(N), Space O(N)
863
1039
  * @returns A new deque with the same content and options.
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+ * @example
1051
+ * // Create independent copy
1052
+ * const dq = new Deque<number>([1, 2, 3]);
1053
+ * const copy = dq.clone();
1054
+ * copy.pop();
1055
+ * console.log(dq.length); // 3;
1056
+ * console.log(copy.length); // 2;
864
1057
  */
865
1058
 
866
1059
  clone(): this {
@@ -877,6 +1070,21 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
877
1070
  * @param predicate - Predicate (value, index, deque) → boolean to keep element.
878
1071
  * @param [thisArg] - Value for `this` inside the predicate.
879
1072
  * @returns A new deque with kept elements.
1073
+
1074
+
1075
+
1076
+
1077
+
1078
+
1079
+
1080
+
1081
+
1082
+
1083
+ * @example
1084
+ * // Filter elements
1085
+ * const dq = new Deque<number>([1, 2, 3, 4]);
1086
+ * const result = dq.filter(x => x > 2);
1087
+ * console.log(result.length); // 2;
880
1088
  */
881
1089
 
882
1090
  filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): this {
@@ -918,6 +1126,20 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
918
1126
  * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
919
1127
  * @param [thisArg] - Value for `this` inside the callback.
920
1128
  * @returns A new Deque with mapped elements.
1129
+
1130
+
1131
+
1132
+
1133
+
1134
+
1135
+
1136
+
1137
+
1138
+ * @example
1139
+ * // Transform elements
1140
+ * const dq = new Deque<number>([1, 2, 3]);
1141
+ * const result = dq.map(x => x * 10);
1142
+ * console.log(result.toArray()); // [10, 20, 30];
921
1143
  */
922
1144
 
923
1145
  map<EM, RM>(