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
package/dist/cjs/index.cjs
CHANGED
|
@@ -11,55 +11,6 @@ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
|
11
11
|
}, "rangeCheck");
|
|
12
12
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
13
13
|
|
|
14
|
-
// src/common/error.ts
|
|
15
|
-
var ERR = {
|
|
16
|
-
// Range / index
|
|
17
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
18
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
19
|
-
// Type / argument
|
|
20
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
21
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
22
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
23
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
24
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
25
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
26
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
27
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
28
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
29
|
-
// State / operation
|
|
30
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
31
|
-
// Matrix
|
|
32
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
33
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
34
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
35
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
36
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// src/common/index.ts
|
|
40
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
41
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
42
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
43
|
-
return DFSOperation2;
|
|
44
|
-
})(DFSOperation || {});
|
|
45
|
-
var Range = class {
|
|
46
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
47
|
-
this.low = low;
|
|
48
|
-
this.high = high;
|
|
49
|
-
this.includeLow = includeLow;
|
|
50
|
-
this.includeHigh = includeHigh;
|
|
51
|
-
}
|
|
52
|
-
static {
|
|
53
|
-
__name(this, "Range");
|
|
54
|
-
}
|
|
55
|
-
// Determine whether a key is within the range
|
|
56
|
-
isInRange(key, comparator) {
|
|
57
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
58
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
59
|
-
return lowCheck && highCheck;
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
|
|
63
14
|
// src/data-structures/base/iterable-element-base.ts
|
|
64
15
|
var IterableElementBase = class {
|
|
65
16
|
static {
|
|
@@ -78,7 +29,7 @@ var IterableElementBase = class {
|
|
|
78
29
|
if (options) {
|
|
79
30
|
const { toElementFn } = options;
|
|
80
31
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
81
|
-
else if (toElementFn) throw new TypeError(
|
|
32
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
82
33
|
}
|
|
83
34
|
}
|
|
84
35
|
/**
|
|
@@ -241,7 +192,7 @@ var IterableElementBase = class {
|
|
|
241
192
|
acc = initialValue;
|
|
242
193
|
} else {
|
|
243
194
|
const first = iter.next();
|
|
244
|
-
if (first.done) throw new TypeError(
|
|
195
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
245
196
|
acc = first.value;
|
|
246
197
|
index = 1;
|
|
247
198
|
}
|
|
@@ -601,19 +552,102 @@ var Deque = class extends LinearBase {
|
|
|
601
552
|
return this._length;
|
|
602
553
|
}
|
|
603
554
|
/**
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
555
|
+
* Get the first element without removing it.
|
|
556
|
+
* @remarks Time O(1), Space O(1)
|
|
557
|
+
* @returns First element or undefined.
|
|
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
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
* @example
|
|
591
|
+
* // Deque peek at both ends
|
|
592
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
593
|
+
*
|
|
594
|
+
* // Get first element without removing
|
|
595
|
+
* const first = deque.at(0);
|
|
596
|
+
* console.log(first); // 10;
|
|
597
|
+
*
|
|
598
|
+
* // Get last element without removing
|
|
599
|
+
* const last = deque.at(deque.length - 1);
|
|
600
|
+
* console.log(last); // 50;
|
|
601
|
+
*
|
|
602
|
+
* // Length unchanged
|
|
603
|
+
* console.log(deque.length); // 5;
|
|
604
|
+
*/
|
|
608
605
|
get first() {
|
|
609
606
|
if (this._length === 0) return;
|
|
610
607
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
611
608
|
}
|
|
612
609
|
/**
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
610
|
+
* Get the last element without removing it.
|
|
611
|
+
* @remarks Time O(1), Space O(1)
|
|
612
|
+
* @returns Last element or undefined.
|
|
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
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
* @example
|
|
646
|
+
* // Peek at the back element
|
|
647
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
648
|
+
* console.log(dq.last); // 'c';
|
|
649
|
+
* console.log(dq.first); // 'a';
|
|
650
|
+
*/
|
|
617
651
|
get last() {
|
|
618
652
|
if (this._length === 0) return;
|
|
619
653
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -632,11 +666,61 @@ var Deque = class extends LinearBase {
|
|
|
632
666
|
return new this(data, options);
|
|
633
667
|
}
|
|
634
668
|
/**
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
669
|
+
* Append one element at the back.
|
|
670
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
671
|
+
* @param element - Element to append.
|
|
672
|
+
* @returns True when appended.
|
|
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
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
* @example
|
|
706
|
+
* // basic Deque creation and push/pop operations
|
|
707
|
+
* // Create a simple Deque with initial values
|
|
708
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
709
|
+
*
|
|
710
|
+
* // Verify the deque maintains insertion order
|
|
711
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
712
|
+
*
|
|
713
|
+
* // Check length
|
|
714
|
+
* console.log(deque.length); // 5;
|
|
715
|
+
*
|
|
716
|
+
* // Push to the end
|
|
717
|
+
* deque.push(6);
|
|
718
|
+
* console.log(deque.length); // 6;
|
|
719
|
+
*
|
|
720
|
+
* // Pop from the end
|
|
721
|
+
* const last = deque.pop();
|
|
722
|
+
* console.log(last); // 6;
|
|
723
|
+
*/
|
|
640
724
|
push(element) {
|
|
641
725
|
if (this._length) {
|
|
642
726
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -656,10 +740,47 @@ var Deque = class extends LinearBase {
|
|
|
656
740
|
return true;
|
|
657
741
|
}
|
|
658
742
|
/**
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
743
|
+
* Remove and return the last element.
|
|
744
|
+
* @remarks Time O(1), Space O(1)
|
|
745
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
* @example
|
|
779
|
+
* // Remove from the back
|
|
780
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
781
|
+
* console.log(dq.pop()); // 3;
|
|
782
|
+
* console.log(dq.length); // 2;
|
|
783
|
+
*/
|
|
663
784
|
pop() {
|
|
664
785
|
if (this._length === 0) return;
|
|
665
786
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -679,10 +800,47 @@ var Deque = class extends LinearBase {
|
|
|
679
800
|
return element;
|
|
680
801
|
}
|
|
681
802
|
/**
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
803
|
+
* Remove and return the first element.
|
|
804
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
805
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
* @example
|
|
839
|
+
* // Remove from the front
|
|
840
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
841
|
+
* console.log(dq.shift()); // 1;
|
|
842
|
+
* console.log(dq.length); // 2;
|
|
843
|
+
*/
|
|
686
844
|
shift() {
|
|
687
845
|
if (this._length === 0) return;
|
|
688
846
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -702,11 +860,58 @@ var Deque = class extends LinearBase {
|
|
|
702
860
|
return element;
|
|
703
861
|
}
|
|
704
862
|
/**
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
863
|
+
* Prepend one element at the front.
|
|
864
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
865
|
+
* @param element - Element to prepend.
|
|
866
|
+
* @returns True when prepended.
|
|
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
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
* @example
|
|
900
|
+
* // Deque shift and unshift operations
|
|
901
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
902
|
+
*
|
|
903
|
+
* // Unshift adds to the front
|
|
904
|
+
* deque.unshift(10);
|
|
905
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
906
|
+
*
|
|
907
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
908
|
+
* const first = deque.shift();
|
|
909
|
+
* console.log(first); // 10;
|
|
910
|
+
*
|
|
911
|
+
* // Verify remaining elements
|
|
912
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
913
|
+
* console.log(deque.length); // 3;
|
|
914
|
+
*/
|
|
710
915
|
unshift(element) {
|
|
711
916
|
if (this._length) {
|
|
712
917
|
if (this._firstInBucket > 0) {
|
|
@@ -760,18 +965,87 @@ var Deque = class extends LinearBase {
|
|
|
760
965
|
return ans;
|
|
761
966
|
}
|
|
762
967
|
/**
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
968
|
+
* Check whether the deque is empty.
|
|
969
|
+
* @remarks Time O(1), Space O(1)
|
|
970
|
+
* @returns True if length is 0.
|
|
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
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
* @example
|
|
1002
|
+
* // Check if empty
|
|
1003
|
+
* const dq = new Deque();
|
|
1004
|
+
* console.log(dq.isEmpty()); // true;
|
|
1005
|
+
*/
|
|
767
1006
|
isEmpty() {
|
|
768
1007
|
return this._length === 0;
|
|
769
1008
|
}
|
|
770
1009
|
/**
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
1010
|
+
* Remove all elements and reset structure.
|
|
1011
|
+
* @remarks Time O(1), Space O(1)
|
|
1012
|
+
* @returns void
|
|
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
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
* @example
|
|
1044
|
+
* // Remove all elements
|
|
1045
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1046
|
+
* dq.clear();
|
|
1047
|
+
* console.log(dq.length); // 0;
|
|
1048
|
+
*/
|
|
775
1049
|
clear() {
|
|
776
1050
|
this._buckets = [new Array(this._bucketSize)];
|
|
777
1051
|
this._bucketCount = 1;
|
|
@@ -779,11 +1053,45 @@ var Deque = class extends LinearBase {
|
|
|
779
1053
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
780
1054
|
}
|
|
781
1055
|
/**
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
1056
|
+
* Get the element at a given position.
|
|
1057
|
+
* @remarks Time O(1), Space O(1)
|
|
1058
|
+
* @param pos - Zero-based position from the front.
|
|
1059
|
+
* @returns Element or undefined.
|
|
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
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
* @example
|
|
1090
|
+
* // Access by index
|
|
1091
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
1092
|
+
* console.log(dq.at(0)); // 'a';
|
|
1093
|
+
* console.log(dq.at(2)); // 'c';
|
|
1094
|
+
*/
|
|
787
1095
|
at(pos) {
|
|
788
1096
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
789
1097
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -942,11 +1250,45 @@ var Deque = class extends LinearBase {
|
|
|
942
1250
|
}
|
|
943
1251
|
}
|
|
944
1252
|
/**
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1253
|
+
* Delete the first occurrence of a value.
|
|
1254
|
+
* @remarks Time O(N), Space O(1)
|
|
1255
|
+
* @param element - Element to remove (using the configured equality).
|
|
1256
|
+
* @returns True if an element was removed.
|
|
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
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
* @example
|
|
1287
|
+
* // Remove element
|
|
1288
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1289
|
+
* dq.delete(2);
|
|
1290
|
+
* console.log(dq.length); // 2;
|
|
1291
|
+
*/
|
|
950
1292
|
delete(element) {
|
|
951
1293
|
const size = this._length;
|
|
952
1294
|
if (size === 0) return false;
|
|
@@ -990,10 +1332,60 @@ var Deque = class extends LinearBase {
|
|
|
990
1332
|
return this;
|
|
991
1333
|
}
|
|
992
1334
|
/**
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1335
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
1336
|
+
* @remarks Time O(N), Space O(N)
|
|
1337
|
+
* @returns This deque.
|
|
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
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
* @example
|
|
1371
|
+
* // Deque for...of iteration and reverse
|
|
1372
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1373
|
+
*
|
|
1374
|
+
* // Iterate forward
|
|
1375
|
+
* const forward: string[] = [];
|
|
1376
|
+
* for (const item of deque) {
|
|
1377
|
+
* forward.push(item);
|
|
1378
|
+
* }
|
|
1379
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1380
|
+
*
|
|
1381
|
+
* // Reverse the deque
|
|
1382
|
+
* deque.reverse();
|
|
1383
|
+
* const backward: string[] = [];
|
|
1384
|
+
* for (const item of deque) {
|
|
1385
|
+
* backward.push(item);
|
|
1386
|
+
* }
|
|
1387
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1388
|
+
*/
|
|
997
1389
|
reverse() {
|
|
998
1390
|
this._buckets.reverse().forEach(function(bucket) {
|
|
999
1391
|
bucket.reverse();
|
|
@@ -1052,10 +1444,46 @@ var Deque = class extends LinearBase {
|
|
|
1052
1444
|
* @returns True if compaction was performed (bucket count reduced).
|
|
1053
1445
|
*/
|
|
1054
1446
|
/**
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1447
|
+
* Compact the deque by removing unused buckets.
|
|
1448
|
+
* @remarks Time O(N), Space O(1)
|
|
1449
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
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
|
+
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
* @example
|
|
1480
|
+
* // Reclaim memory
|
|
1481
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1482
|
+
* dq.shift();
|
|
1483
|
+
* dq.shift();
|
|
1484
|
+
* dq.compact();
|
|
1485
|
+
* console.log(dq.length); // 3;
|
|
1486
|
+
*/
|
|
1059
1487
|
compact() {
|
|
1060
1488
|
const before = this._bucketCount;
|
|
1061
1489
|
this.shrinkToFit();
|
|
@@ -1083,10 +1511,47 @@ var Deque = class extends LinearBase {
|
|
|
1083
1511
|
this._compactCounter = 0;
|
|
1084
1512
|
}
|
|
1085
1513
|
/**
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1514
|
+
* Deep clone this deque, preserving options.
|
|
1515
|
+
* @remarks Time O(N), Space O(N)
|
|
1516
|
+
* @returns A new deque with the same content and options.
|
|
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
|
+
|
|
1545
|
+
|
|
1546
|
+
|
|
1547
|
+
* @example
|
|
1548
|
+
* // Create independent copy
|
|
1549
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1550
|
+
* const copy = dq.clone();
|
|
1551
|
+
* copy.pop();
|
|
1552
|
+
* console.log(dq.length); // 3;
|
|
1553
|
+
* console.log(copy.length); // 2;
|
|
1554
|
+
*/
|
|
1090
1555
|
clone() {
|
|
1091
1556
|
return this._createLike(this, {
|
|
1092
1557
|
bucketSize: this.bucketSize,
|
|
@@ -1095,12 +1560,47 @@ var Deque = class extends LinearBase {
|
|
|
1095
1560
|
});
|
|
1096
1561
|
}
|
|
1097
1562
|
/**
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1563
|
+
* Filter elements into a new deque of the same class.
|
|
1564
|
+
* @remarks Time O(N), Space O(N)
|
|
1565
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1566
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1567
|
+
* @returns A new deque with kept elements.
|
|
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
|
+
|
|
1596
|
+
|
|
1597
|
+
|
|
1598
|
+
* @example
|
|
1599
|
+
* // Filter elements
|
|
1600
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1601
|
+
* const result = dq.filter(x => x > 2);
|
|
1602
|
+
* console.log(result.length); // 2;
|
|
1603
|
+
*/
|
|
1104
1604
|
filter(predicate, thisArg) {
|
|
1105
1605
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1106
1606
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1129,15 +1629,49 @@ var Deque = class extends LinearBase {
|
|
|
1129
1629
|
return out;
|
|
1130
1630
|
}
|
|
1131
1631
|
/**
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1632
|
+
* Map elements into a new deque (possibly different element type).
|
|
1633
|
+
* @remarks Time O(N), Space O(N)
|
|
1634
|
+
* @template EM
|
|
1635
|
+
* @template RM
|
|
1636
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1637
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1638
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1639
|
+
* @returns A new Deque with mapped elements.
|
|
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
|
+
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
* @example
|
|
1670
|
+
* // Transform elements
|
|
1671
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1672
|
+
* const result = dq.map(x => x * 10);
|
|
1673
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
1674
|
+
*/
|
|
1141
1675
|
map(callback, options, thisArg) {
|
|
1142
1676
|
const out = this._createLike([], {
|
|
1143
1677
|
...options ?? {},
|
|
@@ -1260,6 +1794,55 @@ var Deque = class extends LinearBase {
|
|
|
1260
1794
|
}
|
|
1261
1795
|
}
|
|
1262
1796
|
};
|
|
1797
|
+
|
|
1798
|
+
// src/common/error.ts
|
|
1799
|
+
var ERR = {
|
|
1800
|
+
// Range / index
|
|
1801
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1802
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1803
|
+
// Type / argument
|
|
1804
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1805
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1806
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1807
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1808
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1809
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1810
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1811
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1812
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1813
|
+
// State / operation
|
|
1814
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1815
|
+
// Matrix
|
|
1816
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1817
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1818
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1819
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1820
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1821
|
+
};
|
|
1822
|
+
|
|
1823
|
+
// src/common/index.ts
|
|
1824
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1825
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1826
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1827
|
+
return DFSOperation2;
|
|
1828
|
+
})(DFSOperation || {});
|
|
1829
|
+
var Range = class {
|
|
1830
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1831
|
+
this.low = low;
|
|
1832
|
+
this.high = high;
|
|
1833
|
+
this.includeLow = includeLow;
|
|
1834
|
+
this.includeHigh = includeHigh;
|
|
1835
|
+
}
|
|
1836
|
+
static {
|
|
1837
|
+
__name(this, "Range");
|
|
1838
|
+
}
|
|
1839
|
+
// Determine whether a key is within the range
|
|
1840
|
+
isInRange(key, comparator) {
|
|
1841
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1842
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1843
|
+
return lowCheck && highCheck;
|
|
1844
|
+
}
|
|
1845
|
+
};
|
|
1263
1846
|
/**
|
|
1264
1847
|
* data-structure-typed
|
|
1265
1848
|
*
|