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