max-priority-queue-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 (85) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +403 -98
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +402 -97
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +403 -99
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +402 -98
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/max-priority-queue-typed.js +400 -95
  46. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  47. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  48. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -87,50 +87,6 @@ export class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
87
87
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
88
88
  * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
89
89
  * @example
90
- * // basic DoublyLinkedList creation and push operation
91
- * // Create a simple DoublyLinkedList with initial values
92
- * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
93
- *
94
- * // Verify the list maintains insertion order
95
- * console.log([...list]); // [1, 2, 3, 4, 5];
96
- *
97
- * // Check length
98
- * console.log(list.length); // 5;
99
- *
100
- * // Push a new element to the end
101
- * list.push(6);
102
- * console.log(list.length); // 6;
103
- * console.log([...list]); // [1, 2, 3, 4, 5, 6];
104
- * @example
105
- * // DoublyLinkedList pop and shift operations
106
- * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
107
- *
108
- * // Pop removes from the end
109
- * const last = list.pop();
110
- * console.log(last); // 50;
111
- *
112
- * // Shift removes from the beginning
113
- * const first = list.shift();
114
- * console.log(first); // 10;
115
- *
116
- * // Verify remaining elements
117
- * console.log([...list]); // [20, 30, 40];
118
- * console.log(list.length); // 3;
119
- * @example
120
- * // DoublyLinkedList for...of iteration and map operation
121
- * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
122
- *
123
- * // Iterate through list
124
- * const doubled = list.map(value => value * 2);
125
- * console.log(doubled.length); // 5;
126
- *
127
- * // Use for...of loop
128
- * const result: number[] = [];
129
- * for (const item of list) {
130
- * result.push(item);
131
- * }
132
- * console.log(result); // [1, 2, 3, 4, 5];
133
- * @example
134
90
  * // Browser history
135
91
  * const browserHistory = new DoublyLinkedList<string>();
136
92
  *
@@ -180,6 +136,16 @@ export class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
180
136
  * // Access entry (in real LRU, this would move it to end)
181
137
  * const foundEntry = [...cacheList].find(entry => entry.key === 'user:2');
182
138
  * console.log(foundEntry?.value); // 'Bob';
139
+ * @example
140
+ * // Find first matching element
141
+ * const list = new DoublyLinkedList<number>([5, 10, 15, 20]);
142
+ * console.log(list.find(n => n >= 12)); // 15;
143
+ * @example
144
+ * // Iterate over elements
145
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
146
+ * const sum: number[] = [];
147
+ * list.forEach(n => sum.push(n));
148
+ * console.log(sum); // [1, 2, 3];
183
149
  */
