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 +143 -138
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +3288 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +143 -138
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +3273 -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/graph-typed.js +7 -7
- package/dist/umd/graph-typed.js.map +1 -1
- package/dist/umd/graph-typed.min.js +1 -1
- package/dist/umd/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/cjs/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
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
4
|
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
5
|
|
|
8
6
|
// src/utils/utils.ts
|
|
9
7
|
var uuidV4 = /* @__PURE__ */ __name(function() {
|
|
@@ -59,7 +57,10 @@ function isComparable(value, isForceObjectComparable = false) {
|
|
|
59
57
|
__name(isComparable, "isComparable");
|
|
60
58
|
|
|
61
59
|
// src/data-structures/base/iterable-entry-base.ts
|
|
62
|
-
var
|
|
60
|
+
var IterableEntryBase = class {
|
|
61
|
+
static {
|
|
62
|
+
__name(this, "IterableEntryBase");
|
|
63
|
+
}
|
|
63
64
|
/**
|
|
64
65
|
* Default iterator yielding `[key, value]` entries.
|
|
65
66
|
* @returns Iterator of `[K, V]`.
|
|
@@ -108,7 +109,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
108
109
|
every(predicate, thisArg) {
|
|
109
110
|
let index = 0;
|
|
110
111
|
for (const item of this) {
|
|
111
|
-
if (!predicate.call(thisArg, item[
|
|
112
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
112
113
|
return false;
|
|
113
114
|
}
|
|
114
115
|
}
|
|
@@ -124,7 +125,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
124
125
|
some(predicate, thisArg) {
|
|
125
126
|
let index = 0;
|
|
126
127
|
for (const item of this) {
|
|
127
|
-
if (predicate.call(thisArg, item[
|
|
128
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
128
129
|
return true;
|
|
129
130
|
}
|
|
130
131
|
}
|
|
@@ -140,7 +141,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
140
141
|
let index = 0;
|
|
141
142
|
for (const item of this) {
|
|
142
143
|
const [key, value] = item;
|
|
143
|
-
callbackfn.call(thisArg,
|
|
144
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
147
|
/**
|
|
@@ -154,7 +155,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
154
155
|
let index = 0;
|
|
155
156
|
for (const item of this) {
|
|
156
157
|
const [key, value] = item;
|
|
157
|
-
if (callbackfn.call(thisArg,
|
|
158
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
158
159
|
}
|
|
159
160
|
return;
|
|
160
161
|
}
|
|
@@ -228,11 +229,12 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
228
229
|
console.log(this.toVisual());
|
|
229
230
|
}
|
|
230
231
|
};
|
|
231
|
-
__name(_IterableEntryBase, "IterableEntryBase");
|
|
232
|
-
var IterableEntryBase = _IterableEntryBase;
|
|
233
232
|
|
|
234
233
|
// src/data-structures/base/iterable-element-base.ts
|
|
235
|
-
var
|
|
234
|
+
var IterableElementBase = class {
|
|
235
|
+
static {
|
|
236
|
+
__name(this, "IterableElementBase");
|
|
237
|
+
}
|
|
236
238
|
/**
|
|
237
239
|
* Create a new iterable base.
|
|
238
240
|
*
|
|
@@ -243,19 +245,19 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
243
245
|
* Time O(1), Space O(1).
|
|
244
246
|
*/
|
|
245
247
|
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
248
|
if (options) {
|
|
254
249
|
const { toElementFn } = options;
|
|
255
250
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
256
251
|
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
257
252
|
}
|
|
258
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
256
|
+
*
|
|
257
|
+
* @remarks
|
|
258
|
+
* Time O(1), Space O(1).
|
|
259
|
+
*/
|
|
260
|
+
_toElementFn;
|
|
259
261
|
/**
|
|
260
262
|
* Exposes the current `toElementFn`, if configured.
|
|
261
263
|
*
|
|
@@ -450,11 +452,13 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
450
452
|
console.log(this.toVisual());
|
|
451
453
|
}
|
|
452
454
|
};
|
|
453
|
-
__name(_IterableElementBase, "IterableElementBase");
|
|
454
|
-
var IterableElementBase = _IterableElementBase;
|
|
455
455
|
|
|
456
456
|
// src/data-structures/heap/heap.ts
|
|
457
|
-
var
|
|
457
|
+
var Heap = class _Heap extends IterableElementBase {
|
|
458
|
+
static {
|
|
459
|
+
__name(this, "Heap");
|
|
460
|
+
}
|
|
461
|
+
_equals = Object.is;
|
|
458
462
|
/**
|
|
459
463
|
* Create a Heap and optionally bulk-insert elements.
|
|
460
464
|
* @remarks Time O(N), Space O(N)
|
|
@@ -464,23 +468,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
464
468
|
*/
|
|
465
469
|
constructor(elements = [], options) {
|
|
466
470
|
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
471
|
if (options) {
|
|
479
472
|
const { comparator } = options;
|
|
480
473
|
if (comparator) this._comparator = comparator;
|
|
481
474
|
}
|
|
482
475
|
this.addMany(elements);
|
|
483
476
|
}
|
|
477
|
+
_elements = [];
|
|
484
478
|
/**
|
|
485
479
|
* Get the backing array of the heap.
|
|
486
480
|
* @remarks Time O(1), Space O(1)
|
|
@@ -503,8 +497,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
503
497
|
* @returns Last element or undefined.
|
|
504
498
|
*/
|
|
505
499
|
get leaf() {
|
|
506
|
-
|
|
507
|
-
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
500
|
+
return this.elements[this.size - 1] ?? void 0;
|
|
508
501
|
}
|
|
509
502
|
/**
|
|
510
503
|
* Create a heap of the same class from an iterable.
|
|
@@ -777,7 +770,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
777
770
|
* @returns A new heap with mapped elements.
|
|
778
771
|
*/
|
|
779
772
|
map(callback, options, thisArg) {
|
|
780
|
-
const { comparator, toElementFn, ...rest } = options
|
|
773
|
+
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
781
774
|
if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
|
|
782
775
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
783
776
|
let i = 0;
|
|
@@ -803,6 +796,15 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
803
796
|
}
|
|
804
797
|
return out;
|
|
805
798
|
}
|
|
799
|
+
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
800
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
801
|
+
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
802
|
+
}
|
|
803
|
+
if (a > b) return 1;
|
|
804
|
+
if (a < b) return -1;
|
|
805
|
+
return 0;
|
|
806
|
+
}, "_DEFAULT_COMPARATOR");
|
|
807
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
806
808
|
/**
|
|
807
809
|
* Get the comparator used to order elements.
|
|
808
810
|
* @remarks Time O(1), Space O(1)
|
|
@@ -856,7 +858,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
856
858
|
*/
|
|
857
859
|
_createInstance(options) {
|
|
858
860
|
const Ctor = this.constructor;
|
|
859
|
-
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options
|
|
861
|
+
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
|
|
860
862
|
return next;
|
|
861
863
|
}
|
|
862
864
|
/**
|
|
@@ -884,11 +886,12 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
884
886
|
return this._createLike([], options);
|
|
885
887
|
}
|
|
886
888
|
};
|
|
887
|
-
__name(_Heap, "Heap");
|
|
888
|
-
var Heap = _Heap;
|
|
889
889
|
|
|
890
890
|
// src/data-structures/base/linear-base.ts
|
|
891
|
-
var
|
|
891
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
892
|
+
static {
|
|
893
|
+
__name(this, "LinearBase");
|
|
894
|
+
}
|
|
892
895
|
/**
|
|
893
896
|
* Construct a linear container with runtime options.
|
|
894
897
|
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
@@ -896,12 +899,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
896
899
|
*/
|
|
897
900
|
constructor(options) {
|
|
898
901
|
super(options);
|
|
899
|
-
__publicField(this, "_maxLen", -1);
|
|
900
902
|
if (options) {
|
|
901
903
|
const { maxLen } = options;
|
|
902
904
|
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
903
905
|
}
|
|
904
906
|
}
|
|
907
|
+
_maxLen = -1;
|
|
905
908
|
/**
|
|
906
909
|
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
907
910
|
* @returns Maximum allowed length.
|
|
@@ -1034,7 +1037,7 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1034
1037
|
return array;
|
|
1035
1038
|
}
|
|
1036
1039
|
reduceRight(callbackfn, initialValue) {
|
|
1037
|
-
let accumulator = initialValue
|
|
1040
|
+
let accumulator = initialValue ?? 0;
|
|
1038
1041
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
1039
1042
|
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
1040
1043
|
}
|
|
@@ -1076,11 +1079,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1076
1079
|
return this;
|
|
1077
1080
|
}
|
|
1078
1081
|
};
|
|
1079
|
-
__name(_LinearBase, "LinearBase");
|
|
1080
|
-
var LinearBase = _LinearBase;
|
|
1081
1082
|
|
|
1082
1083
|
// src/data-structures/queue/queue.ts
|
|
1083
|
-
var
|
|
1084
|
+
var Queue = class _Queue extends LinearBase {
|
|
1085
|
+
static {
|
|
1086
|
+
__name(this, "Queue");
|
|
1087
|
+
}
|
|
1084
1088
|
/**
|
|
1085
1089
|
* Create a Queue and optionally bulk-insert elements.
|
|
1086
1090
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1090,15 +1094,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1090
1094
|
*/
|
|
1091
1095
|
constructor(elements = [], options) {
|
|
1092
1096
|
super(options);
|
|
1093
|
-
__publicField(this, "_elements", []);
|
|
1094
|
-
__publicField(this, "_offset", 0);
|
|
1095
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
1096
1097
|
if (options) {
|
|
1097
1098
|
const { autoCompactRatio = 0.5 } = options;
|
|
1098
1099
|
this._autoCompactRatio = autoCompactRatio;
|
|
1099
1100
|
}
|
|
1100
1101
|
this.pushMany(elements);
|
|
1101
1102
|
}
|
|
1103
|
+
_elements = [];
|
|
1102
1104
|
/**
|
|
1103
1105
|
* Get the underlying array buffer.
|
|
1104
1106
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1107,6 +1109,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1107
1109
|
get elements() {
|
|
1108
1110
|
return this._elements;
|
|
1109
1111
|
}
|
|
1112
|
+
_offset = 0;
|
|
1110
1113
|
/**
|
|
1111
1114
|
* Get the current start offset into the array.
|
|
1112
1115
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1115,6 +1118,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1115
1118
|
get offset() {
|
|
1116
1119
|
return this._offset;
|
|
1117
1120
|
}
|
|
1121
|
+
_autoCompactRatio = 0.5;
|
|
1118
1122
|
/**
|
|
1119
1123
|
* Get the compaction threshold (offset/size).
|
|
1120
1124
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1359,11 +1363,10 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1359
1363
|
* @returns A new Queue with mapped elements.
|
|
1360
1364
|
*/
|
|
1361
1365
|
map(callback, options, thisArg) {
|
|
1362
|
-
var _a, _b;
|
|
1363
1366
|
const out = new this.constructor([], {
|
|
1364
|
-
toElementFn: options
|
|
1365
|
-
maxLen:
|
|
1366
|
-
autoCompactRatio:
|
|
1367
|
+
toElementFn: options?.toElementFn,
|
|
1368
|
+
maxLen: options?.maxLen ?? this._maxLen,
|
|
1369
|
+
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
1367
1370
|
});
|
|
1368
1371
|
let index = 0;
|
|
1369
1372
|
for (const v of this)
|
|
@@ -1378,14 +1381,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1378
1381
|
* @returns A new queue with mapped elements (same element type).
|
|
1379
1382
|
*/
|
|
1380
1383
|
mapSame(callback, thisArg) {
|
|
1381
|
-
var _a;
|
|
1382
1384
|
const Ctor = this.constructor;
|
|
1383
1385
|
const out = new Ctor([], {
|
|
1384
1386
|
toElementFn: this.toElementFn,
|
|
1385
1387
|
maxLen: this._maxLen,
|
|
1386
1388
|
autoCompactRatio: this._autoCompactRatio
|
|
1387
1389
|
});
|
|
1388
|
-
|
|
1390
|
+
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
1389
1391
|
let index = 0;
|
|
1390
1392
|
for (const v of this) {
|
|
1391
1393
|
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
@@ -1445,36 +1447,39 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1445
1447
|
return new Ctor(elements, options);
|
|
1446
1448
|
}
|
|
1447
1449
|
};
|
|
1448
|
-
__name(_Queue, "Queue");
|
|
1449
|
-
var Queue = _Queue;
|
|
1450
1450
|
|
|
1451
1451
|
// src/data-structures/graph/abstract-graph.ts
|
|
1452
|
-
var
|
|
1452
|
+
var AbstractVertex = class {
|
|
1453
|
+
static {
|
|
1454
|
+
__name(this, "AbstractVertex");
|
|
1455
|
+
}
|
|
1456
|
+
key;
|
|
1457
|
+
value;
|
|
1453
1458
|
constructor(key, value) {
|
|
1454
|
-
__publicField(this, "key");
|
|
1455
|
-
__publicField(this, "value");
|
|
1456
1459
|
this.key = key;
|
|
1457
1460
|
this.value = value;
|
|
1458
1461
|
}
|
|
1459
1462
|
};
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
+
var AbstractEdge = class {
|
|
1464
|
+
static {
|
|
1465
|
+
__name(this, "AbstractEdge");
|
|
1466
|
+
}
|
|
1467
|
+
value;
|
|
1468
|
+
weight;
|
|
1463
1469
|
constructor(weight, value) {
|
|
1464
|
-
__publicField(this, "value");
|
|
1465
|
-
__publicField(this, "weight");
|
|
1466
|
-
__publicField(this, "_hashCode");
|
|
1467
1470
|
this.weight = weight !== void 0 ? weight : 1;
|
|
1468
1471
|
this.value = value;
|
|
1469
1472
|
this._hashCode = uuidV4();
|
|
1470
1473
|
}
|
|
1474
|
+
_hashCode;
|
|
1471
1475
|
get hashCode() {
|
|
1472
1476
|
return this._hashCode;
|
|
1473
1477
|
}
|
|
1474
1478
|
};
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1479
|
+
var AbstractGraph = class extends IterableEntryBase {
|
|
1480
|
+
static {
|
|
1481
|
+
__name(this, "AbstractGraph");
|
|
1482
|
+
}
|
|
1478
1483
|
/**
|
|
1479
1484
|
* Construct a graph with runtime defaults.
|
|
1480
1485
|
* @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -1482,14 +1487,14 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1482
1487
|
*/
|
|
1483
1488
|
constructor(options) {
|
|
1484
1489
|
super();
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
const graph = options == null ? void 0 : options.graph;
|
|
1488
|
-
this._options = { defaultEdgeWeight: 1, ...graph != null ? graph : {} };
|
|
1490
|
+
const graph = options?.graph;
|
|
1491
|
+
this._options = { defaultEdgeWeight: 1, ...graph ?? {} };
|
|
1489
1492
|
}
|
|
1493
|
+
_options = { defaultEdgeWeight: 1 };
|
|
1490
1494
|
get options() {
|
|
1491
1495
|
return this._options;
|
|
1492
1496
|
}
|
|
1497
|
+
_vertexMap = /* @__PURE__ */ new Map();
|
|
1493
1498
|
get vertexMap() {
|
|
1494
1499
|
return this._vertexMap;
|
|
1495
1500
|
}
|
|
@@ -1647,10 +1652,9 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1647
1652
|
* @remarks Time O(L), Space O(1) where L is path length
|
|
1648
1653
|
*/
|
|
1649
1654
|
getPathSumWeight(path) {
|
|
1650
|
-
var _a;
|
|
1651
1655
|
let sum = 0;
|
|
1652
1656
|
for (let i = 0; i < path.length; i++) {
|
|
1653
|
-
sum +=
|
|
1657
|
+
sum += this.getEdge(path[i], path[i + 1])?.weight || 0;
|
|
1654
1658
|
}
|
|
1655
1659
|
return sum;
|
|
1656
1660
|
}
|
|
@@ -1712,7 +1716,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1712
1716
|
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
1713
1717
|
*/
|
|
1714
1718
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
1715
|
-
var _a, _b;
|
|
1716
1719
|
if (isWeight === void 0) isWeight = false;
|
|
1717
1720
|
if (isWeight) {
|
|
1718
1721
|
if (isDFS) {
|
|
@@ -1730,7 +1733,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1730
1733
|
}
|
|
1731
1734
|
return allPaths[minIndex] || void 0;
|
|
1732
1735
|
} else {
|
|
1733
|
-
return
|
|
1736
|
+
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
1734
1737
|
}
|
|
1735
1738
|
} else {
|
|
1736
1739
|
let minPath = [];
|
|
@@ -1859,7 +1862,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1859
1862
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
1860
1863
|
}
|
|
1861
1864
|
dijkstra(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
1862
|
-
var _a;
|
|
1863
1865
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
1864
1866
|
let minDest = void 0;
|
|
1865
1867
|
let minPath = [];
|
|
@@ -1897,8 +1899,8 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1897
1899
|
}, "getPaths");
|
|
1898
1900
|
while (heap.size > 0) {
|
|
1899
1901
|
const curHeapNode = heap.poll();
|
|
1900
|
-
const dist = curHeapNode
|
|
1901
|
-
const cur = curHeapNode
|
|
1902
|
+
const dist = curHeapNode?.key;
|
|
1903
|
+
const cur = curHeapNode?.value;
|
|
1902
1904
|
if (dist !== void 0) {
|
|
1903
1905
|
if (cur) {
|
|
1904
1906
|
seen.add(cur);
|
|
@@ -1914,7 +1916,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
1914
1916
|
const neighbors = this.getNeighbors(cur);
|
|
1915
1917
|
for (const neighbor of neighbors) {
|
|
1916
1918
|
if (!seen.has(neighbor)) {
|
|
1917
|
-
const weight =
|
|
1919
|
+
const weight = this.getEdge(cur, neighbor)?.weight;
|
|
1918
1920
|
if (typeof weight === "number") {
|
|
1919
1921
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
1920
1922
|
if (distSrcToNeighbor !== void 0) {
|
|
@@ -2037,7 +2039,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2037
2039
|
* @remarks Time O(V^3), Space O(V^2)
|
|
2038
2040
|
*/
|
|
2039
2041
|
floydWarshall() {
|
|
2040
|
-
var _a;
|
|
2041
2042
|
const idAndVertices = [...this._vertexMap];
|
|
2042
2043
|
const n = idAndVertices.length;
|
|
2043
2044
|
const costs = [];
|
|
@@ -2051,7 +2052,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2051
2052
|
}
|
|
2052
2053
|
for (let i = 0; i < n; i++) {
|
|
2053
2054
|
for (let j = 0; j < n; j++) {
|
|
2054
|
-
costs[i][j] =
|
|
2055
|
+
costs[i][j] = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])?.weight || Number.MAX_SAFE_INTEGER;
|
|
2055
2056
|
}
|
|
2056
2057
|
}
|
|
2057
2058
|
for (let k = 0; k < n; k++) {
|
|
@@ -2115,7 +2116,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2115
2116
|
const filtered = [];
|
|
2116
2117
|
let index = 0;
|
|
2117
2118
|
for (const [key, value] of this) {
|
|
2118
|
-
if (predicate.call(thisArg,
|
|
2119
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
2119
2120
|
filtered.push([key, value]);
|
|
2120
2121
|
}
|
|
2121
2122
|
index++;
|
|
@@ -2130,7 +2131,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2130
2131
|
const filtered = [];
|
|
2131
2132
|
let index = 0;
|
|
2132
2133
|
for (const [key, value] of this) {
|
|
2133
|
-
if (predicate.call(thisArg,
|
|
2134
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
2134
2135
|
filtered.push([key, value]);
|
|
2135
2136
|
}
|
|
2136
2137
|
index++;
|
|
@@ -2141,7 +2142,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2141
2142
|
const mapped = [];
|
|
2142
2143
|
let index = 0;
|
|
2143
2144
|
for (const [key, value] of this) {
|
|
2144
|
-
mapped.push(callback.call(thisArg,
|
|
2145
|
+
mapped.push(callback.call(thisArg, value, key, index, this));
|
|
2145
2146
|
index++;
|
|
2146
2147
|
}
|
|
2147
2148
|
return mapped;
|
|
@@ -2194,7 +2195,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2194
2195
|
_createInstance(_options) {
|
|
2195
2196
|
const Ctor = this.constructor;
|
|
2196
2197
|
const instance = new Ctor();
|
|
2197
|
-
const graph = _options
|
|
2198
|
+
const graph = _options?.graph;
|
|
2198
2199
|
if (graph) instance._options = { ...instance._options, ...graph };
|
|
2199
2200
|
else instance._options = { ...instance._options, ...this._options };
|
|
2200
2201
|
return instance;
|
|
@@ -2273,29 +2274,32 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2273
2274
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
2274
2275
|
}
|
|
2275
2276
|
};
|
|
2276
|
-
__name(_AbstractGraph, "AbstractGraph");
|
|
2277
|
-
var AbstractGraph = _AbstractGraph;
|
|
2278
2277
|
|
|
2279
2278
|
// src/data-structures/graph/directed-graph.ts
|
|
2280
|
-
var
|
|
2279
|
+
var DirectedVertex = class extends AbstractVertex {
|
|
2280
|
+
static {
|
|
2281
|
+
__name(this, "DirectedVertex");
|
|
2282
|
+
}
|
|
2281
2283
|
constructor(key, value) {
|
|
2282
2284
|
super(key, value);
|
|
2283
2285
|
}
|
|
2284
2286
|
};
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2287
|
+
var DirectedEdge = class extends AbstractEdge {
|
|
2288
|
+
static {
|
|
2289
|
+
__name(this, "DirectedEdge");
|
|
2290
|
+
}
|
|
2291
|
+
src;
|
|
2292
|
+
dest;
|
|
2288
2293
|
constructor(src, dest, weight, value) {
|
|
2289
2294
|
super(weight, value);
|
|
2290
|
-
__publicField(this, "src");
|
|
2291
|
-
__publicField(this, "dest");
|
|
2292
2295
|
this.src = src;
|
|
2293
2296
|
this.dest = dest;
|
|
2294
2297
|
}
|
|
2295
2298
|
};
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
+
var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
2300
|
+
static {
|
|
2301
|
+
__name(this, "DirectedGraph");
|
|
2302
|
+
}
|
|
2299
2303
|
/**
|
|
2300
2304
|
* Construct a directed graph with runtime defaults.
|
|
2301
2305
|
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -2303,15 +2307,15 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2303
2307
|
*/
|
|
2304
2308
|
constructor(options) {
|
|
2305
2309
|
super(options);
|
|
2306
|
-
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
2307
|
-
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
2308
2310
|
}
|
|
2311
|
+
_outEdgeMap = /* @__PURE__ */ new Map();
|
|
2309
2312
|
get outEdgeMap() {
|
|
2310
2313
|
return this._outEdgeMap;
|
|
2311
2314
|
}
|
|
2312
2315
|
set outEdgeMap(v) {
|
|
2313
2316
|
this._outEdgeMap = v;
|
|
2314
2317
|
}
|
|
2318
|
+
_inEdgeMap = /* @__PURE__ */ new Map();
|
|
2315
2319
|
get inEdgeMap() {
|
|
2316
2320
|
return this._inEdgeMap;
|
|
2317
2321
|
}
|
|
@@ -2364,8 +2368,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2364
2368
|
* @remarks Time O(1), Space O(1)
|
|
2365
2369
|
*/
|
|
2366
2370
|
createEdge(src, dest, weight, value) {
|
|
2367
|
-
|
|
2368
|
-
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2371
|
+
return new DirectedEdge(src, dest, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
2369
2372
|
}
|
|
2370
2373
|
/**
|
|
2371
2374
|
* Get the unique edge from `src` to `dest`, if present.
|
|
@@ -2436,7 +2439,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2436
2439
|
if (src && dest) {
|
|
2437
2440
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
2438
2441
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
2439
|
-
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest ===
|
|
2442
|
+
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest === dest?.key);
|
|
2440
2443
|
}
|
|
2441
2444
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
2442
2445
|
if (destInEdges && destInEdges.length > 0) {
|
|
@@ -2558,7 +2561,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2558
2561
|
* @remarks Time O(V + E), Space O(V)
|
|
2559
2562
|
*/
|
|
2560
2563
|
topologicalSort(propertyName) {
|
|
2561
|
-
propertyName = propertyName
|
|
2564
|
+
propertyName = propertyName ?? "key";
|
|
2562
2565
|
const statusMap = /* @__PURE__ */ new Map();
|
|
2563
2566
|
for (const entry of this.vertexMap) {
|
|
2564
2567
|
statusMap.set(entry[1], 0);
|
|
@@ -2751,27 +2754,30 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2751
2754
|
}
|
|
2752
2755
|
}
|
|
2753
2756
|
};
|
|
2754
|
-
__name(_DirectedGraph, "DirectedGraph");
|
|
2755
|
-
var DirectedGraph = _DirectedGraph;
|
|
2756
2757
|
|
|
2757
2758
|
// src/data-structures/graph/undirected-graph.ts
|
|
2758
|
-
var
|
|
2759
|
+
var UndirectedVertex = class extends AbstractVertex {
|
|
2760
|
+
static {
|
|
2761
|
+
__name(this, "UndirectedVertex");
|
|
2762
|
+
}
|
|
2759
2763
|
constructor(key, value) {
|
|
2760
2764
|
super(key, value);
|
|
2761
2765
|
}
|
|
2762
2766
|
};
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2767
|
+
var UndirectedEdge = class extends AbstractEdge {
|
|
2768
|
+
static {
|
|
2769
|
+
__name(this, "UndirectedEdge");
|
|
2770
|
+
}
|
|
2771
|
+
endpoints;
|
|
2766
2772
|
constructor(v1, v2, weight, value) {
|
|
2767
2773
|
super(weight, value);
|
|
2768
|
-
__publicField(this, "endpoints");
|
|
2769
2774
|
this.endpoints = [v1, v2];
|
|
2770
2775
|
}
|
|
2771
2776
|
};
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2777
|
+
var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
2778
|
+
static {
|
|
2779
|
+
__name(this, "UndirectedGraph");
|
|
2780
|
+
}
|
|
2775
2781
|
/**
|
|
2776
2782
|
* Construct an undirected graph with runtime defaults.
|
|
2777
2783
|
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -2779,9 +2785,9 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
2779
2785
|
*/
|
|
2780
2786
|
constructor(options) {
|
|
2781
2787
|
super(options);
|
|
2782
|
-
__publicField(this, "_edgeMap");
|
|
2783
2788
|
this._edgeMap = /* @__PURE__ */ new Map();
|
|
2784
2789
|
}
|
|
2790
|
+
_edgeMap;
|
|
2785
2791
|
get edgeMap() {
|
|
2786
2792
|
return this._edgeMap;
|
|
2787
2793
|
}
|
|
@@ -2834,8 +2840,7 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
2834
2840
|
* @remarks Time O(1), Space O(1)
|
|
2835
2841
|
*/
|
|
2836
2842
|
createEdge(v1, v2, weight, value) {
|
|
2837
|
-
|
|
2838
|
-
return new UndirectedEdge(v1, v2, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2843
|
+
return new UndirectedEdge(v1, v2, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
2839
2844
|
}
|
|
2840
2845
|
/**
|
|
2841
2846
|
* Get an undirected edge between two vertices, if present.
|
|
@@ -2845,13 +2850,12 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
2845
2850
|
* @remarks Time O(1) avg, Space O(1)
|
|
2846
2851
|
*/
|
|
2847
2852
|
getEdge(v1, v2) {
|
|
2848
|
-
var _a;
|
|
2849
2853
|
let edgeMap = [];
|
|
2850
2854
|
if (v1 !== void 0 && v2 !== void 0) {
|
|
2851
2855
|
const vertex1 = this._getVertex(v1);
|
|
2852
2856
|
const vertex2 = this._getVertex(v2);
|
|
2853
2857
|
if (vertex1 && vertex2) {
|
|
2854
|
-
edgeMap =
|
|
2858
|
+
edgeMap = this._edgeMap.get(vertex1)?.filter((e) => e.endpoints.includes(vertex2.key));
|
|
2855
2859
|
}
|
|
2856
2860
|
}
|
|
2857
2861
|
return edgeMap ? edgeMap[0] || void 0 : void 0;
|
|
@@ -2944,10 +2948,9 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
2944
2948
|
* @remarks Time O(1) avg, Space O(1)
|
|
2945
2949
|
*/
|
|
2946
2950
|
degreeOf(vertexOrKey) {
|
|
2947
|
-
var _a;
|
|
2948
2951
|
const vertex = this._getVertex(vertexOrKey);
|
|
2949
2952
|
if (vertex) {
|
|
2950
|
-
return
|
|
2953
|
+
return this._edgeMap.get(vertex)?.length || 0;
|
|
2951
2954
|
} else {
|
|
2952
2955
|
return 0;
|
|
2953
2956
|
}
|
|
@@ -3140,29 +3143,32 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
3140
3143
|
return true;
|
|
3141
3144
|
}
|
|
3142
3145
|
};
|
|
3143
|
-
__name(_UndirectedGraph, "UndirectedGraph");
|
|
3144
|
-
var UndirectedGraph = _UndirectedGraph;
|
|
3145
3146
|
|
|
3146
3147
|
// src/data-structures/graph/map-graph.ts
|
|
3147
|
-
var
|
|
3148
|
+
var MapVertex = class extends DirectedVertex {
|
|
3149
|
+
static {
|
|
3150
|
+
__name(this, "MapVertex");
|
|
3151
|
+
}
|
|
3152
|
+
lat;
|
|
3153
|
+
long;
|
|
3148
3154
|
constructor(key, value, lat, long) {
|
|
3149
3155
|
super(key, value);
|
|
3150
|
-
__publicField(this, "lat");
|
|
3151
|
-
__publicField(this, "long");
|
|
3152
3156
|
this.lat = lat;
|
|
3153
3157
|
this.long = long;
|
|
3154
3158
|
}
|
|
3155
3159
|
};
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3160
|
+
var MapEdge = class extends DirectedEdge {
|
|
3161
|
+
static {
|
|
3162
|
+
__name(this, "MapEdge");
|
|
3163
|
+
}
|
|
3159
3164
|
constructor(src, dest, weight, value) {
|
|
3160
3165
|
super(src, dest, weight, value);
|
|
3161
3166
|
}
|
|
3162
3167
|
};
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3168
|
+
var MapGraph = class _MapGraph extends DirectedGraph {
|
|
3169
|
+
static {
|
|
3170
|
+
__name(this, "MapGraph");
|
|
3171
|
+
}
|
|
3166
3172
|
/**
|
|
3167
3173
|
* Construct a MapGraph.
|
|
3168
3174
|
* @param originCoord - Origin coordinate `[lat, long]` used as default.
|
|
@@ -3171,14 +3177,14 @@ var _MapGraph = class _MapGraph extends DirectedGraph {
|
|
|
3171
3177
|
*/
|
|
3172
3178
|
constructor(originCoord, bottomRight) {
|
|
3173
3179
|
super();
|
|
3174
|
-
__publicField(this, "_originCoord", [0, 0]);
|
|
3175
|
-
__publicField(this, "_bottomRight");
|
|
3176
3180
|
this._originCoord = originCoord;
|
|
3177
3181
|
this._bottomRight = bottomRight;
|
|
3178
3182
|
}
|
|
3183
|
+
_originCoord = [0, 0];
|
|
3179
3184
|
get originCoord() {
|
|
3180
3185
|
return this._originCoord;
|
|
3181
3186
|
}
|
|
3187
|
+
_bottomRight;
|
|
3182
3188
|
get bottomRight() {
|
|
3183
3189
|
return this._bottomRight;
|
|
3184
3190
|
}
|
|
@@ -3230,13 +3236,11 @@ var _MapGraph = class _MapGraph extends DirectedGraph {
|
|
|
3230
3236
|
*/
|
|
3231
3237
|
_createInstance(options) {
|
|
3232
3238
|
const { originCoord, bottomRight } = options || {};
|
|
3233
|
-
const oc = originCoord
|
|
3234
|
-
const br = bottomRight
|
|
3239
|
+
const oc = originCoord ?? this.originCoord;
|
|
3240
|
+
const br = bottomRight ?? this.bottomRight;
|
|
3235
3241
|
return new _MapGraph(oc, br);
|
|
3236
3242
|
}
|
|
3237
3243
|
};
|
|
3238
|
-
__name(_MapGraph, "MapGraph");
|
|
3239
|
-
var MapGraph = _MapGraph;
|
|
3240
3244
|
|
|
3241
3245
|
// src/common/index.ts
|
|
3242
3246
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
@@ -3244,7 +3248,7 @@ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
|
3244
3248
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
3245
3249
|
return DFSOperation2;
|
|
3246
3250
|
})(DFSOperation || {});
|
|
3247
|
-
var
|
|
3251
|
+
var Range = class {
|
|
3248
3252
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
3249
3253
|
this.low = low;
|
|
3250
3254
|
this.high = high;
|
|
@@ -3253,6 +3257,9 @@ var _Range = class _Range {
|
|
|
3253
3257
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
3254
3258
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
3255
3259
|
}
|
|
3260
|
+
static {
|
|
3261
|
+
__name(this, "Range");
|
|
3262
|
+
}
|
|
3256
3263
|
// Determine whether a key is within the range
|
|
3257
3264
|
isInRange(key, comparator) {
|
|
3258
3265
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -3260,8 +3267,6 @@ var _Range = class _Range {
|
|
|
3260
3267
|
return lowCheck && highCheck;
|
|
3261
3268
|
}
|
|
3262
3269
|
};
|
|
3263
|
-
__name(_Range, "Range");
|
|
3264
|
-
var Range = _Range;
|
|
3265
3270
|
/**
|
|
3266
3271
|
* data-structure-typed
|
|
3267
3272
|
*
|