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