deque-typed 2.4.5 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -73
- package/dist/cjs/index.cjs +390 -122
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +389 -121
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +390 -122
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +389 -121
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -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 +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -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 +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/deque-typed.js +387 -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/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -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 +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -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 +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -13,54 +13,6 @@ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
|
13
13
|
}, "rangeCheck");
|
|
14
14
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
15
15
|
|
|
16
|
-
// src/common/error.ts
|
|
17
|
-
var ERR = {
|
|
18
|
-
// Range / index
|
|
19
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
20
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
21
|
-
// Type / argument
|
|
22
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
23
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
24
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
25
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
26
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
27
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
28
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
29
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
30
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
31
|
-
// State / operation
|
|
32
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
33
|
-
// Matrix
|
|
34
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
35
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
36
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
37
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
38
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
// src/common/index.ts
|
|
42
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
43
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
44
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
45
|
-
return DFSOperation2;
|
|
46
|
-
})(DFSOperation || {});
|
|
47
|
-
var _Range = class _Range {
|
|
48
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
49
|
-
this.low = low;
|
|
50
|
-
this.high = high;
|
|
51
|
-
this.includeLow = includeLow;
|
|
52
|
-
this.includeHigh = includeHigh;
|
|
53
|
-
}
|
|
54
|
-
// Determine whether a key is within the range
|
|
55
|
-
isInRange(key, comparator) {
|
|
56
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
57
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
58
|
-
return lowCheck && highCheck;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
__name(_Range, "Range");
|
|
62
|
-
var Range = _Range;
|
|
63
|
-
|
|
64
16
|
// src/data-structures/base/iterable-element-base.ts
|
|
65
17
|
var _IterableElementBase = class _IterableElementBase {
|
|
66
18
|
/**
|
|
@@ -83,7 +35,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
83
35
|
if (options) {
|
|
84
36
|
const { toElementFn } = options;
|
|
85
37
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
86
|
-
else if (toElementFn) throw new TypeError(
|
|
38
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
87
39
|
}
|
|
88
40
|
}
|
|
89
41
|
/**
|
|
@@ -239,7 +191,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
239
191
|
acc = initialValue;
|
|
240
192
|
} else {
|
|
241
193
|
const first = iter.next();
|
|
242
|
-
if (first.done) throw new TypeError(
|
|
194
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
243
195
|
acc = first.value;
|
|
244
196
|
index = 1;
|
|
245
197
|
}
|
|
@@ -597,19 +549,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
597
549
|
return this._length;
|
|
598
550
|
}
|
|
599
551
|
/**
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
552
|
+
* Get the first element without removing it.
|
|
553
|
+
* @remarks Time O(1), Space O(1)
|
|
554
|
+
* @returns First element or undefined.
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
* @example
|
|
567
|
+
* // Deque peek at both ends
|
|
568
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
569
|
+
*
|
|
570
|
+
* // Get first element without removing
|
|
571
|
+
* const first = deque.at(0);
|
|
572
|
+
* console.log(first); // 10;
|
|
573
|
+
*
|
|
574
|
+
* // Get last element without removing
|
|
575
|
+
* const last = deque.at(deque.length - 1);
|
|
576
|
+
* console.log(last); // 50;
|
|
577
|
+
*
|
|
578
|
+
* // Length unchanged
|
|
579
|
+
* console.log(deque.length); // 5;
|
|
580
|
+
*/
|
|
604
581
|
get first() {
|
|
605
582
|
if (this._length === 0) return;
|
|
606
583
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
607
584
|
}
|
|
608
585
|
/**
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
586
|
+
* Get the last element without removing it.
|
|
587
|
+
* @remarks Time O(1), Space O(1)
|
|
588
|
+
* @returns Last element or undefined.
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
* @example
|
|
601
|
+
* // Peek at the back element
|
|
602
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
603
|
+
* console.log(dq.last); // 'c';
|
|
604
|
+
* console.log(dq.first); // 'a';
|
|
605
|
+
*/
|
|
613
606
|
get last() {
|
|
614
607
|
if (this._length === 0) return;
|
|
615
608
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -628,11 +621,40 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
628
621
|
return new this(data, options);
|
|
629
622
|
}
|
|
630
623
|
/**
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
624
|
+
* Append one element at the back.
|
|
625
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
626
|
+
* @param element - Element to append.
|
|
627
|
+
* @returns True when appended.
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
* @example
|
|
640
|
+
* // basic Deque creation and push/pop operations
|
|
641
|
+
* // Create a simple Deque with initial values
|
|
642
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
643
|
+
*
|
|
644
|
+
* // Verify the deque maintains insertion order
|
|
645
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
646
|
+
*
|
|
647
|
+
* // Check length
|
|
648
|
+
* console.log(deque.length); // 5;
|
|
649
|
+
*
|
|
650
|
+
* // Push to the end
|
|
651
|
+
* deque.push(6);
|
|
652
|
+
* console.log(deque.length); // 6;
|
|
653
|
+
*
|
|
654
|
+
* // Pop from the end
|
|
655
|
+
* const last = deque.pop();
|
|
656
|
+
* console.log(last); // 6;
|
|
657
|
+
*/
|
|
636
658
|
push(element) {
|
|
637
659
|
if (this._length) {
|
|
638
660
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -652,10 +674,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
652
674
|
return true;
|
|
653
675
|
}
|
|
654
676
|
/**
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
677
|
+
* Remove and return the last element.
|
|
678
|
+
* @remarks Time O(1), Space O(1)
|
|
679
|
+
* @returns Removed element or undefined.
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
* @example
|
|
692
|
+
* // Remove from the back
|
|
693
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
694
|
+
* console.log(dq.pop()); // 3;
|
|
695
|
+
* console.log(dq.length); // 2;
|
|
696
|
+
*/
|
|
659
697
|
pop() {
|
|
660
698
|
if (this._length === 0) return;
|
|
661
699
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -675,10 +713,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
675
713
|
return element;
|
|
676
714
|
}
|
|
677
715
|
/**
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
716
|
+
* Remove and return the first element.
|
|
717
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
718
|
+
* @returns Removed element or undefined.
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
* @example
|
|
731
|
+
* // Remove from the front
|
|
732
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
733
|
+
* console.log(dq.shift()); // 1;
|
|
734
|
+
* console.log(dq.length); // 2;
|
|
735
|
+
*/
|
|
682
736
|
shift() {
|
|
683
737
|
if (this._length === 0) return;
|
|
684
738
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -698,11 +752,37 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
698
752
|
return element;
|
|
699
753
|
}
|
|
700
754
|
/**
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
755
|
+
* Prepend one element at the front.
|
|
756
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
757
|
+
* @param element - Element to prepend.
|
|
758
|
+
* @returns True when prepended.
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
* @example
|
|
771
|
+
* // Deque shift and unshift operations
|
|
772
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
773
|
+
*
|
|
774
|
+
* // Unshift adds to the front
|
|
775
|
+
* deque.unshift(10);
|
|
776
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
777
|
+
*
|
|
778
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
779
|
+
* const first = deque.shift();
|
|
780
|
+
* console.log(first); // 10;
|
|
781
|
+
*
|
|
782
|
+
* // Verify remaining elements
|
|
783
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
784
|
+
* console.log(deque.length); // 3;
|
|
785
|
+
*/
|
|
706
786
|
unshift(element) {
|
|
707
787
|
if (this._length) {
|
|
708
788
|
if (this._firstInBucket > 0) {
|
|
@@ -756,18 +836,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
756
836
|
return ans;
|
|
757
837
|
}
|
|
758
838
|
/**
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
839
|
+
* Check whether the deque is empty.
|
|
840
|
+
* @remarks Time O(1), Space O(1)
|
|
841
|
+
* @returns True if length is 0.
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
* @example
|
|
852
|
+
* // Check if empty
|
|
853
|
+
* const dq = new Deque();
|
|
854
|
+
* console.log(dq.isEmpty()); // true;
|
|
855
|
+
*/
|
|
763
856
|
isEmpty() {
|
|
764
857
|
return this._length === 0;
|
|
765
858
|
}
|
|
766
859
|
/**
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
860
|
+
* Remove all elements and reset structure.
|
|
861
|
+
* @remarks Time O(1), Space O(1)
|
|
862
|
+
* @returns void
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
* @example
|
|
873
|
+
* // Remove all elements
|
|
874
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
875
|
+
* dq.clear();
|
|
876
|
+
* console.log(dq.length); // 0;
|
|
877
|
+
*/
|
|
771
878
|
clear() {
|
|
772
879
|
this._buckets = [new Array(this._bucketSize)];
|
|
773
880
|
this._bucketCount = 1;
|
|
@@ -775,11 +882,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
775
882
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
776
883
|
}
|
|
777
884
|
/**
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
885
|
+
* Get the element at a given position.
|
|
886
|
+
* @remarks Time O(1), Space O(1)
|
|
887
|
+
* @param pos - Zero-based position from the front.
|
|
888
|
+
* @returns Element or undefined.
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
* @example
|
|
898
|
+
* // Access by index
|
|
899
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
900
|
+
* console.log(dq.at(0)); // 'a';
|
|
901
|
+
* console.log(dq.at(2)); // 'c';
|
|
902
|
+
*/
|
|
783
903
|
at(pos) {
|
|
784
904
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
785
905
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -938,11 +1058,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
938
1058
|
}
|
|
939
1059
|
}
|
|
940
1060
|
/**
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1061
|
+
* Delete the first occurrence of a value.
|
|
1062
|
+
* @remarks Time O(N), Space O(1)
|
|
1063
|
+
* @param element - Element to remove (using the configured equality).
|
|
1064
|
+
* @returns True if an element was removed.
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
* @example
|
|
1074
|
+
* // Remove element
|
|
1075
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1076
|
+
* dq.delete(2);
|
|
1077
|
+
* console.log(dq.length); // 2;
|
|
1078
|
+
*/
|
|
946
1079
|
delete(element) {
|
|
947
1080
|
const size = this._length;
|
|
948
1081
|
if (size === 0) return false;
|
|
@@ -986,10 +1119,39 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
986
1119
|
return this;
|
|
987
1120
|
}
|
|
988
1121
|
/**
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1122
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
1123
|
+
* @remarks Time O(N), Space O(N)
|
|
1124
|
+
* @returns This deque.
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
* @example
|
|
1137
|
+
* // Deque for...of iteration and reverse
|
|
1138
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1139
|
+
*
|
|
1140
|
+
* // Iterate forward
|
|
1141
|
+
* const forward: string[] = [];
|
|
1142
|
+
* for (const item of deque) {
|
|
1143
|
+
* forward.push(item);
|
|
1144
|
+
* }
|
|
1145
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1146
|
+
*
|
|
1147
|
+
* // Reverse the deque
|
|
1148
|
+
* deque.reverse();
|
|
1149
|
+
* const backward: string[] = [];
|
|
1150
|
+
* for (const item of deque) {
|
|
1151
|
+
* backward.push(item);
|
|
1152
|
+
* }
|
|
1153
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1154
|
+
*/
|
|
993
1155
|
reverse() {
|
|
994
1156
|
this._buckets.reverse().forEach(function(bucket) {
|
|
995
1157
|
bucket.reverse();
|
|
@@ -1048,10 +1210,25 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1048
1210
|
* @returns True if compaction was performed (bucket count reduced).
|
|
1049
1211
|
*/
|
|
1050
1212
|
/**
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1213
|
+
* Compact the deque by removing unused buckets.
|
|
1214
|
+
* @remarks Time O(N), Space O(1)
|
|
1215
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
* @example
|
|
1225
|
+
* // Reclaim memory
|
|
1226
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1227
|
+
* dq.shift();
|
|
1228
|
+
* dq.shift();
|
|
1229
|
+
* dq.compact();
|
|
1230
|
+
* console.log(dq.length); // 3;
|
|
1231
|
+
*/
|
|
1055
1232
|
compact() {
|
|
1056
1233
|
const before = this._bucketCount;
|
|
1057
1234
|
this.shrinkToFit();
|
|
@@ -1079,10 +1256,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1079
1256
|
this._compactCounter = 0;
|
|
1080
1257
|
}
|
|
1081
1258
|
/**
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1259
|
+
* Deep clone this deque, preserving options.
|
|
1260
|
+
* @remarks Time O(N), Space O(N)
|
|
1261
|
+
* @returns A new deque with the same content and options.
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
* @example
|
|
1272
|
+
* // Create independent copy
|
|
1273
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1274
|
+
* const copy = dq.clone();
|
|
1275
|
+
* copy.pop();
|
|
1276
|
+
* console.log(dq.length); // 3;
|
|
1277
|
+
* console.log(copy.length); // 2;
|
|
1278
|
+
*/
|
|
1086
1279
|
clone() {
|
|
1087
1280
|
return this._createLike(this, {
|
|
1088
1281
|
bucketSize: this.bucketSize,
|
|
@@ -1091,12 +1284,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1091
1284
|
});
|
|
1092
1285
|
}
|
|
1093
1286
|
/**
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1287
|
+
* Filter elements into a new deque of the same class.
|
|
1288
|
+
* @remarks Time O(N), Space O(N)
|
|
1289
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1290
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1291
|
+
* @returns A new deque with kept elements.
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
* @example
|
|
1302
|
+
* // Filter elements
|
|
1303
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1304
|
+
* const result = dq.filter(x => x > 2);
|
|
1305
|
+
* console.log(result.length); // 2;
|
|
1306
|
+
*/
|
|
1100
1307
|
filter(predicate, thisArg) {
|
|
1101
1308
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1102
1309
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1125,15 +1332,28 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1125
1332
|
return out;
|
|
1126
1333
|
}
|
|
1127
1334
|
/**
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1335
|
+
* Map elements into a new deque (possibly different element type).
|
|
1336
|
+
* @remarks Time O(N), Space O(N)
|
|
1337
|
+
* @template EM
|
|
1338
|
+
* @template RM
|
|
1339
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1340
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1341
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1342
|
+
* @returns A new Deque with mapped elements.
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
* @example
|
|
1352
|
+
* // Transform elements
|
|
1353
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1354
|
+
* const result = dq.map(x => x * 10);
|
|
1355
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
1356
|
+
*/
|
|
1137
1357
|
map(callback, options, thisArg) {
|
|
1138
1358
|
const out = this._createLike([], {
|
|
1139
1359
|
...options != null ? options : {},
|
|
@@ -1258,6 +1478,54 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1258
1478
|
};
|
|
1259
1479
|
__name(_Deque, "Deque");
|
|
1260
1480
|
var Deque = _Deque;
|
|
1481
|
+
|
|
1482
|
+
// src/common/error.ts
|
|
1483
|
+
var ERR = {
|
|
1484
|
+
// Range / index
|
|
1485
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1486
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1487
|
+
// Type / argument
|
|
1488
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1489
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1490
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1491
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1492
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1493
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1494
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1495
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1496
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1497
|
+
// State / operation
|
|
1498
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1499
|
+
// Matrix
|
|
1500
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1501
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1502
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1503
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1504
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1505
|
+
};
|
|
1506
|
+
|
|
1507
|
+
// src/common/index.ts
|
|
1508
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1509
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1510
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1511
|
+
return DFSOperation2;
|
|
1512
|
+
})(DFSOperation || {});
|
|
1513
|
+
var _Range = class _Range {
|
|
1514
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1515
|
+
this.low = low;
|
|
1516
|
+
this.high = high;
|
|
1517
|
+
this.includeLow = includeLow;
|
|
1518
|
+
this.includeHigh = includeHigh;
|
|
1519
|
+
}
|
|
1520
|
+
// Determine whether a key is within the range
|
|
1521
|
+
isInRange(key, comparator) {
|
|
1522
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1523
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1524
|
+
return lowCheck && highCheck;
|
|
1525
|
+
}
|
|
1526
|
+
};
|
|
1527
|
+
__name(_Range, "Range");
|
|
1528
|
+
var Range = _Range;
|
|
1261
1529
|
/**
|
|
1262
1530
|
* data-structure-typed
|
|
1263
1531
|
*
|