directed-graph-typed 2.1.2 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +108 -106
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +2798 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +108 -106
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +2789 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +65 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +61 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +58 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +59 -4
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -3
- package/dist/types/types/data-structures/base/base.d.ts +1 -1
- package/dist/umd/directed-graph-typed.js +7 -7
- package/dist/umd/directed-graph-typed.js.map +1 -1
- package/dist/umd/directed-graph-typed.min.js +1 -1
- package/dist/umd/directed-graph-typed.min.js.map +1 -1
- package/package.json +20 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +103 -12
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
- package/src/data-structures/binary-tree/avl-tree.ts +109 -16
- package/src/data-structures/binary-tree/binary-tree.ts +3 -2
- package/src/data-structures/binary-tree/bst.ts +104 -12
- package/src/data-structures/binary-tree/red-black-tree.ts +110 -19
- package/src/data-structures/binary-tree/tree-counter.ts +102 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +124 -12
- package/src/data-structures/graph/abstract-graph.ts +8 -8
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/types/data-structures/base/base.ts +1 -1
- package/tsup.node.config.js +40 -6
package/dist/esm/index.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
3
|
|
|
6
4
|
// src/utils/utils.ts
|
|
7
5
|
var uuidV4 = /* @__PURE__ */ __name(function() {
|
|
@@ -57,7 +55,10 @@ function isComparable(value, isForceObjectComparable = false) {
|
|
|
57
55
|
__name(isComparable, "isComparable");
|
|
58
56
|
|
|
59
57
|
// src/data-structures/base/iterable-entry-base.ts
|
|
60
|
-
var
|
|
58
|
+
var IterableEntryBase = class {
|
|
59
|
+
static {
|
|
60
|
+
__name(this, "IterableEntryBase");
|
|
61
|
+
}
|
|
61
62
|
/**
|
|
62
63
|
* Default iterator yielding `[key, value]` entries.
|
|
63
64
|
* @returns Iterator of `[K, V]`.
|
|
@@ -106,7 +107,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
106
107
|
every(predicate, thisArg) {
|
|
107
108
|
let index = 0;
|
|
108
109
|
for (const item of this) {
|
|
109
|
-
if (!predicate.call(thisArg, item[
|
|
110
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
110
111
|
return false;
|
|
111
112
|
}
|
|
112
113
|
}
|
|
@@ -122,7 +123,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
122
123
|
some(predicate, thisArg) {
|
|
123
124
|
let index = 0;
|
|
124
125
|
for (const item of this) {
|
|
125
|
-
if (predicate.call(thisArg, item[
|
|
126
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
126
127
|
return true;
|
|
127
128
|
}
|
|
128
129
|
}
|
|
@@ -138,7 +139,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
138
139
|
let index = 0;
|
|
139
140
|
for (const item of this) {
|
|
140
141
|
const [key, value] = item;
|
|
141
|
-
callbackfn.call(thisArg,
|
|
142
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
145
|
/**
|
|
@@ -152,7 +153,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
152
153
|
let index = 0;
|
|
153
154
|
for (const item of this) {
|
|
154
155
|
const [key, value] = item;
|
|
155
|
-
if (callbackfn.call(thisArg,
|
|
156
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
156
157
|
}
|
|
157
158
|
return;
|
|
158
159
|
}
|
|
@@ -226,11 +227,12 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
226
227
|
console.log(this.toVisual());
|
|
227
228
|
}
|
|
228
229
|
};
|
|
229
|
-
__name(_IterableEntryBase, "IterableEntryBase");
|
|
230
|
-
var IterableEntryBase = _IterableEntryBase;
|
|
231
230
|
|
|
232
231
|
// src/data-structures/base/iterable-element-base.ts
|
|
233
|
-
var
|
|
232
|
+
var IterableElementBase = class {
|
|
233
|
+
static {
|
|
234
|
+
__name(this, "IterableElementBase");
|
|
235
|
+
}
|
|
234
236
|
/**
|
|
235
237
|
* Create a new iterable base.
|
|
236
238
|
*
|
|
@@ -241,19 +243,19 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
241
243
|
* Time O(1), Space O(1).
|
|
242
244
|
*/
|
|
243
245
|
constructor(options) {
|
|
244
|
-
/**
|
|
245
|
-
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
246
|
-
*
|
|
247
|
-
* @remarks
|
|
248
|
-
* Time O(1), Space O(1).
|
|
249
|
-
*/
|
|
250
|
-
__publicField(this, "_toElementFn");
|
|
251
246
|
if (options) {
|
|
252
247
|
const { toElementFn } = options;
|
|
253
248
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
254
249
|
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
255
250
|
}
|
|
256
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
254
|
+
*
|
|
255
|
+
* @remarks
|
|
256
|
+
* Time O(1), Space O(1).
|
|
257
|
+
*/
|
|
258
|
+
_toElementFn;
|
|
257
259
|
/**
|
|
258
260
|
* Exposes the current `toElementFn`, if configured.
|
|
259
261
|
*
|
|
@@ -448,11 +450,13 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
448
450
|
console.log(this.toVisual());
|
|
449
451
|
}
|
|
450
452
|
};
|
|
451
|
-
__name(_IterableElementBase, "IterableElementBase");
|
|
452
|
-
var IterableElementBase = _IterableElementBase;
|
|
453
453
|
|
|
454
454
|
// src/data-structures/heap/heap.ts
|
|
455
|
-
var
|
|
455
|
+
var Heap = class _Heap extends IterableElementBase {
|
|
456
|
+
static {
|
|
457
|
+
__name(this, "Heap");
|
|
458
|
+
}
|
|
459
|
+
_equals = Object.is;
|
|
456
460
|
/**
|
|
457
461
|
* Create a Heap and optionally bulk-insert elements.
|
|
458
462
|
* @remarks Time O(N), Space O(N)
|
|
@@ -462,23 +466,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
462
466
|
*/
|
|
463
467
|
constructor(elements = [], options) {
|
|
464
468
|
super(options);
|
|
465
|
-
__publicField(this, "_equals", Object.is);
|
|
466
|
-
__publicField(this, "_elements", []);
|
|
467
|
-
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
468
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
469
|
-
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
470
|
-
}
|
|
471
|
-
if (a > b) return 1;
|
|
472
|
-
if (a < b) return -1;
|
|
473
|
-
return 0;
|
|
474
|
-
}, "_DEFAULT_COMPARATOR"));
|
|
475
|
-
__publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
|
|
476
469
|
if (options) {
|
|
477
470
|
const { comparator } = options;
|
|
478
471
|
if (comparator) this._comparator = comparator;
|
|
479
472
|
}
|
|
480
473
|
this.addMany(elements);
|
|
481
474
|
}
|
|
475
|
+
_elements = [];
|
|
482
476
|
/**
|
|
483
477
|
* Get the backing array of the heap.
|
|
484
478
|
* @remarks Time O(1), Space O(1)
|
|
@@ -501,8 +495,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
501
495
|
* @returns Last element or undefined.
|
|
502
496
|
*/
|
|
503
497
|
get leaf() {
|
|
504
|
-
|
|
505
|
-
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
498
|
+
return this.elements[this.size - 1] ?? void 0;
|
|
506
499
|
}
|
|
507
500
|
/**
|
|
508
501
|
* Create a heap of the same class from an iterable.
|
|
@@ -775,7 +768,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
775
768
|
* @returns A new heap with mapped elements.
|
|
776
769
|
*/
|
|
777
770
|
map(callback, options, thisArg) {
|
|
778
|
-
const { comparator, toElementFn, ...rest } = options
|
|
771
|
+
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
779
772
|
if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
|
|
780
773
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
781
774
|
let i = 0;
|
|
@@ -801,6 +794,15 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
801
794
|
}
|
|
802
795
|
return out;
|
|
803
796
|
}
|
|
797
|
+
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
798
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
799
|
+
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
800
|
+
}
|
|
801
|
+
if (a > b) return 1;
|
|
802
|
+
if (a < b) return -1;
|
|
803
|
+
return 0;
|
|
804
|
+
}, "_DEFAULT_COMPARATOR");
|
|
805
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
804
806
|
/**
|
|
805
807
|
* Get the comparator used to order elements.
|
|
806
808
|
* @remarks Time O(1), Space O(1)
|
|
@@ -854,7 +856,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
854
856
|
*/
|
|
855
857
|
_createInstance(options) {
|
|
856
858
|
const Ctor = this.constructor;
|
|
857
|
-
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options
|
|
859
|
+
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
|
|
858
860
|
return next;
|
|
859
861
|
}
|
|
860
862
|
/**
|
|
@@ -882,11 +884,12 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
882
884
|
return this._createLike([], options);
|
|
883
885
|
}
|
|
884
886
|
};
|
|
885
|
-
__name(_Heap, "Heap");
|
|
886
|
-
var Heap = _Heap;
|
|
887
887
|
|
|
888
888
|
// src/data-structures/base/linear-base.ts
|
|
889
|
-
var
|
|
889
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
890
|
+
static {
|
|
891
|
+
__name(this, "LinearBase");
|
|
892
|
+
}
|
|
890
893
|
/**
|
|
891
894
|
* Construct a linear container with runtime options.
|
|
892
895
|
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
@@ -894,12 +897,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
894
897
|
*/
|
|
895
898
|
constructor(options) {
|
|
896
899
|
super(options);
|
|
897
|
-
__publicField(this, "_maxLen", -1);
|
|
898
900
|
if (options) {
|
|
899
901
|
const { maxLen } = options;
|
|
900
902
|
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
901
903
|
}
|
|
902
904
|
}
|
|
905
|
+
_maxLen = -1;
|
|
903
906
|
/**
|
|
904
907
|
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
905
908
|
* @returns Maximum allowed length.
|
|
@@ -1032,7 +1035,7 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1032
1035
|
return array;
|
|
1033
1036
|
}
|
|
1034
1037
|
reduceRight(callbackfn, initialValue) {
|
|
1035
|
-
let accumulator = initialValue
|
|
1038
|
+
let accumulator = initialValue ?? 0;
|
|
1036
1039
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
1037
1040
|
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
1038
1041
|
}
|
|
@@ -1074,11 +1077,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1074
1077
|
return this;
|
|
1075
1078
|
}
|
|
1076
1079
|
};
|
|
1077
|
-
__name(_LinearBase, "LinearBase");
|
|
1078
|
-
var LinearBase = _LinearBase;
|
|
1079
1080
|
|
|
1080
1081
|
// src/data-structures/queue/queue.ts
|
|
1081
|
-
var
|
|
1082
|
+
var Queue = class _Queue extends LinearBase {
|
|
1083
|
+
static {
|
|
1084
|
+
__name(this, "Queue");
|
|
1085
|
+
}
|
|
1082
1086
|
/**
|
|
1083
1087
|
* Create a Queue and optionally bulk-insert elements.
|
|
1084
1088
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1088,15 +1092,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1088
1092
|
*/
|
|
1089
1093
|
constructor(elements = [], options) {
|
|
1090
1094
|
super(options);
|
|
1091
|
-
__publicField(this, "_elements", []);
|
|
1092
|
-
__publicField(this, "_offset", 0);
|
|
1093
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
1094
1095
|
if (options) {
|
|
1095
1096
|
const { autoCompactRatio = 0.5 } = options;
|
|
1096
1097
|
this._autoCompactRatio = autoCompactRatio;
|
|
1097
1098
|
}
|
|
1098
1099
|
this.pushMany(elements);
|
|
1099
1100
|
}
|
|
1101
|
+
_elements = [];
|
|
1100
1102
|
/**
|
|
1101
1103
|
* Get the underlying array buffer.
|
|
1102
1104
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1105,6 +1107,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1105
1107
|
get elements() {
|
|
1106
1108
|
return this._elements;
|
|
1107
1109
|
}
|
|
1110
|
+
_offset = 0;
|
|
1108
1111
|
/**
|
|
1109
1112
|
* Get the current start offset into the array.
|
|
1110
1113
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1113,6 +1116,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1113
1116
|
get offset() {
|
|
1114
1117
|
return this._offset;
|
|
1115
1118
|
}
|
|
1119
|
+
_autoCompactRatio = 0.5;
|
|
1116
1120
|
/**
|
|
1117
1121
|
* Get the compaction threshold (offset/size).
|
|
1118
1122
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1357,11 +1361,10 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1357
1361
|
* @returns A new Queue with mapped elements.
|
|
1358
1362
|
*/
|
|
1359
1363
|
map(callback, options, thisArg) {
|
|
1360
|
-
var _a, _b;
|
|
1361
1364
|
const out = new this.constructor([], {
|
|
1362
|
-
toElementFn: options
|
|
1363
|
-
maxLen:
|
|
1364
|
-
autoCompactRatio:
|
|
1365
|
+
toElementFn: options?.toElementFn,
|
|
1366
|
+
maxLen: options?.maxLen ?? this._maxLen,
|
|
1367
|
+
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
1365
1368
|
});
|
|
1366
1369
|
let index = 0;
|
|
1367
1370
|
for (const v of this)
|
|
@@ -1376,14 +1379,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1376
1379
|
* @returns A new queue with mapped elements (same element type).
|
|
1377
1380
|
*/
|
|
1378
1381
|
mapSame(callback, thisArg) {
|
|
1379
|
-
var _a;
|
|
1380
1382
|
const Ctor = this.constructor;
|
|
1381
1383
|
const out = new Ctor([], {
|
|
1382
1384
|
toElementFn: this.toElementFn,
|
|
1383
1385
|
maxLen: this._maxLen,
|
|
1384
1386
|
autoCompactRatio: this._autoCompactRatio
|
|
1385
1387
|
});
|
|
1386
|
-
|
|
1388
|
+
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
1387
1389
|
let index = 0;
|
|
1388
1390
|
for (const v of this) {
|
|
1389
1391
|
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
@@ -1443,36 +1445,39 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1443
1445
|
return new Ctor(elements, options);
|
|
1444
1446
|
}
|
|
1445
1447
|
};
|
|
1446
|
-
__name(_Queue, "Queue");
|
|
1447
|
-
var Queue = _Queue;
|
|
1448
1448
|
|
|
1449
1449
|
// src/data-structures/graph/abstract-graph.ts
|
|
1450
|
-
var
|
|
1450
|
+
var AbstractVertex = class {
|
|
1451
|
+
static {
|
|
1452
|
+
__name(this, "AbstractVertex");
|
|
1453
|
+
}
|
|
1454
|
+
key;
|
|
1455
|
+
value;
|
|
1451
1456
|
constructor(key, value) {
|
|
1452
|
-
__publicField(this, "key");
|
|
1453
|
-
__publicField(this, "value");
|
|
1454
1457
|
this.key = key;
|
|
1455
1458
|
this.value = value;
|
|
1456
1459
|
}
|
|
1457
1460
|
};
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
+
var AbstractEdge = class {
|
|
1462
|
+
static {
|
|
1463
|
+
__name(this, "AbstractEdge");
|
|
1464
|
+
}
|
|
1465
|
+
value;
|
|
1466
|
+
weight;
|
|
1461
1467
|
constructor(weight, value) {
|
|
1462
|
-
__publicField(this, "value");
|
|
1463
|
-
__publicField(this, "weight");
|
|
1464
|
-
__publicField(this, "_hashCode");
|
|
1465
1468
|
this.weight = weight !== void 0 ? weight : 1;
|
|
1466
1469
|
this.value = value;
|
|
1467
1470
|
this._hashCode = uuidV4();
|
|
1468
1471
|
}
|
|
1472
|
+
_hashCode;
|
|
1469
1473
|
get hashCode() {
|
|
1470
1474
|
return this._hashCode;
|
|
1471
1475
|
}
|
|
1472
1476
|
};
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1477
|
+
var AbstractGraph = class extends IterableEntryBase {
|
|
1478
|
+
static {
|
|
1479
|
+
__name(this, "AbstractGraph");
|
|
1480
|
+
}
|
|
1476
1481
|
/**
|
|
1477
1482
|
* Construct a graph with runtime defaults.
|
|
1478
1483
|
* @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -1480,14 +1485,14 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1480
1485
|
*/
|
|
1481
1486
|
constructor(options) {
|
|
1482
1487
|
super();
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
const graph = options == null ? void 0 : options.graph;
|
|
1486
|
-
this._options = { defaultEdgeWeight: 1, ...graph != null ? graph : {} };
|
|
1488
|
+
const graph = options?.graph;
|
|
1489
|
+
this._options = { defaultEdgeWeight: 1, ...graph ?? {} };
|
|
1487
1490
|
}
|
|
1491
|
+
_options = { defaultEdgeWeight: 1 };
|
|
1488
1492
|
get options() {
|
|
1489
1493
|
return this._options;
|
|
1490
1494
|
}
|
|
1495
|
+
_vertexMap = /* @__PURE__ */ new Map();
|
|
1491
1496
|
get vertexMap() {
|
|
1492
1497
|
return this._vertexMap;
|
|
1493
1498
|
}
|
|
@@ -1645,10 +1650,9 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1645
1650
|
* @remarks Time O(L), Space O(1) where L is path length
|
|
1646
1651
|
*/
|
|
1647
1652
|
getPathSumWeight(path) {
|
|
1648
|
-
var _a;
|
|
1649
1653
|
let sum = 0;
|
|
1650
1654
|
for (let i = 0; i < path.length; i++) {
|
|
1651
|
-
sum +=
|
|
1655
|
+
sum += this.getEdge(path[i], path[i + 1])?.weight || 0;
|
|
1652
1656
|
}
|
|
1653
1657
|
return sum;
|
|
1654
1658
|
}
|
|
@@ -1710,7 +1714,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1710
1714
|
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
1711
1715
|
*/
|
|
1712
1716
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
1713
|
-
var _a, _b;
|
|
1714
1717
|
if (isWeight === void 0) isWeight = false;
|
|
1715
1718
|
if (isWeight) {
|
|
1716
1719
|
if (isDFS) {
|
|
@@ -1728,7 +1731,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1728
1731
|
}
|
|
1729
1732
|
return allPaths[minIndex] || void 0;
|
|
1730
1733
|
} else {
|
|
1731
|
-
return
|
|
1734
|
+
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
1732
1735
|
}
|
|
1733
1736
|
} else {
|
|
1734
1737
|
let minPath = [];
|
|
@@ -1857,7 +1860,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1857
1860
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
1858
1861
|
}
|
|
1859
1862
|
dijkstra(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
1860
|
-
var _a;
|
|
1861
1863
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
1862
1864
|
let minDest = void 0;
|
|
1863
1865
|
let minPath = [];
|
|
@@ -1895,8 +1897,8 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1895
1897
|
}, "getPaths");
|
|
1896
1898
|
while (heap.size > 0) {
|
|
1897
1899
|
const curHeapNode = heap.poll();
|
|
1898
|
-
const dist = curHeapNode
|
|
1899
|
-
const cur = curHeapNode
|
|
1900
|
+
const dist = curHeapNode?.key;
|
|
1901
|
+
const cur = curHeapNode?.value;
|
|
1900
1902
|
if (dist !== void 0) {
|
|
1901
1903
|
if (cur) {
|
|
1902
1904
|
seen.add(cur);
|
|
@@ -1912,7 +1914,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1912
1914
|
const neighbors = this.getNeighbors(cur);
|
|
1913
1915
|
for (const neighbor of neighbors) {
|
|
1914
1916
|
if (!seen.has(neighbor)) {
|
|
1915
|
-
const weight =
|
|
1917
|
+
const weight = this.getEdge(cur, neighbor)?.weight;
|
|
1916
1918
|
if (typeof weight === "number") {
|
|
1917
1919
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
1918
1920
|
if (distSrcToNeighbor !== void 0) {
|
|
@@ -2035,7 +2037,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2035
2037
|
* @remarks Time O(V^3), Space O(V^2)
|
|
2036
2038
|
*/
|
|
2037
2039
|
floydWarshall() {
|
|
2038
|
-
var _a;
|
|
2039
2040
|
const idAndVertices = [...this._vertexMap];
|
|
2040
2041
|
const n = idAndVertices.length;
|
|
2041
2042
|
const costs = [];
|
|
@@ -2049,7 +2050,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2049
2050
|
}
|
|
2050
2051
|
for (let i = 0; i < n; i++) {
|
|
2051
2052
|
for (let j = 0; j < n; j++) {
|
|
2052
|
-
costs[i][j] =
|
|
2053
|
+
costs[i][j] = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])?.weight || Number.MAX_SAFE_INTEGER;
|
|
2053
2054
|
}
|
|
2054
2055
|
}
|
|
2055
2056
|
for (let k = 0; k < n; k++) {
|
|
@@ -2113,7 +2114,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2113
2114
|
const filtered = [];
|
|
2114
2115
|
let index = 0;
|
|
2115
2116
|
for (const [key, value] of this) {
|
|
2116
|
-
if (predicate.call(thisArg,
|
|
2117
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
2117
2118
|
filtered.push([key, value]);
|
|
2118
2119
|
}
|
|
2119
2120
|
index++;
|
|
@@ -2128,7 +2129,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2128
2129
|
const filtered = [];
|
|
2129
2130
|
let index = 0;
|
|
2130
2131
|
for (const [key, value] of this) {
|
|
2131
|
-
if (predicate.call(thisArg,
|
|
2132
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
2132
2133
|
filtered.push([key, value]);
|
|
2133
2134
|
}
|
|
2134
2135
|
index++;
|
|
@@ -2139,7 +2140,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2139
2140
|
const mapped = [];
|
|
2140
2141
|
let index = 0;
|
|
2141
2142
|
for (const [key, value] of this) {
|
|
2142
|
-
mapped.push(callback.call(thisArg,
|
|
2143
|
+
mapped.push(callback.call(thisArg, value, key, index, this));
|
|
2143
2144
|
index++;
|
|
2144
2145
|
}
|
|
2145
2146
|
return mapped;
|
|
@@ -2192,7 +2193,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2192
2193
|
_createInstance(_options) {
|
|
2193
2194
|
const Ctor = this.constructor;
|
|
2194
2195
|
const instance = new Ctor();
|
|
2195
|
-
const graph = _options
|
|
2196
|
+
const graph = _options?.graph;
|
|
2196
2197
|
if (graph) instance._options = { ...instance._options, ...graph };
|
|
2197
2198
|
else instance._options = { ...instance._options, ...this._options };
|
|
2198
2199
|
return instance;
|
|
@@ -2271,29 +2272,32 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2271
2272
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
2272
2273
|
}
|
|
2273
2274
|
};
|
|
2274
|
-
__name(_AbstractGraph, "AbstractGraph");
|
|
2275
|
-
var AbstractGraph = _AbstractGraph;
|
|
2276
2275
|
|
|
2277
2276
|
// src/data-structures/graph/directed-graph.ts
|
|
2278
|
-
var
|
|
2277
|
+
var DirectedVertex = class extends AbstractVertex {
|
|
2278
|
+
static {
|
|
2279
|
+
__name(this, "DirectedVertex");
|
|
2280
|
+
}
|
|
2279
2281
|
constructor(key, value) {
|
|
2280
2282
|
super(key, value);
|
|
2281
2283
|
}
|
|
2282
2284
|
};
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2285
|
+
var DirectedEdge = class extends AbstractEdge {
|
|
2286
|
+
static {
|
|
2287
|
+
__name(this, "DirectedEdge");
|
|
2288
|
+
}
|
|
2289
|
+
src;
|
|
2290
|
+
dest;
|
|
2286
2291
|
constructor(src, dest, weight, value) {
|
|
2287
2292
|
super(weight, value);
|
|
2288
|
-
__publicField(this, "src");
|
|
2289
|
-
__publicField(this, "dest");
|
|
2290
2293
|
this.src = src;
|
|
2291
2294
|
this.dest = dest;
|
|
2292
2295
|
}
|
|
2293
2296
|
};
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
+
var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
2298
|
+
static {
|
|
2299
|
+
__name(this, "DirectedGraph");
|
|
2300
|
+
}
|
|
2297
2301
|
/**
|
|
2298
2302
|
* Construct a directed graph with runtime defaults.
|
|
2299
2303
|
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -2301,15 +2305,15 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2301
2305
|
*/
|
|
2302
2306
|
constructor(options) {
|
|
2303
2307
|
super(options);
|
|
2304
|
-
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
2305
|
-
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
2306
2308
|
}
|
|
2309
|
+
_outEdgeMap = /* @__PURE__ */ new Map();
|
|
2307
2310
|
get outEdgeMap() {
|
|
2308
2311
|
return this._outEdgeMap;
|
|
2309
2312
|
}
|
|
2310
2313
|
set outEdgeMap(v) {
|
|
2311
2314
|
this._outEdgeMap = v;
|
|
2312
2315
|
}
|
|
2316
|
+
_inEdgeMap = /* @__PURE__ */ new Map();
|
|
2313
2317
|
get inEdgeMap() {
|
|
2314
2318
|
return this._inEdgeMap;
|
|
2315
2319
|
}
|
|
@@ -2362,8 +2366,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2362
2366
|
* @remarks Time O(1), Space O(1)
|
|
2363
2367
|
*/
|
|
2364
2368
|
createEdge(src, dest, weight, value) {
|
|
2365
|
-
|
|
2366
|
-
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2369
|
+
return new DirectedEdge(src, dest, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
2367
2370
|
}
|
|
2368
2371
|
/**
|
|
2369
2372
|
* Get the unique edge from `src` to `dest`, if present.
|
|
@@ -2434,7 +2437,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2434
2437
|
if (src && dest) {
|
|
2435
2438
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
2436
2439
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
2437
|
-
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest ===
|
|
2440
|
+
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest === dest?.key);
|
|
2438
2441
|
}
|
|
2439
2442
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
2440
2443
|
if (destInEdges && destInEdges.length > 0) {
|
|
@@ -2556,7 +2559,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2556
2559
|
* @remarks Time O(V + E), Space O(V)
|
|
2557
2560
|
*/
|
|
2558
2561
|
topologicalSort(propertyName) {
|
|
2559
|
-
propertyName = propertyName
|
|
2562
|
+
propertyName = propertyName ?? "key";
|
|
2560
2563
|
const statusMap = /* @__PURE__ */ new Map();
|
|
2561
2564
|
for (const entry of this.vertexMap) {
|
|
2562
2565
|
statusMap.set(entry[1], 0);
|
|
@@ -2749,8 +2752,6 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2749
2752
|
}
|
|
2750
2753
|
}
|
|
2751
2754
|
};
|
|
2752
|
-
__name(_DirectedGraph, "DirectedGraph");
|
|
2753
|
-
var DirectedGraph = _DirectedGraph;
|
|
2754
2755
|
|
|
2755
2756
|
// src/common/index.ts
|
|
2756
2757
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
@@ -2758,7 +2759,7 @@ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
|
2758
2759
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2759
2760
|
return DFSOperation2;
|
|
2760
2761
|
})(DFSOperation || {});
|
|
2761
|
-
var
|
|
2762
|
+
var Range = class {
|
|
2762
2763
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2763
2764
|
this.low = low;
|
|
2764
2765
|
this.high = high;
|
|
@@ -2767,6 +2768,9 @@ var _Range = class _Range {
|
|
|
2767
2768
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
2768
2769
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
2769
2770
|
}
|
|
2771
|
+
static {
|
|
2772
|
+
__name(this, "Range");
|
|
2773
|
+
}
|
|
2770
2774
|
// Determine whether a key is within the range
|
|
2771
2775
|
isInRange(key, comparator) {
|
|
2772
2776
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -2774,8 +2778,6 @@ var _Range = class _Range {
|
|
|
2774
2778
|
return lowCheck && highCheck;
|
|
2775
2779
|
}
|
|
2776
2780
|
};
|
|
2777
|
-
__name(_Range, "Range");
|
|
2778
|
-
var Range = _Range;
|
|
2779
2781
|
/**
|
|
2780
2782
|
* data-structure-typed
|
|
2781
2783
|
*
|