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