directed-graph-typed 2.4.5 → 2.5.1
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 +0 -51
- package/dist/cjs/index.cjs +1819 -356
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1818 -355
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1819 -356
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1818 -355
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/directed-graph-typed.js +1816 -353
- package/dist/umd/directed-graph-typed.js.map +1 -1
- package/dist/umd/directed-graph-typed.min.js +3 -3
- package/dist/umd/directed-graph-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -302,7 +302,7 @@ var directedGraphTyped = (() => {
|
|
|
302
302
|
if (options) {
|
|
303
303
|
const { toElementFn } = options;
|
|
304
304
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
305
|
-
else if (toElementFn) throw new TypeError(
|
|
305
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
306
306
|
}
|
|
307
307
|
}
|
|
308
308
|
/**
|
|
@@ -458,7 +458,7 @@ var directedGraphTyped = (() => {
|
|
|
458
458
|
acc = initialValue;
|
|
459
459
|
} else {
|
|
460
460
|
const first = iter.next();
|
|
461
|
-
if (first.done) throw new TypeError(
|
|
461
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
462
462
|
acc = first.value;
|
|
463
463
|
index = 1;
|
|
464
464
|
}
|
|
@@ -500,6 +500,196 @@ var directedGraphTyped = (() => {
|
|
|
500
500
|
}
|
|
501
501
|
};
|
|
502
502
|
|
|
503
|
+
// src/data-structures/base/linear-base.ts
|
|
504
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
505
|
+
/**
|
|
506
|
+
* Construct a linear container with runtime options.
|
|
507
|
+
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
508
|
+
* @remarks Time O(1), Space O(1)
|
|
509
|
+
*/
|
|
510
|
+
constructor(options) {
|
|
511
|
+
super(options);
|
|
512
|
+
__publicField(this, "_maxLen", -1);
|
|
513
|
+
if (options) {
|
|
514
|
+
const { maxLen } = options;
|
|
515
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
520
|
+
* @returns Maximum allowed length.
|
|
521
|
+
* @remarks Time O(1), Space O(1)
|
|
522
|
+
*/
|
|
523
|
+
get maxLen() {
|
|
524
|
+
return this._maxLen;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* First index of a value from the left.
|
|
528
|
+
* @param searchElement - Value to match.
|
|
529
|
+
* @param fromIndex - Start position (supports negative index).
|
|
530
|
+
* @returns Index or `-1` if not found.
|
|
531
|
+
* @remarks Time O(n), Space O(1)
|
|
532
|
+
*/
|
|
533
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
534
|
+
if (this.length === 0) return -1;
|
|
535
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
536
|
+
if (fromIndex < 0) fromIndex = 0;
|
|
537
|
+
for (let i = fromIndex; i < this.length; i++) {
|
|
538
|
+
const element = this.at(i);
|
|
539
|
+
if (element === searchElement) return i;
|
|
540
|
+
}
|
|
541
|
+
return -1;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Last index of a value from the right.
|
|
545
|
+
* @param searchElement - Value to match.
|
|
546
|
+
* @param fromIndex - Start position (supports negative index).
|
|
547
|
+
* @returns Index or `-1` if not found.
|
|
548
|
+
* @remarks Time O(n), Space O(1)
|
|
549
|
+
*/
|
|
550
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
551
|
+
if (this.length === 0) return -1;
|
|
552
|
+
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
553
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
554
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
555
|
+
const element = this.at(i);
|
|
556
|
+
if (element === searchElement) return i;
|
|
557
|
+
}
|
|
558
|
+
return -1;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Find the first index matching a predicate.
|
|
562
|
+
* @param predicate - `(element, index, self) => boolean`.
|
|
563
|
+
* @param thisArg - Optional `this` for callback.
|
|
564
|
+
* @returns Index or `-1`.
|
|
565
|
+
* @remarks Time O(n), Space O(1)
|
|
566
|
+
*/
|
|
567
|
+
findIndex(predicate, thisArg) {
|
|
568
|
+
for (let i = 0; i < this.length; i++) {
|
|
569
|
+
const item = this.at(i);
|
|
570
|
+
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
571
|
+
}
|
|
572
|
+
return -1;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Concatenate elements and/or containers.
|
|
576
|
+
* @param items - Elements or other containers.
|
|
577
|
+
* @returns New container with combined elements (`this` type).
|
|
578
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
579
|
+
*/
|
|
580
|
+
concat(...items) {
|
|
581
|
+
const newList = this.clone();
|
|
582
|
+
for (const item of items) {
|
|
583
|
+
if (item instanceof _LinearBase) {
|
|
584
|
+
newList.pushMany(item);
|
|
585
|
+
} else {
|
|
586
|
+
newList.push(item);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return newList;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* In-place stable order via array sort semantics.
|
|
593
|
+
* @param compareFn - Comparator `(a, b) => number`.
|
|
594
|
+
* @returns This container.
|
|
595
|
+
* @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
|
|
596
|
+
*/
|
|
597
|
+
sort(compareFn) {
|
|
598
|
+
const arr = this.toArray();
|
|
599
|
+
arr.sort(compareFn);
|
|
600
|
+
this.clear();
|
|
601
|
+
for (const item of arr) this.push(item);
|
|
602
|
+
return this;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Remove and/or insert elements at a position (array-compatible).
|
|
606
|
+
* @param start - Start index (supports negative index).
|
|
607
|
+
* @param deleteCount - How many to remove.
|
|
608
|
+
* @param items - Elements to insert.
|
|
609
|
+
* @returns Removed elements as a new list (`this` type).
|
|
610
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
611
|
+
*/
|
|
612
|
+
splice(start, deleteCount = 0, ...items) {
|
|
613
|
+
const removedList = this._createInstance();
|
|
614
|
+
start = start < 0 ? this.length + start : start;
|
|
615
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
616
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
617
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
618
|
+
const removed = this.deleteAt(start);
|
|
619
|
+
if (removed !== void 0) {
|
|
620
|
+
removedList.push(removed);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
for (let i = 0; i < items.length; i++) {
|
|
624
|
+
this.addAt(start + i, items[i]);
|
|
625
|
+
}
|
|
626
|
+
return removedList;
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Join all elements into a string.
|
|
630
|
+
* @param separator - Separator string.
|
|
631
|
+
* @returns Concatenated string.
|
|
632
|
+
* @remarks Time O(n), Space O(n)
|
|
633
|
+
*/
|
|
634
|
+
join(separator = ",") {
|
|
635
|
+
return this.toArray().join(separator);
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Snapshot elements into a reversed array.
|
|
639
|
+
* @returns New reversed array.
|
|
640
|
+
* @remarks Time O(n), Space O(n)
|
|
641
|
+
*/
|
|
642
|
+
toReversedArray() {
|
|
643
|
+
const array = [];
|
|
644
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
645
|
+
array.push(this.at(i));
|
|
646
|
+
}
|
|
647
|
+
return array;
|
|
648
|
+
}
|
|
649
|
+
reduceRight(callbackfn, initialValue) {
|
|
650
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
651
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
652
|
+
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
653
|
+
}
|
|
654
|
+
return accumulator;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Create a shallow copy of a subrange.
|
|
658
|
+
* @param start - Inclusive start (supports negative index).
|
|
659
|
+
* @param end - Exclusive end (supports negative index).
|
|
660
|
+
* @returns New list with the range (`this` type).
|
|
661
|
+
* @remarks Time O(n), Space O(n)
|
|
662
|
+
*/
|
|
663
|
+
slice(start = 0, end = this.length) {
|
|
664
|
+
start = start < 0 ? this.length + start : start;
|
|
665
|
+
end = end < 0 ? this.length + end : end;
|
|
666
|
+
const newList = this._createInstance();
|
|
667
|
+
for (let i = start; i < end; i++) {
|
|
668
|
+
newList.push(this.at(i));
|
|
669
|
+
}
|
|
670
|
+
return newList;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Fill a range with a value.
|
|
674
|
+
* @param value - Value to set.
|
|
675
|
+
* @param start - Inclusive start.
|
|
676
|
+
* @param end - Exclusive end.
|
|
677
|
+
* @returns This list.
|
|
678
|
+
* @remarks Time O(n), Space O(1)
|
|
679
|
+
*/
|
|
680
|
+
fill(value, start = 0, end = this.length) {
|
|
681
|
+
start = start < 0 ? this.length + start : start;
|
|
682
|
+
end = end < 0 ? this.length + end : end;
|
|
683
|
+
if (start < 0) start = 0;
|
|
684
|
+
if (end > this.length) end = this.length;
|
|
685
|
+
if (start >= end) return this;
|
|
686
|
+
for (let i = start; i < end; i++) {
|
|
687
|
+
this.setAt(i, value);
|
|
688
|
+
}
|
|
689
|
+
return this;
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
|
|
503
693
|
// src/data-structures/heap/heap.ts
|
|
504
694
|
var Heap = class _Heap extends IterableElementBase {
|
|
505
695
|
/**
|
|
@@ -537,10 +727,51 @@ var directedGraphTyped = (() => {
|
|
|
537
727
|
return this._elements;
|
|
538
728
|
}
|
|
539
729
|
/**
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
730
|
+
* Get the number of elements.
|
|
731
|
+
* @remarks Time O(1), Space O(1)
|
|
732
|
+
* @returns Heap size.
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
* @example
|
|
766
|
+
* // Track heap capacity
|
|
767
|
+
* const heap = new Heap<number>();
|
|
768
|
+
* console.log(heap.size); // 0;
|
|
769
|
+
* heap.add(10);
|
|
770
|
+
* heap.add(20);
|
|
771
|
+
* console.log(heap.size); // 2;
|
|
772
|
+
* heap.poll();
|
|
773
|
+
* console.log(heap.size); // 1;
|
|
774
|
+
*/
|
|
544
775
|
get size() {
|
|
545
776
|
return this.elements.length;
|
|
546
777
|
}
|
|
@@ -579,21 +810,103 @@ var directedGraphTyped = (() => {
|
|
|
579
810
|
return new _Heap(elements, options);
|
|
580
811
|
}
|
|
581
812
|
/**
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
813
|
+
* Insert an element.
|
|
814
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
815
|
+
* @param element - Element to insert.
|
|
816
|
+
* @returns True.
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
* @example
|
|
850
|
+
* // basic Heap creation and add operation
|
|
851
|
+
* // Create a min heap (default)
|
|
852
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
853
|
+
*
|
|
854
|
+
* // Verify size
|
|
855
|
+
* console.log(minHeap.size); // 6;
|
|
856
|
+
*
|
|
857
|
+
* // Add new element
|
|
858
|
+
* minHeap.add(4);
|
|
859
|
+
* console.log(minHeap.size); // 7;
|
|
860
|
+
*
|
|
861
|
+
* // Min heap property: smallest element at root
|
|
862
|
+
* const min = minHeap.peek();
|
|
863
|
+
* console.log(min); // 1;
|
|
864
|
+
*/
|
|
587
865
|
add(element) {
|
|
588
866
|
this._elements.push(element);
|
|
589
867
|
return this._bubbleUp(this.elements.length - 1);
|
|
590
868
|
}
|
|
591
869
|
/**
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
870
|
+
* Insert many elements from an iterable.
|
|
871
|
+
* @remarks Time O(N log N), Space O(1)
|
|
872
|
+
* @param elements - Iterable of elements or raw values.
|
|
873
|
+
* @returns Array of per-element success flags.
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
* @example
|
|
904
|
+
* // Add multiple elements
|
|
905
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
906
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
907
|
+
* console.log(heap.peek()); // 1;
|
|
908
|
+
* console.log(heap.size); // 4;
|
|
909
|
+
*/
|
|
597
910
|
addMany(elements) {
|
|
598
911
|
const flags = [];
|
|
599
912
|
for (const el of elements) {
|
|
@@ -608,10 +921,67 @@ var directedGraphTyped = (() => {
|
|
|
608
921
|
return flags;
|
|
609
922
|
}
|
|
610
923
|
/**
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
924
|
+
* Remove and return the top element.
|
|
925
|
+
* @remarks Time O(log N), Space O(1)
|
|
926
|
+
* @returns Top element or undefined.
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
* @example
|
|
960
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
961
|
+
* interface Task {
|
|
962
|
+
* id: number;
|
|
963
|
+
* priority: number;
|
|
964
|
+
* name: string;
|
|
965
|
+
* }
|
|
966
|
+
*
|
|
967
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
968
|
+
* const tasks: Task[] = [
|
|
969
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
970
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
971
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
972
|
+
* ];
|
|
973
|
+
*
|
|
974
|
+
* const maxHeap = new Heap(tasks, {
|
|
975
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
976
|
+
* });
|
|
977
|
+
*
|
|
978
|
+
* console.log(maxHeap.size); // 3;
|
|
979
|
+
*
|
|
980
|
+
* // Peek returns highest priority task
|
|
981
|
+
* const topTask = maxHeap.peek();
|
|
982
|
+
* console.log(topTask?.priority); // 8;
|
|
983
|
+
* console.log(topTask?.name); // 'Alert';
|
|
984
|
+
*/
|
|
615
985
|
poll() {
|
|
616
986
|
if (this.elements.length === 0) return;
|
|
617
987
|
const value = this.elements[0];
|
|
@@ -623,26 +993,188 @@ var directedGraphTyped = (() => {
|
|
|
623
993
|
return value;
|
|
624
994
|
}
|
|
625
995
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
996
|
+
* Get the current top element without removing it.
|
|
997
|
+
* @remarks Time O(1), Space O(1)
|
|
998
|
+
* @returns Top element or undefined.
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
* @example
|
|
1032
|
+
* // Heap for event processing with priority
|
|
1033
|
+
* interface Event {
|
|
1034
|
+
* id: number;
|
|
1035
|
+
* type: 'critical' | 'warning' | 'info';
|
|
1036
|
+
* timestamp: number;
|
|
1037
|
+
* message: string;
|
|
1038
|
+
* }
|
|
1039
|
+
*
|
|
1040
|
+
* // Custom priority: critical > warning > info
|
|
1041
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
1042
|
+
*
|
|
1043
|
+
* const eventHeap = new Heap<Event>([], {
|
|
1044
|
+
* comparator: (a: Event, b: Event) => {
|
|
1045
|
+
* const priorityA = priorityMap[a.type];
|
|
1046
|
+
* const priorityB = priorityMap[b.type];
|
|
1047
|
+
* return priorityB - priorityA; // Higher priority first
|
|
1048
|
+
* }
|
|
1049
|
+
* });
|
|
1050
|
+
*
|
|
1051
|
+
* // Add events in random order
|
|
1052
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
1053
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
1054
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
1055
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
1056
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
1057
|
+
*
|
|
1058
|
+
* console.log(eventHeap.size); // 5;
|
|
1059
|
+
*
|
|
1060
|
+
* // Process events by priority (critical first)
|
|
1061
|
+
* const processedOrder: Event[] = [];
|
|
1062
|
+
* while (eventHeap.size > 0) {
|
|
1063
|
+
* const event = eventHeap.poll();
|
|
1064
|
+
* if (event) {
|
|
1065
|
+
* processedOrder.push(event);
|
|
1066
|
+
* }
|
|
1067
|
+
* }
|
|
1068
|
+
*
|
|
1069
|
+
* // Verify critical events came first
|
|
1070
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
1071
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
1072
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
1073
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
1074
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
1075
|
+
*
|
|
1076
|
+
* // Verify O(log n) operations
|
|
1077
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
1078
|
+
*
|
|
1079
|
+
* // Add - O(log n)
|
|
1080
|
+
* newHeap.add(2);
|
|
1081
|
+
* console.log(newHeap.size); // 5;
|
|
1082
|
+
*
|
|
1083
|
+
* // Poll - O(log n)
|
|
1084
|
+
* const removed = newHeap.poll();
|
|
1085
|
+
* console.log(removed); // 1;
|
|
1086
|
+
*
|
|
1087
|
+
* // Peek - O(1)
|
|
1088
|
+
* const top = newHeap.peek();
|
|
1089
|
+
* console.log(top); // 2;
|
|
1090
|
+
*/
|
|
630
1091
|
peek() {
|
|
631
1092
|
return this.elements[0];
|
|
632
1093
|
}
|
|
633
1094
|
/**
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
1095
|
+
* Check whether the heap is empty.
|
|
1096
|
+
* @remarks Time O(1), Space O(1)
|
|
1097
|
+
* @returns True if size is 0.
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
* @example
|
|
1129
|
+
* // Check if heap is empty
|
|
1130
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
1131
|
+
* console.log(heap.isEmpty()); // true;
|
|
1132
|
+
* heap.add(1);
|
|
1133
|
+
* console.log(heap.isEmpty()); // false;
|
|
1134
|
+
*/
|
|
638
1135
|
isEmpty() {
|
|
639
1136
|
return this.size === 0;
|
|
640
1137
|
}
|
|
641
1138
|
/**
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
1139
|
+
* Remove all elements.
|
|
1140
|
+
* @remarks Time O(1), Space O(1)
|
|
1141
|
+
* @returns void
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
* @example
|
|
1173
|
+
* // Remove all elements
|
|
1174
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1175
|
+
* heap.clear();
|
|
1176
|
+
* console.log(heap.isEmpty()); // true;
|
|
1177
|
+
*/
|
|
646
1178
|
clear() {
|
|
647
1179
|
this._elements = [];
|
|
648
1180
|
}
|
|
@@ -657,21 +1189,83 @@ var directedGraphTyped = (() => {
|
|
|
657
1189
|
return this.fix();
|
|
658
1190
|
}
|
|
659
1191
|
/**
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
1192
|
+
* Check if an equal element exists in the heap.
|
|
1193
|
+
* @remarks Time O(N), Space O(1)
|
|
1194
|
+
* @param element - Element to search for.
|
|
1195
|
+
* @returns True if found.
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
* @example
|
|
1220
|
+
* // Check element existence
|
|
1221
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1222
|
+
* console.log(heap.has(1)); // true;
|
|
1223
|
+
* console.log(heap.has(99)); // false;
|
|
1224
|
+
*/
|
|
665
1225
|
has(element) {
|
|
666
1226
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
667
1227
|
return false;
|
|
668
1228
|
}
|
|
669
1229
|
/**
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
1230
|
+
* Delete one occurrence of an element.
|
|
1231
|
+
* @remarks Time O(N), Space O(1)
|
|
1232
|
+
* @param element - Element to delete.
|
|
1233
|
+
* @returns True if an element was removed.
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
* @example
|
|
1264
|
+
* // Remove specific element
|
|
1265
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1266
|
+
* heap.delete(4);
|
|
1267
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
1268
|
+
*/
|
|
675
1269
|
delete(element) {
|
|
676
1270
|
let index = -1;
|
|
677
1271
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -729,11 +1323,39 @@ var directedGraphTyped = (() => {
|
|
|
729
1323
|
return this;
|
|
730
1324
|
}
|
|
731
1325
|
/**
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
1326
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
1327
|
+
* @remarks Time O(N), Space O(H)
|
|
1328
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
1329
|
+
* @returns Array of visited elements.
|
|
1330
|
+
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
* @example
|
|
1354
|
+
* // Depth-first traversal
|
|
1355
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1356
|
+
* const result = heap.dfs('IN');
|
|
1357
|
+
* console.log(result.length); // 3;
|
|
1358
|
+
*/
|
|
737
1359
|
dfs(order = "PRE") {
|
|
738
1360
|
const result = [];
|
|
739
1361
|
const _dfs = (index) => {
|
|
@@ -770,10 +1392,47 @@ var directedGraphTyped = (() => {
|
|
|
770
1392
|
return results;
|
|
771
1393
|
}
|
|
772
1394
|
/**
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
1395
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
1396
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1397
|
+
* @returns Sorted array of elements.
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
|
|
1405
|
+
|
|
1406
|
+
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
* @example
|
|
1431
|
+
* // Sort elements using heap
|
|
1432
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1433
|
+
* const sorted = heap.sort();
|
|
1434
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1435
|
+
*/
|
|
777
1436
|
sort() {
|
|
778
1437
|
const visited = [];
|
|
779
1438
|
const cloned = this._createInstance();
|
|
@@ -785,22 +1444,94 @@ var directedGraphTyped = (() => {
|
|
|
785
1444
|
return visited;
|
|
786
1445
|
}
|
|
787
1446
|
/**
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1447
|
+
* Deep clone this heap.
|
|
1448
|
+
* @remarks Time O(N), Space O(N)
|
|
1449
|
+
* @returns A new heap with the same elements.
|
|
1450
|
+
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
|
|
1476
|
+
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
|
|
1480
|
+
* @example
|
|
1481
|
+
* // Create independent copy
|
|
1482
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1483
|
+
* const copy = heap.clone();
|
|
1484
|
+
* copy.poll();
|
|
1485
|
+
* console.log(heap.size); // 3;
|
|
1486
|
+
* console.log(copy.size); // 2;
|
|
1487
|
+
*/
|
|
792
1488
|
clone() {
|
|
793
1489
|
const next = this._createInstance();
|
|
794
1490
|
for (const x of this.elements) next.add(x);
|
|
795
1491
|
return next;
|
|
796
1492
|
}
|
|
797
1493
|
/**
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
1494
|
+
* Filter elements into a new heap of the same class.
|
|
1495
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1496
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1497
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1498
|
+
* @returns A new heap with the kept elements.
|
|
1499
|
+
|
|
1500
|
+
|
|
1501
|
+
|
|
1502
|
+
|
|
1503
|
+
|
|
1504
|
+
|
|
1505
|
+
|
|
1506
|
+
|
|
1507
|
+
|
|
1508
|
+
|
|
1509
|
+
|
|
1510
|
+
|
|
1511
|
+
|
|
1512
|
+
|
|
1513
|
+
|
|
1514
|
+
|
|
1515
|
+
|
|
1516
|
+
|
|
1517
|
+
|
|
1518
|
+
|
|
1519
|
+
|
|
1520
|
+
|
|
1521
|
+
|
|
1522
|
+
|
|
1523
|
+
|
|
1524
|
+
|
|
1525
|
+
|
|
1526
|
+
|
|
1527
|
+
|
|
1528
|
+
|
|
1529
|
+
* @example
|
|
1530
|
+
* // Filter elements
|
|
1531
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1532
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1533
|
+
* console.log(evens.size); // 2;
|
|
1534
|
+
*/
|
|
804
1535
|
filter(callback, thisArg) {
|
|
805
1536
|
const out = this._createInstance();
|
|
806
1537
|
let i = 0;
|
|
@@ -814,15 +1545,49 @@ var directedGraphTyped = (() => {
|
|
|
814
1545
|
return out;
|
|
815
1546
|
}
|
|
816
1547
|
/**
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
1548
|
+
* Map elements into a new heap of possibly different element type.
|
|
1549
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1550
|
+
* @template EM
|
|
1551
|
+
* @template RM
|
|
1552
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1553
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1554
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1555
|
+
* @returns A new heap with mapped elements.
|
|
1556
|
+
|
|
1557
|
+
|
|
1558
|
+
|
|
1559
|
+
|
|
1560
|
+
|
|
1561
|
+
|
|
1562
|
+
|
|
1563
|
+
|
|
1564
|
+
|
|
1565
|
+
|
|
1566
|
+
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
|
|
1574
|
+
|
|
1575
|
+
|
|
1576
|
+
|
|
1577
|
+
|
|
1578
|
+
|
|
1579
|
+
|
|
1580
|
+
|
|
1581
|
+
|
|
1582
|
+
|
|
1583
|
+
|
|
1584
|
+
|
|
1585
|
+
* @example
|
|
1586
|
+
* // Transform elements
|
|
1587
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1588
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1589
|
+
* console.log(doubled.peek()); // 2;
|
|
1590
|
+
*/
|
|
826
1591
|
map(callback, options, thisArg) {
|
|
827
1592
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
828
1593
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|
|
@@ -926,196 +1691,6 @@ var directedGraphTyped = (() => {
|
|
|
926
1691
|
}
|
|
927
1692
|
};
|
|
928
1693
|
|
|
929
|
-
// src/data-structures/base/linear-base.ts
|
|
930
|
-
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
931
|
-
/**
|
|
932
|
-
* Construct a linear container with runtime options.
|
|
933
|
-
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
934
|
-
* @remarks Time O(1), Space O(1)
|
|
935
|
-
*/
|
|
936
|
-
constructor(options) {
|
|
937
|
-
super(options);
|
|
938
|
-
__publicField(this, "_maxLen", -1);
|
|
939
|
-
if (options) {
|
|
940
|
-
const { maxLen } = options;
|
|
941
|
-
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
/**
|
|
945
|
-
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
946
|
-
* @returns Maximum allowed length.
|
|
947
|
-
* @remarks Time O(1), Space O(1)
|
|
948
|
-
*/
|
|
949
|
-
get maxLen() {
|
|
950
|
-
return this._maxLen;
|
|
951
|
-
}
|
|
952
|
-
/**
|
|
953
|
-
* First index of a value from the left.
|
|
954
|
-
* @param searchElement - Value to match.
|
|
955
|
-
* @param fromIndex - Start position (supports negative index).
|
|
956
|
-
* @returns Index or `-1` if not found.
|
|
957
|
-
* @remarks Time O(n), Space O(1)
|
|
958
|
-
*/
|
|
959
|
-
indexOf(searchElement, fromIndex = 0) {
|
|
960
|
-
if (this.length === 0) return -1;
|
|
961
|
-
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
962
|
-
if (fromIndex < 0) fromIndex = 0;
|
|
963
|
-
for (let i = fromIndex; i < this.length; i++) {
|
|
964
|
-
const element = this.at(i);
|
|
965
|
-
if (element === searchElement) return i;
|
|
966
|
-
}
|
|
967
|
-
return -1;
|
|
968
|
-
}
|
|
969
|
-
/**
|
|
970
|
-
* Last index of a value from the right.
|
|
971
|
-
* @param searchElement - Value to match.
|
|
972
|
-
* @param fromIndex - Start position (supports negative index).
|
|
973
|
-
* @returns Index or `-1` if not found.
|
|
974
|
-
* @remarks Time O(n), Space O(1)
|
|
975
|
-
*/
|
|
976
|
-
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
977
|
-
if (this.length === 0) return -1;
|
|
978
|
-
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
979
|
-
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
980
|
-
for (let i = fromIndex; i >= 0; i--) {
|
|
981
|
-
const element = this.at(i);
|
|
982
|
-
if (element === searchElement) return i;
|
|
983
|
-
}
|
|
984
|
-
return -1;
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
* Find the first index matching a predicate.
|
|
988
|
-
* @param predicate - `(element, index, self) => boolean`.
|
|
989
|
-
* @param thisArg - Optional `this` for callback.
|
|
990
|
-
* @returns Index or `-1`.
|
|
991
|
-
* @remarks Time O(n), Space O(1)
|
|
992
|
-
*/
|
|
993
|
-
findIndex(predicate, thisArg) {
|
|
994
|
-
for (let i = 0; i < this.length; i++) {
|
|
995
|
-
const item = this.at(i);
|
|
996
|
-
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
997
|
-
}
|
|
998
|
-
return -1;
|
|
999
|
-
}
|
|
1000
|
-
/**
|
|
1001
|
-
* Concatenate elements and/or containers.
|
|
1002
|
-
* @param items - Elements or other containers.
|
|
1003
|
-
* @returns New container with combined elements (`this` type).
|
|
1004
|
-
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
1005
|
-
*/
|
|
1006
|
-
concat(...items) {
|
|
1007
|
-
const newList = this.clone();
|
|
1008
|
-
for (const item of items) {
|
|
1009
|
-
if (item instanceof _LinearBase) {
|
|
1010
|
-
newList.pushMany(item);
|
|
1011
|
-
} else {
|
|
1012
|
-
newList.push(item);
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
return newList;
|
|
1016
|
-
}
|
|
1017
|
-
/**
|
|
1018
|
-
* In-place stable order via array sort semantics.
|
|
1019
|
-
* @param compareFn - Comparator `(a, b) => number`.
|
|
1020
|
-
* @returns This container.
|
|
1021
|
-
* @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
|
|
1022
|
-
*/
|
|
1023
|
-
sort(compareFn) {
|
|
1024
|
-
const arr = this.toArray();
|
|
1025
|
-
arr.sort(compareFn);
|
|
1026
|
-
this.clear();
|
|
1027
|
-
for (const item of arr) this.push(item);
|
|
1028
|
-
return this;
|
|
1029
|
-
}
|
|
1030
|
-
/**
|
|
1031
|
-
* Remove and/or insert elements at a position (array-compatible).
|
|
1032
|
-
* @param start - Start index (supports negative index).
|
|
1033
|
-
* @param deleteCount - How many to remove.
|
|
1034
|
-
* @param items - Elements to insert.
|
|
1035
|
-
* @returns Removed elements as a new list (`this` type).
|
|
1036
|
-
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
1037
|
-
*/
|
|
1038
|
-
splice(start, deleteCount = 0, ...items) {
|
|
1039
|
-
const removedList = this._createInstance();
|
|
1040
|
-
start = start < 0 ? this.length + start : start;
|
|
1041
|
-
start = Math.max(0, Math.min(start, this.length));
|
|
1042
|
-
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
1043
|
-
for (let i = 0; i < deleteCount; i++) {
|
|
1044
|
-
const removed = this.deleteAt(start);
|
|
1045
|
-
if (removed !== void 0) {
|
|
1046
|
-
removedList.push(removed);
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
for (let i = 0; i < items.length; i++) {
|
|
1050
|
-
this.addAt(start + i, items[i]);
|
|
1051
|
-
}
|
|
1052
|
-
return removedList;
|
|
1053
|
-
}
|
|
1054
|
-
/**
|
|
1055
|
-
* Join all elements into a string.
|
|
1056
|
-
* @param separator - Separator string.
|
|
1057
|
-
* @returns Concatenated string.
|
|
1058
|
-
* @remarks Time O(n), Space O(n)
|
|
1059
|
-
*/
|
|
1060
|
-
join(separator = ",") {
|
|
1061
|
-
return this.toArray().join(separator);
|
|
1062
|
-
}
|
|
1063
|
-
/**
|
|
1064
|
-
* Snapshot elements into a reversed array.
|
|
1065
|
-
* @returns New reversed array.
|
|
1066
|
-
* @remarks Time O(n), Space O(n)
|
|
1067
|
-
*/
|
|
1068
|
-
toReversedArray() {
|
|
1069
|
-
const array = [];
|
|
1070
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
1071
|
-
array.push(this.at(i));
|
|
1072
|
-
}
|
|
1073
|
-
return array;
|
|
1074
|
-
}
|
|
1075
|
-
reduceRight(callbackfn, initialValue) {
|
|
1076
|
-
let accumulator = initialValue != null ? initialValue : 0;
|
|
1077
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
1078
|
-
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
1079
|
-
}
|
|
1080
|
-
return accumulator;
|
|
1081
|
-
}
|
|
1082
|
-
/**
|
|
1083
|
-
* Create a shallow copy of a subrange.
|
|
1084
|
-
* @param start - Inclusive start (supports negative index).
|
|
1085
|
-
* @param end - Exclusive end (supports negative index).
|
|
1086
|
-
* @returns New list with the range (`this` type).
|
|
1087
|
-
* @remarks Time O(n), Space O(n)
|
|
1088
|
-
*/
|
|
1089
|
-
slice(start = 0, end = this.length) {
|
|
1090
|
-
start = start < 0 ? this.length + start : start;
|
|
1091
|
-
end = end < 0 ? this.length + end : end;
|
|
1092
|
-
const newList = this._createInstance();
|
|
1093
|
-
for (let i = start; i < end; i++) {
|
|
1094
|
-
newList.push(this.at(i));
|
|
1095
|
-
}
|
|
1096
|
-
return newList;
|
|
1097
|
-
}
|
|
1098
|
-
/**
|
|
1099
|
-
* Fill a range with a value.
|
|
1100
|
-
* @param value - Value to set.
|
|
1101
|
-
* @param start - Inclusive start.
|
|
1102
|
-
* @param end - Exclusive end.
|
|
1103
|
-
* @returns This list.
|
|
1104
|
-
* @remarks Time O(n), Space O(1)
|
|
1105
|
-
*/
|
|
1106
|
-
fill(value, start = 0, end = this.length) {
|
|
1107
|
-
start = start < 0 ? this.length + start : start;
|
|
1108
|
-
end = end < 0 ? this.length + end : end;
|
|
1109
|
-
if (start < 0) start = 0;
|
|
1110
|
-
if (end > this.length) end = this.length;
|
|
1111
|
-
if (start >= end) return this;
|
|
1112
|
-
for (let i = start; i < end; i++) {
|
|
1113
|
-
this.setAt(i, value);
|
|
1114
|
-
}
|
|
1115
|
-
return this;
|
|
1116
|
-
}
|
|
1117
|
-
};
|
|
1118
|
-
|
|
1119
1694
|
// src/data-structures/queue/queue.ts
|
|
1120
1695
|
var Queue = class _Queue extends LinearBase {
|
|
1121
1696
|
/**
|
|
@@ -1170,18 +1745,94 @@ var directedGraphTyped = (() => {
|
|
|
1170
1745
|
this._autoCompactRatio = value;
|
|
1171
1746
|
}
|
|
1172
1747
|
/**
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1748
|
+
* Get the number of elements currently in the queue.
|
|
1749
|
+
* @remarks Time O(1), Space O(1)
|
|
1750
|
+
* @returns Current length.
|
|
1751
|
+
|
|
1752
|
+
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
|
|
1762
|
+
|
|
1763
|
+
|
|
1764
|
+
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
|
|
1768
|
+
|
|
1769
|
+
|
|
1770
|
+
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
|
|
1774
|
+
|
|
1775
|
+
|
|
1776
|
+
|
|
1777
|
+
|
|
1778
|
+
|
|
1779
|
+
|
|
1780
|
+
|
|
1781
|
+
|
|
1782
|
+
|
|
1783
|
+
* @example
|
|
1784
|
+
* // Track queue length
|
|
1785
|
+
* const q = new Queue<number>();
|
|
1786
|
+
* console.log(q.length); // 0;
|
|
1787
|
+
* q.push(1);
|
|
1788
|
+
* q.push(2);
|
|
1789
|
+
* console.log(q.length); // 2;
|
|
1790
|
+
*/
|
|
1177
1791
|
get length() {
|
|
1178
1792
|
return this.elements.length - this._offset;
|
|
1179
1793
|
}
|
|
1180
1794
|
/**
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1795
|
+
* Get the first element (front) without removing it.
|
|
1796
|
+
* @remarks Time O(1), Space O(1)
|
|
1797
|
+
* @returns Front element or undefined.
|
|
1798
|
+
|
|
1799
|
+
|
|
1800
|
+
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
|
|
1804
|
+
|
|
1805
|
+
|
|
1806
|
+
|
|
1807
|
+
|
|
1808
|
+
|
|
1809
|
+
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
|
|
1815
|
+
|
|
1816
|
+
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
|
|
1820
|
+
|
|
1821
|
+
|
|
1822
|
+
|
|
1823
|
+
|
|
1824
|
+
|
|
1825
|
+
|
|
1826
|
+
|
|
1827
|
+
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
* @example
|
|
1831
|
+
* // View the front element
|
|
1832
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1833
|
+
* console.log(q.first); // 'first';
|
|
1834
|
+
* console.log(q.length); // 3;
|
|
1835
|
+
*/
|
|
1185
1836
|
get first() {
|
|
1186
1837
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1187
1838
|
}
|
|
@@ -1204,19 +1855,111 @@ var directedGraphTyped = (() => {
|
|
|
1204
1855
|
return new _Queue(elements);
|
|
1205
1856
|
}
|
|
1206
1857
|
/**
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1858
|
+
* Check whether the queue is empty.
|
|
1859
|
+
* @remarks Time O(1), Space O(1)
|
|
1860
|
+
* @returns True if length is 0.
|
|
1861
|
+
|
|
1862
|
+
|
|
1863
|
+
|
|
1864
|
+
|
|
1865
|
+
|
|
1866
|
+
|
|
1867
|
+
|
|
1868
|
+
|
|
1869
|
+
|
|
1870
|
+
|
|
1871
|
+
|
|
1872
|
+
|
|
1873
|
+
|
|
1874
|
+
|
|
1875
|
+
|
|
1876
|
+
|
|
1877
|
+
|
|
1878
|
+
|
|
1879
|
+
|
|
1880
|
+
|
|
1881
|
+
|
|
1882
|
+
|
|
1883
|
+
|
|
1884
|
+
|
|
1885
|
+
|
|
1886
|
+
|
|
1887
|
+
|
|
1888
|
+
|
|
1889
|
+
|
|
1890
|
+
|
|
1891
|
+
|
|
1892
|
+
|
|
1893
|
+
* @example
|
|
1894
|
+
* // Queue for...of iteration and isEmpty check
|
|
1895
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1896
|
+
*
|
|
1897
|
+
* const elements: string[] = [];
|
|
1898
|
+
* for (const item of queue) {
|
|
1899
|
+
* elements.push(item);
|
|
1900
|
+
* }
|
|
1901
|
+
*
|
|
1902
|
+
* // Verify all elements are iterated in order
|
|
1903
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1904
|
+
*
|
|
1905
|
+
* // Process all elements
|
|
1906
|
+
* while (queue.length > 0) {
|
|
1907
|
+
* queue.shift();
|
|
1908
|
+
* }
|
|
1909
|
+
*
|
|
1910
|
+
* console.log(queue.length); // 0;
|
|
1911
|
+
*/
|
|
1211
1912
|
isEmpty() {
|
|
1212
1913
|
return this.length === 0;
|
|
1213
1914
|
}
|
|
1214
1915
|
/**
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1916
|
+
* Enqueue one element at the back.
|
|
1917
|
+
* @remarks Time O(1), Space O(1)
|
|
1918
|
+
* @param element - Element to enqueue.
|
|
1919
|
+
* @returns True on success.
|
|
1920
|
+
|
|
1921
|
+
|
|
1922
|
+
|
|
1923
|
+
|
|
1924
|
+
|
|
1925
|
+
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
|
|
1929
|
+
|
|
1930
|
+
|
|
1931
|
+
|
|
1932
|
+
|
|
1933
|
+
|
|
1934
|
+
|
|
1935
|
+
|
|
1936
|
+
|
|
1937
|
+
|
|
1938
|
+
|
|
1939
|
+
|
|
1940
|
+
|
|
1941
|
+
|
|
1942
|
+
|
|
1943
|
+
|
|
1944
|
+
|
|
1945
|
+
|
|
1946
|
+
|
|
1947
|
+
|
|
1948
|
+
|
|
1949
|
+
|
|
1950
|
+
|
|
1951
|
+
|
|
1952
|
+
* @example
|
|
1953
|
+
* // basic Queue creation and push operation
|
|
1954
|
+
* // Create a simple Queue with initial values
|
|
1955
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1956
|
+
*
|
|
1957
|
+
* // Verify the queue maintains insertion order
|
|
1958
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1959
|
+
*
|
|
1960
|
+
* // Check length
|
|
1961
|
+
* console.log(queue.length); // 5;
|
|
1962
|
+
*/
|
|
1220
1963
|
push(element) {
|
|
1221
1964
|
this.elements.push(element);
|
|
1222
1965
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1237,10 +1980,56 @@ var directedGraphTyped = (() => {
|
|
|
1237
1980
|
return ans;
|
|
1238
1981
|
}
|
|
1239
1982
|
/**
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1983
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1984
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1985
|
+
* @returns Removed element or undefined.
|
|
1986
|
+
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
|
|
2003
|
+
|
|
2004
|
+
|
|
2005
|
+
|
|
2006
|
+
|
|
2007
|
+
|
|
2008
|
+
|
|
2009
|
+
|
|
2010
|
+
|
|
2011
|
+
|
|
2012
|
+
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
|
|
2017
|
+
|
|
2018
|
+
* @example
|
|
2019
|
+
* // Queue shift and peek operations
|
|
2020
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2021
|
+
*
|
|
2022
|
+
* // Peek at the front element without removing it
|
|
2023
|
+
* console.log(queue.first); // 10;
|
|
2024
|
+
*
|
|
2025
|
+
* // Remove and get the first element (FIFO)
|
|
2026
|
+
* const first = queue.shift();
|
|
2027
|
+
* console.log(first); // 10;
|
|
2028
|
+
*
|
|
2029
|
+
* // Verify remaining elements and length decreased
|
|
2030
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
2031
|
+
* console.log(queue.length); // 3;
|
|
2032
|
+
*/
|
|
1244
2033
|
shift() {
|
|
1245
2034
|
if (this.length === 0) return void 0;
|
|
1246
2035
|
const first = this.first;
|
|
@@ -1249,11 +2038,45 @@ var directedGraphTyped = (() => {
|
|
|
1249
2038
|
return first;
|
|
1250
2039
|
}
|
|
1251
2040
|
/**
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
2041
|
+
* Delete the first occurrence of a specific element.
|
|
2042
|
+
* @remarks Time O(N), Space O(1)
|
|
2043
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
2044
|
+
* @returns True if an element was removed.
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
|
|
2051
|
+
|
|
2052
|
+
|
|
2053
|
+
|
|
2054
|
+
|
|
2055
|
+
|
|
2056
|
+
|
|
2057
|
+
|
|
2058
|
+
|
|
2059
|
+
|
|
2060
|
+
|
|
2061
|
+
|
|
2062
|
+
|
|
2063
|
+
|
|
2064
|
+
|
|
2065
|
+
|
|
2066
|
+
|
|
2067
|
+
|
|
2068
|
+
|
|
2069
|
+
|
|
2070
|
+
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
* @example
|
|
2075
|
+
* // Remove specific element
|
|
2076
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2077
|
+
* q.delete(2);
|
|
2078
|
+
* console.log(q.length); // 3;
|
|
2079
|
+
*/
|
|
1257
2080
|
delete(element) {
|
|
1258
2081
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1259
2082
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1264,11 +2087,45 @@ var directedGraphTyped = (() => {
|
|
|
1264
2087
|
return false;
|
|
1265
2088
|
}
|
|
1266
2089
|
/**
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
2090
|
+
* Get the element at a given logical index.
|
|
2091
|
+
* @remarks Time O(1), Space O(1)
|
|
2092
|
+
* @param index - Zero-based index from the front.
|
|
2093
|
+
* @returns Element or undefined.
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
|
|
2098
|
+
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
|
|
2102
|
+
|
|
2103
|
+
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
|
|
2115
|
+
|
|
2116
|
+
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
* @example
|
|
2124
|
+
* // Access element by index
|
|
2125
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2126
|
+
* console.log(q.at(0)); // 'a';
|
|
2127
|
+
* console.log(q.at(2)); // 'c';
|
|
2128
|
+
*/
|
|
1272
2129
|
at(index) {
|
|
1273
2130
|
if (index < 0 || index >= this.length) return void 0;
|
|
1274
2131
|
return this._elements[this._offset + index];
|
|
@@ -1320,19 +2177,90 @@ var directedGraphTyped = (() => {
|
|
|
1320
2177
|
return this;
|
|
1321
2178
|
}
|
|
1322
2179
|
/**
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
2180
|
+
* Remove all elements and reset offset.
|
|
2181
|
+
* @remarks Time O(1), Space O(1)
|
|
2182
|
+
* @returns void
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2190
|
+
|
|
2191
|
+
|
|
2192
|
+
|
|
2193
|
+
|
|
2194
|
+
|
|
2195
|
+
|
|
2196
|
+
|
|
2197
|
+
|
|
2198
|
+
|
|
2199
|
+
|
|
2200
|
+
|
|
2201
|
+
|
|
2202
|
+
|
|
2203
|
+
|
|
2204
|
+
|
|
2205
|
+
|
|
2206
|
+
|
|
2207
|
+
|
|
2208
|
+
|
|
2209
|
+
|
|
2210
|
+
|
|
2211
|
+
|
|
2212
|
+
|
|
2213
|
+
* @example
|
|
2214
|
+
* // Remove all elements
|
|
2215
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2216
|
+
* q.clear();
|
|
2217
|
+
* console.log(q.length); // 0;
|
|
2218
|
+
*/
|
|
1327
2219
|
clear() {
|
|
1328
2220
|
this._elements = [];
|
|
1329
2221
|
this._offset = 0;
|
|
1330
2222
|
}
|
|
1331
2223
|
/**
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
2224
|
+
* Compact storage by discarding consumed head elements.
|
|
2225
|
+
* @remarks Time O(N), Space O(N)
|
|
2226
|
+
* @returns True when compaction performed.
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2239
|
+
|
|
2240
|
+
|
|
2241
|
+
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
|
|
2252
|
+
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
|
|
2256
|
+
* @example
|
|
2257
|
+
* // Reclaim unused memory
|
|
2258
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2259
|
+
* q.shift();
|
|
2260
|
+
* q.shift();
|
|
2261
|
+
* q.compact();
|
|
2262
|
+
* console.log(q.length); // 3;
|
|
2263
|
+
*/
|
|
1336
2264
|
compact() {
|
|
1337
2265
|
this._elements = this.elements.slice(this._offset);
|
|
1338
2266
|
this._offset = 0;
|
|
@@ -1358,10 +2286,47 @@ var directedGraphTyped = (() => {
|
|
|
1358
2286
|
return removed;
|
|
1359
2287
|
}
|
|
1360
2288
|
/**
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
2289
|
+
* Deep clone this queue and its parameters.
|
|
2290
|
+
* @remarks Time O(N), Space O(N)
|
|
2291
|
+
* @returns A new queue with the same content and options.
|
|
2292
|
+
|
|
2293
|
+
|
|
2294
|
+
|
|
2295
|
+
|
|
2296
|
+
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
|
|
2302
|
+
|
|
2303
|
+
|
|
2304
|
+
|
|
2305
|
+
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
|
|
2309
|
+
|
|
2310
|
+
|
|
2311
|
+
|
|
2312
|
+
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
|
|
2320
|
+
|
|
2321
|
+
|
|
2322
|
+
* @example
|
|
2323
|
+
* // Create independent copy
|
|
2324
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2325
|
+
* const copy = q.clone();
|
|
2326
|
+
* copy.shift();
|
|
2327
|
+
* console.log(q.length); // 3;
|
|
2328
|
+
* console.log(copy.length); // 2;
|
|
2329
|
+
*/
|
|
1365
2330
|
clone() {
|
|
1366
2331
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1367
2332
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1369,12 +2334,47 @@ var directedGraphTyped = (() => {
|
|
|
1369
2334
|
return out;
|
|
1370
2335
|
}
|
|
1371
2336
|
/**
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
2337
|
+
* Filter elements into a new queue of the same class.
|
|
2338
|
+
* @remarks Time O(N), Space O(N)
|
|
2339
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2340
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2341
|
+
* @returns A new queue with kept elements.
|
|
2342
|
+
|
|
2343
|
+
|
|
2344
|
+
|
|
2345
|
+
|
|
2346
|
+
|
|
2347
|
+
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
|
|
2351
|
+
|
|
2352
|
+
|
|
2353
|
+
|
|
2354
|
+
|
|
2355
|
+
|
|
2356
|
+
|
|
2357
|
+
|
|
2358
|
+
|
|
2359
|
+
|
|
2360
|
+
|
|
2361
|
+
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
|
|
2372
|
+
* @example
|
|
2373
|
+
* // Filter elements
|
|
2374
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2375
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2376
|
+
* console.log(evens.length); // 2;
|
|
2377
|
+
*/
|
|
1378
2378
|
filter(predicate, thisArg) {
|
|
1379
2379
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1380
2380
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1386,15 +2386,49 @@ var directedGraphTyped = (() => {
|
|
|
1386
2386
|
return out;
|
|
1387
2387
|
}
|
|
1388
2388
|
/**
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
2389
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2390
|
+
* @remarks Time O(N), Space O(N)
|
|
2391
|
+
* @template EM
|
|
2392
|
+
* @template RM
|
|
2393
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2394
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2395
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2396
|
+
* @returns A new Queue with mapped elements.
|
|
2397
|
+
|
|
2398
|
+
|
|
2399
|
+
|
|
2400
|
+
|
|
2401
|
+
|
|
2402
|
+
|
|
2403
|
+
|
|
2404
|
+
|
|
2405
|
+
|
|
2406
|
+
|
|
2407
|
+
|
|
2408
|
+
|
|
2409
|
+
|
|
2410
|
+
|
|
2411
|
+
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
|
|
2415
|
+
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
|
|
2424
|
+
|
|
2425
|
+
|
|
2426
|
+
* @example
|
|
2427
|
+
* // Transform elements
|
|
2428
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2429
|
+
* const doubled = q.map(x => x * 2);
|
|
2430
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2431
|
+
*/
|
|
1398
2432
|
map(callback, options, thisArg) {
|
|
1399
2433
|
var _a, _b;
|
|
1400
2434
|
const out = new this.constructor([], {
|
|
@@ -2482,12 +3516,49 @@ var directedGraphTyped = (() => {
|
|
|
2482
3516
|
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2483
3517
|
}
|
|
2484
3518
|
/**
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
3519
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
3520
|
+
* @param srcOrKey - Source vertex or key.
|
|
3521
|
+
* @param destOrKey - Destination vertex or key.
|
|
3522
|
+
* @returns Edge instance or `undefined`.
|
|
3523
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3524
|
+
|
|
3525
|
+
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
|
|
3529
|
+
|
|
3530
|
+
|
|
3531
|
+
|
|
3532
|
+
|
|
3533
|
+
|
|
3534
|
+
|
|
3535
|
+
|
|
3536
|
+
|
|
3537
|
+
|
|
3538
|
+
|
|
3539
|
+
|
|
3540
|
+
|
|
3541
|
+
|
|
3542
|
+
|
|
3543
|
+
|
|
3544
|
+
|
|
3545
|
+
|
|
3546
|
+
|
|
3547
|
+
|
|
3548
|
+
|
|
3549
|
+
|
|
3550
|
+
|
|
3551
|
+
|
|
3552
|
+
|
|
3553
|
+
* @example
|
|
3554
|
+
* // Get edge between vertices
|
|
3555
|
+
* const g = new DirectedGraph();
|
|
3556
|
+
* g.addVertex('A');
|
|
3557
|
+
* g.addVertex('B');
|
|
3558
|
+
* g.addEdge('A', 'B', 5);
|
|
3559
|
+
* const edge = g.getEdge('A', 'B');
|
|
3560
|
+
* console.log(edge?.weight); // 5;
|
|
3561
|
+
*/
|
|
2491
3562
|
getEdge(srcOrKey, destOrKey) {
|
|
2492
3563
|
let edgeMap = [];
|
|
2493
3564
|
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
@@ -2527,12 +3598,69 @@ var directedGraphTyped = (() => {
|
|
|
2527
3598
|
return removed;
|
|
2528
3599
|
}
|
|
2529
3600
|
/**
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
3601
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
3602
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
3603
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
3604
|
+
* @returns Removed edge or `undefined`.
|
|
3605
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3606
|
+
|
|
3607
|
+
|
|
3608
|
+
|
|
3609
|
+
|
|
3610
|
+
|
|
3611
|
+
|
|
3612
|
+
|
|
3613
|
+
|
|
3614
|
+
|
|
3615
|
+
|
|
3616
|
+
|
|
3617
|
+
|
|
3618
|
+
|
|
3619
|
+
|
|
3620
|
+
|
|
3621
|
+
|
|
3622
|
+
|
|
3623
|
+
|
|
3624
|
+
|
|
3625
|
+
|
|
3626
|
+
|
|
3627
|
+
|
|
3628
|
+
|
|
3629
|
+
|
|
3630
|
+
|
|
3631
|
+
|
|
3632
|
+
|
|
3633
|
+
|
|
3634
|
+
|
|
3635
|
+
|
|
3636
|
+
|
|
3637
|
+
|
|
3638
|
+
* @example
|
|
3639
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
3640
|
+
* const graph = new DirectedGraph<string>();
|
|
3641
|
+
*
|
|
3642
|
+
* // Build a small graph
|
|
3643
|
+
* graph.addVertex('X');
|
|
3644
|
+
* graph.addVertex('Y');
|
|
3645
|
+
* graph.addVertex('Z');
|
|
3646
|
+
* graph.addEdge('X', 'Y', 1);
|
|
3647
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
3648
|
+
*
|
|
3649
|
+
* // Delete an edge
|
|
3650
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
3651
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
3652
|
+
*
|
|
3653
|
+
* // Edge in other direction should not exist
|
|
3654
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
3655
|
+
*
|
|
3656
|
+
* // Other edges should remain
|
|
3657
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
3658
|
+
*
|
|
3659
|
+
* // Delete a vertex
|
|
3660
|
+
* graph.deleteVertex('Y');
|
|
3661
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
3662
|
+
* console.log(graph.size); // 2;
|
|
3663
|
+
*/
|
|
2536
3664
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
2537
3665
|
let removed = void 0;
|
|
2538
3666
|
let src, dest;
|
|
@@ -2559,6 +3687,47 @@ var directedGraphTyped = (() => {
|
|
|
2559
3687
|
}
|
|
2560
3688
|
return removed;
|
|
2561
3689
|
}
|
|
3690
|
+
/**
|
|
3691
|
+
* Remove a vertex
|
|
3692
|
+
|
|
3693
|
+
|
|
3694
|
+
|
|
3695
|
+
|
|
3696
|
+
|
|
3697
|
+
|
|
3698
|
+
|
|
3699
|
+
|
|
3700
|
+
|
|
3701
|
+
|
|
3702
|
+
|
|
3703
|
+
|
|
3704
|
+
|
|
3705
|
+
|
|
3706
|
+
|
|
3707
|
+
|
|
3708
|
+
|
|
3709
|
+
|
|
3710
|
+
|
|
3711
|
+
|
|
3712
|
+
|
|
3713
|
+
|
|
3714
|
+
|
|
3715
|
+
|
|
3716
|
+
|
|
3717
|
+
|
|
3718
|
+
|
|
3719
|
+
|
|
3720
|
+
|
|
3721
|
+
* @example
|
|
3722
|
+
* // Remove a vertex
|
|
3723
|
+
* const g = new DirectedGraph();
|
|
3724
|
+
* g.addVertex('A');
|
|
3725
|
+
* g.addVertex('B');
|
|
3726
|
+
* g.addEdge('A', 'B');
|
|
3727
|
+
* g.deleteVertex('A');
|
|
3728
|
+
* console.log(g.hasVertex('A')); // false;
|
|
3729
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
3730
|
+
*/
|
|
2562
3731
|
deleteVertex(vertexOrKey) {
|
|
2563
3732
|
let vertexKey;
|
|
2564
3733
|
let vertex;
|
|
@@ -2590,11 +3759,49 @@ var directedGraphTyped = (() => {
|
|
|
2590
3759
|
return removed;
|
|
2591
3760
|
}
|
|
2592
3761
|
/**
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
3762
|
+
* Incoming edges of a vertex.
|
|
3763
|
+
* @param vertexOrKey - Vertex or key.
|
|
3764
|
+
* @returns Array of incoming edges.
|
|
3765
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
3766
|
+
|
|
3767
|
+
|
|
3768
|
+
|
|
3769
|
+
|
|
3770
|
+
|
|
3771
|
+
|
|
3772
|
+
|
|
3773
|
+
|
|
3774
|
+
|
|
3775
|
+
|
|
3776
|
+
|
|
3777
|
+
|
|
3778
|
+
|
|
3779
|
+
|
|
3780
|
+
|
|
3781
|
+
|
|
3782
|
+
|
|
3783
|
+
|
|
3784
|
+
|
|
3785
|
+
|
|
3786
|
+
|
|
3787
|
+
|
|
3788
|
+
|
|
3789
|
+
|
|
3790
|
+
|
|
3791
|
+
|
|
3792
|
+
|
|
3793
|
+
|
|
3794
|
+
|
|
3795
|
+
* @example
|
|
3796
|
+
* // Get incoming edges
|
|
3797
|
+
* const g = new DirectedGraph();
|
|
3798
|
+
* g.addVertex('A');
|
|
3799
|
+
* g.addVertex('B');
|
|
3800
|
+
* g.addVertex('C');
|
|
3801
|
+
* g.addEdge('A', 'C');
|
|
3802
|
+
* g.addEdge('B', 'C');
|
|
3803
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
3804
|
+
*/
|
|
2598
3805
|
incomingEdgesOf(vertexOrKey) {
|
|
2599
3806
|
const target = this._getVertex(vertexOrKey);
|
|
2600
3807
|
if (target) {
|
|
@@ -2603,11 +3810,49 @@ var directedGraphTyped = (() => {
|
|
|
2603
3810
|
return [];
|
|
2604
3811
|
}
|
|
2605
3812
|
/**
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
3813
|
+
* Outgoing edges of a vertex.
|
|
3814
|
+
* @param vertexOrKey - Vertex or key.
|
|
3815
|
+
* @returns Array of outgoing edges.
|
|
3816
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
3817
|
+
|
|
3818
|
+
|
|
3819
|
+
|
|
3820
|
+
|
|
3821
|
+
|
|
3822
|
+
|
|
3823
|
+
|
|
3824
|
+
|
|
3825
|
+
|
|
3826
|
+
|
|
3827
|
+
|
|
3828
|
+
|
|
3829
|
+
|
|
3830
|
+
|
|
3831
|
+
|
|
3832
|
+
|
|
3833
|
+
|
|
3834
|
+
|
|
3835
|
+
|
|
3836
|
+
|
|
3837
|
+
|
|
3838
|
+
|
|
3839
|
+
|
|
3840
|
+
|
|
3841
|
+
|
|
3842
|
+
|
|
3843
|
+
|
|
3844
|
+
|
|
3845
|
+
|
|
3846
|
+
* @example
|
|
3847
|
+
* // Get outgoing edges
|
|
3848
|
+
* const g = new DirectedGraph();
|
|
3849
|
+
* g.addVertex('A');
|
|
3850
|
+
* g.addVertex('B');
|
|
3851
|
+
* g.addVertex('C');
|
|
3852
|
+
* g.addEdge('A', 'B');
|
|
3853
|
+
* g.addEdge('A', 'C');
|
|
3854
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
3855
|
+
*/
|
|
2611
3856
|
outgoingEdgesOf(vertexOrKey) {
|
|
2612
3857
|
const target = this._getVertex(vertexOrKey);
|
|
2613
3858
|
if (target) {
|
|
@@ -2666,11 +3911,65 @@ var directedGraphTyped = (() => {
|
|
|
2666
3911
|
return destinations;
|
|
2667
3912
|
}
|
|
2668
3913
|
/**
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
3914
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
3915
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
3916
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
3917
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3918
|
+
|
|
3919
|
+
|
|
3920
|
+
|
|
3921
|
+
|
|
3922
|
+
|
|
3923
|
+
|
|
3924
|
+
|
|
3925
|
+
|
|
3926
|
+
|
|
3927
|
+
|
|
3928
|
+
|
|
3929
|
+
|
|
3930
|
+
|
|
3931
|
+
|
|
3932
|
+
|
|
3933
|
+
|
|
3934
|
+
|
|
3935
|
+
|
|
3936
|
+
|
|
3937
|
+
|
|
3938
|
+
|
|
3939
|
+
|
|
3940
|
+
|
|
3941
|
+
|
|
3942
|
+
|
|
3943
|
+
|
|
3944
|
+
|
|
3945
|
+
|
|
3946
|
+
|
|
3947
|
+
|
|
3948
|
+
|
|
3949
|
+
|
|
3950
|
+
* @example
|
|
3951
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
3952
|
+
* const graph = new DirectedGraph<string>();
|
|
3953
|
+
*
|
|
3954
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
3955
|
+
* graph.addVertex('Design');
|
|
3956
|
+
* graph.addVertex('Implement');
|
|
3957
|
+
* graph.addVertex('Test');
|
|
3958
|
+
* graph.addVertex('Deploy');
|
|
3959
|
+
*
|
|
3960
|
+
* // Add dependency edges
|
|
3961
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
3962
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
3963
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
3964
|
+
*
|
|
3965
|
+
* // Topological sort gives valid execution order
|
|
3966
|
+
* const executionOrder = graph.topologicalSort();
|
|
3967
|
+
* console.log(executionOrder); // defined;
|
|
3968
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
3969
|
+
*
|
|
3970
|
+
* // All vertices should be included
|
|
3971
|
+
* console.log(executionOrder?.length); // 4;
|
|
3972
|
+
*/
|
|
2674
3973
|
topologicalSort(propertyName) {
|
|
2675
3974
|
propertyName = propertyName != null ? propertyName : "key";
|
|
2676
3975
|
const statusMap = /* @__PURE__ */ new Map();
|
|
@@ -2702,6 +4001,45 @@ var directedGraphTyped = (() => {
|
|
|
2702
4001
|
if (propertyName === "key") sorted = sorted.map((vertex) => vertex instanceof DirectedVertex ? vertex.key : vertex);
|
|
2703
4002
|
return sorted.reverse();
|
|
2704
4003
|
}
|
|
4004
|
+
/**
|
|
4005
|
+
* Get all edges
|
|
4006
|
+
|
|
4007
|
+
|
|
4008
|
+
|
|
4009
|
+
|
|
4010
|
+
|
|
4011
|
+
|
|
4012
|
+
|
|
4013
|
+
|
|
4014
|
+
|
|
4015
|
+
|
|
4016
|
+
|
|
4017
|
+
|
|
4018
|
+
|
|
4019
|
+
|
|
4020
|
+
|
|
4021
|
+
|
|
4022
|
+
|
|
4023
|
+
|
|
4024
|
+
|
|
4025
|
+
|
|
4026
|
+
|
|
4027
|
+
|
|
4028
|
+
|
|
4029
|
+
|
|
4030
|
+
|
|
4031
|
+
|
|
4032
|
+
|
|
4033
|
+
|
|
4034
|
+
|
|
4035
|
+
* @example
|
|
4036
|
+
* // Get all edges
|
|
4037
|
+
* const g = new DirectedGraph();
|
|
4038
|
+
* g.addVertex('A');
|
|
4039
|
+
* g.addVertex('B');
|
|
4040
|
+
* g.addEdge('A', 'B', 3);
|
|
4041
|
+
* console.log(g.edgeSet().length); // 1;
|
|
4042
|
+
*/
|
|
2705
4043
|
edgeSet() {
|
|
2706
4044
|
let edgeMap = [];
|
|
2707
4045
|
this._outEdgeMap.forEach((outEdges) => {
|
|
@@ -2709,6 +4047,49 @@ var directedGraphTyped = (() => {
|
|
|
2709
4047
|
});
|
|
2710
4048
|
return edgeMap;
|
|
2711
4049
|
}
|
|
4050
|
+
/**
|
|
4051
|
+
* Get outgoing neighbors
|
|
4052
|
+
|
|
4053
|
+
|
|
4054
|
+
|
|
4055
|
+
|
|
4056
|
+
|
|
4057
|
+
|
|
4058
|
+
|
|
4059
|
+
|
|
4060
|
+
|
|
4061
|
+
|
|
4062
|
+
|
|
4063
|
+
|
|
4064
|
+
|
|
4065
|
+
|
|
4066
|
+
|
|
4067
|
+
|
|
4068
|
+
|
|
4069
|
+
|
|
4070
|
+
|
|
4071
|
+
|
|
4072
|
+
|
|
4073
|
+
|
|
4074
|
+
|
|
4075
|
+
|
|
4076
|
+
|
|
4077
|
+
|
|
4078
|
+
|
|
4079
|
+
|
|
4080
|
+
|
|
4081
|
+
|
|
4082
|
+
* @example
|
|
4083
|
+
* // Get outgoing neighbors
|
|
4084
|
+
* const g = new DirectedGraph();
|
|
4085
|
+
* g.addVertex('A');
|
|
4086
|
+
* g.addVertex('B');
|
|
4087
|
+
* g.addVertex('C');
|
|
4088
|
+
* g.addEdge('A', 'B');
|
|
4089
|
+
* g.addEdge('A', 'C');
|
|
4090
|
+
* const neighbors = g.getNeighbors('A');
|
|
4091
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
4092
|
+
*/
|
|
2712
4093
|
getNeighbors(vertexOrKey) {
|
|
2713
4094
|
const neighbors = [];
|
|
2714
4095
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -2766,10 +4147,52 @@ var directedGraphTyped = (() => {
|
|
|
2766
4147
|
return super.clone();
|
|
2767
4148
|
}
|
|
2768
4149
|
/**
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
4150
|
+
* Tarjan's algorithm for strongly connected components.
|
|
4151
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
4152
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
4153
|
+
|
|
4154
|
+
|
|
4155
|
+
|
|
4156
|
+
|
|
4157
|
+
|
|
4158
|
+
|
|
4159
|
+
|
|
4160
|
+
|
|
4161
|
+
|
|
4162
|
+
|
|
4163
|
+
|
|
4164
|
+
|
|
4165
|
+
|
|
4166
|
+
|
|
4167
|
+
|
|
4168
|
+
|
|
4169
|
+
|
|
4170
|
+
|
|
4171
|
+
|
|
4172
|
+
|
|
4173
|
+
|
|
4174
|
+
|
|
4175
|
+
|
|
4176
|
+
|
|
4177
|
+
|
|
4178
|
+
|
|
4179
|
+
|
|
4180
|
+
|
|
4181
|
+
|
|
4182
|
+
* @example
|
|
4183
|
+
* // Find strongly connected components
|
|
4184
|
+
* const g = new DirectedGraph();
|
|
4185
|
+
* g.addVertex('A');
|
|
4186
|
+
* g.addVertex('B');
|
|
4187
|
+
* g.addVertex('C');
|
|
4188
|
+
* g.addEdge('A', 'B');
|
|
4189
|
+
* g.addEdge('B', 'C');
|
|
4190
|
+
* g.addEdge('C', 'A');
|
|
4191
|
+
* const { SCCs } = g.tarjan();
|
|
4192
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
4193
|
+
* const sccArrays = [...SCCs.values()];
|
|
4194
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
4195
|
+
*/
|
|
2773
4196
|
tarjan() {
|
|
2774
4197
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
2775
4198
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -2827,10 +4250,50 @@ var directedGraphTyped = (() => {
|
|
|
2827
4250
|
return this.tarjan().lowMap;
|
|
2828
4251
|
}
|
|
2829
4252
|
/**
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
4253
|
+
* Strongly connected components computed by `tarjan()`.
|
|
4254
|
+
* @returns Map from SCC id to vertices.
|
|
4255
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
4256
|
+
|
|
4257
|
+
|
|
4258
|
+
|
|
4259
|
+
|
|
4260
|
+
|
|
4261
|
+
|
|
4262
|
+
|
|
4263
|
+
|
|
4264
|
+
|
|
4265
|
+
|
|
4266
|
+
|
|
4267
|
+
|
|
4268
|
+
|
|
4269
|
+
|
|
4270
|
+
|
|
4271
|
+
|
|
4272
|
+
|
|
4273
|
+
|
|
4274
|
+
|
|
4275
|
+
|
|
4276
|
+
|
|
4277
|
+
|
|
4278
|
+
|
|
4279
|
+
|
|
4280
|
+
|
|
4281
|
+
|
|
4282
|
+
|
|
4283
|
+
|
|
4284
|
+
|
|
4285
|
+
* @example
|
|
4286
|
+
* // Get strongly connected components
|
|
4287
|
+
* const g = new DirectedGraph();
|
|
4288
|
+
* g.addVertex(1);
|
|
4289
|
+
* g.addVertex(2);
|
|
4290
|
+
* g.addVertex(3);
|
|
4291
|
+
* g.addEdge(1, 2);
|
|
4292
|
+
* g.addEdge(2, 3);
|
|
4293
|
+
* g.addEdge(3, 1);
|
|
4294
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
4295
|
+
* console.log(sccs.size); // >= 1;
|
|
4296
|
+
*/
|
|
2834
4297
|
getSCCs() {
|
|
2835
4298
|
return this.tarjan().SCCs;
|
|
2836
4299
|
}
|