184
150
  export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, DoublyLinkedListNode<E>> {
185
151
  protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
@@ -302,6 +268,32 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
302
268
  * @remarks Time O(1), Space O(1)
303
269
  * @param elementOrNode - Element or node to append.
304
270
  * @returns True when appended.
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+ * @example
283
+ * // basic DoublyLinkedList creation and push operation
284
+ * // Create a simple DoublyLinkedList with initial values
285
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
286
+ *
287
+ * // Verify the list maintains insertion order
288
+ * console.log([...list]); // [1, 2, 3, 4, 5];
289
+ *
290
+ * // Check length
291
+ * console.log(list.length); // 5;
292
+ *
293
+ * // Push a new element to the end
294
+ * list.push(6);
295
+ * console.log(list.length); // 6;
296
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
305
297
  */
306
298
 
307
299
  push(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
@@ -323,6 +315,32 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
323
315
  * Remove and return the tail element.
324
316
  * @remarks Time O(1), Space O(1)
325
317
  * @returns Removed element or undefined.
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+ * @example
330
+ * // DoublyLinkedList pop and shift operations
331
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
332
+ *
333
+ * // Pop removes from the end
334
+ * const last = list.pop();
335
+ * console.log(last); // 50;
336
+ *
337
+ * // Shift removes from the beginning
338
+ * const first = list.shift();
339
+ * console.log(first); // 10;
340
+ *
341
+ * // Verify remaining elements
342
+ * console.log([...list]); // [20, 30, 40];
343
+ * console.log(list.length); // 3;
326
344
  */
327
345
 
328
346
  pop(): E | undefined {
@@ -343,6 +361,22 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
343
361
  * Remove and return the head element.
344
362
  * @remarks Time O(1), Space O(1)
345
363
  * @returns Removed element or undefined.
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+ * @example
376
+ * // Remove from the front
377
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
378
+ * console.log(list.shift()); // 10;
379
+ * console.log(list.first); // 20;
346
380
  */
347
381
 
348
382
  shift(): E | undefined {
@@ -364,6 +398,22 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
364
398
  * @remarks Time O(1), Space O(1)
365
399
  * @param elementOrNode - Element or node to prepend.
366
400
  * @returns True when prepended.
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+ * @example
413
+ * // Add to the front
414
+ * const list = new DoublyLinkedList<number>([2, 3]);
415
+ * list.unshift(1);
416
+ * console.log([...list]); // [1, 2, 3];
367
417
  */
368
418
 
369
419
  unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
@@ -418,6 +468,22 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
418
468
  * @remarks Time O(N), Space O(1)
419
469
  * @param index - Zero-based index.
420
470
  * @returns Element or undefined.
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+ * @example
483
+ * // Access by index
484
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
485
+ * console.log(list.at(1)); // 'b';
486
+ * console.log(list.at(2)); // 'c';
421
487
  */
422
488
 
423
489
  at(index: number): E | undefined {
@@ -432,6 +498,18 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
432
498
  * @remarks Time O(N), Space O(1)
433
499
  * @param index - Zero-based index.
434
500
  * @returns Node or undefined.
501
+
502
+
503
+
504
+
505
+
506
+
507
+
508
+
509
+ * @example
510
+ * // Get node at index
511
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
512
+ * console.log(list.getNodeAt(1)?.value); // 'b';
435
513
  */
436
514
 
437
515
  getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
@@ -486,6 +564,19 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
486
564
  * @param index - Zero-based index.
487
565
  * @param newElementOrNode - Element or node to insert.
488
566
  * @returns True if inserted.
567
+
568
+
569
+
570
+
571
+
572
+
573
+
574
+
575
+ * @example
576
+ * // Insert at position
577
+ * const list = new DoublyLinkedList<number>([1, 3]);
578
+ * list.addAt(1, 2);
579
+ * console.log(list.toArray()); // [1, 2, 3];
489
580
  */
490
581
 
491
582
  addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
@@ -575,6 +666,19 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
575
666
  * @remarks Time O(N), Space O(1)
576
667
  * @param index - Zero-based index.
577
668
  * @returns Removed element or undefined.
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+ * @example
678
+ * // Remove by index
679
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
680
+ * list.deleteAt(1);
681
+ * console.log(list.toArray()); // ['a', 'c'];
578
682
  */
579
683
 
580
684
  deleteAt(index: number): E | undefined {
@@ -596,6 +700,19 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
596
700
  * @remarks Time O(N), Space O(1)
597
701
  * @param [elementOrNode] - Element or node to remove.
598
702
  * @returns True if removed.
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
711
+ * @example
712
+ * // Remove first occurrence
713
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
714
+ * list.delete(2);
715
+ * console.log(list.toArray()); // [1, 3, 2];
599
716
  */
600
717
 
601
718
  delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
@@ -618,6 +735,18 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
618
735
  * Check whether the list is empty.
619
736
  * @remarks Time O(1), Space O(1)
620
737
  * @returns True if length is 0.
738
+
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+ * @example
748
+ * // Check empty
749
+ * console.log(new DoublyLinkedList().isEmpty()); // true;
621
750
  */
622
751
 
623
752
  isEmpty(): boolean {
@@ -628,6 +757,20 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
628
757
  * Remove all nodes and reset length.
629
758
  * @remarks Time O(N), Space O(1)
630
759
  * @returns void
760
+
761
+
762
+
763
+
764
+
765
+
766
+
767
+
768
+
769
+ * @example
770
+ * // Remove all
771
+ * const list = new DoublyLinkedList<number>([1, 2]);
772
+ * list.clear();
773
+ * console.log(list.isEmpty()); // true;
631
774
  */
632
775
 
633
776
  clear(): void {
@@ -641,6 +784,19 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
641
784
  * @remarks Time O(N), Space O(1)
642
785
  * @param elementNodeOrPredicate - Element, node, or predicate to match.
643
786
  * @returns Matched value or undefined.
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+ * @example
796
+ * // Search with predicate
797
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
798
+ * const found = list.search(node => node.value > 15);
799
+ * console.log(found); // 20;
644
800
  */
645
801
 
646
802
  search(
@@ -660,6 +816,20 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
660
816
  * @remarks Time O(N), Space O(1)
661
817
  * @param elementNodeOrPredicate - Element, node, or predicate to match.
662
818
  * @returns Matched value or undefined.
819
+
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+ * @example
828
+ * // Find value scanning from tail
829
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
830
+ * // getBackward scans from tail to head, returns first match
831
+ * const found = list.getBackward(node => node.value < 4);
832
+ * console.log(found); // 3;
663
833
  */
664
834
 
665
835
  getBackward(
@@ -678,6 +848,22 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
678
848
  * Reverse the list in place.
679
849
  * @remarks Time O(N), Space O(1)
680
850
  * @returns This list.
851
+
852
+
853
+
854
+
855
+
856
+
857
+
858
+
859
+
860
+
861
+
862
+ * @example
863
+ * // Reverse in-place
864
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
865
+ * list.reverse();
866
+ * console.log([...list]); // [3, 2, 1];
681
867
  */
682
868
 
683
869
  reverse(): this {
@@ -707,6 +893,21 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
707
893
  * Deep clone this list (values are copied by reference).
708
894
  * @remarks Time O(N), Space O(N)
709
895
  * @returns A new list with the same element sequence.
896
+
897
+
898
+
899
+
900
+
901
+
902
+
903
+
904
+
905
+ * @example
906
+ * // Deep copy
907
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
908
+ * const copy = list.clone();
909
+ * copy.pop();
910
+ * console.log(list.length); // 3;
710
911
  */
711
912
 
712
913
  clone(): this {
@@ -721,6 +922,22 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
721
922
  * @param callback - Predicate (value, index, list) → boolean to keep value.
722
923
  * @param [thisArg] - Value for `this` inside the callback.
723
924
  * @returns A new list with kept values.
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+ * @example
937
+ * // Filter elements
938
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
939
+ * const evens = list.filter(n => n % 2 === 0);
940
+ * console.log([...evens]); // [2, 4];
724
941
  */
725
942
 
726
943
  filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this {
@@ -757,6 +974,31 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
757
974
  * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
758
975
  * @param [thisArg] - Value for `this` inside the callback.
759
976
  * @returns A new DoublyLinkedList with mapped values.
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+ * @example
989
+ * // DoublyLinkedList for...of iteration and map operation
990
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
991
+ *
992
+ * // Iterate through list
993
+ * const doubled = list.map(value => value * 2);
994
+ * console.log(doubled.length); // 5;
995
+ *
996
+ * // Use for...of loop
997
+ * const result: number[] = [];
998
+ * for (const item of list) {
999
+ * result.push(item);
1000
+ * }
1001
+ * console.log(result); // [1, 2, 3, 4, 5];
760
1002
  */
761
1003
 
762
1004
  map<EM, RM>(