deque-typed 2.4.4 → 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 +428 -81
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +428 -81
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +428 -82
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +428 -82
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- 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 +439 -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 +217 -31
- 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/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -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 +313 -66
- 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/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/deque-typed.js +428 -81
- 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/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- 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 +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- 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 +343 -68
- 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 +215 -44
- 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/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,8 +4,10 @@ var __defProp = Object.defineProperty;
|
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
6
|
// src/utils/utils.ts
|
|
7
|
-
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message
|
|
8
|
-
if (index < min || index > max)
|
|
7
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
8
|
+
if (index < min || index > max) {
|
|
9
|
+
throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
|
|
10
|
+
}
|
|
9
11
|
}, "rangeCheck");
|
|
10
12
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
11
13
|
|
|
@@ -431,18 +433,12 @@ var Deque = class extends LinearBase {
|
|
|
431
433
|
__name(this, "Deque");
|
|
432
434
|
}
|
|
433
435
|
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
434
|
-
/**
|
|
435
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
436
|
-
* @remarks Time O(N), Space O(N)
|
|
437
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
438
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
439
|
-
* @returns New Deque instance.
|
|
440
|
-
*/
|
|
441
436
|
constructor(elements = [], options) {
|
|
442
437
|
super(options);
|
|
443
438
|
if (options) {
|
|
444
|
-
const { bucketSize } = options;
|
|
439
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
445
440
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
441
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
446
442
|
}
|
|
447
443
|
let _size;
|
|
448
444
|
if ("length" in elements) {
|
|
@@ -468,6 +464,30 @@ var Deque = class extends LinearBase {
|
|
|
468
464
|
get bucketSize() {
|
|
469
465
|
return this._bucketSize;
|
|
470
466
|
}
|
|
467
|
+
_autoCompactRatio = 0.5;
|
|
468
|
+
/**
|
|
469
|
+
* Get the auto-compaction ratio.
|
|
470
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
471
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
472
|
+
* @remarks Time O(1), Space O(1)
|
|
473
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
474
|
+
*/
|
|
475
|
+
get autoCompactRatio() {
|
|
476
|
+
return this._autoCompactRatio;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Set the auto-compaction ratio.
|
|
480
|
+
* @remarks Time O(1), Space O(1)
|
|
481
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
482
|
+
*/
|
|
483
|
+
set autoCompactRatio(value) {
|
|
484
|
+
this._autoCompactRatio = value;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Counter for shift/pop operations since last compaction check.
|
|
488
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
489
|
+
*/
|
|
490
|
+
_compactCounter = 0;
|
|
471
491
|
_bucketFirst = 0;
|
|
472
492
|
/**
|
|
473
493
|
* Get the index of the first bucket in use.
|
|
@@ -532,19 +552,60 @@ var Deque = class extends LinearBase {
|
|
|
532
552
|
return this._length;
|
|
533
553
|
}
|
|
534
554
|
/**
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
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
|
+
*/
|
|
539
584
|
get first() {
|
|
540
585
|
if (this._length === 0) return;
|
|
541
586
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
542
587
|
}
|
|
543
588
|
/**
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
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
|
+
*/
|
|
548
609
|
get last() {
|
|
549
610
|
if (this._length === 0) return;
|
|
550
611
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -563,11 +624,40 @@ var Deque = class extends LinearBase {
|
|
|
563
624
|
return new this(data, options);
|
|
564
625
|
}
|
|
565
626
|
/**
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
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
|
+
*/
|
|
571
661
|
push(element) {
|
|
572
662
|
if (this._length) {
|
|
573
663
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -587,10 +677,26 @@ var Deque = class extends LinearBase {
|
|
|
587
677
|
return true;
|
|
588
678
|
}
|
|
589
679
|
/**
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
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
|
+
*/
|
|
594
700
|
pop() {
|
|
595
701
|
if (this._length === 0) return;
|
|
596
702
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -606,13 +712,30 @@ var Deque = class extends LinearBase {
|
|
|
606
712
|
}
|
|
607
713
|
}
|
|
608
714
|
this._length -= 1;
|
|
715
|
+
this._autoCompact();
|
|
609
716
|
return element;
|
|
610
717
|
}
|
|
611
718
|
/**
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
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
|
+
*/
|
|
616
739
|
shift() {
|
|
617
740
|
if (this._length === 0) return;
|
|
618
741
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -628,14 +751,41 @@ var Deque = class extends LinearBase {
|
|
|
628
751
|
}
|
|
629
752
|
}
|
|
630
753
|
this._length -= 1;
|
|
754
|
+
this._autoCompact();
|
|
631
755
|
return element;
|
|
632
756
|
}
|
|
633
757
|
/**
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
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
|
+
*/
|
|
639
789
|
unshift(element) {
|
|
640
790
|
if (this._length) {
|
|
641
791
|
if (this._firstInBucket > 0) {
|
|
@@ -689,18 +839,45 @@ var Deque = class extends LinearBase {
|
|
|
689
839
|
return ans;
|
|
690
840
|
}
|
|
691
841
|
/**
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
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
|
+
*/
|
|
696
859
|
isEmpty() {
|
|
697
860
|
return this._length === 0;
|
|
698
861
|
}
|
|
699
862
|
/**
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
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
|
+
*/
|
|
704
881
|
clear() {
|
|
705
882
|
this._buckets = [new Array(this._bucketSize)];
|
|
706
883
|
this._bucketCount = 1;
|
|
@@ -708,11 +885,24 @@ var Deque = class extends LinearBase {
|
|
|
708
885
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
709
886
|
}
|
|
710
887
|
/**
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
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
|
+
*/
|
|
716
906
|
at(pos) {
|
|
717
907
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
718
908
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -871,11 +1061,24 @@ var Deque = class extends LinearBase {
|
|
|
871
1061
|
}
|
|
872
1062
|
}
|
|
873
1063
|
/**
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
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
|
+
*/
|
|
879
1082
|
delete(element) {
|
|
880
1083
|
const size = this._length;
|
|
881
1084
|
if (size === 0) return false;
|
|
@@ -919,10 +1122,39 @@ var Deque = class extends LinearBase {
|
|
|
919
1122
|
return this;
|
|
920
1123
|
}
|
|
921
1124
|
/**
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
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
|
+
*/
|
|
926
1158
|
reverse() {
|
|
927
1159
|
this._buckets.reverse().forEach(function(bucket) {
|
|
928
1160
|
bucket.reverse();
|
|
@@ -960,11 +1192,55 @@ var Deque = class extends LinearBase {
|
|
|
960
1192
|
* @remarks Time O(N), Space O(1)
|
|
961
1193
|
* @returns void
|
|
962
1194
|
*/
|
|
1195
|
+
/**
|
|
1196
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
1197
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
1198
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
1199
|
+
*/
|
|
1200
|
+
_autoCompact() {
|
|
1201
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
1202
|
+
this._compactCounter++;
|
|
1203
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
1204
|
+
this._compactCounter = 0;
|
|
1205
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
1206
|
+
if (utilization < this._autoCompactRatio) {
|
|
1207
|
+
this.shrinkToFit();
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Compact the deque by removing unused buckets.
|
|
1212
|
+
* @remarks Time O(N), Space O(1)
|
|
1213
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1214
|
+
*/
|
|
1215
|
+
/**
|
|
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
|
+
*/
|
|
1235
|
+
compact() {
|
|
1236
|
+
const before = this._bucketCount;
|
|
1237
|
+
this.shrinkToFit();
|
|
1238
|
+
return this._bucketCount < before;
|
|
1239
|
+
}
|
|
963
1240
|
shrinkToFit() {
|
|
964
1241
|
if (this._length === 0) return;
|
|
965
1242
|
const newBuckets = [];
|
|
966
|
-
if (this._bucketFirst
|
|
967
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
1243
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
968
1244
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
969
1245
|
newBuckets.push(this._buckets[i]);
|
|
970
1246
|
}
|
|
@@ -979,12 +1255,30 @@ var Deque = class extends LinearBase {
|
|
|
979
1255
|
this._bucketFirst = 0;
|
|
980
1256
|
this._bucketLast = newBuckets.length - 1;
|
|
981
1257
|
this._buckets = newBuckets;
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1258
|
+
this._bucketCount = newBuckets.length;
|
|
1259
|
+
this._compactCounter = 0;
|
|
1260
|
+
}
|
|
1261
|
+
/**
|
|
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
|
+
*/
|
|
988
1282
|
clone() {
|
|
989
1283
|
return this._createLike(this, {
|
|
990
1284
|
bucketSize: this.bucketSize,
|
|
@@ -993,12 +1287,26 @@ var Deque = class extends LinearBase {
|
|
|
993
1287
|
});
|
|
994
1288
|
}
|
|
995
1289
|
/**
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
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
|
+
*/
|
|
1002
1310
|
filter(predicate, thisArg) {
|
|
1003
1311
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1004
1312
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1027,15 +1335,28 @@ var Deque = class extends LinearBase {
|
|
|
1027
1335
|
return out;
|
|
1028
1336
|
}
|
|
1029
1337
|
/**
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
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
|
+
*/
|
|
1039
1360
|
map(callback, options, thisArg) {
|
|
1040
1361
|
const out = this._createLike([], {
|
|
1041
1362
|
...options ?? {},
|
|
@@ -1159,6 +1480,31 @@ var Deque = class extends LinearBase {
|
|
|
1159
1480
|
}
|
|
1160
1481
|
};
|
|
1161
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
|
+
|
|
1162
1508
|
// src/common/index.ts
|
|
1163
1509
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1164
1510
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
@@ -1192,6 +1538,7 @@ var Range = class {
|
|
|
1192
1538
|
|
|
1193
1539
|
exports.DFSOperation = DFSOperation;
|
|
1194
1540
|
exports.Deque = Deque;
|
|
1541
|
+
exports.ERR = ERR;
|
|
1195
1542
|
exports.Range = Range;
|
|
1196
1543
|
//# sourceMappingURL=index.cjs.map
|
|
1197
1544
|
//# sourceMappingURL=index.cjs.map
|