directed-graph-typed 2.1.1 → 2.1.2
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/dist/cjs/index.cjs +2798 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +2789 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +13 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/directed-graph-typed.js +2791 -0
- package/dist/umd/directed-graph-typed.js.map +1 -0
- package/dist/umd/directed-graph-typed.min.js +9 -0
- package/dist/umd/directed-graph-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -30
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -0,0 +1,2798 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
|
|
8
|
+
// src/utils/utils.ts
|
|
9
|
+
var uuidV4 = /* @__PURE__ */ __name(function() {
|
|
10
|
+
return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g, function(c) {
|
|
11
|
+
const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
|
|
12
|
+
return v.toString(16);
|
|
13
|
+
});
|
|
14
|
+
}, "uuidV4");
|
|
15
|
+
var arrayRemove = /* @__PURE__ */ __name(function(array, predicate) {
|
|
16
|
+
let i = -1, len = array ? array.length : 0;
|
|
17
|
+
const result = [];
|
|
18
|
+
while (++i < len) {
|
|
19
|
+
const value = array[i];
|
|
20
|
+
if (predicate(value, i, array)) {
|
|
21
|
+
result.push(value);
|
|
22
|
+
Array.prototype.splice.call(array, i--, 1);
|
|
23
|
+
len--;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}, "arrayRemove");
|
|
28
|
+
function isPrimitiveComparable(value) {
|
|
29
|
+
const valueType = typeof value;
|
|
30
|
+
if (valueType === "number") return true;
|
|
31
|
+
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
32
|
+
}
|
|
33
|
+
__name(isPrimitiveComparable, "isPrimitiveComparable");
|
|
34
|
+
function tryObjectToPrimitive(obj) {
|
|
35
|
+
if (typeof obj.valueOf === "function") {
|
|
36
|
+
const valueOfResult = obj.valueOf();
|
|
37
|
+
if (valueOfResult !== obj) {
|
|
38
|
+
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
39
|
+
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (typeof obj.toString === "function") {
|
|
43
|
+
const stringResult = obj.toString();
|
|
44
|
+
if (stringResult !== "[object Object]") return stringResult;
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
__name(tryObjectToPrimitive, "tryObjectToPrimitive");
|
|
49
|
+
function isComparable(value, isForceObjectComparable = false) {
|
|
50
|
+
if (value === null || value === void 0) return false;
|
|
51
|
+
if (isPrimitiveComparable(value)) return true;
|
|
52
|
+
if (typeof value !== "object") return false;
|
|
53
|
+
if (value instanceof Date) return true;
|
|
54
|
+
if (isForceObjectComparable) return true;
|
|
55
|
+
const comparableValue = tryObjectToPrimitive(value);
|
|
56
|
+
if (comparableValue === null || comparableValue === void 0) return false;
|
|
57
|
+
return isPrimitiveComparable(comparableValue);
|
|
58
|
+
}
|
|
59
|
+
__name(isComparable, "isComparable");
|
|
60
|
+
|
|
61
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
62
|
+
var _IterableEntryBase = class _IterableEntryBase {
|
|
63
|
+
/**
|
|
64
|
+
* Default iterator yielding `[key, value]` entries.
|
|
65
|
+
* @returns Iterator of `[K, V]`.
|
|
66
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
67
|
+
*/
|
|
68
|
+
*[Symbol.iterator](...args) {
|
|
69
|
+
yield* this._getIterator(...args);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
73
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
74
|
+
* @remarks Time O(n), Space O(1)
|
|
75
|
+
*/
|
|
76
|
+
*entries() {
|
|
77
|
+
for (const item of this) {
|
|
78
|
+
yield item;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Iterate over keys only.
|
|
83
|
+
* @returns Iterator of keys.
|
|
84
|
+
* @remarks Time O(n), Space O(1)
|
|
85
|
+
*/
|
|
86
|
+
*keys() {
|
|
87
|
+
for (const item of this) {
|
|
88
|
+
yield item[0];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Iterate over values only.
|
|
93
|
+
* @returns Iterator of values.
|
|
94
|
+
* @remarks Time O(n), Space O(1)
|
|
95
|
+
*/
|
|
96
|
+
*values() {
|
|
97
|
+
for (const item of this) {
|
|
98
|
+
yield item[1];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Test whether all entries satisfy the predicate.
|
|
103
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
104
|
+
* @param thisArg - Optional `this` for callback.
|
|
105
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
106
|
+
* @remarks Time O(n), Space O(1)
|
|
107
|
+
*/
|
|
108
|
+
every(predicate, thisArg) {
|
|
109
|
+
let index = 0;
|
|
110
|
+
for (const item of this) {
|
|
111
|
+
if (!predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Test whether any entry satisfies the predicate.
|
|
119
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
120
|
+
* @param thisArg - Optional `this` for callback.
|
|
121
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
122
|
+
* @remarks Time O(n), Space O(1)
|
|
123
|
+
*/
|
|
124
|
+
some(predicate, thisArg) {
|
|
125
|
+
let index = 0;
|
|
126
|
+
for (const item of this) {
|
|
127
|
+
if (predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Visit each entry, left-to-right.
|
|
135
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
136
|
+
* @param thisArg - Optional `this` for callback.
|
|
137
|
+
* @remarks Time O(n), Space O(1)
|
|
138
|
+
*/
|
|
139
|
+
forEach(callbackfn, thisArg) {
|
|
140
|
+
let index = 0;
|
|
141
|
+
for (const item of this) {
|
|
142
|
+
const [key, value] = item;
|
|
143
|
+
callbackfn.call(thisArg, key, value, index++, this);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Find the first entry that matches a predicate.
|
|
148
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
149
|
+
* @param thisArg - Optional `this` for callback.
|
|
150
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
151
|
+
* @remarks Time O(n), Space O(1)
|
|
152
|
+
*/
|
|
153
|
+
find(callbackfn, thisArg) {
|
|
154
|
+
let index = 0;
|
|
155
|
+
for (const item of this) {
|
|
156
|
+
const [key, value] = item;
|
|
157
|
+
if (callbackfn.call(thisArg, key, value, index++, this)) return item;
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Whether the given key exists.
|
|
163
|
+
* @param key - Key to test.
|
|
164
|
+
* @returns `true` if found; otherwise `false`.
|
|
165
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
166
|
+
*/
|
|
167
|
+
has(key) {
|
|
168
|
+
for (const item of this) {
|
|
169
|
+
const [itemKey] = item;
|
|
170
|
+
if (itemKey === key) return true;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Whether there exists an entry with the given value.
|
|
176
|
+
* @param value - Value to test.
|
|
177
|
+
* @returns `true` if found; otherwise `false`.
|
|
178
|
+
* @remarks Time O(n), Space O(1)
|
|
179
|
+
*/
|
|
180
|
+
hasValue(value) {
|
|
181
|
+
for (const [, elementValue] of this) {
|
|
182
|
+
if (elementValue === value) return true;
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get the value under a key.
|
|
188
|
+
* @param key - Key to look up.
|
|
189
|
+
* @returns Value or `undefined`.
|
|
190
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
191
|
+
*/
|
|
192
|
+
get(key) {
|
|
193
|
+
for (const item of this) {
|
|
194
|
+
const [itemKey, value] = item;
|
|
195
|
+
if (itemKey === key) return value;
|
|
196
|
+
}
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Reduce entries into a single accumulator.
|
|
201
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
202
|
+
* @param initialValue - Initial accumulator.
|
|
203
|
+
* @returns Final accumulator.
|
|
204
|
+
* @remarks Time O(n), Space O(1)
|
|
205
|
+
*/
|
|
206
|
+
reduce(callbackfn, initialValue) {
|
|
207
|
+
let accumulator = initialValue;
|
|
208
|
+
let index = 0;
|
|
209
|
+
for (const item of this) {
|
|
210
|
+
const [key, value] = item;
|
|
211
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
212
|
+
}
|
|
213
|
+
return accumulator;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
217
|
+
* @returns Array of entries (default) or a string.
|
|
218
|
+
* @remarks Time O(n), Space O(n)
|
|
219
|
+
*/
|
|
220
|
+
toVisual() {
|
|
221
|
+
return [...this];
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Print a human-friendly representation to the console.
|
|
225
|
+
* @remarks Time O(n), Space O(n)
|
|
226
|
+
*/
|
|
227
|
+
print() {
|
|
228
|
+
console.log(this.toVisual());
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
__name(_IterableEntryBase, "IterableEntryBase");
|
|
232
|
+
var IterableEntryBase = _IterableEntryBase;
|
|
233
|
+
|
|
234
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
235
|
+
var _IterableElementBase = class _IterableElementBase {
|
|
236
|
+
/**
|
|
237
|
+
* Create a new iterable base.
|
|
238
|
+
*
|
|
239
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
240
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* Time O(1), Space O(1).
|
|
244
|
+
*/
|
|
245
|
+
constructor(options) {
|
|
246
|
+
/**
|
|
247
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
248
|
+
*
|
|
249
|
+
* @remarks
|
|
250
|
+
* Time O(1), Space O(1).
|
|
251
|
+
*/
|
|
252
|
+
__publicField(this, "_toElementFn");
|
|
253
|
+
if (options) {
|
|
254
|
+
const { toElementFn } = options;
|
|
255
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
256
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Exposes the current `toElementFn`, if configured.
|
|
261
|
+
*
|
|
262
|
+
* @returns The converter function or `undefined` when not set.
|
|
263
|
+
* @remarks
|
|
264
|
+
* Time O(1), Space O(1).
|
|
265
|
+
*/
|
|
266
|
+
get toElementFn() {
|
|
267
|
+
return this._toElementFn;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Returns an iterator over the structure's elements.
|
|
271
|
+
*
|
|
272
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
273
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
274
|
+
*
|
|
275
|
+
* @remarks
|
|
276
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
277
|
+
*/
|
|
278
|
+
*[Symbol.iterator](...args) {
|
|
279
|
+
yield* this._getIterator(...args);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
283
|
+
*
|
|
284
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
285
|
+
* @remarks
|
|
286
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
287
|
+
*/
|
|
288
|
+
*values() {
|
|
289
|
+
for (const item of this) yield item;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Tests whether all elements satisfy the predicate.
|
|
293
|
+
*
|
|
294
|
+
* @template TReturn
|
|
295
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
296
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
297
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
298
|
+
*
|
|
299
|
+
* @remarks
|
|
300
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
301
|
+
*/
|
|
302
|
+
every(predicate, thisArg) {
|
|
303
|
+
let index = 0;
|
|
304
|
+
for (const item of this) {
|
|
305
|
+
if (thisArg === void 0) {
|
|
306
|
+
if (!predicate(item, index++, this)) return false;
|
|
307
|
+
} else {
|
|
308
|
+
const fn = predicate;
|
|
309
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Tests whether at least one element satisfies the predicate.
|
|
316
|
+
*
|
|
317
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
318
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
319
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
320
|
+
*
|
|
321
|
+
* @remarks
|
|
322
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
323
|
+
*/
|
|
324
|
+
some(predicate, thisArg) {
|
|
325
|
+
let index = 0;
|
|
326
|
+
for (const item of this) {
|
|
327
|
+
if (thisArg === void 0) {
|
|
328
|
+
if (predicate(item, index++, this)) return true;
|
|
329
|
+
} else {
|
|
330
|
+
const fn = predicate;
|
|
331
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Invokes a callback for each element in iteration order.
|
|
338
|
+
*
|
|
339
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
340
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
341
|
+
* @returns `void`.
|
|
342
|
+
*
|
|
343
|
+
* @remarks
|
|
344
|
+
* Time O(n), Space O(1).
|
|
345
|
+
*/
|
|
346
|
+
forEach(callbackfn, thisArg) {
|
|
347
|
+
let index = 0;
|
|
348
|
+
for (const item of this) {
|
|
349
|
+
if (thisArg === void 0) {
|
|
350
|
+
callbackfn(item, index++, this);
|
|
351
|
+
} else {
|
|
352
|
+
const fn = callbackfn;
|
|
353
|
+
fn.call(thisArg, item, index++, this);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// Implementation signature
|
|
358
|
+
find(predicate, thisArg) {
|
|
359
|
+
let index = 0;
|
|
360
|
+
for (const item of this) {
|
|
361
|
+
if (thisArg === void 0) {
|
|
362
|
+
if (predicate(item, index++, this)) return item;
|
|
363
|
+
} else {
|
|
364
|
+
const fn = predicate;
|
|
365
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
372
|
+
*
|
|
373
|
+
* @param element The element to test with `===` equality.
|
|
374
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
375
|
+
*
|
|
376
|
+
* @remarks
|
|
377
|
+
* Time O(n) in the worst case. Space O(1).
|
|
378
|
+
*/
|
|
379
|
+
has(element) {
|
|
380
|
+
for (const ele of this) if (ele === element) return true;
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Reduces all elements to a single accumulated value.
|
|
385
|
+
*
|
|
386
|
+
* @overload
|
|
387
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
388
|
+
* @returns The final accumulated value typed as `E`.
|
|
389
|
+
*
|
|
390
|
+
* @overload
|
|
391
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
392
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
393
|
+
* @returns The final accumulated value typed as `E`.
|
|
394
|
+
*
|
|
395
|
+
* @overload
|
|
396
|
+
* @template U The accumulator type when it differs from `E`.
|
|
397
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
398
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
399
|
+
* @returns The final accumulated value typed as `U`.
|
|
400
|
+
*
|
|
401
|
+
* @remarks
|
|
402
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
403
|
+
*/
|
|
404
|
+
reduce(callbackfn, initialValue) {
|
|
405
|
+
let index = 0;
|
|
406
|
+
const iter = this[Symbol.iterator]();
|
|
407
|
+
let acc;
|
|
408
|
+
if (arguments.length >= 2) {
|
|
409
|
+
acc = initialValue;
|
|
410
|
+
} else {
|
|
411
|
+
const first = iter.next();
|
|
412
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
413
|
+
acc = first.value;
|
|
414
|
+
index = 1;
|
|
415
|
+
}
|
|
416
|
+
for (const value of iter) {
|
|
417
|
+
acc = callbackfn(acc, value, index++, this);
|
|
418
|
+
}
|
|
419
|
+
return acc;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Materializes the elements into a new array.
|
|
423
|
+
*
|
|
424
|
+
* @returns A shallow array copy of the iteration order.
|
|
425
|
+
* @remarks
|
|
426
|
+
* Time O(n), Space O(n).
|
|
427
|
+
*/
|
|
428
|
+
toArray() {
|
|
429
|
+
return [...this];
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
433
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
434
|
+
*
|
|
435
|
+
* @returns A visual representation (array by default).
|
|
436
|
+
* @remarks
|
|
437
|
+
* Time O(n), Space O(n).
|
|
438
|
+
*/
|
|
439
|
+
toVisual() {
|
|
440
|
+
return [...this];
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
444
|
+
*
|
|
445
|
+
* @returns `void`.
|
|
446
|
+
* @remarks
|
|
447
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
448
|
+
*/
|
|
449
|
+
print() {
|
|
450
|
+
console.log(this.toVisual());
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
__name(_IterableElementBase, "IterableElementBase");
|
|
454
|
+
var IterableElementBase = _IterableElementBase;
|
|
455
|
+
|
|
456
|
+
// src/data-structures/heap/heap.ts
|
|
457
|
+
var _Heap = class _Heap extends IterableElementBase {
|
|
458
|
+
/**
|
|
459
|
+
* Create a Heap and optionally bulk-insert elements.
|
|
460
|
+
* @remarks Time O(N), Space O(N)
|
|
461
|
+
* @param [elements] - Iterable of elements (or raw values if toElementFn is set).
|
|
462
|
+
* @param [options] - Options such as comparator and toElementFn.
|
|
463
|
+
* @returns New Heap instance.
|
|
464
|
+
*/
|
|
465
|
+
constructor(elements = [], options) {
|
|
466
|
+
super(options);
|
|
467
|
+
__publicField(this, "_equals", Object.is);
|
|
468
|
+
__publicField(this, "_elements", []);
|
|
469
|
+
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
470
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
471
|
+
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
472
|
+
}
|
|
473
|
+
if (a > b) return 1;
|
|
474
|
+
if (a < b) return -1;
|
|
475
|
+
return 0;
|
|
476
|
+
}, "_DEFAULT_COMPARATOR"));
|
|
477
|
+
__publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
|
|
478
|
+
if (options) {
|
|
479
|
+
const { comparator } = options;
|
|
480
|
+
if (comparator) this._comparator = comparator;
|
|
481
|
+
}
|
|
482
|
+
this.addMany(elements);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Get the backing array of the heap.
|
|
486
|
+
* @remarks Time O(1), Space O(1)
|
|
487
|
+
* @returns Internal elements array.
|
|
488
|
+
*/
|
|
489
|
+
get elements() {
|
|
490
|
+
return this._elements;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Get the number of elements.
|
|
494
|
+
* @remarks Time O(1), Space O(1)
|
|
495
|
+
* @returns Heap size.
|
|
496
|
+
*/
|
|
497
|
+
get size() {
|
|
498
|
+
return this.elements.length;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Get the last leaf element.
|
|
502
|
+
* @remarks Time O(1), Space O(1)
|
|
503
|
+
* @returns Last element or undefined.
|
|
504
|
+
*/
|
|
505
|
+
get leaf() {
|
|
506
|
+
var _a;
|
|
507
|
+
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Create a heap of the same class from an iterable.
|
|
511
|
+
* @remarks Time O(N), Space O(N)
|
|
512
|
+
* @template T
|
|
513
|
+
* @template R
|
|
514
|
+
* @template S
|
|
515
|
+
* @param [elements] - Iterable of elements or raw records.
|
|
516
|
+
* @param [options] - Heap options including comparator.
|
|
517
|
+
* @returns A new heap instance of this class.
|
|
518
|
+
*/
|
|
519
|
+
static from(elements, options) {
|
|
520
|
+
return new this(elements, options);
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Build a Heap from an iterable in linear time given a comparator.
|
|
524
|
+
* @remarks Time O(N), Space O(N)
|
|
525
|
+
* @template EE
|
|
526
|
+
* @template RR
|
|
527
|
+
* @param elements - Iterable of elements.
|
|
528
|
+
* @param options - Heap options including comparator.
|
|
529
|
+
* @returns A new Heap built from elements.
|
|
530
|
+
*/
|
|
531
|
+
static heapify(elements, options) {
|
|
532
|
+
return new _Heap(elements, options);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Insert an element.
|
|
536
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
537
|
+
* @param element - Element to insert.
|
|
538
|
+
* @returns True.
|
|
539
|
+
*/
|
|
540
|
+
add(element) {
|
|
541
|
+
this._elements.push(element);
|
|
542
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Insert many elements from an iterable.
|
|
546
|
+
* @remarks Time O(N log N), Space O(1)
|
|
547
|
+
* @param elements - Iterable of elements or raw values.
|
|
548
|
+
* @returns Array of per-element success flags.
|
|
549
|
+
*/
|
|
550
|
+
addMany(elements) {
|
|
551
|
+
const flags = [];
|
|
552
|
+
for (const el of elements) {
|
|
553
|
+
if (this.toElementFn) {
|
|
554
|
+
const ok = this.add(this.toElementFn(el));
|
|
555
|
+
flags.push(ok);
|
|
556
|
+
} else {
|
|
557
|
+
const ok = this.add(el);
|
|
558
|
+
flags.push(ok);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return flags;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Remove and return the top element.
|
|
565
|
+
* @remarks Time O(log N), Space O(1)
|
|
566
|
+
* @returns Top element or undefined.
|
|
567
|
+
*/
|
|
568
|
+
poll() {
|
|
569
|
+
if (this.elements.length === 0) return;
|
|
570
|
+
const value = this.elements[0];
|
|
571
|
+
const last = this.elements.pop();
|
|
572
|
+
if (this.elements.length) {
|
|
573
|
+
this.elements[0] = last;
|
|
574
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
575
|
+
}
|
|
576
|
+
return value;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Get the current top element without removing it.
|
|
580
|
+
* @remarks Time O(1), Space O(1)
|
|
581
|
+
* @returns Top element or undefined.
|
|
582
|
+
*/
|
|
583
|
+
peek() {
|
|
584
|
+
return this.elements[0];
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Check whether the heap is empty.
|
|
588
|
+
* @remarks Time O(1), Space O(1)
|
|
589
|
+
* @returns True if size is 0.
|
|
590
|
+
*/
|
|
591
|
+
isEmpty() {
|
|
592
|
+
return this.size === 0;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Remove all elements.
|
|
596
|
+
* @remarks Time O(1), Space O(1)
|
|
597
|
+
* @returns void
|
|
598
|
+
*/
|
|
599
|
+
clear() {
|
|
600
|
+
this._elements = [];
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Replace the backing array and rebuild the heap.
|
|
604
|
+
* @remarks Time O(N), Space O(N)
|
|
605
|
+
* @param elements - Iterable used to refill the heap.
|
|
606
|
+
* @returns Array of per-node results from fixing steps.
|
|
607
|
+
*/
|
|
608
|
+
refill(elements) {
|
|
609
|
+
this._elements = Array.from(elements);
|
|
610
|
+
return this.fix();
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Check if an equal element exists in the heap.
|
|
614
|
+
* @remarks Time O(N), Space O(1)
|
|
615
|
+
* @param element - Element to search for.
|
|
616
|
+
* @returns True if found.
|
|
617
|
+
*/
|
|
618
|
+
has(element) {
|
|
619
|
+
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
620
|
+
return false;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Delete one occurrence of an element.
|
|
624
|
+
* @remarks Time O(N), Space O(1)
|
|
625
|
+
* @param element - Element to delete.
|
|
626
|
+
* @returns True if an element was removed.
|
|
627
|
+
*/
|
|
628
|
+
delete(element) {
|
|
629
|
+
let index = -1;
|
|
630
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
631
|
+
if (this._equals(this.elements[i], element)) {
|
|
632
|
+
index = i;
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
if (index < 0) return false;
|
|
637
|
+
if (index === 0) {
|
|
638
|
+
this.poll();
|
|
639
|
+
} else if (index === this.elements.length - 1) {
|
|
640
|
+
this.elements.pop();
|
|
641
|
+
} else {
|
|
642
|
+
this.elements.splice(index, 1, this.elements.pop());
|
|
643
|
+
this._bubbleUp(index);
|
|
644
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
645
|
+
}
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Delete the first element that matches a predicate.
|
|
650
|
+
* @remarks Time O(N), Space O(1)
|
|
651
|
+
* @param predicate - Function (element, index, heap) → boolean.
|
|
652
|
+
* @returns True if an element was removed.
|
|
653
|
+
*/
|
|
654
|
+
deleteBy(predicate) {
|
|
655
|
+
let idx = -1;
|
|
656
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
657
|
+
if (predicate(this.elements[i], i, this)) {
|
|
658
|
+
idx = i;
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (idx < 0) return false;
|
|
663
|
+
if (idx === 0) {
|
|
664
|
+
this.poll();
|
|
665
|
+
} else if (idx === this.elements.length - 1) {
|
|
666
|
+
this.elements.pop();
|
|
667
|
+
} else {
|
|
668
|
+
this.elements.splice(idx, 1, this.elements.pop());
|
|
669
|
+
this._bubbleUp(idx);
|
|
670
|
+
this._sinkDown(idx, this.elements.length >> 1);
|
|
671
|
+
}
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Set the equality comparator used by has/delete operations.
|
|
676
|
+
* @remarks Time O(1), Space O(1)
|
|
677
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
678
|
+
* @returns This heap.
|
|
679
|
+
*/
|
|
680
|
+
setEquality(equals) {
|
|
681
|
+
this._equals = equals;
|
|
682
|
+
return this;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
686
|
+
* @remarks Time O(N), Space O(H)
|
|
687
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
688
|
+
* @returns Array of visited elements.
|
|
689
|
+
*/
|
|
690
|
+
dfs(order = "PRE") {
|
|
691
|
+
const result = [];
|
|
692
|
+
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
693
|
+
const left = 2 * index + 1, right = left + 1;
|
|
694
|
+
if (index < this.size) {
|
|
695
|
+
if (order === "IN") {
|
|
696
|
+
_dfs(left);
|
|
697
|
+
result.push(this.elements[index]);
|
|
698
|
+
_dfs(right);
|
|
699
|
+
} else if (order === "PRE") {
|
|
700
|
+
result.push(this.elements[index]);
|
|
701
|
+
_dfs(left);
|
|
702
|
+
_dfs(right);
|
|
703
|
+
} else if (order === "POST") {
|
|
704
|
+
_dfs(left);
|
|
705
|
+
_dfs(right);
|
|
706
|
+
result.push(this.elements[index]);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}, "_dfs");
|
|
710
|
+
_dfs(0);
|
|
711
|
+
return result;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Restore heap order bottom-up (heapify in-place).
|
|
715
|
+
* @remarks Time O(N), Space O(1)
|
|
716
|
+
* @returns Array of per-node results from fixing steps.
|
|
717
|
+
*/
|
|
718
|
+
fix() {
|
|
719
|
+
const results = [];
|
|
720
|
+
for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
|
|
721
|
+
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
722
|
+
}
|
|
723
|
+
return results;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
727
|
+
* @remarks Time O(N log N), Space O(N)
|
|
728
|
+
* @returns Sorted array of elements.
|
|
729
|
+
*/
|
|
730
|
+
sort() {
|
|
731
|
+
const visited = [];
|
|
732
|
+
const cloned = this._createInstance();
|
|
733
|
+
for (const x of this.elements) cloned.add(x);
|
|
734
|
+
while (!cloned.isEmpty()) {
|
|
735
|
+
const top = cloned.poll();
|
|
736
|
+
if (top !== void 0) visited.push(top);
|
|
737
|
+
}
|
|
738
|
+
return visited;
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Deep clone this heap.
|
|
742
|
+
* @remarks Time O(N), Space O(N)
|
|
743
|
+
* @returns A new heap with the same elements.
|
|
744
|
+
*/
|
|
745
|
+
clone() {
|
|
746
|
+
const next = this._createInstance();
|
|
747
|
+
for (const x of this.elements) next.add(x);
|
|
748
|
+
return next;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Filter elements into a new heap of the same class.
|
|
752
|
+
* @remarks Time O(N log N), Space O(N)
|
|
753
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
754
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
755
|
+
* @returns A new heap with the kept elements.
|
|
756
|
+
*/
|
|
757
|
+
filter(callback, thisArg) {
|
|
758
|
+
const out = this._createInstance();
|
|
759
|
+
let i = 0;
|
|
760
|
+
for (const x of this) {
|
|
761
|
+
if (thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this)) {
|
|
762
|
+
out.add(x);
|
|
763
|
+
} else {
|
|
764
|
+
i++;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return out;
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* Map elements into a new heap of possibly different element type.
|
|
771
|
+
* @remarks Time O(N log N), Space O(N)
|
|
772
|
+
* @template EM
|
|
773
|
+
* @template RM
|
|
774
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
775
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
776
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
777
|
+
* @returns A new heap with mapped elements.
|
|
778
|
+
*/
|
|
779
|
+
map(callback, options, thisArg) {
|
|
780
|
+
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
781
|
+
if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
|
|
782
|
+
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
783
|
+
let i = 0;
|
|
784
|
+
for (const x of this) {
|
|
785
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
786
|
+
out.add(v);
|
|
787
|
+
}
|
|
788
|
+
return out;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Map elements into a new heap of the same element type.
|
|
792
|
+
* @remarks Time O(N log N), Space O(N)
|
|
793
|
+
* @param callback - Mapping function (element, index, heap) → element.
|
|
794
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
795
|
+
* @returns A new heap with mapped elements.
|
|
796
|
+
*/
|
|
797
|
+
mapSame(callback, thisArg) {
|
|
798
|
+
const out = this._createInstance();
|
|
799
|
+
let i = 0;
|
|
800
|
+
for (const x of this) {
|
|
801
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
802
|
+
out.add(v);
|
|
803
|
+
}
|
|
804
|
+
return out;
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Get the comparator used to order elements.
|
|
808
|
+
* @remarks Time O(1), Space O(1)
|
|
809
|
+
* @returns Comparator function.
|
|
810
|
+
*/
|
|
811
|
+
/**
|
|
812
|
+
* Get the comparator used to order elements.
|
|
813
|
+
* @remarks Time O(1), Space O(1)
|
|
814
|
+
* @returns Comparator function.
|
|
815
|
+
*/
|
|
816
|
+
get comparator() {
|
|
817
|
+
return this._comparator;
|
|
818
|
+
}
|
|
819
|
+
*_getIterator() {
|
|
820
|
+
for (const element of this.elements) yield element;
|
|
821
|
+
}
|
|
822
|
+
_bubbleUp(index) {
|
|
823
|
+
const element = this.elements[index];
|
|
824
|
+
while (index > 0) {
|
|
825
|
+
const parent = index - 1 >> 1;
|
|
826
|
+
const parentItem = this.elements[parent];
|
|
827
|
+
if (this.comparator(parentItem, element) <= 0) break;
|
|
828
|
+
this.elements[index] = parentItem;
|
|
829
|
+
index = parent;
|
|
830
|
+
}
|
|
831
|
+
this.elements[index] = element;
|
|
832
|
+
return true;
|
|
833
|
+
}
|
|
834
|
+
_sinkDown(index, halfLength) {
|
|
835
|
+
const element = this.elements[index];
|
|
836
|
+
while (index < halfLength) {
|
|
837
|
+
let left = index << 1 | 1;
|
|
838
|
+
const right = left + 1;
|
|
839
|
+
let minItem = this.elements[left];
|
|
840
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
841
|
+
left = right;
|
|
842
|
+
minItem = this.elements[right];
|
|
843
|
+
}
|
|
844
|
+
if (this.comparator(minItem, element) >= 0) break;
|
|
845
|
+
this.elements[index] = minItem;
|
|
846
|
+
index = left;
|
|
847
|
+
}
|
|
848
|
+
this.elements[index] = element;
|
|
849
|
+
return true;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
853
|
+
* @remarks Time O(1), Space O(1)
|
|
854
|
+
* @param [options] - Options to override comparator or toElementFn.
|
|
855
|
+
* @returns A like-kind empty heap instance.
|
|
856
|
+
*/
|
|
857
|
+
_createInstance(options) {
|
|
858
|
+
const Ctor = this.constructor;
|
|
859
|
+
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options != null ? options : {} });
|
|
860
|
+
return next;
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* (Protected) Create a like-kind instance seeded by elements.
|
|
864
|
+
* @remarks Time O(N log N), Space O(N)
|
|
865
|
+
* @template EM
|
|
866
|
+
* @template RM
|
|
867
|
+
* @param [elements] - Iterable of elements or raw values to seed.
|
|
868
|
+
* @param [options] - Options forwarded to the constructor.
|
|
869
|
+
* @returns A like-kind heap instance.
|
|
870
|
+
*/
|
|
871
|
+
_createLike(elements = [], options) {
|
|
872
|
+
const Ctor = this.constructor;
|
|
873
|
+
return new Ctor(elements, options);
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* (Protected) Spawn an empty like-kind heap instance.
|
|
877
|
+
* @remarks Time O(1), Space O(1)
|
|
878
|
+
* @template EM
|
|
879
|
+
* @template RM
|
|
880
|
+
* @param [options] - Options forwarded to the constructor.
|
|
881
|
+
* @returns An empty like-kind heap instance.
|
|
882
|
+
*/
|
|
883
|
+
_spawnLike(options) {
|
|
884
|
+
return this._createLike([], options);
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
__name(_Heap, "Heap");
|
|
888
|
+
var Heap = _Heap;
|
|
889
|
+
|
|
890
|
+
// src/data-structures/base/linear-base.ts
|
|
891
|
+
var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
892
|
+
/**
|
|
893
|
+
* Construct a linear container with runtime options.
|
|
894
|
+
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
895
|
+
* @remarks Time O(1), Space O(1)
|
|
896
|
+
*/
|
|
897
|
+
constructor(options) {
|
|
898
|
+
super(options);
|
|
899
|
+
__publicField(this, "_maxLen", -1);
|
|
900
|
+
if (options) {
|
|
901
|
+
const { maxLen } = options;
|
|
902
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
907
|
+
* @returns Maximum allowed length.
|
|
908
|
+
* @remarks Time O(1), Space O(1)
|
|
909
|
+
*/
|
|
910
|
+
get maxLen() {
|
|
911
|
+
return this._maxLen;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* First index of a value from the left.
|
|
915
|
+
* @param searchElement - Value to match.
|
|
916
|
+
* @param fromIndex - Start position (supports negative index).
|
|
917
|
+
* @returns Index or `-1` if not found.
|
|
918
|
+
* @remarks Time O(n), Space O(1)
|
|
919
|
+
*/
|
|
920
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
921
|
+
if (this.length === 0) return -1;
|
|
922
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
923
|
+
if (fromIndex < 0) fromIndex = 0;
|
|
924
|
+
for (let i = fromIndex; i < this.length; i++) {
|
|
925
|
+
const element = this.at(i);
|
|
926
|
+
if (element === searchElement) return i;
|
|
927
|
+
}
|
|
928
|
+
return -1;
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* Last index of a value from the right.
|
|
932
|
+
* @param searchElement - Value to match.
|
|
933
|
+
* @param fromIndex - Start position (supports negative index).
|
|
934
|
+
* @returns Index or `-1` if not found.
|
|
935
|
+
* @remarks Time O(n), Space O(1)
|
|
936
|
+
*/
|
|
937
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
938
|
+
if (this.length === 0) return -1;
|
|
939
|
+
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
940
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
941
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
942
|
+
const element = this.at(i);
|
|
943
|
+
if (element === searchElement) return i;
|
|
944
|
+
}
|
|
945
|
+
return -1;
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Find the first index matching a predicate.
|
|
949
|
+
* @param predicate - `(element, index, self) => boolean`.
|
|
950
|
+
* @param thisArg - Optional `this` for callback.
|
|
951
|
+
* @returns Index or `-1`.
|
|
952
|
+
* @remarks Time O(n), Space O(1)
|
|
953
|
+
*/
|
|
954
|
+
findIndex(predicate, thisArg) {
|
|
955
|
+
for (let i = 0; i < this.length; i++) {
|
|
956
|
+
const item = this.at(i);
|
|
957
|
+
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
958
|
+
}
|
|
959
|
+
return -1;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Concatenate elements and/or containers.
|
|
963
|
+
* @param items - Elements or other containers.
|
|
964
|
+
* @returns New container with combined elements (`this` type).
|
|
965
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
966
|
+
*/
|
|
967
|
+
concat(...items) {
|
|
968
|
+
const newList = this.clone();
|
|
969
|
+
for (const item of items) {
|
|
970
|
+
if (item instanceof _LinearBase) {
|
|
971
|
+
newList.pushMany(item);
|
|
972
|
+
} else {
|
|
973
|
+
newList.push(item);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return newList;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* In-place stable order via array sort semantics.
|
|
980
|
+
* @param compareFn - Comparator `(a, b) => number`.
|
|
981
|
+
* @returns This container.
|
|
982
|
+
* @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
|
|
983
|
+
*/
|
|
984
|
+
sort(compareFn) {
|
|
985
|
+
const arr = this.toArray();
|
|
986
|
+
arr.sort(compareFn);
|
|
987
|
+
this.clear();
|
|
988
|
+
for (const item of arr) this.push(item);
|
|
989
|
+
return this;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Remove and/or insert elements at a position (array-compatible).
|
|
993
|
+
* @param start - Start index (supports negative index).
|
|
994
|
+
* @param deleteCount - How many to remove.
|
|
995
|
+
* @param items - Elements to insert.
|
|
996
|
+
* @returns Removed elements as a new list (`this` type).
|
|
997
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
998
|
+
*/
|
|
999
|
+
splice(start, deleteCount = 0, ...items) {
|
|
1000
|
+
const removedList = this._createInstance();
|
|
1001
|
+
start = start < 0 ? this.length + start : start;
|
|
1002
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
1003
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
1004
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
1005
|
+
const removed = this.deleteAt(start);
|
|
1006
|
+
if (removed !== void 0) {
|
|
1007
|
+
removedList.push(removed);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
for (let i = 0; i < items.length; i++) {
|
|
1011
|
+
this.addAt(start + i, items[i]);
|
|
1012
|
+
}
|
|
1013
|
+
return removedList;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Join all elements into a string.
|
|
1017
|
+
* @param separator - Separator string.
|
|
1018
|
+
* @returns Concatenated string.
|
|
1019
|
+
* @remarks Time O(n), Space O(n)
|
|
1020
|
+
*/
|
|
1021
|
+
join(separator = ",") {
|
|
1022
|
+
return this.toArray().join(separator);
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Snapshot elements into a reversed array.
|
|
1026
|
+
* @returns New reversed array.
|
|
1027
|
+
* @remarks Time O(n), Space O(n)
|
|
1028
|
+
*/
|
|
1029
|
+
toReversedArray() {
|
|
1030
|
+
const array = [];
|
|
1031
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1032
|
+
array.push(this.at(i));
|
|
1033
|
+
}
|
|
1034
|
+
return array;
|
|
1035
|
+
}
|
|
1036
|
+
reduceRight(callbackfn, initialValue) {
|
|
1037
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
1038
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1039
|
+
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
1040
|
+
}
|
|
1041
|
+
return accumulator;
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Create a shallow copy of a subrange.
|
|
1045
|
+
* @param start - Inclusive start (supports negative index).
|
|
1046
|
+
* @param end - Exclusive end (supports negative index).
|
|
1047
|
+
* @returns New list with the range (`this` type).
|
|
1048
|
+
* @remarks Time O(n), Space O(n)
|
|
1049
|
+
*/
|
|
1050
|
+
slice(start = 0, end = this.length) {
|
|
1051
|
+
start = start < 0 ? this.length + start : start;
|
|
1052
|
+
end = end < 0 ? this.length + end : end;
|
|
1053
|
+
const newList = this._createInstance();
|
|
1054
|
+
for (let i = start; i < end; i++) {
|
|
1055
|
+
newList.push(this.at(i));
|
|
1056
|
+
}
|
|
1057
|
+
return newList;
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Fill a range with a value.
|
|
1061
|
+
* @param value - Value to set.
|
|
1062
|
+
* @param start - Inclusive start.
|
|
1063
|
+
* @param end - Exclusive end.
|
|
1064
|
+
* @returns This list.
|
|
1065
|
+
* @remarks Time O(n), Space O(1)
|
|
1066
|
+
*/
|
|
1067
|
+
fill(value, start = 0, end = this.length) {
|
|
1068
|
+
start = start < 0 ? this.length + start : start;
|
|
1069
|
+
end = end < 0 ? this.length + end : end;
|
|
1070
|
+
if (start < 0) start = 0;
|
|
1071
|
+
if (end > this.length) end = this.length;
|
|
1072
|
+
if (start >= end) return this;
|
|
1073
|
+
for (let i = start; i < end; i++) {
|
|
1074
|
+
this.setAt(i, value);
|
|
1075
|
+
}
|
|
1076
|
+
return this;
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
__name(_LinearBase, "LinearBase");
|
|
1080
|
+
var LinearBase = _LinearBase;
|
|
1081
|
+
|
|
1082
|
+
// src/data-structures/queue/queue.ts
|
|
1083
|
+
var _Queue = class _Queue extends LinearBase {
|
|
1084
|
+
/**
|
|
1085
|
+
* Create a Queue and optionally bulk-insert elements.
|
|
1086
|
+
* @remarks Time O(N), Space O(N)
|
|
1087
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
1088
|
+
* @param [options] - Options such as toElementFn, maxLen, and autoCompactRatio.
|
|
1089
|
+
* @returns New Queue instance.
|
|
1090
|
+
*/
|
|
1091
|
+
constructor(elements = [], options) {
|
|
1092
|
+
super(options);
|
|
1093
|
+
__publicField(this, "_elements", []);
|
|
1094
|
+
__publicField(this, "_offset", 0);
|
|
1095
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
1096
|
+
if (options) {
|
|
1097
|
+
const { autoCompactRatio = 0.5 } = options;
|
|
1098
|
+
this._autoCompactRatio = autoCompactRatio;
|
|
1099
|
+
}
|
|
1100
|
+
this.pushMany(elements);
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Get the underlying array buffer.
|
|
1104
|
+
* @remarks Time O(1), Space O(1)
|
|
1105
|
+
* @returns Backing array of elements.
|
|
1106
|
+
*/
|
|
1107
|
+
get elements() {
|
|
1108
|
+
return this._elements;
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Get the current start offset into the array.
|
|
1112
|
+
* @remarks Time O(1), Space O(1)
|
|
1113
|
+
* @returns Zero-based offset.
|
|
1114
|
+
*/
|
|
1115
|
+
get offset() {
|
|
1116
|
+
return this._offset;
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Get the compaction threshold (offset/size).
|
|
1120
|
+
* @remarks Time O(1), Space O(1)
|
|
1121
|
+
* @returns Auto-compaction ratio in (0,1].
|
|
1122
|
+
*/
|
|
1123
|
+
get autoCompactRatio() {
|
|
1124
|
+
return this._autoCompactRatio;
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Set the compaction threshold.
|
|
1128
|
+
* @remarks Time O(1), Space O(1)
|
|
1129
|
+
* @param value - New ratio; compacts when offset/size exceeds this value.
|
|
1130
|
+
* @returns void
|
|
1131
|
+
*/
|
|
1132
|
+
set autoCompactRatio(value) {
|
|
1133
|
+
this._autoCompactRatio = value;
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Get the number of elements currently in the queue.
|
|
1137
|
+
* @remarks Time O(1), Space O(1)
|
|
1138
|
+
* @returns Current length.
|
|
1139
|
+
*/
|
|
1140
|
+
get length() {
|
|
1141
|
+
return this.elements.length - this._offset;
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Get the first element (front) without removing it.
|
|
1145
|
+
* @remarks Time O(1), Space O(1)
|
|
1146
|
+
* @returns Front element or undefined.
|
|
1147
|
+
*/
|
|
1148
|
+
get first() {
|
|
1149
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Get the last element (back) without removing it.
|
|
1153
|
+
* @remarks Time O(1), Space O(1)
|
|
1154
|
+
* @returns Back element or undefined.
|
|
1155
|
+
*/
|
|
1156
|
+
get last() {
|
|
1157
|
+
return this.length > 0 ? this.elements[this.elements.length - 1] : void 0;
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Create a queue from an array of elements.
|
|
1161
|
+
* @remarks Time O(N), Space O(N)
|
|
1162
|
+
* @template E
|
|
1163
|
+
* @param elements - Array of elements to enqueue in order.
|
|
1164
|
+
* @returns A new queue populated from the array.
|
|
1165
|
+
*/
|
|
1166
|
+
static fromArray(elements) {
|
|
1167
|
+
return new _Queue(elements);
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Check whether the queue is empty.
|
|
1171
|
+
* @remarks Time O(1), Space O(1)
|
|
1172
|
+
* @returns True if length is 0.
|
|
1173
|
+
*/
|
|
1174
|
+
isEmpty() {
|
|
1175
|
+
return this.length === 0;
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Enqueue one element at the back.
|
|
1179
|
+
* @remarks Time O(1), Space O(1)
|
|
1180
|
+
* @param element - Element to enqueue.
|
|
1181
|
+
* @returns True on success.
|
|
1182
|
+
*/
|
|
1183
|
+
push(element) {
|
|
1184
|
+
this.elements.push(element);
|
|
1185
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
1186
|
+
return true;
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Enqueue many elements from an iterable.
|
|
1190
|
+
* @remarks Time O(N), Space O(1)
|
|
1191
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
1192
|
+
* @returns Array of per-element success flags.
|
|
1193
|
+
*/
|
|
1194
|
+
pushMany(elements) {
|
|
1195
|
+
const ans = [];
|
|
1196
|
+
for (const el of elements) {
|
|
1197
|
+
if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
|
|
1198
|
+
else ans.push(this.push(el));
|
|
1199
|
+
}
|
|
1200
|
+
return ans;
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1204
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1205
|
+
* @returns Removed element or undefined.
|
|
1206
|
+
*/
|
|
1207
|
+
shift() {
|
|
1208
|
+
if (this.length === 0) return void 0;
|
|
1209
|
+
const first = this.first;
|
|
1210
|
+
this._offset += 1;
|
|
1211
|
+
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio) this.compact();
|
|
1212
|
+
return first;
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Delete the first occurrence of a specific element.
|
|
1216
|
+
* @remarks Time O(N), Space O(1)
|
|
1217
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1218
|
+
* @returns True if an element was removed.
|
|
1219
|
+
*/
|
|
1220
|
+
delete(element) {
|
|
1221
|
+
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1222
|
+
if (Object.is(this.elements[i], element)) {
|
|
1223
|
+
this.elements.splice(i, 1);
|
|
1224
|
+
return true;
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
return false;
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Get the element at a given logical index.
|
|
1231
|
+
* @remarks Time O(1), Space O(1)
|
|
1232
|
+
* @param index - Zero-based index from the front.
|
|
1233
|
+
* @returns Element or undefined.
|
|
1234
|
+
*/
|
|
1235
|
+
at(index) {
|
|
1236
|
+
if (index < 0 || index >= this.length) return void 0;
|
|
1237
|
+
return this._elements[this._offset + index];
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Delete the element at a given index.
|
|
1241
|
+
* @remarks Time O(N), Space O(1)
|
|
1242
|
+
* @param index - Zero-based index from the front.
|
|
1243
|
+
* @returns Removed element or undefined.
|
|
1244
|
+
*/
|
|
1245
|
+
deleteAt(index) {
|
|
1246
|
+
if (index < 0 || index >= this.length) return void 0;
|
|
1247
|
+
const gi = this._offset + index;
|
|
1248
|
+
const [deleted] = this.elements.splice(gi, 1);
|
|
1249
|
+
return deleted;
|
|
1250
|
+
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Insert a new element at a given index.
|
|
1253
|
+
* @remarks Time O(N), Space O(1)
|
|
1254
|
+
* @param index - Zero-based index from the front.
|
|
1255
|
+
* @param newElement - Element to insert.
|
|
1256
|
+
* @returns True if inserted.
|
|
1257
|
+
*/
|
|
1258
|
+
addAt(index, newElement) {
|
|
1259
|
+
if (index < 0 || index > this.length) return false;
|
|
1260
|
+
this._elements.splice(this._offset + index, 0, newElement);
|
|
1261
|
+
return true;
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Replace the element at a given index.
|
|
1265
|
+
* @remarks Time O(1), Space O(1)
|
|
1266
|
+
* @param index - Zero-based index from the front.
|
|
1267
|
+
* @param newElement - New element to set.
|
|
1268
|
+
* @returns True if updated.
|
|
1269
|
+
*/
|
|
1270
|
+
setAt(index, newElement) {
|
|
1271
|
+
if (index < 0 || index >= this.length) return false;
|
|
1272
|
+
this._elements[this._offset + index] = newElement;
|
|
1273
|
+
return true;
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Reverse the queue in-place by compacting then reversing.
|
|
1277
|
+
* @remarks Time O(N), Space O(N)
|
|
1278
|
+
* @returns This queue.
|
|
1279
|
+
*/
|
|
1280
|
+
reverse() {
|
|
1281
|
+
this._elements = this.elements.slice(this._offset).reverse();
|
|
1282
|
+
this._offset = 0;
|
|
1283
|
+
return this;
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Remove all elements and reset offset.
|
|
1287
|
+
* @remarks Time O(1), Space O(1)
|
|
1288
|
+
* @returns void
|
|
1289
|
+
*/
|
|
1290
|
+
clear() {
|
|
1291
|
+
this._elements = [];
|
|
1292
|
+
this._offset = 0;
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Compact storage by discarding consumed head elements.
|
|
1296
|
+
* @remarks Time O(N), Space O(N)
|
|
1297
|
+
* @returns True when compaction performed.
|
|
1298
|
+
*/
|
|
1299
|
+
compact() {
|
|
1300
|
+
this._elements = this.elements.slice(this._offset);
|
|
1301
|
+
this._offset = 0;
|
|
1302
|
+
return true;
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Remove and/or insert elements at a position (array-like).
|
|
1306
|
+
* @remarks Time O(N + M), Space O(M)
|
|
1307
|
+
* @param start - Start index (clamped to [0, length]).
|
|
1308
|
+
* @param [deleteCount] - Number of elements to remove (default 0).
|
|
1309
|
+
* @param [items] - Elements to insert after `start`.
|
|
1310
|
+
* @returns A new queue containing the removed elements (typed as `this`).
|
|
1311
|
+
*/
|
|
1312
|
+
splice(start, deleteCount = 0, ...items) {
|
|
1313
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
1314
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
1315
|
+
const gi = this._offset + start;
|
|
1316
|
+
const removedArray = this._elements.splice(gi, deleteCount, ...items);
|
|
1317
|
+
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio) this.compact();
|
|
1318
|
+
const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1319
|
+
removed._setAutoCompactRatio(this._autoCompactRatio);
|
|
1320
|
+
removed.pushMany(removedArray);
|
|
1321
|
+
return removed;
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Deep clone this queue and its parameters.
|
|
1325
|
+
* @remarks Time O(N), Space O(N)
|
|
1326
|
+
* @returns A new queue with the same content and options.
|
|
1327
|
+
*/
|
|
1328
|
+
clone() {
|
|
1329
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1330
|
+
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
1331
|
+
for (let i = this._offset; i < this.elements.length; i++) out.push(this.elements[i]);
|
|
1332
|
+
return out;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* Filter elements into a new queue of the same class.
|
|
1336
|
+
* @remarks Time O(N), Space O(N)
|
|
1337
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1338
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1339
|
+
* @returns A new queue with kept elements.
|
|
1340
|
+
*/
|
|
1341
|
+
filter(predicate, thisArg) {
|
|
1342
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1343
|
+
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
1344
|
+
let index = 0;
|
|
1345
|
+
for (const v of this) {
|
|
1346
|
+
if (predicate.call(thisArg, v, index, this)) out.push(v);
|
|
1347
|
+
index++;
|
|
1348
|
+
}
|
|
1349
|
+
return out;
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1353
|
+
* @remarks Time O(N), Space O(N)
|
|
1354
|
+
* @template EM
|
|
1355
|
+
* @template RM
|
|
1356
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1357
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1358
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1359
|
+
* @returns A new Queue with mapped elements.
|
|
1360
|
+
*/
|
|
1361
|
+
map(callback, options, thisArg) {
|
|
1362
|
+
var _a, _b;
|
|
1363
|
+
const out = new this.constructor([], {
|
|
1364
|
+
toElementFn: options == null ? void 0 : options.toElementFn,
|
|
1365
|
+
maxLen: (_a = options == null ? void 0 : options.maxLen) != null ? _a : this._maxLen,
|
|
1366
|
+
autoCompactRatio: (_b = options == null ? void 0 : options.autoCompactRatio) != null ? _b : this._autoCompactRatio
|
|
1367
|
+
});
|
|
1368
|
+
let index = 0;
|
|
1369
|
+
for (const v of this)
|
|
1370
|
+
out.push(thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
|
|
1371
|
+
return out;
|
|
1372
|
+
}
|
|
1373
|
+
/**
|
|
1374
|
+
* Map each element to a new value of the same type.
|
|
1375
|
+
* @remarks Time O(N), Space O(N)
|
|
1376
|
+
* @param callback - Mapping function (element, index, queue) → element.
|
|
1377
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1378
|
+
* @returns A new queue with mapped elements (same element type).
|
|
1379
|
+
*/
|
|
1380
|
+
mapSame(callback, thisArg) {
|
|
1381
|
+
var _a;
|
|
1382
|
+
const Ctor = this.constructor;
|
|
1383
|
+
const out = new Ctor([], {
|
|
1384
|
+
toElementFn: this.toElementFn,
|
|
1385
|
+
maxLen: this._maxLen,
|
|
1386
|
+
autoCompactRatio: this._autoCompactRatio
|
|
1387
|
+
});
|
|
1388
|
+
(_a = out._setAutoCompactRatio) == null ? void 0 : _a.call(out, this._autoCompactRatio);
|
|
1389
|
+
let index = 0;
|
|
1390
|
+
for (const v of this) {
|
|
1391
|
+
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
1392
|
+
out.push(mv);
|
|
1393
|
+
}
|
|
1394
|
+
return out;
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* (Protected) Set the internal auto-compaction ratio.
|
|
1398
|
+
* @remarks Time O(1), Space O(1)
|
|
1399
|
+
* @param value - New ratio to assign.
|
|
1400
|
+
* @returns void
|
|
1401
|
+
*/
|
|
1402
|
+
_setAutoCompactRatio(value) {
|
|
1403
|
+
this._autoCompactRatio = value;
|
|
1404
|
+
}
|
|
1405
|
+
/**
|
|
1406
|
+
* (Protected) Iterate elements from front to back.
|
|
1407
|
+
* @remarks Time O(N), Space O(1)
|
|
1408
|
+
* @returns Iterator of E.
|
|
1409
|
+
*/
|
|
1410
|
+
*_getIterator() {
|
|
1411
|
+
for (let i = this._offset; i < this.elements.length; i++) yield this.elements[i];
|
|
1412
|
+
}
|
|
1413
|
+
/**
|
|
1414
|
+
* (Protected) Iterate elements from back to front.
|
|
1415
|
+
* @remarks Time O(N), Space O(1)
|
|
1416
|
+
* @returns Iterator of E.
|
|
1417
|
+
*/
|
|
1418
|
+
*_getReverseIterator() {
|
|
1419
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1420
|
+
const cur = this.at(i);
|
|
1421
|
+
if (cur !== void 0) yield cur;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
/**
|
|
1425
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
1426
|
+
* @remarks Time O(1), Space O(1)
|
|
1427
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1428
|
+
* @returns An empty like-kind queue instance.
|
|
1429
|
+
*/
|
|
1430
|
+
_createInstance(options) {
|
|
1431
|
+
const Ctor = this.constructor;
|
|
1432
|
+
return new Ctor([], options);
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* (Protected) Create a like-kind queue and seed it from an iterable.
|
|
1436
|
+
* @remarks Time O(N), Space O(N)
|
|
1437
|
+
* @template EM
|
|
1438
|
+
* @template RM
|
|
1439
|
+
* @param [elements] - Iterable used to seed the new queue.
|
|
1440
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1441
|
+
* @returns A like-kind Queue instance.
|
|
1442
|
+
*/
|
|
1443
|
+
_createLike(elements = [], options) {
|
|
1444
|
+
const Ctor = this.constructor;
|
|
1445
|
+
return new Ctor(elements, options);
|
|
1446
|
+
}
|
|
1447
|
+
};
|
|
1448
|
+
__name(_Queue, "Queue");
|
|
1449
|
+
var Queue = _Queue;
|
|
1450
|
+
|
|
1451
|
+
// src/data-structures/graph/abstract-graph.ts
|
|
1452
|
+
var _AbstractVertex = class _AbstractVertex {
|
|
1453
|
+
constructor(key, value) {
|
|
1454
|
+
__publicField(this, "key");
|
|
1455
|
+
__publicField(this, "value");
|
|
1456
|
+
this.key = key;
|
|
1457
|
+
this.value = value;
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
__name(_AbstractVertex, "AbstractVertex");
|
|
1461
|
+
var AbstractVertex = _AbstractVertex;
|
|
1462
|
+
var _AbstractEdge = class _AbstractEdge {
|
|
1463
|
+
constructor(weight, value) {
|
|
1464
|
+
__publicField(this, "value");
|
|
1465
|
+
__publicField(this, "weight");
|
|
1466
|
+
__publicField(this, "_hashCode");
|
|
1467
|
+
this.weight = weight !== void 0 ? weight : 1;
|
|
1468
|
+
this.value = value;
|
|
1469
|
+
this._hashCode = uuidV4();
|
|
1470
|
+
}
|
|
1471
|
+
get hashCode() {
|
|
1472
|
+
return this._hashCode;
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1475
|
+
__name(_AbstractEdge, "AbstractEdge");
|
|
1476
|
+
var AbstractEdge = _AbstractEdge;
|
|
1477
|
+
var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
1478
|
+
/**
|
|
1479
|
+
* Construct a graph with runtime defaults.
|
|
1480
|
+
* @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
1481
|
+
* @remarks Time O(1), Space O(1)
|
|
1482
|
+
*/
|
|
1483
|
+
constructor(options) {
|
|
1484
|
+
super();
|
|
1485
|
+
__publicField(this, "_options", { defaultEdgeWeight: 1 });
|
|
1486
|
+
__publicField(this, "_vertexMap", /* @__PURE__ */ new Map());
|
|
1487
|
+
const graph = options == null ? void 0 : options.graph;
|
|
1488
|
+
this._options = { defaultEdgeWeight: 1, ...graph != null ? graph : {} };
|
|
1489
|
+
}
|
|
1490
|
+
get options() {
|
|
1491
|
+
return this._options;
|
|
1492
|
+
}
|
|
1493
|
+
get vertexMap() {
|
|
1494
|
+
return this._vertexMap;
|
|
1495
|
+
}
|
|
1496
|
+
set vertexMap(v) {
|
|
1497
|
+
this._vertexMap = v;
|
|
1498
|
+
}
|
|
1499
|
+
get size() {
|
|
1500
|
+
return this._vertexMap.size;
|
|
1501
|
+
}
|
|
1502
|
+
/**
|
|
1503
|
+
* Get vertex instance by key.
|
|
1504
|
+
* @param vertexKey - Vertex key.
|
|
1505
|
+
* @returns Vertex instance or `undefined`.
|
|
1506
|
+
* @remarks Time O(1), Space O(1)
|
|
1507
|
+
*/
|
|
1508
|
+
getVertex(vertexKey) {
|
|
1509
|
+
return this._vertexMap.get(vertexKey) || void 0;
|
|
1510
|
+
}
|
|
1511
|
+
/**
|
|
1512
|
+
* Whether a vertex exists.
|
|
1513
|
+
* @param vertexOrKey - Vertex or key.
|
|
1514
|
+
* @returns `true` if present, otherwise `false`.
|
|
1515
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
1516
|
+
*/
|
|
1517
|
+
hasVertex(vertexOrKey) {
|
|
1518
|
+
return this._vertexMap.has(this._getVertexKey(vertexOrKey));
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Add a vertex by key/value or by pre-built vertex.
|
|
1522
|
+
* @param keyOrVertex - Vertex key or existing vertex instance.
|
|
1523
|
+
* @param value - Optional payload.
|
|
1524
|
+
* @returns `true` if inserted; `false` when key already exists.
|
|
1525
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
1526
|
+
*/
|
|
1527
|
+
addVertex(keyOrVertex, value) {
|
|
1528
|
+
if (keyOrVertex instanceof AbstractVertex) {
|
|
1529
|
+
return this._addVertex(keyOrVertex);
|
|
1530
|
+
} else {
|
|
1531
|
+
const newVertex = this.createVertex(keyOrVertex, value);
|
|
1532
|
+
return this._addVertex(newVertex);
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* Type guard: check if a value is a valid vertex key.
|
|
1537
|
+
* @param potentialKey - Value to test.
|
|
1538
|
+
* @returns `true` if string/number; else `false`.
|
|
1539
|
+
* @remarks Time O(1), Space O(1)
|
|
1540
|
+
*/
|
|
1541
|
+
isVertexKey(potentialKey) {
|
|
1542
|
+
const potentialKeyType = typeof potentialKey;
|
|
1543
|
+
return potentialKeyType === "string" || potentialKeyType === "number";
|
|
1544
|
+
}
|
|
1545
|
+
/**
|
|
1546
|
+
* Delete multiple vertices.
|
|
1547
|
+
* @param vertexMap - Array of vertices or keys.
|
|
1548
|
+
* @returns `true` if any vertex was removed.
|
|
1549
|
+
* @remarks Time O(sum(deg)), Space O(1)
|
|
1550
|
+
*/
|
|
1551
|
+
removeManyVertices(vertexMap) {
|
|
1552
|
+
const removed = [];
|
|
1553
|
+
for (const v of vertexMap) {
|
|
1554
|
+
removed.push(this.deleteVertex(v));
|
|
1555
|
+
}
|
|
1556
|
+
return removed.length > 0;
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Whether an edge exists between two vertices.
|
|
1560
|
+
* @param v1 - Endpoint A vertex or key.
|
|
1561
|
+
* @param v2 - Endpoint B vertex or key.
|
|
1562
|
+
* @returns `true` if present; otherwise `false`.
|
|
1563
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
1564
|
+
*/
|
|
1565
|
+
hasEdge(v1, v2) {
|
|
1566
|
+
const edge = this.getEdge(v1, v2);
|
|
1567
|
+
return !!edge;
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* Add an edge by instance or by `(src, dest, weight?, value?)`.
|
|
1571
|
+
* @param srcOrEdge - Edge instance or source vertex/key.
|
|
1572
|
+
* @param dest - Destination vertex/key (when adding by pair).
|
|
1573
|
+
* @param weight - Edge weight.
|
|
1574
|
+
* @param value - Edge payload.
|
|
1575
|
+
* @returns `true` if inserted; otherwise `false`.
|
|
1576
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
1577
|
+
*/
|
|
1578
|
+
addEdge(srcOrEdge, dest, weight, value) {
|
|
1579
|
+
if (srcOrEdge instanceof AbstractEdge) {
|
|
1580
|
+
return this._addEdge(srcOrEdge);
|
|
1581
|
+
} else {
|
|
1582
|
+
if (dest instanceof AbstractVertex || typeof dest === "string" || typeof dest === "number") {
|
|
1583
|
+
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
1584
|
+
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
1585
|
+
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
1586
|
+
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
1587
|
+
return this._addEdge(newEdge);
|
|
1588
|
+
} else {
|
|
1589
|
+
throw new Error("dest must be a Vertex or vertex key while srcOrEdge is an Edge");
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
/**
|
|
1594
|
+
* Set the weight of an existing edge.
|
|
1595
|
+
* @param srcOrKey - Source vertex or key.
|
|
1596
|
+
* @param destOrKey - Destination vertex or key.
|
|
1597
|
+
* @param weight - New weight.
|
|
1598
|
+
* @returns `true` if updated; otherwise `false`.
|
|
1599
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
1600
|
+
*/
|
|
1601
|
+
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
1602
|
+
const edge = this.getEdge(srcOrKey, destOrKey);
|
|
1603
|
+
if (edge) {
|
|
1604
|
+
edge.weight = weight;
|
|
1605
|
+
return true;
|
|
1606
|
+
} else {
|
|
1607
|
+
return false;
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
/**
|
|
1611
|
+
* Enumerate simple paths up to a limit.
|
|
1612
|
+
* @param v1 - Source vertex or key.
|
|
1613
|
+
* @param v2 - Destination vertex or key.
|
|
1614
|
+
* @param limit - Maximum number of paths to collect.
|
|
1615
|
+
* @returns Array of paths (each path is an array of vertices).
|
|
1616
|
+
* @remarks Time O(paths) worst-case exponential, Space O(V + paths)
|
|
1617
|
+
*/
|
|
1618
|
+
getAllPathsBetween(v1, v2, limit = 1e3) {
|
|
1619
|
+
const paths = [];
|
|
1620
|
+
const vertex1 = this._getVertex(v1);
|
|
1621
|
+
const vertex2 = this._getVertex(v2);
|
|
1622
|
+
if (!(vertex1 && vertex2)) {
|
|
1623
|
+
return [];
|
|
1624
|
+
}
|
|
1625
|
+
const stack = [];
|
|
1626
|
+
stack.push({ vertex: vertex1, path: [vertex1] });
|
|
1627
|
+
while (stack.length > 0) {
|
|
1628
|
+
const { vertex, path } = stack.pop();
|
|
1629
|
+
if (vertex === vertex2) {
|
|
1630
|
+
paths.push(path);
|
|
1631
|
+
if (paths.length >= limit) return paths;
|
|
1632
|
+
}
|
|
1633
|
+
const neighbors = this.getNeighbors(vertex);
|
|
1634
|
+
for (const neighbor of neighbors) {
|
|
1635
|
+
if (!path.includes(neighbor)) {
|
|
1636
|
+
const newPath = [...path, neighbor];
|
|
1637
|
+
stack.push({ vertex: neighbor, path: newPath });
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
return paths;
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Sum the weights along a vertex path.
|
|
1645
|
+
* @param path - Sequence of vertices.
|
|
1646
|
+
* @returns Path weight sum (0 if empty or edge missing).
|
|
1647
|
+
* @remarks Time O(L), Space O(1) where L is path length
|
|
1648
|
+
*/
|
|
1649
|
+
getPathSumWeight(path) {
|
|
1650
|
+
var _a;
|
|
1651
|
+
let sum = 0;
|
|
1652
|
+
for (let i = 0; i < path.length; i++) {
|
|
1653
|
+
sum += ((_a = this.getEdge(path[i], path[i + 1])) == null ? void 0 : _a.weight) || 0;
|
|
1654
|
+
}
|
|
1655
|
+
return sum;
|
|
1656
|
+
}
|
|
1657
|
+
/**
|
|
1658
|
+
* Minimum hops/weight between two vertices.
|
|
1659
|
+
* @param v1 - Source vertex or key.
|
|
1660
|
+
* @param v2 - Destination vertex or key.
|
|
1661
|
+
* @param isWeight - If `true`, compare by path weight; otherwise by hop count.
|
|
1662
|
+
* @returns Minimum cost or `undefined` if missing/unreachable.
|
|
1663
|
+
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
1664
|
+
*/
|
|
1665
|
+
getMinCostBetween(v1, v2, isWeight) {
|
|
1666
|
+
if (isWeight === void 0) isWeight = false;
|
|
1667
|
+
if (isWeight) {
|
|
1668
|
+
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
1669
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
1670
|
+
for (const path of allPaths) {
|
|
1671
|
+
min = Math.min(this.getPathSumWeight(path), min);
|
|
1672
|
+
}
|
|
1673
|
+
return min;
|
|
1674
|
+
} else {
|
|
1675
|
+
const vertex2 = this._getVertex(v2);
|
|
1676
|
+
const vertex1 = this._getVertex(v1);
|
|
1677
|
+
if (!(vertex1 && vertex2)) {
|
|
1678
|
+
return void 0;
|
|
1679
|
+
}
|
|
1680
|
+
const visited = /* @__PURE__ */ new Map();
|
|
1681
|
+
const queue = new Queue([vertex1]);
|
|
1682
|
+
visited.set(vertex1, true);
|
|
1683
|
+
let cost = 0;
|
|
1684
|
+
while (queue.length > 0) {
|
|
1685
|
+
for (let i = 0, layerSize = queue.length; i < layerSize; i++) {
|
|
1686
|
+
const cur = queue.shift();
|
|
1687
|
+
if (cur === vertex2) {
|
|
1688
|
+
return cost;
|
|
1689
|
+
}
|
|
1690
|
+
if (cur !== void 0) {
|
|
1691
|
+
const neighbors = this.getNeighbors(cur);
|
|
1692
|
+
for (const neighbor of neighbors) {
|
|
1693
|
+
if (!visited.has(neighbor)) {
|
|
1694
|
+
visited.set(neighbor, true);
|
|
1695
|
+
queue.push(neighbor);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
cost++;
|
|
1701
|
+
}
|
|
1702
|
+
return void 0;
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Minimum path (as vertex sequence) between two vertices.
|
|
1707
|
+
* @param v1 - Source vertex or key.
|
|
1708
|
+
* @param v2 - Destination vertex or key.
|
|
1709
|
+
* @param isWeight - If `true`, compare by path weight; otherwise by hop count.
|
|
1710
|
+
* @param isDFS - For weighted mode only: if `true`, brute-force all paths; if `false`, use Dijkstra.
|
|
1711
|
+
* @returns Vertex sequence, or `undefined`/empty when unreachable depending on branch.
|
|
1712
|
+
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
1713
|
+
*/
|
|
1714
|
+
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
1715
|
+
var _a, _b;
|
|
1716
|
+
if (isWeight === void 0) isWeight = false;
|
|
1717
|
+
if (isWeight) {
|
|
1718
|
+
if (isDFS) {
|
|
1719
|
+
const allPaths = this.getAllPathsBetween(v1, v2, 1e4);
|
|
1720
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
1721
|
+
let minIndex = -1;
|
|
1722
|
+
let index = 0;
|
|
1723
|
+
for (const path of allPaths) {
|
|
1724
|
+
const pathSumWeight = this.getPathSumWeight(path);
|
|
1725
|
+
if (pathSumWeight < min) {
|
|
1726
|
+
min = pathSumWeight;
|
|
1727
|
+
minIndex = index;
|
|
1728
|
+
}
|
|
1729
|
+
index++;
|
|
1730
|
+
}
|
|
1731
|
+
return allPaths[minIndex] || void 0;
|
|
1732
|
+
} else {
|
|
1733
|
+
return (_b = (_a = this.dijkstra(v1, v2, true, true)) == null ? void 0 : _a.minPath) != null ? _b : [];
|
|
1734
|
+
}
|
|
1735
|
+
} else {
|
|
1736
|
+
let minPath = [];
|
|
1737
|
+
const vertex1 = this._getVertex(v1);
|
|
1738
|
+
const vertex2 = this._getVertex(v2);
|
|
1739
|
+
if (!(vertex1 && vertex2)) return [];
|
|
1740
|
+
const dfs = /* @__PURE__ */ __name((cur, dest, visiting, path) => {
|
|
1741
|
+
visiting.add(cur);
|
|
1742
|
+
if (cur === dest) {
|
|
1743
|
+
minPath = [vertex1, ...path];
|
|
1744
|
+
return;
|
|
1745
|
+
}
|
|
1746
|
+
const neighbors = this.getNeighbors(cur);
|
|
1747
|
+
for (const neighbor of neighbors) {
|
|
1748
|
+
if (!visiting.has(neighbor)) {
|
|
1749
|
+
path.push(neighbor);
|
|
1750
|
+
dfs(neighbor, dest, visiting, path);
|
|
1751
|
+
path.pop();
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
visiting.delete(cur);
|
|
1755
|
+
}, "dfs");
|
|
1756
|
+
dfs(vertex1, vertex2, /* @__PURE__ */ new Set(), []);
|
|
1757
|
+
return minPath;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Dijkstra without heap (array-based selection).
|
|
1762
|
+
* @param src - Source vertex or key.
|
|
1763
|
+
* @param dest - Optional destination for early stop.
|
|
1764
|
+
* @param getMinDist - If `true`, compute global minimum distance.
|
|
1765
|
+
* @param genPaths - If `true`, also generate path arrays.
|
|
1766
|
+
* @returns Result bag or `undefined` if source missing.
|
|
1767
|
+
* @remarks Time O(V^2 + E), Space O(V + E)
|
|
1768
|
+
*/
|
|
1769
|
+
dijkstraWithoutHeap(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
1770
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
1771
|
+
let minDest = void 0;
|
|
1772
|
+
let minPath = [];
|
|
1773
|
+
const paths = [];
|
|
1774
|
+
const vertexMap = this._vertexMap;
|
|
1775
|
+
const distMap = /* @__PURE__ */ new Map();
|
|
1776
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1777
|
+
const preMap = /* @__PURE__ */ new Map();
|
|
1778
|
+
const srcVertex = this._getVertex(src);
|
|
1779
|
+
const destVertex = dest ? this._getVertex(dest) : void 0;
|
|
1780
|
+
if (!srcVertex) {
|
|
1781
|
+
return void 0;
|
|
1782
|
+
}
|
|
1783
|
+
for (const vertex of vertexMap) {
|
|
1784
|
+
const vertexOrKey = vertex[1];
|
|
1785
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
1786
|
+
}
|
|
1787
|
+
distMap.set(srcVertex, 0);
|
|
1788
|
+
preMap.set(srcVertex, void 0);
|
|
1789
|
+
const getMinOfNoSeen = /* @__PURE__ */ __name(() => {
|
|
1790
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
1791
|
+
let minV = void 0;
|
|
1792
|
+
for (const [key, value] of distMap) {
|
|
1793
|
+
if (!seen.has(key)) {
|
|
1794
|
+
if (value < min) {
|
|
1795
|
+
min = value;
|
|
1796
|
+
minV = key;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
return minV;
|
|
1801
|
+
}, "getMinOfNoSeen");
|
|
1802
|
+
const getPaths = /* @__PURE__ */ __name((minV) => {
|
|
1803
|
+
for (const vertex of vertexMap) {
|
|
1804
|
+
const vertexOrKey = vertex[1];
|
|
1805
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
1806
|
+
const path = [vertexOrKey];
|
|
1807
|
+
let parent = preMap.get(vertexOrKey);
|
|
1808
|
+
while (parent) {
|
|
1809
|
+
path.push(parent);
|
|
1810
|
+
parent = preMap.get(parent);
|
|
1811
|
+
}
|
|
1812
|
+
const reversed = path.reverse();
|
|
1813
|
+
if (vertex[1] === minV) minPath = reversed;
|
|
1814
|
+
paths.push(reversed);
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
}, "getPaths");
|
|
1818
|
+
for (let i = 1; i < vertexMap.size; i++) {
|
|
1819
|
+
const cur = getMinOfNoSeen();
|
|
1820
|
+
if (cur) {
|
|
1821
|
+
seen.add(cur);
|
|
1822
|
+
if (destVertex && destVertex === cur) {
|
|
1823
|
+
if (getMinDist) {
|
|
1824
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
1825
|
+
}
|
|
1826
|
+
if (genPaths) {
|
|
1827
|
+
getPaths(destVertex);
|
|
1828
|
+
}
|
|
1829
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
1830
|
+
}
|
|
1831
|
+
const neighbors = this.getNeighbors(cur);
|
|
1832
|
+
for (const neighbor of neighbors) {
|
|
1833
|
+
if (!seen.has(neighbor)) {
|
|
1834
|
+
const edge = this.getEdge(cur, neighbor);
|
|
1835
|
+
if (edge) {
|
|
1836
|
+
const curFromMap = distMap.get(cur);
|
|
1837
|
+
const neighborFromMap = distMap.get(neighbor);
|
|
1838
|
+
if (curFromMap !== void 0 && neighborFromMap !== void 0) {
|
|
1839
|
+
if (edge.weight + curFromMap < neighborFromMap) {
|
|
1840
|
+
distMap.set(neighbor, edge.weight + curFromMap);
|
|
1841
|
+
preMap.set(neighbor, cur);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
if (getMinDist)
|
|
1850
|
+
distMap.forEach((d, v) => {
|
|
1851
|
+
if (v !== srcVertex) {
|
|
1852
|
+
if (d < minDist) {
|
|
1853
|
+
minDist = d;
|
|
1854
|
+
if (genPaths) minDest = v;
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
});
|
|
1858
|
+
if (genPaths) getPaths(minDest);
|
|
1859
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
1860
|
+
}
|
|
1861
|
+
dijkstra(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
1862
|
+
var _a;
|
|
1863
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
1864
|
+
let minDest = void 0;
|
|
1865
|
+
let minPath = [];
|
|
1866
|
+
const paths = [];
|
|
1867
|
+
const vertexMap = this._vertexMap;
|
|
1868
|
+
const distMap = /* @__PURE__ */ new Map();
|
|
1869
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1870
|
+
const preMap = /* @__PURE__ */ new Map();
|
|
1871
|
+
const srcVertex = this._getVertex(src);
|
|
1872
|
+
const destVertex = dest ? this._getVertex(dest) : void 0;
|
|
1873
|
+
if (!srcVertex) return void 0;
|
|
1874
|
+
for (const vertex of vertexMap) {
|
|
1875
|
+
const vertexOrKey = vertex[1];
|
|
1876
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
1877
|
+
}
|
|
1878
|
+
const heap = new Heap([], { comparator: /* @__PURE__ */ __name((a, b) => a.key - b.key, "comparator") });
|
|
1879
|
+
heap.add({ key: 0, value: srcVertex });
|
|
1880
|
+
distMap.set(srcVertex, 0);
|
|
1881
|
+
preMap.set(srcVertex, void 0);
|
|
1882
|
+
const getPaths = /* @__PURE__ */ __name((minV) => {
|
|
1883
|
+
for (const vertex of vertexMap) {
|
|
1884
|
+
const vertexOrKey = vertex[1];
|
|
1885
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
1886
|
+
const path = [vertexOrKey];
|
|
1887
|
+
let parent = preMap.get(vertexOrKey);
|
|
1888
|
+
while (parent) {
|
|
1889
|
+
path.push(parent);
|
|
1890
|
+
parent = preMap.get(parent);
|
|
1891
|
+
}
|
|
1892
|
+
const reversed = path.reverse();
|
|
1893
|
+
if (vertex[1] === minV) minPath = reversed;
|
|
1894
|
+
paths.push(reversed);
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
}, "getPaths");
|
|
1898
|
+
while (heap.size > 0) {
|
|
1899
|
+
const curHeapNode = heap.poll();
|
|
1900
|
+
const dist = curHeapNode == null ? void 0 : curHeapNode.key;
|
|
1901
|
+
const cur = curHeapNode == null ? void 0 : curHeapNode.value;
|
|
1902
|
+
if (dist !== void 0) {
|
|
1903
|
+
if (cur) {
|
|
1904
|
+
seen.add(cur);
|
|
1905
|
+
if (destVertex && destVertex === cur) {
|
|
1906
|
+
if (getMinDist) {
|
|
1907
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
1908
|
+
}
|
|
1909
|
+
if (genPaths) {
|
|
1910
|
+
getPaths(destVertex);
|
|
1911
|
+
}
|
|
1912
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
1913
|
+
}
|
|
1914
|
+
const neighbors = this.getNeighbors(cur);
|
|
1915
|
+
for (const neighbor of neighbors) {
|
|
1916
|
+
if (!seen.has(neighbor)) {
|
|
1917
|
+
const weight = (_a = this.getEdge(cur, neighbor)) == null ? void 0 : _a.weight;
|
|
1918
|
+
if (typeof weight === "number") {
|
|
1919
|
+
const distSrcToNeighbor = distMap.get(neighbor);
|
|
1920
|
+
if (distSrcToNeighbor !== void 0) {
|
|
1921
|
+
if (dist + weight < distSrcToNeighbor) {
|
|
1922
|
+
heap.add({ key: dist + weight, value: neighbor });
|
|
1923
|
+
preMap.set(neighbor, cur);
|
|
1924
|
+
distMap.set(neighbor, dist + weight);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
if (getMinDist) {
|
|
1934
|
+
distMap.forEach((d, v) => {
|
|
1935
|
+
if (v !== srcVertex) {
|
|
1936
|
+
if (d < minDist) {
|
|
1937
|
+
minDist = d;
|
|
1938
|
+
if (genPaths) minDest = v;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
});
|
|
1942
|
+
}
|
|
1943
|
+
if (genPaths) {
|
|
1944
|
+
getPaths(minDest);
|
|
1945
|
+
}
|
|
1946
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1949
|
+
* Bellman-Ford single-source shortest paths with option to scan negative cycles.
|
|
1950
|
+
* @param src - Source vertex or key.
|
|
1951
|
+
* @param scanNegativeCycle - If `true`, also detect negative cycles.
|
|
1952
|
+
* @param getMin - If `true`, compute global minimum distance.
|
|
1953
|
+
* @param genPath - If `true`, generate path arrays via predecessor map.
|
|
1954
|
+
* @returns Result bag including distances, predecessors, and optional cycle flag.
|
|
1955
|
+
* @remarks Time O(V * E), Space O(V + E)
|
|
1956
|
+
*/
|
|
1957
|
+
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
1958
|
+
if (getMin === void 0) getMin = false;
|
|
1959
|
+
if (genPath === void 0) genPath = false;
|
|
1960
|
+
const srcVertex = this._getVertex(src);
|
|
1961
|
+
const paths = [];
|
|
1962
|
+
const distMap = /* @__PURE__ */ new Map();
|
|
1963
|
+
const preMap = /* @__PURE__ */ new Map();
|
|
1964
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
1965
|
+
let minPath = [];
|
|
1966
|
+
let hasNegativeCycle;
|
|
1967
|
+
if (scanNegativeCycle) hasNegativeCycle = false;
|
|
1968
|
+
if (!srcVertex) return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
1969
|
+
const vertexMap = this._vertexMap;
|
|
1970
|
+
const numOfVertices = vertexMap.size;
|
|
1971
|
+
const edgeMap = this.edgeSet();
|
|
1972
|
+
const numOfEdges = edgeMap.length;
|
|
1973
|
+
this._vertexMap.forEach((vertex) => {
|
|
1974
|
+
distMap.set(vertex, Number.MAX_SAFE_INTEGER);
|
|
1975
|
+
});
|
|
1976
|
+
distMap.set(srcVertex, 0);
|
|
1977
|
+
for (let i = 1; i < numOfVertices; ++i) {
|
|
1978
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
1979
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
1980
|
+
if (ends) {
|
|
1981
|
+
const [s, d] = ends;
|
|
1982
|
+
const weight = edgeMap[j].weight;
|
|
1983
|
+
const sWeight = distMap.get(s);
|
|
1984
|
+
const dWeight = distMap.get(d);
|
|
1985
|
+
if (sWeight !== void 0 && dWeight !== void 0) {
|
|
1986
|
+
if (distMap.get(s) !== Number.MAX_SAFE_INTEGER && sWeight + weight < dWeight) {
|
|
1987
|
+
distMap.set(d, sWeight + weight);
|
|
1988
|
+
if (genPath) preMap.set(d, s);
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
let minDest = void 0;
|
|
1995
|
+
if (getMin) {
|
|
1996
|
+
distMap.forEach((d, v) => {
|
|
1997
|
+
if (v !== srcVertex) {
|
|
1998
|
+
if (d < min) {
|
|
1999
|
+
min = d;
|
|
2000
|
+
if (genPath) minDest = v;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
});
|
|
2004
|
+
}
|
|
2005
|
+
if (genPath) {
|
|
2006
|
+
for (const vertex of vertexMap) {
|
|
2007
|
+
const vertexOrKey = vertex[1];
|
|
2008
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
2009
|
+
const path = [vertexOrKey];
|
|
2010
|
+
let parent = preMap.get(vertexOrKey);
|
|
2011
|
+
while (parent !== void 0) {
|
|
2012
|
+
path.push(parent);
|
|
2013
|
+
parent = preMap.get(parent);
|
|
2014
|
+
}
|
|
2015
|
+
const reversed = path.reverse();
|
|
2016
|
+
if (vertex[1] === minDest) minPath = reversed;
|
|
2017
|
+
paths.push(reversed);
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
for (let j = 0; j < numOfEdges; ++j) {
|
|
2022
|
+
const ends = this.getEndsOfEdge(edgeMap[j]);
|
|
2023
|
+
if (ends) {
|
|
2024
|
+
const [s] = ends;
|
|
2025
|
+
const weight = edgeMap[j].weight;
|
|
2026
|
+
const sWeight = distMap.get(s);
|
|
2027
|
+
if (sWeight) {
|
|
2028
|
+
if (sWeight !== Number.MAX_SAFE_INTEGER && sWeight + weight < sWeight) hasNegativeCycle = true;
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* Floyd–Warshall all-pairs shortest paths.
|
|
2036
|
+
* @returns `{ costs, predecessor }` matrices.
|
|
2037
|
+
* @remarks Time O(V^3), Space O(V^2)
|
|
2038
|
+
*/
|
|
2039
|
+
floydWarshall() {
|
|
2040
|
+
var _a;
|
|
2041
|
+
const idAndVertices = [...this._vertexMap];
|
|
2042
|
+
const n = idAndVertices.length;
|
|
2043
|
+
const costs = [];
|
|
2044
|
+
const predecessor = [];
|
|
2045
|
+
for (let i = 0; i < n; i++) {
|
|
2046
|
+
costs[i] = [];
|
|
2047
|
+
predecessor[i] = [];
|
|
2048
|
+
for (let j = 0; j < n; j++) {
|
|
2049
|
+
predecessor[i][j] = void 0;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
for (let i = 0; i < n; i++) {
|
|
2053
|
+
for (let j = 0; j < n; j++) {
|
|
2054
|
+
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) == null ? void 0 : _a.weight) || Number.MAX_SAFE_INTEGER;
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
for (let k = 0; k < n; k++) {
|
|
2058
|
+
for (let i = 0; i < n; i++) {
|
|
2059
|
+
for (let j = 0; j < n; j++) {
|
|
2060
|
+
if (costs[i][j] > costs[i][k] + costs[k][j]) {
|
|
2061
|
+
costs[i][j] = costs[i][k] + costs[k][j];
|
|
2062
|
+
predecessor[i][j] = idAndVertices[k][1];
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
return { costs, predecessor };
|
|
2068
|
+
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Enumerate simple cycles (may be expensive).
|
|
2071
|
+
* @param isInclude2Cycle - If `true`, include 2-cycles when graph semantics allow.
|
|
2072
|
+
* @returns Array of cycles (each as array of vertex keys).
|
|
2073
|
+
* @remarks Time exponential in worst-case, Space O(V + E)
|
|
2074
|
+
*/
|
|
2075
|
+
getCycles(isInclude2Cycle = false) {
|
|
2076
|
+
const cycles = [];
|
|
2077
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2078
|
+
const dfs = /* @__PURE__ */ __name((vertex, currentPath, visited2) => {
|
|
2079
|
+
if (visited2.has(vertex)) {
|
|
2080
|
+
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
|
|
2081
|
+
cycles.push([...currentPath]);
|
|
2082
|
+
}
|
|
2083
|
+
return;
|
|
2084
|
+
}
|
|
2085
|
+
visited2.add(vertex);
|
|
2086
|
+
currentPath.push(vertex.key);
|
|
2087
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
2088
|
+
if (neighbor) dfs(neighbor, currentPath, visited2);
|
|
2089
|
+
}
|
|
2090
|
+
visited2.delete(vertex);
|
|
2091
|
+
currentPath.pop();
|
|
2092
|
+
}, "dfs");
|
|
2093
|
+
for (const vertex of this.vertexMap.values()) {
|
|
2094
|
+
dfs(vertex, [], visited);
|
|
2095
|
+
}
|
|
2096
|
+
const uniqueCycles = /* @__PURE__ */ new Map();
|
|
2097
|
+
for (const cycle of cycles) {
|
|
2098
|
+
const sorted = [...cycle].sort().toString();
|
|
2099
|
+
if (uniqueCycles.has(sorted)) continue;
|
|
2100
|
+
else {
|
|
2101
|
+
uniqueCycles.set(sorted, cycle);
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
return [...uniqueCycles].map((cycleString) => cycleString[1]);
|
|
2105
|
+
}
|
|
2106
|
+
/**
|
|
2107
|
+
* Induced-subgraph filter: keep vertices where `predicate(key, value)` is true,
|
|
2108
|
+
* and only keep edges whose endpoints both survive.
|
|
2109
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
2110
|
+
* @param thisArg - Optional `this` for callback.
|
|
2111
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
2112
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2113
|
+
*/
|
|
2114
|
+
filter(predicate, thisArg) {
|
|
2115
|
+
const filtered = [];
|
|
2116
|
+
let index = 0;
|
|
2117
|
+
for (const [key, value] of this) {
|
|
2118
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
2119
|
+
filtered.push([key, value]);
|
|
2120
|
+
}
|
|
2121
|
+
index++;
|
|
2122
|
+
}
|
|
2123
|
+
return this._createLike(filtered, this._snapshotOptions());
|
|
2124
|
+
}
|
|
2125
|
+
/**
|
|
2126
|
+
* Preserve the old behavior: return filtered entries as an array.
|
|
2127
|
+
* @remarks Time O(V), Space O(V)
|
|
2128
|
+
*/
|
|
2129
|
+
filterEntries(predicate, thisArg) {
|
|
2130
|
+
const filtered = [];
|
|
2131
|
+
let index = 0;
|
|
2132
|
+
for (const [key, value] of this) {
|
|
2133
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
2134
|
+
filtered.push([key, value]);
|
|
2135
|
+
}
|
|
2136
|
+
index++;
|
|
2137
|
+
}
|
|
2138
|
+
return filtered;
|
|
2139
|
+
}
|
|
2140
|
+
map(callback, thisArg) {
|
|
2141
|
+
const mapped = [];
|
|
2142
|
+
let index = 0;
|
|
2143
|
+
for (const [key, value] of this) {
|
|
2144
|
+
mapped.push(callback.call(thisArg, key, value, index, this));
|
|
2145
|
+
index++;
|
|
2146
|
+
}
|
|
2147
|
+
return mapped;
|
|
2148
|
+
}
|
|
2149
|
+
/**
|
|
2150
|
+
* Create a deep clone of the graph with the same species.
|
|
2151
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2152
|
+
*/
|
|
2153
|
+
/**
|
|
2154
|
+
* Create a deep clone of the graph with the same species.
|
|
2155
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
2156
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2157
|
+
*/
|
|
2158
|
+
clone() {
|
|
2159
|
+
return this._createLike(void 0, this._snapshotOptions());
|
|
2160
|
+
}
|
|
2161
|
+
// ===== Same-species factory & cloning helpers =====
|
|
2162
|
+
/**
|
|
2163
|
+
* Internal iterator over `[key, value]` entries in insertion order.
|
|
2164
|
+
* @returns Iterator of `[VertexKey, V | undefined]`.
|
|
2165
|
+
* @remarks Time O(V), Space O(1)
|
|
2166
|
+
*/
|
|
2167
|
+
*_getIterator() {
|
|
2168
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2169
|
+
yield [vertex.key, vertex.value];
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
/**
|
|
2173
|
+
* Capture configuration needed to reproduce the current graph.
|
|
2174
|
+
* Currently the graph has no runtime options, so we return an empty object.
|
|
2175
|
+
*/
|
|
2176
|
+
/**
|
|
2177
|
+
* Capture configuration needed to reproduce the current graph.
|
|
2178
|
+
* @returns Options bag (opaque to callers).
|
|
2179
|
+
* @remarks Time O(1), Space O(1)
|
|
2180
|
+
*/
|
|
2181
|
+
_snapshotOptions() {
|
|
2182
|
+
return { graph: { ...this._options } };
|
|
2183
|
+
}
|
|
2184
|
+
/**
|
|
2185
|
+
* Create an empty graph instance of the same concrete species (Directed/Undirected/etc).
|
|
2186
|
+
* @remarks Time O(1), Space O(1)
|
|
2187
|
+
*/
|
|
2188
|
+
/**
|
|
2189
|
+
* Create an empty graph instance of the same concrete species.
|
|
2190
|
+
* @param _options - Snapshot options from `_snapshotOptions()`.
|
|
2191
|
+
* @returns A new empty graph instance of `this` type.
|
|
2192
|
+
* @remarks Time O(1), Space O(1)
|
|
2193
|
+
*/
|
|
2194
|
+
_createInstance(_options) {
|
|
2195
|
+
const Ctor = this.constructor;
|
|
2196
|
+
const instance = new Ctor();
|
|
2197
|
+
const graph = _options == null ? void 0 : _options.graph;
|
|
2198
|
+
if (graph) instance._options = { ...instance._options, ...graph };
|
|
2199
|
+
else instance._options = { ...instance._options, ...this._options };
|
|
2200
|
+
return instance;
|
|
2201
|
+
}
|
|
2202
|
+
/**
|
|
2203
|
+
* Create a same-species graph populated with the given entries.
|
|
2204
|
+
* Also preserves edges between kept vertices from the source graph.
|
|
2205
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2206
|
+
*/
|
|
2207
|
+
/**
|
|
2208
|
+
* Create a same-species graph populated with entries; preserves edges among kept vertices.
|
|
2209
|
+
* @param iter - Optional entries to seed the new graph.
|
|
2210
|
+
* @param options - Snapshot options.
|
|
2211
|
+
* @returns A new graph of `this` type.
|
|
2212
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2213
|
+
*/
|
|
2214
|
+
_createLike(iter, options) {
|
|
2215
|
+
const g = this._createInstance(options);
|
|
2216
|
+
if (iter) {
|
|
2217
|
+
for (const [k, v] of iter) {
|
|
2218
|
+
g.addVertex(k, v);
|
|
2219
|
+
}
|
|
2220
|
+
} else {
|
|
2221
|
+
for (const [k, v] of this) {
|
|
2222
|
+
g.addVertex(k, v);
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
const edges = this.edgeSet();
|
|
2226
|
+
for (const e of edges) {
|
|
2227
|
+
const ends = this.getEndsOfEdge(e);
|
|
2228
|
+
if (!ends) continue;
|
|
2229
|
+
const [va, vb] = ends;
|
|
2230
|
+
const ka = va.key;
|
|
2231
|
+
const kb = vb.key;
|
|
2232
|
+
const hasA = g.hasVertex ? g.hasVertex(ka) : false;
|
|
2233
|
+
const hasB = g.hasVertex ? g.hasVertex(kb) : false;
|
|
2234
|
+
if (hasA && hasB) {
|
|
2235
|
+
const w = e.weight;
|
|
2236
|
+
const val = e.value;
|
|
2237
|
+
const newEdge = g.createEdge(ka, kb, w, val);
|
|
2238
|
+
g._addEdge(newEdge);
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
return g;
|
|
2242
|
+
}
|
|
2243
|
+
/**
|
|
2244
|
+
* Insert a pre-built vertex into the graph.
|
|
2245
|
+
* @param newVertex - Concrete vertex instance.
|
|
2246
|
+
* @returns `true` if inserted; `false` if key already exists.
|
|
2247
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2248
|
+
*/
|
|
2249
|
+
_addVertex(newVertex) {
|
|
2250
|
+
if (this.hasVertex(newVertex)) {
|
|
2251
|
+
return false;
|
|
2252
|
+
}
|
|
2253
|
+
this._vertexMap.set(newVertex.key, newVertex);
|
|
2254
|
+
return true;
|
|
2255
|
+
}
|
|
2256
|
+
/**
|
|
2257
|
+
* Resolve a vertex key or instance to the concrete vertex instance.
|
|
2258
|
+
* @param vertexOrKey - Vertex key or existing vertex.
|
|
2259
|
+
* @returns Vertex instance or `undefined`.
|
|
2260
|
+
* @remarks Time O(1), Space O(1)
|
|
2261
|
+
*/
|
|
2262
|
+
_getVertex(vertexOrKey) {
|
|
2263
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
2264
|
+
return this._vertexMap.get(vertexKey) || void 0;
|
|
2265
|
+
}
|
|
2266
|
+
/**
|
|
2267
|
+
* Resolve a vertex key from a key or vertex instance.
|
|
2268
|
+
* @param vertexOrKey - Vertex key or existing vertex.
|
|
2269
|
+
* @returns The vertex key.
|
|
2270
|
+
* @remarks Time O(1), Space O(1)
|
|
2271
|
+
*/
|
|
2272
|
+
_getVertexKey(vertexOrKey) {
|
|
2273
|
+
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
2274
|
+
}
|
|
2275
|
+
};
|
|
2276
|
+
__name(_AbstractGraph, "AbstractGraph");
|
|
2277
|
+
var AbstractGraph = _AbstractGraph;
|
|
2278
|
+
|
|
2279
|
+
// src/data-structures/graph/directed-graph.ts
|
|
2280
|
+
var _DirectedVertex = class _DirectedVertex extends AbstractVertex {
|
|
2281
|
+
constructor(key, value) {
|
|
2282
|
+
super(key, value);
|
|
2283
|
+
}
|
|
2284
|
+
};
|
|
2285
|
+
__name(_DirectedVertex, "DirectedVertex");
|
|
2286
|
+
var DirectedVertex = _DirectedVertex;
|
|
2287
|
+
var _DirectedEdge = class _DirectedEdge extends AbstractEdge {
|
|
2288
|
+
constructor(src, dest, weight, value) {
|
|
2289
|
+
super(weight, value);
|
|
2290
|
+
__publicField(this, "src");
|
|
2291
|
+
__publicField(this, "dest");
|
|
2292
|
+
this.src = src;
|
|
2293
|
+
this.dest = dest;
|
|
2294
|
+
}
|
|
2295
|
+
};
|
|
2296
|
+
__name(_DirectedEdge, "DirectedEdge");
|
|
2297
|
+
var DirectedEdge = _DirectedEdge;
|
|
2298
|
+
var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
2299
|
+
/**
|
|
2300
|
+
* Construct a directed graph with runtime defaults.
|
|
2301
|
+
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
2302
|
+
* @remarks Time O(1), Space O(1)
|
|
2303
|
+
*/
|
|
2304
|
+
constructor(options) {
|
|
2305
|
+
super(options);
|
|
2306
|
+
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
2307
|
+
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
2308
|
+
}
|
|
2309
|
+
get outEdgeMap() {
|
|
2310
|
+
return this._outEdgeMap;
|
|
2311
|
+
}
|
|
2312
|
+
set outEdgeMap(v) {
|
|
2313
|
+
this._outEdgeMap = v;
|
|
2314
|
+
}
|
|
2315
|
+
get inEdgeMap() {
|
|
2316
|
+
return this._inEdgeMap;
|
|
2317
|
+
}
|
|
2318
|
+
set inEdgeMap(v) {
|
|
2319
|
+
this._inEdgeMap = v;
|
|
2320
|
+
}
|
|
2321
|
+
/**
|
|
2322
|
+
* Construct a directed graph from keys with value initializer `v => v`.
|
|
2323
|
+
* @template K - Vertex key type.
|
|
2324
|
+
* @param keys - Iterable of vertex keys.
|
|
2325
|
+
* @returns DirectedGraph with all keys added.
|
|
2326
|
+
* @remarks Time O(V), Space O(V)
|
|
2327
|
+
*/
|
|
2328
|
+
static fromKeys(keys) {
|
|
2329
|
+
const g = new _DirectedGraph({
|
|
2330
|
+
vertexValueInitializer: /* @__PURE__ */ __name((k) => k, "vertexValueInitializer")
|
|
2331
|
+
});
|
|
2332
|
+
for (const k of keys) g.addVertex(k);
|
|
2333
|
+
return g;
|
|
2334
|
+
}
|
|
2335
|
+
/**
|
|
2336
|
+
* Construct a directed graph from `[key, value]` entries.
|
|
2337
|
+
* @template V - Vertex value type.
|
|
2338
|
+
* @param entries - Iterable of `[key, value]` pairs.
|
|
2339
|
+
* @returns DirectedGraph with all vertices added.
|
|
2340
|
+
* @remarks Time O(V), Space O(V)
|
|
2341
|
+
*/
|
|
2342
|
+
static fromEntries(entries) {
|
|
2343
|
+
const g = new _DirectedGraph();
|
|
2344
|
+
for (const [k, v] of entries) g.addVertex(k, v);
|
|
2345
|
+
return g;
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* Create a directed vertex instance. Does not insert into the graph.
|
|
2349
|
+
* @param key - Vertex identifier.
|
|
2350
|
+
* @param value - Optional payload.
|
|
2351
|
+
* @returns Concrete vertex instance.
|
|
2352
|
+
* @remarks Time O(1), Space O(1)
|
|
2353
|
+
*/
|
|
2354
|
+
createVertex(key, value) {
|
|
2355
|
+
return new DirectedVertex(key, value);
|
|
2356
|
+
}
|
|
2357
|
+
/**
|
|
2358
|
+
* Create a directed edge instance. Does not insert into the graph.
|
|
2359
|
+
* @param src - Source vertex key.
|
|
2360
|
+
* @param dest - Destination vertex key.
|
|
2361
|
+
* @param weight - Edge weight; defaults to `defaultEdgeWeight`.
|
|
2362
|
+
* @param value - Edge payload.
|
|
2363
|
+
* @returns Concrete edge instance.
|
|
2364
|
+
* @remarks Time O(1), Space O(1)
|
|
2365
|
+
*/
|
|
2366
|
+
createEdge(src, dest, weight, value) {
|
|
2367
|
+
var _a;
|
|
2368
|
+
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2369
|
+
}
|
|
2370
|
+
/**
|
|
2371
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
2372
|
+
* @param srcOrKey - Source vertex or key.
|
|
2373
|
+
* @param destOrKey - Destination vertex or key.
|
|
2374
|
+
* @returns Edge instance or `undefined`.
|
|
2375
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2376
|
+
*/
|
|
2377
|
+
getEdge(srcOrKey, destOrKey) {
|
|
2378
|
+
let edgeMap = [];
|
|
2379
|
+
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
2380
|
+
const src = this._getVertex(srcOrKey);
|
|
2381
|
+
const dest = this._getVertex(destOrKey);
|
|
2382
|
+
if (src && dest) {
|
|
2383
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
2384
|
+
if (srcOutEdges) {
|
|
2385
|
+
edgeMap = srcOutEdges.filter((edge) => edge.dest === dest.key);
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
return edgeMap[0] || void 0;
|
|
2390
|
+
}
|
|
2391
|
+
/**
|
|
2392
|
+
* Delete edge `src -> dest` if present.
|
|
2393
|
+
* @param srcOrKey - Source vertex or key.
|
|
2394
|
+
* @param destOrKey - Destination vertex or key.
|
|
2395
|
+
* @returns Removed edge or `undefined`.
|
|
2396
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2397
|
+
*/
|
|
2398
|
+
deleteEdgeSrcToDest(srcOrKey, destOrKey) {
|
|
2399
|
+
const src = this._getVertex(srcOrKey);
|
|
2400
|
+
const dest = this._getVertex(destOrKey);
|
|
2401
|
+
let removed = void 0;
|
|
2402
|
+
if (!src || !dest) {
|
|
2403
|
+
return void 0;
|
|
2404
|
+
}
|
|
2405
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
2406
|
+
if (srcOutEdges) {
|
|
2407
|
+
arrayRemove(srcOutEdges, (edge) => edge.dest === dest.key);
|
|
2408
|
+
}
|
|
2409
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
2410
|
+
if (destInEdges) {
|
|
2411
|
+
removed = arrayRemove(destInEdges, (edge) => edge.src === src.key)[0] || void 0;
|
|
2412
|
+
}
|
|
2413
|
+
return removed;
|
|
2414
|
+
}
|
|
2415
|
+
/**
|
|
2416
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
2417
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
2418
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
2419
|
+
* @returns Removed edge or `undefined`.
|
|
2420
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2421
|
+
*/
|
|
2422
|
+
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
2423
|
+
let removed = void 0;
|
|
2424
|
+
let src, dest;
|
|
2425
|
+
if (this.isVertexKey(edgeOrSrcVertexKey)) {
|
|
2426
|
+
if (this.isVertexKey(destVertexKey)) {
|
|
2427
|
+
src = this._getVertex(edgeOrSrcVertexKey);
|
|
2428
|
+
dest = this._getVertex(destVertexKey);
|
|
2429
|
+
} else {
|
|
2430
|
+
return;
|
|
2431
|
+
}
|
|
2432
|
+
} else {
|
|
2433
|
+
src = this._getVertex(edgeOrSrcVertexKey.src);
|
|
2434
|
+
dest = this._getVertex(edgeOrSrcVertexKey.dest);
|
|
2435
|
+
}
|
|
2436
|
+
if (src && dest) {
|
|
2437
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
2438
|
+
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
2439
|
+
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest === (dest == null ? void 0 : dest.key));
|
|
2440
|
+
}
|
|
2441
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
2442
|
+
if (destInEdges && destInEdges.length > 0) {
|
|
2443
|
+
removed = arrayRemove(destInEdges, (edge) => edge.src === src.key && edge.dest === dest.key)[0];
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
return removed;
|
|
2447
|
+
}
|
|
2448
|
+
deleteVertex(vertexOrKey) {
|
|
2449
|
+
let vertexKey;
|
|
2450
|
+
let vertex;
|
|
2451
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
2452
|
+
vertex = this.getVertex(vertexOrKey);
|
|
2453
|
+
vertexKey = vertexOrKey;
|
|
2454
|
+
} else {
|
|
2455
|
+
vertex = vertexOrKey;
|
|
2456
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
2457
|
+
}
|
|
2458
|
+
if (vertex) {
|
|
2459
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2460
|
+
for (const neighbor of neighbors) {
|
|
2461
|
+
this.deleteEdgeSrcToDest(vertex, neighbor);
|
|
2462
|
+
}
|
|
2463
|
+
this._outEdgeMap.delete(vertex);
|
|
2464
|
+
this._inEdgeMap.delete(vertex);
|
|
2465
|
+
}
|
|
2466
|
+
return this._vertexMap.delete(vertexKey);
|
|
2467
|
+
}
|
|
2468
|
+
deleteEdgesBetween(v1, v2) {
|
|
2469
|
+
const removed = [];
|
|
2470
|
+
if (v1 && v2) {
|
|
2471
|
+
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
2472
|
+
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
2473
|
+
if (v1ToV2) removed.push(v1ToV2);
|
|
2474
|
+
if (v2ToV1) removed.push(v2ToV1);
|
|
2475
|
+
}
|
|
2476
|
+
return removed;
|
|
2477
|
+
}
|
|
2478
|
+
/**
|
|
2479
|
+
* Incoming edges of a vertex.
|
|
2480
|
+
* @param vertexOrKey - Vertex or key.
|
|
2481
|
+
* @returns Array of incoming edges.
|
|
2482
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
2483
|
+
*/
|
|
2484
|
+
incomingEdgesOf(vertexOrKey) {
|
|
2485
|
+
const target = this._getVertex(vertexOrKey);
|
|
2486
|
+
if (target) {
|
|
2487
|
+
return this.inEdgeMap.get(target) || [];
|
|
2488
|
+
}
|
|
2489
|
+
return [];
|
|
2490
|
+
}
|
|
2491
|
+
/**
|
|
2492
|
+
* Outgoing edges of a vertex.
|
|
2493
|
+
* @param vertexOrKey - Vertex or key.
|
|
2494
|
+
* @returns Array of outgoing edges.
|
|
2495
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
2496
|
+
*/
|
|
2497
|
+
outgoingEdgesOf(vertexOrKey) {
|
|
2498
|
+
const target = this._getVertex(vertexOrKey);
|
|
2499
|
+
if (target) {
|
|
2500
|
+
return this._outEdgeMap.get(target) || [];
|
|
2501
|
+
}
|
|
2502
|
+
return [];
|
|
2503
|
+
}
|
|
2504
|
+
/**
|
|
2505
|
+
* Degree (in + out) of a vertex.
|
|
2506
|
+
* @param vertexOrKey - Vertex or key.
|
|
2507
|
+
* @returns Non-negative integer.
|
|
2508
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2509
|
+
*/
|
|
2510
|
+
degreeOf(vertexOrKey) {
|
|
2511
|
+
return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
|
|
2512
|
+
}
|
|
2513
|
+
inDegreeOf(vertexOrKey) {
|
|
2514
|
+
return this.incomingEdgesOf(vertexOrKey).length;
|
|
2515
|
+
}
|
|
2516
|
+
outDegreeOf(vertexOrKey) {
|
|
2517
|
+
return this.outgoingEdgesOf(vertexOrKey).length;
|
|
2518
|
+
}
|
|
2519
|
+
/**
|
|
2520
|
+
* All incident edges of a vertex.
|
|
2521
|
+
* @param vertexOrKey - Vertex or key.
|
|
2522
|
+
* @returns Array of incident edges.
|
|
2523
|
+
* @remarks Time O(deg_in + deg_out), Space O(deg_in + deg_out)
|
|
2524
|
+
*/
|
|
2525
|
+
edgesOf(vertexOrKey) {
|
|
2526
|
+
return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
|
|
2527
|
+
}
|
|
2528
|
+
getEdgeSrc(e) {
|
|
2529
|
+
return this._getVertex(e.src);
|
|
2530
|
+
}
|
|
2531
|
+
getEdgeDest(e) {
|
|
2532
|
+
return this._getVertex(e.dest);
|
|
2533
|
+
}
|
|
2534
|
+
/**
|
|
2535
|
+
* Direct children reachable by one outgoing edge.
|
|
2536
|
+
* @param vertex - Vertex or key.
|
|
2537
|
+
* @returns Array of neighbor vertices.
|
|
2538
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
2539
|
+
*/
|
|
2540
|
+
getDestinations(vertex) {
|
|
2541
|
+
if (vertex === void 0) {
|
|
2542
|
+
return [];
|
|
2543
|
+
}
|
|
2544
|
+
const destinations = [];
|
|
2545
|
+
const outgoingEdges = this.outgoingEdgesOf(vertex);
|
|
2546
|
+
for (const outEdge of outgoingEdges) {
|
|
2547
|
+
const child = this.getEdgeDest(outEdge);
|
|
2548
|
+
if (child) {
|
|
2549
|
+
destinations.push(child);
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
return destinations;
|
|
2553
|
+
}
|
|
2554
|
+
/**
|
|
2555
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
2556
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
2557
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
2558
|
+
* @remarks Time O(V + E), Space O(V)
|
|
2559
|
+
*/
|
|
2560
|
+
topologicalSort(propertyName) {
|
|
2561
|
+
propertyName = propertyName != null ? propertyName : "key";
|
|
2562
|
+
const statusMap = /* @__PURE__ */ new Map();
|
|
2563
|
+
for (const entry of this.vertexMap) {
|
|
2564
|
+
statusMap.set(entry[1], 0);
|
|
2565
|
+
}
|
|
2566
|
+
let sorted = [];
|
|
2567
|
+
let hasCycle = false;
|
|
2568
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
2569
|
+
statusMap.set(cur, 1);
|
|
2570
|
+
const children = this.getDestinations(cur);
|
|
2571
|
+
for (const child of children) {
|
|
2572
|
+
const childStatus = statusMap.get(child);
|
|
2573
|
+
if (childStatus === 0) {
|
|
2574
|
+
dfs(child);
|
|
2575
|
+
} else if (childStatus === 1) {
|
|
2576
|
+
hasCycle = true;
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
statusMap.set(cur, 2);
|
|
2580
|
+
sorted.push(cur);
|
|
2581
|
+
}, "dfs");
|
|
2582
|
+
for (const entry of this.vertexMap) {
|
|
2583
|
+
if (statusMap.get(entry[1]) === 0) {
|
|
2584
|
+
dfs(entry[1]);
|
|
2585
|
+
}
|
|
2586
|
+
}
|
|
2587
|
+
if (hasCycle) return void 0;
|
|
2588
|
+
if (propertyName === "key") sorted = sorted.map((vertex) => vertex instanceof DirectedVertex ? vertex.key : vertex);
|
|
2589
|
+
return sorted.reverse();
|
|
2590
|
+
}
|
|
2591
|
+
edgeSet() {
|
|
2592
|
+
let edgeMap = [];
|
|
2593
|
+
this._outEdgeMap.forEach((outEdges) => {
|
|
2594
|
+
edgeMap = [...edgeMap, ...outEdges];
|
|
2595
|
+
});
|
|
2596
|
+
return edgeMap;
|
|
2597
|
+
}
|
|
2598
|
+
getNeighbors(vertexOrKey) {
|
|
2599
|
+
const neighbors = [];
|
|
2600
|
+
const vertex = this._getVertex(vertexOrKey);
|
|
2601
|
+
if (vertex) {
|
|
2602
|
+
const outEdges = this.outgoingEdgesOf(vertex);
|
|
2603
|
+
for (const outEdge of outEdges) {
|
|
2604
|
+
const neighbor = this._getVertex(outEdge.dest);
|
|
2605
|
+
if (neighbor) {
|
|
2606
|
+
neighbors.push(neighbor);
|
|
2607
|
+
}
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
return neighbors;
|
|
2611
|
+
}
|
|
2612
|
+
/**
|
|
2613
|
+
* Resolve an edge's `[src, dest]` endpoints to vertex instances.
|
|
2614
|
+
* @param edge - Edge instance.
|
|
2615
|
+
* @returns `[src, dest]` or `undefined` if either endpoint is missing.
|
|
2616
|
+
* @remarks Time O(1), Space O(1)
|
|
2617
|
+
*/
|
|
2618
|
+
getEndsOfEdge(edge) {
|
|
2619
|
+
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
2620
|
+
return void 0;
|
|
2621
|
+
}
|
|
2622
|
+
const v1 = this._getVertex(edge.src);
|
|
2623
|
+
const v2 = this._getVertex(edge.dest);
|
|
2624
|
+
if (v1 && v2) {
|
|
2625
|
+
return [v1, v2];
|
|
2626
|
+
} else {
|
|
2627
|
+
return void 0;
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2630
|
+
/**
|
|
2631
|
+
* Whether the graph has no vertices and no edges.
|
|
2632
|
+
* @remarks Time O(1), Space O(1)
|
|
2633
|
+
*/
|
|
2634
|
+
isEmpty() {
|
|
2635
|
+
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
|
|
2636
|
+
}
|
|
2637
|
+
/**
|
|
2638
|
+
* Remove all vertices and edges.
|
|
2639
|
+
* @remarks Time O(V + E), Space O(1)
|
|
2640
|
+
*/
|
|
2641
|
+
clear() {
|
|
2642
|
+
this._vertexMap = /* @__PURE__ */ new Map();
|
|
2643
|
+
this._inEdgeMap = /* @__PURE__ */ new Map();
|
|
2644
|
+
this._outEdgeMap = /* @__PURE__ */ new Map();
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* Deep clone as the same concrete class.
|
|
2648
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
2649
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2650
|
+
*/
|
|
2651
|
+
clone() {
|
|
2652
|
+
return super.clone();
|
|
2653
|
+
}
|
|
2654
|
+
/**
|
|
2655
|
+
* Tarjan's algorithm for strongly connected components.
|
|
2656
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
2657
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
2658
|
+
*/
|
|
2659
|
+
tarjan() {
|
|
2660
|
+
const dfnMap = /* @__PURE__ */ new Map();
|
|
2661
|
+
const lowMap = /* @__PURE__ */ new Map();
|
|
2662
|
+
const SCCs = /* @__PURE__ */ new Map();
|
|
2663
|
+
let time = 0;
|
|
2664
|
+
const stack = [];
|
|
2665
|
+
const inStack = /* @__PURE__ */ new Set();
|
|
2666
|
+
const dfs = /* @__PURE__ */ __name((vertex) => {
|
|
2667
|
+
dfnMap.set(vertex, time);
|
|
2668
|
+
lowMap.set(vertex, time);
|
|
2669
|
+
time++;
|
|
2670
|
+
stack.push(vertex);
|
|
2671
|
+
inStack.add(vertex);
|
|
2672
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2673
|
+
for (const neighbor of neighbors) {
|
|
2674
|
+
if (!dfnMap.has(neighbor)) {
|
|
2675
|
+
dfs(neighbor);
|
|
2676
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
|
|
2677
|
+
} else if (inStack.has(neighbor)) {
|
|
2678
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
|
|
2679
|
+
}
|
|
2680
|
+
}
|
|
2681
|
+
if (dfnMap.get(vertex) === lowMap.get(vertex)) {
|
|
2682
|
+
const SCC = [];
|
|
2683
|
+
let poppedVertex;
|
|
2684
|
+
do {
|
|
2685
|
+
poppedVertex = stack.pop();
|
|
2686
|
+
inStack.delete(poppedVertex);
|
|
2687
|
+
SCC.push(poppedVertex);
|
|
2688
|
+
} while (poppedVertex !== vertex);
|
|
2689
|
+
SCCs.set(SCCs.size, SCC);
|
|
2690
|
+
}
|
|
2691
|
+
}, "dfs");
|
|
2692
|
+
for (const vertex of this.vertexMap.values()) {
|
|
2693
|
+
if (!dfnMap.has(vertex)) {
|
|
2694
|
+
dfs(vertex);
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
return { dfnMap, lowMap, SCCs };
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2700
|
+
* DFN index map computed by `tarjan()`.
|
|
2701
|
+
* @returns Map from vertex to DFN index.
|
|
2702
|
+
* @remarks Time O(V), Space O(V)
|
|
2703
|
+
*/
|
|
2704
|
+
getDFNMap() {
|
|
2705
|
+
return this.tarjan().dfnMap;
|
|
2706
|
+
}
|
|
2707
|
+
/**
|
|
2708
|
+
* LOW link map computed by `tarjan()`.
|
|
2709
|
+
* @returns Map from vertex to LOW value.
|
|
2710
|
+
* @remarks Time O(V), Space O(V)
|
|
2711
|
+
*/
|
|
2712
|
+
getLowMap() {
|
|
2713
|
+
return this.tarjan().lowMap;
|
|
2714
|
+
}
|
|
2715
|
+
/**
|
|
2716
|
+
* Strongly connected components computed by `tarjan()`.
|
|
2717
|
+
* @returns Map from SCC id to vertices.
|
|
2718
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
2719
|
+
*/
|
|
2720
|
+
getSCCs() {
|
|
2721
|
+
return this.tarjan().SCCs;
|
|
2722
|
+
}
|
|
2723
|
+
/**
|
|
2724
|
+
* Internal hook to attach a directed edge into adjacency maps.
|
|
2725
|
+
* @param edge - Edge instance.
|
|
2726
|
+
* @returns `true` if inserted; otherwise `false`.
|
|
2727
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2728
|
+
*/
|
|
2729
|
+
_addEdge(edge) {
|
|
2730
|
+
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
2731
|
+
return false;
|
|
2732
|
+
}
|
|
2733
|
+
const srcVertex = this._getVertex(edge.src);
|
|
2734
|
+
const destVertex = this._getVertex(edge.dest);
|
|
2735
|
+
if (srcVertex && destVertex) {
|
|
2736
|
+
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
2737
|
+
if (srcOutEdges) {
|
|
2738
|
+
srcOutEdges.push(edge);
|
|
2739
|
+
} else {
|
|
2740
|
+
this._outEdgeMap.set(srcVertex, [edge]);
|
|
2741
|
+
}
|
|
2742
|
+
const destInEdges = this._inEdgeMap.get(destVertex);
|
|
2743
|
+
if (destInEdges) {
|
|
2744
|
+
destInEdges.push(edge);
|
|
2745
|
+
} else {
|
|
2746
|
+
this._inEdgeMap.set(destVertex, [edge]);
|
|
2747
|
+
}
|
|
2748
|
+
return true;
|
|
2749
|
+
} else {
|
|
2750
|
+
return false;
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
};
|
|
2754
|
+
__name(_DirectedGraph, "DirectedGraph");
|
|
2755
|
+
var DirectedGraph = _DirectedGraph;
|
|
2756
|
+
|
|
2757
|
+
// src/common/index.ts
|
|
2758
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2759
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2760
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2761
|
+
return DFSOperation2;
|
|
2762
|
+
})(DFSOperation || {});
|
|
2763
|
+
var _Range = class _Range {
|
|
2764
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2765
|
+
this.low = low;
|
|
2766
|
+
this.high = high;
|
|
2767
|
+
this.includeLow = includeLow;
|
|
2768
|
+
this.includeHigh = includeHigh;
|
|
2769
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
2770
|
+
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
2771
|
+
}
|
|
2772
|
+
// Determine whether a key is within the range
|
|
2773
|
+
isInRange(key, comparator) {
|
|
2774
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2775
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2776
|
+
return lowCheck && highCheck;
|
|
2777
|
+
}
|
|
2778
|
+
};
|
|
2779
|
+
__name(_Range, "Range");
|
|
2780
|
+
var Range = _Range;
|
|
2781
|
+
/**
|
|
2782
|
+
* data-structure-typed
|
|
2783
|
+
*
|
|
2784
|
+
* @author Pablo Zeng
|
|
2785
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
2786
|
+
* @license MIT License
|
|
2787
|
+
*/
|
|
2788
|
+
|
|
2789
|
+
exports.AbstractEdge = AbstractEdge;
|
|
2790
|
+
exports.AbstractGraph = AbstractGraph;
|
|
2791
|
+
exports.AbstractVertex = AbstractVertex;
|
|
2792
|
+
exports.DFSOperation = DFSOperation;
|
|
2793
|
+
exports.DirectedEdge = DirectedEdge;
|
|
2794
|
+
exports.DirectedGraph = DirectedGraph;
|
|
2795
|
+
exports.DirectedVertex = DirectedVertex;
|
|
2796
|
+
exports.Range = Range;
|
|
2797
|
+
//# sourceMappingURL=index.cjs.map
|
|
2798
|
+
//# sourceMappingURL=index.cjs.map
|