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
|
@@ -11,54 +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 _Range {
|
|
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
|
-
// Determine whether a key is within the range
|
|
53
|
-
isInRange(key, comparator) {
|
|
54
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
55
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
56
|
-
return lowCheck && highCheck;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
__name(_Range, "Range");
|
|
60
|
-
var Range = _Range;
|
|
61
|
-
|
|
62
14
|
// src/data-structures/base/iterable-element-base.ts
|
|
63
15
|
var _IterableElementBase = class _IterableElementBase {
|
|
64
16
|
/**
|
|
@@ -81,7 +33,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
81
33
|
if (options) {
|
|
82
34
|
const { toElementFn } = options;
|
|
83
35
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
84
|
-
else if (toElementFn) throw new TypeError(
|
|
36
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
85
37
|
}
|
|
86
38
|
}
|
|
87
39
|
/**
|
|
@@ -237,7 +189,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
237
189
|
acc = initialValue;
|
|
238
190
|
} else {
|
|
239
191
|
const first = iter.next();
|
|
240
|
-
if (first.done) throw new TypeError(
|
|
192
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
241
193
|
acc = first.value;
|
|
242
194
|
index = 1;
|
|
243
195
|
}
|
|
@@ -595,19 +547,102 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
595
547
|
return this._length;
|
|
596
548
|
}
|
|
597
549
|
/**
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
550
|
+
* Get the first element without removing it.
|
|
551
|
+
* @remarks Time O(1), Space O(1)
|
|
552
|
+
* @returns First element or undefined.
|
|
553
|
+
|
|
554
|
+
|
|
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
|
+
* @example
|
|
586
|
+
* // Deque peek at both ends
|
|
587
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
588
|
+
*
|
|
589
|
+
* // Get first element without removing
|
|
590
|
+
* const first = deque.at(0);
|
|
591
|
+
* console.log(first); // 10;
|
|
592
|
+
*
|
|
593
|
+
* // Get last element without removing
|
|
594
|
+
* const last = deque.at(deque.length - 1);
|
|
595
|
+
* console.log(last); // 50;
|
|
596
|
+
*
|
|
597
|
+
* // Length unchanged
|
|
598
|
+
* console.log(deque.length); // 5;
|
|
599
|
+
*/
|
|
602
600
|
get first() {
|
|
603
601
|
if (this._length === 0) return;
|
|
604
602
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
605
603
|
}
|
|
606
604
|
/**
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
605
|
+
* Get the last element without removing it.
|
|
606
|
+
* @remarks Time O(1), Space O(1)
|
|
607
|
+
* @returns Last element or undefined.
|
|
608
|
+
|
|
609
|
+
|
|
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
|
+
* @example
|
|
641
|
+
* // Peek at the back element
|
|
642
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
643
|
+
* console.log(dq.last); // 'c';
|
|
644
|
+
* console.log(dq.first); // 'a';
|
|
645
|
+
*/
|
|
611
646
|
get last() {
|
|
612
647
|
if (this._length === 0) return;
|
|
613
648
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -626,11 +661,61 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
626
661
|
return new this(data, options);
|
|
627
662
|
}
|
|
628
663
|
/**
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
664
|
+
* Append one element at the back.
|
|
665
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
666
|
+
* @param element - Element to append.
|
|
667
|
+
* @returns True when appended.
|
|
668
|
+
|
|
669
|
+
|
|
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
|
+
* @example
|
|
701
|
+
* // basic Deque creation and push/pop operations
|
|
702
|
+
* // Create a simple Deque with initial values
|
|
703
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
704
|
+
*
|
|
705
|
+
* // Verify the deque maintains insertion order
|
|
706
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
707
|
+
*
|
|
708
|
+
* // Check length
|
|
709
|
+
* console.log(deque.length); // 5;
|
|
710
|
+
*
|
|
711
|
+
* // Push to the end
|
|
712
|
+
* deque.push(6);
|
|
713
|
+
* console.log(deque.length); // 6;
|
|
714
|
+
*
|
|
715
|
+
* // Pop from the end
|
|
716
|
+
* const last = deque.pop();
|
|
717
|
+
* console.log(last); // 6;
|
|
718
|
+
*/
|
|
634
719
|
push(element) {
|
|
635
720
|
if (this._length) {
|
|
636
721
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -650,10 +735,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
650
735
|
return true;
|
|
651
736
|
}
|
|
652
737
|
/**
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
738
|
+
* Remove and return the last element.
|
|
739
|
+
* @remarks Time O(1), Space O(1)
|
|
740
|
+
* @returns Removed element or undefined.
|
|
741
|
+
|
|
742
|
+
|
|
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
|
+
* @example
|
|
774
|
+
* // Remove from the back
|
|
775
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
776
|
+
* console.log(dq.pop()); // 3;
|
|
777
|
+
* console.log(dq.length); // 2;
|
|
778
|
+
*/
|
|
657
779
|
pop() {
|
|
658
780
|
if (this._length === 0) return;
|
|
659
781
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -673,10 +795,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
673
795
|
return element;
|
|
674
796
|
}
|
|
675
797
|
/**
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
798
|
+
* Remove and return the first element.
|
|
799
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
800
|
+
* @returns Removed element or undefined.
|
|
801
|
+
|
|
802
|
+
|
|
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
|
+
* @example
|
|
834
|
+
* // Remove from the front
|
|
835
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
836
|
+
* console.log(dq.shift()); // 1;
|
|
837
|
+
* console.log(dq.length); // 2;
|
|
838
|
+
*/
|
|
680
839
|
shift() {
|
|
681
840
|
if (this._length === 0) return;
|
|
682
841
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -696,11 +855,58 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
696
855
|
return element;
|
|
697
856
|
}
|
|
698
857
|
/**
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
858
|
+
* Prepend one element at the front.
|
|
859
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
860
|
+
* @param element - Element to prepend.
|
|
861
|
+
* @returns True when prepended.
|
|
862
|
+
|
|
863
|
+
|
|
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
|
+
* @example
|
|
895
|
+
* // Deque shift and unshift operations
|
|
896
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
897
|
+
*
|
|
898
|
+
* // Unshift adds to the front
|
|
899
|
+
* deque.unshift(10);
|
|
900
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
901
|
+
*
|
|
902
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
903
|
+
* const first = deque.shift();
|
|
904
|
+
* console.log(first); // 10;
|
|
905
|
+
*
|
|
906
|
+
* // Verify remaining elements
|
|
907
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
908
|
+
* console.log(deque.length); // 3;
|
|
909
|
+
*/
|
|
704
910
|
unshift(element) {
|
|
705
911
|
if (this._length) {
|
|
706
912
|
if (this._firstInBucket > 0) {
|
|
@@ -754,18 +960,87 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
754
960
|
return ans;
|
|
755
961
|
}
|
|
756
962
|
/**
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
963
|
+
* Check whether the deque is empty.
|
|
964
|
+
* @remarks Time O(1), Space O(1)
|
|
965
|
+
* @returns True if length is 0.
|
|
966
|
+
|
|
967
|
+
|
|
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
|
+
* @example
|
|
997
|
+
* // Check if empty
|
|
998
|
+
* const dq = new Deque();
|
|
999
|
+
* console.log(dq.isEmpty()); // true;
|
|
1000
|
+
*/
|
|
761
1001
|
isEmpty() {
|
|
762
1002
|
return this._length === 0;
|
|
763
1003
|
}
|
|
764
1004
|
/**
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
1005
|
+
* Remove all elements and reset structure.
|
|
1006
|
+
* @remarks Time O(1), Space O(1)
|
|
1007
|
+
* @returns void
|
|
1008
|
+
|
|
1009
|
+
|
|
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
|
+
* @example
|
|
1039
|
+
* // Remove all elements
|
|
1040
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1041
|
+
* dq.clear();
|
|
1042
|
+
* console.log(dq.length); // 0;
|
|
1043
|
+
*/
|
|
769
1044
|
clear() {
|
|
770
1045
|
this._buckets = [new Array(this._bucketSize)];
|
|
771
1046
|
this._bucketCount = 1;
|
|
@@ -773,11 +1048,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
773
1048
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
774
1049
|
}
|
|
775
1050
|
/**
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
1051
|
+
* Get the element at a given position.
|
|
1052
|
+
* @remarks Time O(1), Space O(1)
|
|
1053
|
+
* @param pos - Zero-based position from the front.
|
|
1054
|
+
* @returns Element or undefined.
|
|
1055
|
+
|
|
1056
|
+
|
|
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
|
+
* @example
|
|
1085
|
+
* // Access by index
|
|
1086
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
1087
|
+
* console.log(dq.at(0)); // 'a';
|
|
1088
|
+
* console.log(dq.at(2)); // 'c';
|
|
1089
|
+
*/
|
|
781
1090
|
at(pos) {
|
|
782
1091
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
783
1092
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -936,11 +1245,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
936
1245
|
}
|
|
937
1246
|
}
|
|
938
1247
|
/**
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1248
|
+
* Delete the first occurrence of a value.
|
|
1249
|
+
* @remarks Time O(N), Space O(1)
|
|
1250
|
+
* @param element - Element to remove (using the configured equality).
|
|
1251
|
+
* @returns True if an element was removed.
|
|
1252
|
+
|
|
1253
|
+
|
|
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
|
+
* @example
|
|
1282
|
+
* // Remove element
|
|
1283
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1284
|
+
* dq.delete(2);
|
|
1285
|
+
* console.log(dq.length); // 2;
|
|
1286
|
+
*/
|
|
944
1287
|
delete(element) {
|
|
945
1288
|
const size = this._length;
|
|
946
1289
|
if (size === 0) return false;
|
|
@@ -984,10 +1327,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
984
1327
|
return this;
|
|
985
1328
|
}
|
|
986
1329
|
/**
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1330
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
1331
|
+
* @remarks Time O(N), Space O(N)
|
|
1332
|
+
* @returns This deque.
|
|
1333
|
+
|
|
1334
|
+
|
|
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
|
+
* @example
|
|
1366
|
+
* // Deque for...of iteration and reverse
|
|
1367
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1368
|
+
*
|
|
1369
|
+
* // Iterate forward
|
|
1370
|
+
* const forward: string[] = [];
|
|
1371
|
+
* for (const item of deque) {
|
|
1372
|
+
* forward.push(item);
|
|
1373
|
+
* }
|
|
1374
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1375
|
+
*
|
|
1376
|
+
* // Reverse the deque
|
|
1377
|
+
* deque.reverse();
|
|
1378
|
+
* const backward: string[] = [];
|
|
1379
|
+
* for (const item of deque) {
|
|
1380
|
+
* backward.push(item);
|
|
1381
|
+
* }
|
|
1382
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1383
|
+
*/
|
|
991
1384
|
reverse() {
|
|
992
1385
|
this._buckets.reverse().forEach(function(bucket) {
|
|
993
1386
|
bucket.reverse();
|
|
@@ -1046,10 +1439,46 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1046
1439
|
* @returns True if compaction was performed (bucket count reduced).
|
|
1047
1440
|
*/
|
|
1048
1441
|
/**
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1442
|
+
* Compact the deque by removing unused buckets.
|
|
1443
|
+
* @remarks Time O(N), Space O(1)
|
|
1444
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1445
|
+
|
|
1446
|
+
|
|
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
|
+
* @example
|
|
1475
|
+
* // Reclaim memory
|
|
1476
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1477
|
+
* dq.shift();
|
|
1478
|
+
* dq.shift();
|
|
1479
|
+
* dq.compact();
|
|
1480
|
+
* console.log(dq.length); // 3;
|
|
1481
|
+
*/
|
|
1053
1482
|
compact() {
|
|
1054
1483
|
const before = this._bucketCount;
|
|
1055
1484
|
this.shrinkToFit();
|
|
@@ -1077,10 +1506,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1077
1506
|
this._compactCounter = 0;
|
|
1078
1507
|
}
|
|
1079
1508
|
/**
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1509
|
+
* Deep clone this deque, preserving options.
|
|
1510
|
+
* @remarks Time O(N), Space O(N)
|
|
1511
|
+
* @returns A new deque with the same content and options.
|
|
1512
|
+
|
|
1513
|
+
|
|
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
|
+
* @example
|
|
1543
|
+
* // Create independent copy
|
|
1544
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1545
|
+
* const copy = dq.clone();
|
|
1546
|
+
* copy.pop();
|
|
1547
|
+
* console.log(dq.length); // 3;
|
|
1548
|
+
* console.log(copy.length); // 2;
|
|
1549
|
+
*/
|
|
1084
1550
|
clone() {
|
|
1085
1551
|
return this._createLike(this, {
|
|
1086
1552
|
bucketSize: this.bucketSize,
|
|
@@ -1089,12 +1555,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1089
1555
|
});
|
|
1090
1556
|
}
|
|
1091
1557
|
/**
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1558
|
+
* Filter elements into a new deque of the same class.
|
|
1559
|
+
* @remarks Time O(N), Space O(N)
|
|
1560
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1561
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1562
|
+
* @returns A new deque with kept elements.
|
|
1563
|
+
|
|
1564
|
+
|
|
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
|
+
* @example
|
|
1594
|
+
* // Filter elements
|
|
1595
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1596
|
+
* const result = dq.filter(x => x > 2);
|
|
1597
|
+
* console.log(result.length); // 2;
|
|
1598
|
+
*/
|
|
1098
1599
|
filter(predicate, thisArg) {
|
|
1099
1600
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1100
1601
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1123,15 +1624,49 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1123
1624
|
return out;
|
|
1124
1625
|
}
|
|
1125
1626
|
/**
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1627
|
+
* Map elements into a new deque (possibly different element type).
|
|
1628
|
+
* @remarks Time O(N), Space O(N)
|
|
1629
|
+
* @template EM
|
|
1630
|
+
* @template RM
|
|
1631
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1632
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1633
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1634
|
+
* @returns A new Deque with mapped elements.
|
|
1635
|
+
|
|
1636
|
+
|
|
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
|
+
* @example
|
|
1665
|
+
* // Transform elements
|
|
1666
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1667
|
+
* const result = dq.map(x => x * 10);
|
|
1668
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
1669
|
+
*/
|
|
1135
1670
|
map(callback, options, thisArg) {
|
|
1136
1671
|
const out = this._createLike([], {
|
|
1137
1672
|
...options != null ? options : {},
|
|
@@ -1256,6 +1791,54 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1256
1791
|
};
|
|
1257
1792
|
__name(_Deque, "Deque");
|
|
1258
1793
|
var Deque = _Deque;
|
|
1794
|
+
|
|
1795
|
+
// src/common/error.ts
|
|
1796
|
+
var ERR = {
|
|
1797
|
+
// Range / index
|
|
1798
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1799
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1800
|
+
// Type / argument
|
|
1801
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1802
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1803
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1804
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1805
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1806
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1807
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1808
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1809
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1810
|
+
// State / operation
|
|
1811
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1812
|
+
// Matrix
|
|
1813
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1814
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1815
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1816
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1817
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1818
|
+
};
|
|
1819
|
+
|
|
1820
|
+
// src/common/index.ts
|
|
1821
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1822
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1823
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1824
|
+
return DFSOperation2;
|
|
1825
|
+
})(DFSOperation || {});
|
|
1826
|
+
var _Range = class _Range {
|
|
1827
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1828
|
+
this.low = low;
|
|
1829
|
+
this.high = high;
|
|
1830
|
+
this.includeLow = includeLow;
|
|
1831
|
+
this.includeHigh = includeHigh;
|
|
1832
|
+
}
|
|
1833
|
+
// Determine whether a key is within the range
|
|
1834
|
+
isInRange(key, comparator) {
|
|
1835
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1836
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1837
|
+
return lowCheck && highCheck;
|
|
1838
|
+
}
|
|
1839
|
+
};
|
|
1840
|
+
__name(_Range, "Range");
|
|
1841
|
+
var Range = _Range;
|
|
1259
1842
|
/**
|
|
1260
1843
|
* data-structure-typed
|
|
1261
1844
|
*
|