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
|
@@ -4,8 +4,10 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
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 != null ? 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
|
|
|
@@ -425,17 +427,16 @@ var LinearBase = _LinearBase;
|
|
|
425
427
|
|
|
426
428
|
// src/data-structures/queue/deque.ts
|
|
427
429
|
var _Deque = class _Deque extends LinearBase {
|
|
428
|
-
/**
|
|
429
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
430
|
-
* @remarks Time O(N), Space O(N)
|
|
431
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
432
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
433
|
-
* @returns New Deque instance.
|
|
434
|
-
*/
|
|
435
430
|
constructor(elements = [], options) {
|
|
436
431
|
super(options);
|
|
437
432
|
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
438
433
|
__publicField(this, "_bucketSize", 1 << 12);
|
|
434
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
435
|
+
/**
|
|
436
|
+
* Counter for shift/pop operations since last compaction check.
|
|
437
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
438
|
+
*/
|
|
439
|
+
__publicField(this, "_compactCounter", 0);
|
|
439
440
|
__publicField(this, "_bucketFirst", 0);
|
|
440
441
|
__publicField(this, "_firstInBucket", 0);
|
|
441
442
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -444,8 +445,9 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
444
445
|
__publicField(this, "_buckets", []);
|
|
445
446
|
__publicField(this, "_length", 0);
|
|
446
447
|
if (options) {
|
|
447
|
-
const { bucketSize } = options;
|
|
448
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
448
449
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
450
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
449
451
|
}
|
|
450
452
|
let _size;
|
|
451
453
|
if ("length" in elements) {
|
|
@@ -470,6 +472,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
470
472
|
get bucketSize() {
|
|
471
473
|
return this._bucketSize;
|
|
472
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Get the auto-compaction ratio.
|
|
477
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
478
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
479
|
+
* @remarks Time O(1), Space O(1)
|
|
480
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
481
|
+
*/
|
|
482
|
+
get autoCompactRatio() {
|
|
483
|
+
return this._autoCompactRatio;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Set the auto-compaction ratio.
|
|
487
|
+
* @remarks Time O(1), Space O(1)
|
|
488
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
489
|
+
*/
|
|
490
|
+
set autoCompactRatio(value) {
|
|
491
|
+
this._autoCompactRatio = value;
|
|
492
|
+
}
|
|
473
493
|
/**
|
|
474
494
|
* Get the index of the first bucket in use.
|
|
475
495
|
* @remarks Time O(1), Space O(1)
|
|
@@ -527,19 +547,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
527
547
|
return this._length;
|
|
528
548
|
}
|
|
529
549
|
/**
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
550
|
+
* Get the first element without removing it.
|
|
551
|
+
* @remarks Time O(1), Space O(1)
|
|
552
|
+
* @returns First element or undefined.
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
* @example
|
|
565
|
+
* // Deque peek at both ends
|
|
566
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
567
|
+
*
|
|
568
|
+
* // Get first element without removing
|
|
569
|
+
* const first = deque.at(0);
|
|
570
|
+
* console.log(first); // 10;
|
|
571
|
+
*
|
|
572
|
+
* // Get last element without removing
|
|
573
|
+
* const last = deque.at(deque.length - 1);
|
|
574
|
+
* console.log(last); // 50;
|
|
575
|
+
*
|
|
576
|
+
* // Length unchanged
|
|
577
|
+
* console.log(deque.length); // 5;
|
|
578
|
+
*/
|
|
534
579
|
get first() {
|
|
535
580
|
if (this._length === 0) return;
|
|
536
581
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
537
582
|
}
|
|
538
583
|
/**
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
584
|
+
* Get the last element without removing it.
|
|
585
|
+
* @remarks Time O(1), Space O(1)
|
|
586
|
+
* @returns Last element or undefined.
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
* @example
|
|
599
|
+
* // Peek at the back element
|
|
600
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
601
|
+
* console.log(dq.last); // 'c';
|
|
602
|
+
* console.log(dq.first); // 'a';
|
|
603
|
+
*/
|
|
543
604
|
get last() {
|
|
544
605
|
if (this._length === 0) return;
|
|
545
606
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -558,11 +619,40 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
558
619
|
return new this(data, options);
|
|
559
620
|
}
|
|
560
621
|
/**
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
622
|
+
* Append one element at the back.
|
|
623
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
624
|
+
* @param element - Element to append.
|
|
625
|
+
* @returns True when appended.
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
* @example
|
|
638
|
+
* // basic Deque creation and push/pop operations
|
|
639
|
+
* // Create a simple Deque with initial values
|
|
640
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
641
|
+
*
|
|
642
|
+
* // Verify the deque maintains insertion order
|
|
643
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
644
|
+
*
|
|
645
|
+
* // Check length
|
|
646
|
+
* console.log(deque.length); // 5;
|
|
647
|
+
*
|
|
648
|
+
* // Push to the end
|
|
649
|
+
* deque.push(6);
|
|
650
|
+
* console.log(deque.length); // 6;
|
|
651
|
+
*
|
|
652
|
+
* // Pop from the end
|
|
653
|
+
* const last = deque.pop();
|
|
654
|
+
* console.log(last); // 6;
|
|
655
|
+
*/
|
|
566
656
|
push(element) {
|
|
567
657
|
if (this._length) {
|
|
568
658
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -582,10 +672,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
582
672
|
return true;
|
|
583
673
|
}
|
|
584
674
|
/**
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
675
|
+
* Remove and return the last element.
|
|
676
|
+
* @remarks Time O(1), Space O(1)
|
|
677
|
+
* @returns Removed element or undefined.
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
* @example
|
|
690
|
+
* // Remove from the back
|
|
691
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
692
|
+
* console.log(dq.pop()); // 3;
|
|
693
|
+
* console.log(dq.length); // 2;
|
|
694
|
+
*/
|
|
589
695
|
pop() {
|
|
590
696
|
if (this._length === 0) return;
|
|
591
697
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -601,13 +707,30 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
601
707
|
}
|
|
602
708
|
}
|
|
603
709
|
this._length -= 1;
|
|
710
|
+
this._autoCompact();
|
|
604
711
|
return element;
|
|
605
712
|
}
|
|
606
713
|
/**
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
714
|
+
* Remove and return the first element.
|
|
715
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
716
|
+
* @returns Removed element or undefined.
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
* @example
|
|
729
|
+
* // Remove from the front
|
|
730
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
731
|
+
* console.log(dq.shift()); // 1;
|
|
732
|
+
* console.log(dq.length); // 2;
|
|
733
|
+
*/
|
|
611
734
|
shift() {
|
|
612
735
|
if (this._length === 0) return;
|
|
613
736
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -623,14 +746,41 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
623
746
|
}
|
|
624
747
|
}
|
|
625
748
|
this._length -= 1;
|
|
749
|
+
this._autoCompact();
|
|
626
750
|
return element;
|
|
627
751
|
}
|
|
628
752
|
/**
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
753
|
+
* Prepend one element at the front.
|
|
754
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
755
|
+
* @param element - Element to prepend.
|
|
756
|
+
* @returns True when prepended.
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
* @example
|
|
769
|
+
* // Deque shift and unshift operations
|
|
770
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
771
|
+
*
|
|
772
|
+
* // Unshift adds to the front
|
|
773
|
+
* deque.unshift(10);
|
|
774
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
775
|
+
*
|
|
776
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
777
|
+
* const first = deque.shift();
|
|
778
|
+
* console.log(first); // 10;
|
|
779
|
+
*
|
|
780
|
+
* // Verify remaining elements
|
|
781
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
782
|
+
* console.log(deque.length); // 3;
|
|
783
|
+
*/
|
|
634
784
|
unshift(element) {
|
|
635
785
|
if (this._length) {
|
|
636
786
|
if (this._firstInBucket > 0) {
|
|
@@ -684,18 +834,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
684
834
|
return ans;
|
|
685
835
|
}
|
|
686
836
|
/**
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
837
|
+
* Check whether the deque is empty.
|
|
838
|
+
* @remarks Time O(1), Space O(1)
|
|
839
|
+
* @returns True if length is 0.
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
* @example
|
|
850
|
+
* // Check if empty
|
|
851
|
+
* const dq = new Deque();
|
|
852
|
+
* console.log(dq.isEmpty()); // true;
|
|
853
|
+
*/
|
|
691
854
|
isEmpty() {
|
|
692
855
|
return this._length === 0;
|
|
693
856
|
}
|
|
694
857
|
/**
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
858
|
+
* Remove all elements and reset structure.
|
|
859
|
+
* @remarks Time O(1), Space O(1)
|
|
860
|
+
* @returns void
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
* @example
|
|
871
|
+
* // Remove all elements
|
|
872
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
873
|
+
* dq.clear();
|
|
874
|
+
* console.log(dq.length); // 0;
|
|
875
|
+
*/
|
|
699
876
|
clear() {
|
|
700
877
|
this._buckets = [new Array(this._bucketSize)];
|
|
701
878
|
this._bucketCount = 1;
|
|
@@ -703,11 +880,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
703
880
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
704
881
|
}
|
|
705
882
|
/**
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
883
|
+
* Get the element at a given position.
|
|
884
|
+
* @remarks Time O(1), Space O(1)
|
|
885
|
+
* @param pos - Zero-based position from the front.
|
|
886
|
+
* @returns Element or undefined.
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
* @example
|
|
896
|
+
* // Access by index
|
|
897
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
898
|
+
* console.log(dq.at(0)); // 'a';
|
|
899
|
+
* console.log(dq.at(2)); // 'c';
|
|
900
|
+
*/
|
|
711
901
|
at(pos) {
|
|
712
902
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
713
903
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -866,11 +1056,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
866
1056
|
}
|
|
867
1057
|
}
|
|
868
1058
|
/**
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
1059
|
+
* Delete the first occurrence of a value.
|
|
1060
|
+
* @remarks Time O(N), Space O(1)
|
|
1061
|
+
* @param element - Element to remove (using the configured equality).
|
|
1062
|
+
* @returns True if an element was removed.
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
* @example
|
|
1072
|
+
* // Remove element
|
|
1073
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1074
|
+
* dq.delete(2);
|
|
1075
|
+
* console.log(dq.length); // 2;
|
|
1076
|
+
*/
|
|
874
1077
|
delete(element) {
|
|
875
1078
|
const size = this._length;
|
|
876
1079
|
if (size === 0) return false;
|
|
@@ -914,10 +1117,39 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
914
1117
|
return this;
|
|
915
1118
|
}
|
|
916
1119
|
/**
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1120
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
1121
|
+
* @remarks Time O(N), Space O(N)
|
|
1122
|
+
* @returns This deque.
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
* @example
|
|
1135
|
+
* // Deque for...of iteration and reverse
|
|
1136
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1137
|
+
*
|
|
1138
|
+
* // Iterate forward
|
|
1139
|
+
* const forward: string[] = [];
|
|
1140
|
+
* for (const item of deque) {
|
|
1141
|
+
* forward.push(item);
|
|
1142
|
+
* }
|
|
1143
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1144
|
+
*
|
|
1145
|
+
* // Reverse the deque
|
|
1146
|
+
* deque.reverse();
|
|
1147
|
+
* const backward: string[] = [];
|
|
1148
|
+
* for (const item of deque) {
|
|
1149
|
+
* backward.push(item);
|
|
1150
|
+
* }
|
|
1151
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1152
|
+
*/
|
|
921
1153
|
reverse() {
|
|
922
1154
|
this._buckets.reverse().forEach(function(bucket) {
|
|
923
1155
|
bucket.reverse();
|
|
@@ -955,11 +1187,55 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
955
1187
|
* @remarks Time O(N), Space O(1)
|
|
956
1188
|
* @returns void
|
|
957
1189
|
*/
|
|
1190
|
+
/**
|
|
1191
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
1192
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
1193
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
1194
|
+
*/
|
|
1195
|
+
_autoCompact() {
|
|
1196
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
1197
|
+
this._compactCounter++;
|
|
1198
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
1199
|
+
this._compactCounter = 0;
|
|
1200
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
1201
|
+
if (utilization < this._autoCompactRatio) {
|
|
1202
|
+
this.shrinkToFit();
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Compact the deque by removing unused buckets.
|
|
1207
|
+
* @remarks Time O(N), Space O(1)
|
|
1208
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
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
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
* @example
|
|
1223
|
+
* // Reclaim memory
|
|
1224
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1225
|
+
* dq.shift();
|
|
1226
|
+
* dq.shift();
|
|
1227
|
+
* dq.compact();
|
|
1228
|
+
* console.log(dq.length); // 3;
|
|
1229
|
+
*/
|
|
1230
|
+
compact() {
|
|
1231
|
+
const before = this._bucketCount;
|
|
1232
|
+
this.shrinkToFit();
|
|
1233
|
+
return this._bucketCount < before;
|
|
1234
|
+
}
|
|
958
1235
|
shrinkToFit() {
|
|
959
1236
|
if (this._length === 0) return;
|
|
960
1237
|
const newBuckets = [];
|
|
961
|
-
if (this._bucketFirst
|
|
962
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
1238
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
963
1239
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
964
1240
|
newBuckets.push(this._buckets[i]);
|
|
965
1241
|
}
|
|
@@ -974,12 +1250,30 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
974
1250
|
this._bucketFirst = 0;
|
|
975
1251
|
this._bucketLast = newBuckets.length - 1;
|
|
976
1252
|
this._buckets = newBuckets;
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1253
|
+
this._bucketCount = newBuckets.length;
|
|
1254
|
+
this._compactCounter = 0;
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Deep clone this deque, preserving options.
|
|
1258
|
+
* @remarks Time O(N), Space O(N)
|
|
1259
|
+
* @returns A new deque with the same content and options.
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
* @example
|
|
1270
|
+
* // Create independent copy
|
|
1271
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1272
|
+
* const copy = dq.clone();
|
|
1273
|
+
* copy.pop();
|
|
1274
|
+
* console.log(dq.length); // 3;
|
|
1275
|
+
* console.log(copy.length); // 2;
|
|
1276
|
+
*/
|
|
983
1277
|
clone() {
|
|
984
1278
|
return this._createLike(this, {
|
|
985
1279
|
bucketSize: this.bucketSize,
|
|
@@ -988,12 +1282,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
988
1282
|
});
|
|
989
1283
|
}
|
|
990
1284
|
/**
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1285
|
+
* Filter elements into a new deque of the same class.
|
|
1286
|
+
* @remarks Time O(N), Space O(N)
|
|
1287
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1288
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1289
|
+
* @returns A new deque with kept elements.
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
* @example
|
|
1300
|
+
* // Filter elements
|
|
1301
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1302
|
+
* const result = dq.filter(x => x > 2);
|
|
1303
|
+
* console.log(result.length); // 2;
|
|
1304
|
+
*/
|
|
997
1305
|
filter(predicate, thisArg) {
|
|
998
1306
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
999
1307
|
out._setBucketSize(this._bucketSize);
|
|
@@ -1022,15 +1330,28 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1022
1330
|
return out;
|
|
1023
1331
|
}
|
|
1024
1332
|
/**
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1333
|
+
* Map elements into a new deque (possibly different element type).
|
|
1334
|
+
* @remarks Time O(N), Space O(N)
|
|
1335
|
+
* @template EM
|
|
1336
|
+
* @template RM
|
|
1337
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1338
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1339
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1340
|
+
* @returns A new Deque with mapped elements.
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
* @example
|
|
1350
|
+
* // Transform elements
|
|
1351
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1352
|
+
* const result = dq.map(x => x * 10);
|
|
1353
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
1354
|
+
*/
|
|
1034
1355
|
map(callback, options, thisArg) {
|
|
1035
1356
|
const out = this._createLike([], {
|
|
1036
1357
|
...options != null ? options : {},
|
|
@@ -1156,6 +1477,31 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1156
1477
|
__name(_Deque, "Deque");
|
|
1157
1478
|
var Deque = _Deque;
|
|
1158
1479
|
|
|
1480
|
+
// src/common/error.ts
|
|
1481
|
+
var ERR = {
|
|
1482
|
+
// Range / index
|
|
1483
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1484
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1485
|
+
// Type / argument
|
|
1486
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1487
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1488
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1489
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1490
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1491
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1492
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1493
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1494
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1495
|
+
// State / operation
|
|
1496
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1497
|
+
// Matrix
|
|
1498
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1499
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1500
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1501
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1502
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1503
|
+
};
|
|
1504
|
+
|
|
1159
1505
|
// src/common/index.ts
|
|
1160
1506
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1161
1507
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
@@ -1186,6 +1532,6 @@ var Range = _Range;
|
|
|
1186
1532
|
* @license MIT License
|
|
1187
1533
|
*/
|
|
1188
1534
|
|
|
1189
|
-
export { DFSOperation, Deque, Range };
|
|
1535
|
+
export { DFSOperation, Deque, ERR, Range };
|
|
1190
1536
|
//# sourceMappingURL=index.mjs.map
|
|
1191
1537
|
//# sourceMappingURL=index.mjs.map
|