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/esm/index.mjs
CHANGED
|
@@ -2,8 +2,10 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/utils/utils.ts
|
|
5
|
-
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message
|
|
6
|
-
if (index < min || index > max)
|
|
5
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
6
|
+
if (index < min || index > max) {
|
|
7
|
+
throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
|
|
8
|
+
}
|
|
7
9
|
}, "rangeCheck");
|
|
8
10
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
9
11
|
|
|
@@ -429,18 +431,12 @@ var Deque = class extends LinearBase {
|
|
|
429
431
|
__name(this, "Deque");
|
|
430
432
|
}
|
|
431
433
|
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
432
|
-
/**
|
|
433
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
434
|
-
* @remarks Time O(N), Space O(N)
|
|
435
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
436
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
437
|
-
* @returns New Deque instance.
|
|
438
|
-
*/
|
|
439
434
|
constructor(elements = [], options) {
|
|
440
435
|
super(options);
|
|
441
436
|
if (options) {
|
|
442
|
-
const { bucketSize } = options;
|
|
437
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
443
438
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
439
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
444
440
|
}
|
|
445
441
|
let _size;
|
|
446
442
|
if ("length" in elements) {
|
|
@@ -466,6 +462,30 @@ var Deque = class extends LinearBase {
|
|
|
466
462
|
get bucketSize() {
|
|
467
463
|
return this._bucketSize;
|
|
468
464
|
}
|
|
465
|
+
_autoCompactRatio = 0.5;
|
|
466
|
+
/**
|
|
467
|
+
* Get the auto-compaction ratio.
|
|
468
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
469
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
470
|
+
* @remarks Time O(1), Space O(1)
|
|
471
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
472
|
+
*/
|
|
473
|
+
get autoCompactRatio() {
|
|
474
|
+
return this._autoCompactRatio;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Set the auto-compaction ratio.
|
|
478
|
+
* @remarks Time O(1), Space O(1)
|
|
479
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
480
|
+
*/
|
|
481
|
+
set autoCompactRatio(value) {
|
|
482
|
+
this._autoCompactRatio = value;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Counter for shift/pop operations since last compaction check.
|
|
486
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
487
|
+
*/
|
|
488
|
+
_compactCounter = 0;
|
|
469
489
|
_bucketFirst = 0;
|
|
470
490
|
/**
|
|
471
491
|
* Get the index of the first bucket in use.
|
|
@@ -530,19 +550,60 @@ var Deque = class extends LinearBase {
|
|
|
530
550
|
return this._length;
|
|
531
551
|
}
|
|
532
552
|
/**
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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
|
+
*/
|
|
537
582
|
get first() {
|
|
538
583
|
if (this._length === 0) return;
|
|
539
584
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
540
585
|
}
|
|
541
586
|
/**
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
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
|
+
*/
|
|
546
607
|
get last() {
|
|
547
608
|
if (this._length === 0) return;
|
|
548
609
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -561,11 +622,40 @@ var Deque = class extends LinearBase {
|
|
|
561
622
|
return new this(data, options);
|
|
562
623
|
}
|
|
563
624
|
/**
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
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
|
+
*/
|
|
569
659
|
push(element) {
|
|
570
660
|
if (this._length) {
|
|
571
661
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -585,10 +675,26 @@ var Deque = class extends LinearBase {
|
|
|
585
675
|
return true;
|
|
586
676
|
}
|
|
587
677
|
/**
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
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
|
+
*/
|
|
592
698
|
pop() {
|
|
593
699
|
if (this._length === 0) return;
|
|
594
700
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -604,13 +710,30 @@ var Deque = class extends LinearBase {
|
|
|
604
710
|
}
|
|
605
711
|
}
|
|
606
712
|
this._length -= 1;
|
|
713
|
+
this._autoCompact();
|
|
607
714
|
return element;
|
|
608
715
|
}
|
|
609
716
|
/**
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
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
|
+
*/
|
|
614
737
|
shift() {
|
|
615
738
|
if (this._length === 0) return;
|
|
616
739
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -626,14 +749,41 @@ var Deque = class extends LinearBase {
|
|
|
626
749
|
}
|
|
627
750
|
}
|
|
628
751
|
this._length -= 1;
|
|
752
|
+
this._autoCompact();
|
|
629
753
|
return element;
|
|
630
754
|
}
|
|
631
755
|
/**
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
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
|
+
*/
|
|
637
787
|
unshift(element) {
|
|
638
788
|
if (this._length) {
|
|
639
789
|
if (this._firstInBucket > 0) {
|
|
@@ -687,18 +837,45 @@ var Deque = class extends LinearBase {
|
|
|
687
837
|
return ans;
|
|
688
838
|
}
|
|
689
839
|
/**
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
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
|
+
*/
|
|
694
857
|
isEmpty() {
|
|
695
858
|
return this._length === 0;
|
|
696
859
|
}
|
|
697
860
|
/**
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
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
|
+
*/
|
|
702
879
|
clear() {
|
|
703
880
|
this._buckets = [new Array(this._bucketSize)];
|
|
704
881
|
this._bucketCount = 1;
|
|
@@ -706,11 +883,24 @@ var Deque = class extends LinearBase {
|
|
|
706
883
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
707
884
|
}
|
|
708
885
|
/**
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
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
|
+
*/
|
|
714
904
|
at(pos) {
|
|
715
905
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
716
906
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -869,11 +1059,24 @@ var Deque = class extends LinearBase {
|
|
|
869
1059
|
}
|
|
870
1060
|
}
|
|
871
1061
|
/**
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
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
|
+
*/
|
|
877
1080
|
delete(element) {
|
|
878
1081
|
const size = this._length;
|
|
879
1082
|
if (size === 0) return false;
|
|
@@ -917,10 +1120,39 @@ var Deque = class extends LinearBase {
|
|
|
917
1120
|
return this;
|
|
918
1121
|
}
|
|
919
1122
|
/**
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
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
|
+
*/
|
|
924
1156
|
reverse() {
|
|
925
1157
|
this._buckets.reverse().forEach(function(bucket) {
|
|
926
1158
|
bucket.reverse();
|
|
@@ -958,11 +1190,55 @@ var Deque = class extends LinearBase {
|
|
|
958
1190
|
* @remarks Time O(N), Space O(1)
|
|
959
1191
|
* @returns void
|
|
960
1192
|
*/
|
|
1193
|
+
/**
|
|
1194
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
1195
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
1196
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
1197
|
+
*/
|
|
1198
|
+
_autoCompact() {
|
|
1199
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
1200
|
+
this._compactCounter++;
|
|
1201
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
1202
|
+
this._compactCounter = 0;
|
|
1203
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
1204
|
+
if (utilization < this._autoCompactRatio) {
|
|
1205
|
+
this.shrinkToFit();
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
/**
|
|
1209
|
+
* Compact the deque by removing unused buckets.
|
|
1210
|
+
* @remarks Time O(N), Space O(1)
|
|
1211
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1212
|
+
*/
|
|
1213
|
+
/**
|
|
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
|
+
*/
|
|
1233
|
+
compact() {
|
|
1234
|
+
const before = this._bucketCount;
|
|
1235
|
+
this.shrinkToFit();
|
|
1236
|
+
return this._bucketCount < before;
|
|
1237
|
+
}
|
|
961
1238
|
shrinkToFit() {
|
|
962
1239
|
if (this._length === 0) return;
|
|
963
1240
|
const newBuckets = [];
|
|
964
|
-
if (this._bucketFirst
|
|
965
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
1241
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
966
1242
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
967
1243
|
newBuckets.push(this._buckets[i]);
|
|
968
1244
|
}
|
|
@@ -977,12 +1253,30 @@ var Deque = class extends LinearBase {
|
|
|
977
1253
|
this._bucketFirst = 0;
|
|
978
1254
|
this._bucketLast = newBuckets.length - 1;
|
|
979
1255
|
this._buckets = newBuckets;
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1256
|
+
this._bucketCount = newBuckets.length;
|
|
1257
|
+
this._compactCounter = 0;
|
|
1258
|
+
}
|
|
1259
|
+
/**
|
|
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
|
+
*/
|
|
986
1280
|
clone() {
|
|
987
1281
|
return this._createLike(this, {
|
|
988
1282
|
bucketSize: this.bucketSize,
|
|
@@ -991,12 +1285,26 @@ var Deque = class extends LinearBase {
|
|
|
991
1285
|
});
|
|
992
1286
|
}
|
|
993
1287
|
/**
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
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
|
+
*/
|
|
1000
1308
|
filter(predicate, thisArg) {
|
|
1001
1309
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1002
1310
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1025,15 +1333,28 @@ var Deque = class extends LinearBase {
|
|
|
1025
1333
|
return out;
|
|
1026
1334
|
}
|
|
1027
1335
|
/**
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
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
|
+
*/
|
|
1037
1358
|
map(callback, options, thisArg) {
|
|
1038
1359
|
const out = this._createLike([], {
|
|
1039
1360
|
...options ?? {},
|
|
@@ -1157,6 +1478,31 @@ var Deque = class extends LinearBase {
|
|
|
1157
1478
|
}
|
|
1158
1479
|
};
|
|
1159
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
|
+
|
|
1160
1506
|
// src/common/index.ts
|
|
1161
1507
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1162
1508
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
@@ -1188,6 +1534,6 @@ var Range = class {
|
|
|
1188
1534
|
* @license MIT License
|
|
1189
1535
|
*/
|
|
1190
1536
|
|
|
1191
|
-
export { DFSOperation, Deque, Range };
|
|
1537
|
+
export { DFSOperation, Deque, ERR, Range };
|
|
1192
1538
|
//# sourceMappingURL=index.mjs.map
|
|
1193
1539
|
//# sourceMappingURL=index.mjs.map
|