deque-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 +6 -73
- package/dist/cjs/index.cjs +705 -122
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +704 -121
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +705 -122
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +704 -121
- 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/deque-typed.js +702 -119
- package/dist/umd/deque-typed.js.map +1 -1
- package/dist/umd/deque-typed.min.js +1 -1
- package/dist/umd/deque-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
|
@@ -13,54 +13,6 @@ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
|
13
13
|
}, "rangeCheck");
|
|
14
14
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
15
15
|
|
|
16
|
-
// src/common/error.ts
|
|
17
|
-
var ERR = {
|
|
18
|
-
// Range / index
|
|
19
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
20
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
21
|
-
// Type / argument
|
|
22
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
23
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
24
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
25
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
26
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
27
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
28
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
29
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
30
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
31
|
-
// State / operation
|
|
32
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
33
|
-
// Matrix
|
|
34
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
35
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
36
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
37
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
38
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
// src/common/index.ts
|
|
42
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
43
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
44
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
45
|
-
return DFSOperation2;
|
|
46
|
-
})(DFSOperation || {});
|
|
47
|
-
var _Range = class _Range {
|
|
48
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
49
|
-
this.low = low;
|
|
50
|
-
this.high = high;
|
|
51
|
-
this.includeLow = includeLow;
|
|
52
|
-
this.includeHigh = includeHigh;
|
|
53
|
-
}
|
|
54
|
-
// Determine whether a key is within the range
|
|
55
|
-
isInRange(key, comparator) {
|
|
56
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
57
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
58
|
-
return lowCheck && highCheck;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
__name(_Range, "Range");
|
|
62
|
-
var Range = _Range;
|
|
63
|
-
|
|
64
16
|
// src/data-structures/base/iterable-element-base.ts
|
|
65
17
|
var _IterableElementBase = class _IterableElementBase {
|
|
66
18
|
/**
|
|
@@ -83,7 +35,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
83
35
|
if (options) {
|
|
84
36
|
const { toElementFn } = options;
|
|
85
37
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
86
|
-
else if (toElementFn) throw new TypeError(
|
|
38
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
87
39
|
}
|
|
88
40
|
}
|
|
89
41
|
/**
|
|
@@ -239,7 +191,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
239
191
|
acc = initialValue;
|
|
240
192
|
} else {
|
|
241
193
|
const first = iter.next();
|
|
242
|
-
if (first.done) throw new TypeError(
|
|
194
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
243
195
|
acc = first.value;
|
|
244
196
|
index = 1;
|
|
245
197
|
}
|
|
@@ -597,19 +549,102 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
597
549
|
return this._length;
|
|
598
550
|
}
|
|
599
551
|
/**
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
552
|
+
* Get the first element without removing it.
|
|
553
|
+
* @remarks Time O(1), Space O(1)
|
|
554
|
+
* @returns First element or undefined.
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
* @example
|
|
588
|
+
* // Deque peek at both ends
|
|
589
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
590
|
+
*
|
|
591
|
+
* // Get first element without removing
|
|
592
|
+
* const first = deque.at(0);
|
|
593
|
+
* console.log(first); // 10;
|
|
594
|
+
*
|
|
595
|
+
* // Get last element without removing
|
|
596
|
+
* const last = deque.at(deque.length - 1);
|
|
597
|
+
* console.log(last); // 50;
|
|
598
|
+
*
|
|
599
|
+
* // Length unchanged
|
|
600
|
+
* console.log(deque.length); // 5;
|
|
601
|
+
*/
|
|
604
602
|
get first() {
|
|
605
603
|
if (this._length === 0) return;
|
|
606
604
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
607
605
|
}
|
|
608
606
|
/**
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
607
|
+
* Get the last element without removing it.
|
|
608
|
+
* @remarks Time O(1), Space O(1)
|
|
609
|
+
* @returns Last element or undefined.
|
|
610
|
+
|
|
611
|
+
|
|
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
|
+
* // Peek at the back element
|
|
644
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
645
|
+
* console.log(dq.last); // 'c';
|
|
646
|
+
* console.log(dq.first); // 'a';
|
|
647
|
+
*/
|
|
613
648
|
get last() {
|
|
614
649
|
if (this._length === 0) return;
|
|
615
650
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -628,11 +663,61 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
628
663
|
return new this(data, options);
|
|
629
664
|
}
|
|
630
665
|
/**
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
666
|
+
* Append one element at the back.
|
|
667
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
668
|
+
* @param element - Element to append.
|
|
669
|
+
* @returns True when appended.
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
* @example
|
|
703
|
+
* // basic Deque creation and push/pop operations
|
|
704
|
+
* // Create a simple Deque with initial values
|
|
705
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
706
|
+
*
|
|
707
|
+
* // Verify the deque maintains insertion order
|
|
708
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
709
|
+
*
|
|
710
|
+
* // Check length
|
|
711
|
+
* console.log(deque.length); // 5;
|
|
712
|
+
*
|
|
713
|
+
* // Push to the end
|
|
714
|
+
* deque.push(6);
|
|
715
|
+
* console.log(deque.length); // 6;
|
|
716
|
+
*
|
|
717
|
+
* // Pop from the end
|
|
718
|
+
* const last = deque.pop();
|
|
719
|
+
* console.log(last); // 6;
|
|
720
|
+
*/
|
|
636
721
|
push(element) {
|
|
637
722
|
if (this._length) {
|
|
638
723
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -652,10 +737,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
652
737
|
return true;
|
|
653
738
|
}
|
|
654
739
|
/**
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
740
|
+
* Remove and return the last element.
|
|
741
|
+
* @remarks Time O(1), Space O(1)
|
|
742
|
+
* @returns Removed element or undefined.
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
* @example
|
|
776
|
+
* // Remove from the back
|
|
777
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
778
|
+
* console.log(dq.pop()); // 3;
|
|
779
|
+
* console.log(dq.length); // 2;
|
|
780
|
+
*/
|
|
659
781
|
pop() {
|
|
660
782
|
if (this._length === 0) return;
|
|
661
783
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -675,10 +797,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
675
797
|
return element;
|
|
676
798
|
}
|
|
677
799
|
/**
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
800
|
+
* Remove and return the first element.
|
|
801
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
802
|
+
* @returns Removed element or undefined.
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
* @example
|
|
836
|
+
* // Remove from the front
|
|
837
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
838
|
+
* console.log(dq.shift()); // 1;
|
|
839
|
+
* console.log(dq.length); // 2;
|
|
840
|
+
*/
|
|
682
841
|
shift() {
|
|
683
842
|
if (this._length === 0) return;
|
|
684
843
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -698,11 +857,58 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
698
857
|
return element;
|
|
699
858
|
}
|
|
700
859
|
/**
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
860
|
+
* Prepend one element at the front.
|
|
861
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
862
|
+
* @param element - Element to prepend.
|
|
863
|
+
* @returns True when prepended.
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
* @example
|
|
897
|
+
* // Deque shift and unshift operations
|
|
898
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
899
|
+
*
|
|
900
|
+
* // Unshift adds to the front
|
|
901
|
+
* deque.unshift(10);
|
|
902
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
903
|
+
*
|
|
904
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
905
|
+
* const first = deque.shift();
|
|
906
|
+
* console.log(first); // 10;
|
|
907
|
+
*
|
|
908
|
+
* // Verify remaining elements
|
|
909
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
910
|
+
* console.log(deque.length); // 3;
|
|
911
|
+
*/
|
|
706
912
|
unshift(element) {
|
|
707
913
|
if (this._length) {
|
|
708
914
|
if (this._firstInBucket > 0) {
|
|
@@ -756,18 +962,87 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
756
962
|
return ans;
|
|
757
963
|
}
|
|
758
964
|
/**
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
965
|
+
* Check whether the deque is empty.
|
|
966
|
+
* @remarks Time O(1), Space O(1)
|
|
967
|
+
* @returns True if length is 0.
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
* @example
|
|
999
|
+
* // Check if empty
|
|
1000
|
+
* const dq = new Deque();
|
|
1001
|
+
* console.log(dq.isEmpty()); // true;
|
|
1002
|
+
*/
|
|
763
1003
|
isEmpty() {
|
|
764
1004
|
return this._length === 0;
|
|
765
1005
|
}
|
|
766
1006
|
/**
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
1007
|
+
* Remove all elements and reset structure.
|
|
1008
|
+
* @remarks Time O(1), Space O(1)
|
|
1009
|
+
* @returns void
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
* @example
|
|
1041
|
+
* // Remove all elements
|
|
1042
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1043
|
+
* dq.clear();
|
|
1044
|
+
* console.log(dq.length); // 0;
|
|
1045
|
+
*/
|
|
771
1046
|
clear() {
|
|
772
1047
|
this._buckets = [new Array(this._bucketSize)];
|
|
773
1048
|
this._bucketCount = 1;
|
|
@@ -775,11 +1050,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
775
1050
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
776
1051
|
}
|
|
777
1052
|
/**
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
1053
|
+
* Get the element at a given position.
|
|
1054
|
+
* @remarks Time O(1), Space O(1)
|
|
1055
|
+
* @param pos - Zero-based position from the front.
|
|
1056
|
+
* @returns Element or undefined.
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
* @example
|
|
1087
|
+
* // Access by index
|
|
1088
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
1089
|
+
* console.log(dq.at(0)); // 'a';
|
|
1090
|
+
* console.log(dq.at(2)); // 'c';
|
|
1091
|
+
*/
|
|
783
1092
|
at(pos) {
|
|
784
1093
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
785
1094
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -938,11 +1247,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
938
1247
|
}
|
|
939
1248
|
}
|
|
940
1249
|
/**
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1250
|
+
* Delete the first occurrence of a value.
|
|
1251
|
+
* @remarks Time O(N), Space O(1)
|
|
1252
|
+
* @param element - Element to remove (using the configured equality).
|
|
1253
|
+
* @returns True if an element was removed.
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1277
|
+
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
* @example
|
|
1284
|
+
* // Remove element
|
|
1285
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1286
|
+
* dq.delete(2);
|
|
1287
|
+
* console.log(dq.length); // 2;
|
|
1288
|
+
*/
|
|
946
1289
|
delete(element) {
|
|
947
1290
|
const size = this._length;
|
|
948
1291
|
if (size === 0) return false;
|
|
@@ -986,10 +1329,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
986
1329
|
return this;
|
|
987
1330
|
}
|
|
988
1331
|
/**
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1332
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
1333
|
+
* @remarks Time O(N), Space O(N)
|
|
1334
|
+
* @returns This deque.
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
* @example
|
|
1368
|
+
* // Deque for...of iteration and reverse
|
|
1369
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1370
|
+
*
|
|
1371
|
+
* // Iterate forward
|
|
1372
|
+
* const forward: string[] = [];
|
|
1373
|
+
* for (const item of deque) {
|
|
1374
|
+
* forward.push(item);
|
|
1375
|
+
* }
|
|
1376
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1377
|
+
*
|
|
1378
|
+
* // Reverse the deque
|
|
1379
|
+
* deque.reverse();
|
|
1380
|
+
* const backward: string[] = [];
|
|
1381
|
+
* for (const item of deque) {
|
|
1382
|
+
* backward.push(item);
|
|
1383
|
+
* }
|
|
1384
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1385
|
+
*/
|
|
993
1386
|
reverse() {
|
|
994
1387
|
this._buckets.reverse().forEach(function(bucket) {
|
|
995
1388
|
bucket.reverse();
|
|
@@ -1048,10 +1441,46 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1048
1441
|
* @returns True if compaction was performed (bucket count reduced).
|
|
1049
1442
|
*/
|
|
1050
1443
|
/**
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1444
|
+
* Compact the deque by removing unused buckets.
|
|
1445
|
+
* @remarks Time O(N), Space O(1)
|
|
1446
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1447
|
+
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
|
|
1476
|
+
* @example
|
|
1477
|
+
* // Reclaim memory
|
|
1478
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1479
|
+
* dq.shift();
|
|
1480
|
+
* dq.shift();
|
|
1481
|
+
* dq.compact();
|
|
1482
|
+
* console.log(dq.length); // 3;
|
|
1483
|
+
*/
|
|
1055
1484
|
compact() {
|
|
1056
1485
|
const before = this._bucketCount;
|
|
1057
1486
|
this.shrinkToFit();
|
|
@@ -1079,10 +1508,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1079
1508
|
this._compactCounter = 0;
|
|
1080
1509
|
}
|
|
1081
1510
|
/**
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1511
|
+
* Deep clone this deque, preserving options.
|
|
1512
|
+
* @remarks Time O(N), Space O(N)
|
|
1513
|
+
* @returns A new deque with the same content and options.
|
|
1514
|
+
|
|
1515
|
+
|
|
1516
|
+
|
|
1517
|
+
|
|
1518
|
+
|
|
1519
|
+
|
|
1520
|
+
|
|
1521
|
+
|
|
1522
|
+
|
|
1523
|
+
|
|
1524
|
+
|
|
1525
|
+
|
|
1526
|
+
|
|
1527
|
+
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
|
|
1531
|
+
|
|
1532
|
+
|
|
1533
|
+
|
|
1534
|
+
|
|
1535
|
+
|
|
1536
|
+
|
|
1537
|
+
|
|
1538
|
+
|
|
1539
|
+
|
|
1540
|
+
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
|
|
1544
|
+
* @example
|
|
1545
|
+
* // Create independent copy
|
|
1546
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1547
|
+
* const copy = dq.clone();
|
|
1548
|
+
* copy.pop();
|
|
1549
|
+
* console.log(dq.length); // 3;
|
|
1550
|
+
* console.log(copy.length); // 2;
|
|
1551
|
+
*/
|
|
1086
1552
|
clone() {
|
|
1087
1553
|
return this._createLike(this, {
|
|
1088
1554
|
bucketSize: this.bucketSize,
|
|
@@ -1091,12 +1557,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1091
1557
|
});
|
|
1092
1558
|
}
|
|
1093
1559
|
/**
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1560
|
+
* Filter elements into a new deque of the same class.
|
|
1561
|
+
* @remarks Time O(N), Space O(N)
|
|
1562
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1563
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1564
|
+
* @returns A new deque with kept elements.
|
|
1565
|
+
|
|
1566
|
+
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
|
|
1574
|
+
|
|
1575
|
+
|
|
1576
|
+
|
|
1577
|
+
|
|
1578
|
+
|
|
1579
|
+
|
|
1580
|
+
|
|
1581
|
+
|
|
1582
|
+
|
|
1583
|
+
|
|
1584
|
+
|
|
1585
|
+
|
|
1586
|
+
|
|
1587
|
+
|
|
1588
|
+
|
|
1589
|
+
|
|
1590
|
+
|
|
1591
|
+
|
|
1592
|
+
|
|
1593
|
+
|
|
1594
|
+
|
|
1595
|
+
* @example
|
|
1596
|
+
* // Filter elements
|
|
1597
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1598
|
+
* const result = dq.filter(x => x > 2);
|
|
1599
|
+
* console.log(result.length); // 2;
|
|
1600
|
+
*/
|
|
1100
1601
|
filter(predicate, thisArg) {
|
|
1101
1602
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1102
1603
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1125,15 +1626,49 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1125
1626
|
return out;
|
|
1126
1627
|
}
|
|
1127
1628
|
/**
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1629
|
+
* Map elements into a new deque (possibly different element type).
|
|
1630
|
+
* @remarks Time O(N), Space O(N)
|
|
1631
|
+
* @template EM
|
|
1632
|
+
* @template RM
|
|
1633
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1634
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1635
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1636
|
+
* @returns A new Deque with mapped elements.
|
|
1637
|
+
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
|
|
1646
|
+
|
|
1647
|
+
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
|
|
1656
|
+
|
|
1657
|
+
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
|
|
1661
|
+
|
|
1662
|
+
|
|
1663
|
+
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
* @example
|
|
1667
|
+
* // Transform elements
|
|
1668
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1669
|
+
* const result = dq.map(x => x * 10);
|
|
1670
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
1671
|
+
*/
|
|
1137
1672
|
map(callback, options, thisArg) {
|
|
1138
1673
|
const out = this._createLike([], {
|
|
1139
1674
|
...options != null ? options : {},
|
|
@@ -1258,6 +1793,54 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1258
1793
|
};
|
|
1259
1794
|
__name(_Deque, "Deque");
|
|
1260
1795
|
var Deque = _Deque;
|
|
1796
|
+
|
|
1797
|
+
// src/common/error.ts
|
|
1798
|
+
var ERR = {
|
|
1799
|
+
// Range / index
|
|
1800
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1801
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1802
|
+
// Type / argument
|
|
1803
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1804
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1805
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1806
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1807
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1808
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1809
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1810
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1811
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1812
|
+
// State / operation
|
|
1813
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1814
|
+
// Matrix
|
|
1815
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1816
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1817
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1818
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1819
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1820
|
+
};
|
|
1821
|
+
|
|
1822
|
+
// src/common/index.ts
|
|
1823
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1824
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1825
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1826
|
+
return DFSOperation2;
|
|
1827
|
+
})(DFSOperation || {});
|
|
1828
|
+
var _Range = class _Range {
|
|
1829
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1830
|
+
this.low = low;
|
|
1831
|
+
this.high = high;
|
|
1832
|
+
this.includeLow = includeLow;
|
|
1833
|
+
this.includeHigh = includeHigh;
|
|
1834
|
+
}
|
|
1835
|
+
// Determine whether a key is within the range
|
|
1836
|
+
isInRange(key, comparator) {
|
|
1837
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1838
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1839
|
+
return lowCheck && highCheck;
|
|
1840
|
+
}
|
|
1841
|
+
};
|
|
1842
|
+
__name(_Range, "Range");
|
|
1843
|
+
var Range = _Range;
|
|
1261
1844
|
/**
|
|
1262
1845
|
* data-structure-typed
|
|
1263
1846
|
*
|