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
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,60 @@ 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
|
+
* @example
|
|
568
|
+
* // Deque peek at both ends
|
|
569
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
570
|
+
*
|
|
571
|
+
* // Get first element without removing
|
|
572
|
+
* const first = deque.at(0);
|
|
573
|
+
* console.log(first); // 10;
|
|
574
|
+
*
|
|
575
|
+
* // Get last element without removing
|
|
576
|
+
* const last = deque.at(deque.length - 1);
|
|
577
|
+
* console.log(last); // 50;
|
|
578
|
+
*
|
|
579
|
+
* // Length unchanged
|
|
580
|
+
* console.log(deque.length); // 5;
|
|
581
|
+
*/
|
|
606
582
|
get first() {
|
|
607
583
|
if (this._length === 0) return;
|
|
608
584
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
609
585
|
}
|
|
610
586
|
/**
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
587
|
+
* Get the last element without removing it.
|
|
588
|
+
* @remarks Time O(1), Space O(1)
|
|
589
|
+
* @returns Last element or undefined.
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
* @example
|
|
602
|
+
* // Peek at the back element
|
|
603
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
604
|
+
* console.log(dq.last); // 'c';
|
|
605
|
+
* console.log(dq.first); // 'a';
|
|
606
|
+
*/
|
|
615
607
|
get last() {
|
|
616
608
|
if (this._length === 0) return;
|
|
617
609
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -630,11 +622,40 @@ var Deque = class extends LinearBase {
|
|
|
630
622
|
return new this(data, options);
|
|
631
623
|
}
|
|
632
624
|
/**
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
625
|
+
* Append one element at the back.
|
|
626
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
627
|
+
* @param element - Element to append.
|
|
628
|
+
* @returns True when appended.
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
* @example
|
|
641
|
+
* // basic Deque creation and push/pop operations
|
|
642
|
+
* // Create a simple Deque with initial values
|
|
643
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
644
|
+
*
|
|
645
|
+
* // Verify the deque maintains insertion order
|
|
646
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
647
|
+
*
|
|
648
|
+
* // Check length
|
|
649
|
+
* console.log(deque.length); // 5;
|
|
650
|
+
*
|
|
651
|
+
* // Push to the end
|
|
652
|
+
* deque.push(6);
|
|
653
|
+
* console.log(deque.length); // 6;
|
|
654
|
+
*
|
|
655
|
+
* // Pop from the end
|
|
656
|
+
* const last = deque.pop();
|
|
657
|
+
* console.log(last); // 6;
|
|
658
|
+
*/
|
|
638
659
|
push(element) {
|
|
639
660
|
if (this._length) {
|
|
640
661
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -654,10 +675,26 @@ var Deque = class extends LinearBase {
|
|
|
654
675
|
return true;
|
|
655
676
|
}
|
|
656
677
|
/**
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
678
|
+
* Remove and return the last element.
|
|
679
|
+
* @remarks Time O(1), Space O(1)
|
|
680
|
+
* @returns Removed element or undefined.
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
* @example
|
|
693
|
+
* // Remove from the back
|
|
694
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
695
|
+
* console.log(dq.pop()); // 3;
|
|
696
|
+
* console.log(dq.length); // 2;
|
|
697
|
+
*/
|
|
661
698
|
pop() {
|
|
662
699
|
if (this._length === 0) return;
|
|
663
700
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -677,10 +714,26 @@ var Deque = class extends LinearBase {
|
|
|
677
714
|
return element;
|
|
678
715
|
}
|
|
679
716
|
/**
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
717
|
+
* Remove and return the first element.
|
|
718
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
719
|
+
* @returns Removed element or undefined.
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
* @example
|
|
732
|
+
* // Remove from the front
|
|
733
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
734
|
+
* console.log(dq.shift()); // 1;
|
|
735
|
+
* console.log(dq.length); // 2;
|
|
736
|
+
*/
|
|
684
737
|
shift() {
|
|
685
738
|
if (this._length === 0) return;
|
|
686
739
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -700,11 +753,37 @@ var Deque = class extends LinearBase {
|
|
|
700
753
|
return element;
|
|
701
754
|
}
|
|
702
755
|
/**
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
756
|
+
* Prepend one element at the front.
|
|
757
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
758
|
+
* @param element - Element to prepend.
|
|
759
|
+
* @returns True when prepended.
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
* @example
|
|
772
|
+
* // Deque shift and unshift operations
|
|
773
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
774
|
+
*
|
|
775
|
+
* // Unshift adds to the front
|
|
776
|
+
* deque.unshift(10);
|
|
777
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
778
|
+
*
|
|
779
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
780
|
+
* const first = deque.shift();
|
|
781
|
+
* console.log(first); // 10;
|
|
782
|
+
*
|
|
783
|
+
* // Verify remaining elements
|
|
784
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
785
|
+
* console.log(deque.length); // 3;
|
|
786
|
+
*/
|
|
708
787
|
unshift(element) {
|
|
709
788
|
if (this._length) {
|
|
710
789
|
if (this._firstInBucket > 0) {
|
|
@@ -758,18 +837,45 @@ var Deque = class extends LinearBase {
|
|
|
758
837
|
return ans;
|
|
759
838
|
}
|
|
760
839
|
/**
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
840
|
+
* Check whether the deque is empty.
|
|
841
|
+
* @remarks Time O(1), Space O(1)
|
|
842
|
+
* @returns True if length is 0.
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
* @example
|
|
853
|
+
* // Check if empty
|
|
854
|
+
* const dq = new Deque();
|
|
855
|
+
* console.log(dq.isEmpty()); // true;
|
|
856
|
+
*/
|
|
765
857
|
isEmpty() {
|
|
766
858
|
return this._length === 0;
|
|
767
859
|
}
|
|
768
860
|
/**
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
861
|
+
* Remove all elements and reset structure.
|
|
862
|
+
* @remarks Time O(1), Space O(1)
|
|
863
|
+
* @returns void
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
* @example
|
|
874
|
+
* // Remove all elements
|
|
875
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
876
|
+
* dq.clear();
|
|
877
|
+
* console.log(dq.length); // 0;
|
|
878
|
+
*/
|
|
773
879
|
clear() {
|
|
774
880
|
this._buckets = [new Array(this._bucketSize)];
|
|
775
881
|
this._bucketCount = 1;
|
|
@@ -777,11 +883,24 @@ var Deque = class extends LinearBase {
|
|
|
777
883
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
778
884
|
}
|
|
779
885
|
/**
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
886
|
+
* Get the element at a given position.
|
|
887
|
+
* @remarks Time O(1), Space O(1)
|
|
888
|
+
* @param pos - Zero-based position from the front.
|
|
889
|
+
* @returns Element or undefined.
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
* @example
|
|
899
|
+
* // Access by index
|
|
900
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
901
|
+
* console.log(dq.at(0)); // 'a';
|
|
902
|
+
* console.log(dq.at(2)); // 'c';
|
|
903
|
+
*/
|
|
785
904
|
at(pos) {
|
|
786
905
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
787
906
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -940,11 +1059,24 @@ var Deque = class extends LinearBase {
|
|
|
940
1059
|
}
|
|
941
1060
|
}
|
|
942
1061
|
/**
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1062
|
+
* Delete the first occurrence of a value.
|
|
1063
|
+
* @remarks Time O(N), Space O(1)
|
|
1064
|
+
* @param element - Element to remove (using the configured equality).
|
|
1065
|
+
* @returns True if an element was removed.
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
* @example
|
|
1075
|
+
* // Remove element
|
|
1076
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1077
|
+
* dq.delete(2);
|
|
1078
|
+
* console.log(dq.length); // 2;
|
|
1079
|
+
*/
|
|
948
1080
|
delete(element) {
|
|
949
1081
|
const size = this._length;
|
|
950
1082
|
if (size === 0) return false;
|
|
@@ -988,10 +1120,39 @@ var Deque = class extends LinearBase {
|
|
|
988
1120
|
return this;
|
|
989
1121
|
}
|
|
990
1122
|
/**
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1123
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
1124
|
+
* @remarks Time O(N), Space O(N)
|
|
1125
|
+
* @returns This deque.
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
* @example
|
|
1138
|
+
* // Deque for...of iteration and reverse
|
|
1139
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1140
|
+
*
|
|
1141
|
+
* // Iterate forward
|
|
1142
|
+
* const forward: string[] = [];
|
|
1143
|
+
* for (const item of deque) {
|
|
1144
|
+
* forward.push(item);
|
|
1145
|
+
* }
|
|
1146
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1147
|
+
*
|
|
1148
|
+
* // Reverse the deque
|
|
1149
|
+
* deque.reverse();
|
|
1150
|
+
* const backward: string[] = [];
|
|
1151
|
+
* for (const item of deque) {
|
|
1152
|
+
* backward.push(item);
|
|
1153
|
+
* }
|
|
1154
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1155
|
+
*/
|
|
995
1156
|
reverse() {
|
|
996
1157
|
this._buckets.reverse().forEach(function(bucket) {
|
|
997
1158
|
bucket.reverse();
|
|
@@ -1050,10 +1211,25 @@ var Deque = class extends LinearBase {
|
|
|
1050
1211
|
* @returns True if compaction was performed (bucket count reduced).
|
|
1051
1212
|
*/
|
|
1052
1213
|
/**
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1214
|
+
* Compact the deque by removing unused buckets.
|
|
1215
|
+
* @remarks Time O(N), Space O(1)
|
|
1216
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
* @example
|
|
1226
|
+
* // Reclaim memory
|
|
1227
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1228
|
+
* dq.shift();
|
|
1229
|
+
* dq.shift();
|
|
1230
|
+
* dq.compact();
|
|
1231
|
+
* console.log(dq.length); // 3;
|
|
1232
|
+
*/
|
|
1057
1233
|
compact() {
|
|
1058
1234
|
const before = this._bucketCount;
|
|
1059
1235
|
this.shrinkToFit();
|
|
@@ -1081,10 +1257,26 @@ var Deque = class extends LinearBase {
|
|
|
1081
1257
|
this._compactCounter = 0;
|
|
1082
1258
|
}
|
|
1083
1259
|
/**
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1260
|
+
* Deep clone this deque, preserving options.
|
|
1261
|
+
* @remarks Time O(N), Space O(N)
|
|
1262
|
+
* @returns A new deque with the same content and options.
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
* @example
|
|
1273
|
+
* // Create independent copy
|
|
1274
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1275
|
+
* const copy = dq.clone();
|
|
1276
|
+
* copy.pop();
|
|
1277
|
+
* console.log(dq.length); // 3;
|
|
1278
|
+
* console.log(copy.length); // 2;
|
|
1279
|
+
*/
|
|
1088
1280
|
clone() {
|
|
1089
1281
|
return this._createLike(this, {
|
|
1090
1282
|
bucketSize: this.bucketSize,
|
|
@@ -1093,12 +1285,26 @@ var Deque = class extends LinearBase {
|
|
|
1093
1285
|
});
|
|
1094
1286
|
}
|
|
1095
1287
|
/**
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1288
|
+
* Filter elements into a new deque of the same class.
|
|
1289
|
+
* @remarks Time O(N), Space O(N)
|
|
1290
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1291
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1292
|
+
* @returns A new deque with kept elements.
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
* @example
|
|
1303
|
+
* // Filter elements
|
|
1304
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1305
|
+
* const result = dq.filter(x => x > 2);
|
|
1306
|
+
* console.log(result.length); // 2;
|
|
1307
|
+
*/
|
|
1102
1308
|
filter(predicate, thisArg) {
|
|
1103
1309
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1104
1310
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1127,15 +1333,28 @@ var Deque = class extends LinearBase {
|
|
|
1127
1333
|
return out;
|
|
1128
1334
|
}
|
|
1129
1335
|
/**
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1336
|
+
* Map elements into a new deque (possibly different element type).
|
|
1337
|
+
* @remarks Time O(N), Space O(N)
|
|
1338
|
+
* @template EM
|
|
1339
|
+
* @template RM
|
|
1340
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1341
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1342
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1343
|
+
* @returns A new Deque with mapped elements.
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
* @example
|
|
1353
|
+
* // Transform elements
|
|
1354
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1355
|
+
* const result = dq.map(x => x * 10);
|
|
1356
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
1357
|
+
*/
|
|
1139
1358
|
map(callback, options, thisArg) {
|
|
1140
1359
|
const out = this._createLike([], {
|
|
1141
1360
|
...options ?? {},
|
|
@@ -1258,6 +1477,55 @@ var Deque = class extends LinearBase {
|
|
|
1258
1477
|
}
|
|
1259
1478
|
}
|
|
1260
1479
|
};
|
|
1480
|
+
|
|
1481
|
+
// src/common/error.ts
|
|
1482
|
+
var ERR = {
|
|
1483
|
+
// Range / index
|
|
1484
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1485
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1486
|
+
// Type / argument
|
|
1487
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1488
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1489
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1490
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1491
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1492
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1493
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1494
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1495
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1496
|
+
// State / operation
|
|
1497
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1498
|
+
// Matrix
|
|
1499
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1500
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1501
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1502
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1503
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
// src/common/index.ts
|
|
1507
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1508
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1509
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1510
|
+
return DFSOperation2;
|
|
1511
|
+
})(DFSOperation || {});
|
|
1512
|
+
var Range = class {
|
|
1513
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1514
|
+
this.low = low;
|
|
1515
|
+
this.high = high;
|
|
1516
|
+
this.includeLow = includeLow;
|
|
1517
|
+
this.includeHigh = includeHigh;
|
|
1518
|
+
}
|
|
1519
|
+
static {
|
|
1520
|
+
__name(this, "Range");
|
|
1521
|
+
}
|
|
1522
|
+
// Determine whether a key is within the range
|
|
1523
|
+
isInRange(key, comparator) {
|
|
1524
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1525
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1526
|
+
return lowCheck && highCheck;
|
|
1527
|
+
}
|
|
1528
|
+
};
|
|
1261
1529
|
/**
|
|
1262
1530
|
* data-structure-typed
|
|
1263
1531
|
*
|