directed-graph-typed 2.4.3 → 2.4.5
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 +150 -43
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +151 -42
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +150 -44
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +151 -43
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
- package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +3 -7
- package/dist/types/data-structures/queue/deque.d.ts +41 -1
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
- package/dist/umd/directed-graph-typed.js +149 -40
- package/dist/umd/directed-graph-typed.js.map +1 -1
- package/dist/umd/directed-graph-typed.min.js +3 -1
- package/dist/umd/directed-graph-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +5 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
- package/src/data-structures/binary-tree/binary-tree.ts +121 -49
- package/src/data-structures/binary-tree/bst.ts +12 -4
- package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
- package/src/data-structures/binary-tree/tree-map.ts +8 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
- package/src/data-structures/binary-tree/tree-set.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +124 -19
- package/src/data-structures/graph/directed-graph.ts +8 -4
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +99 -4
- package/src/data-structures/hash/hash-map.ts +19 -6
- package/src/data-structures/heap/heap.ts +21 -17
- package/src/data-structures/heap/max-heap.ts +2 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
- package/src/data-structures/matrix/matrix.ts +9 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
- package/src/data-structures/queue/deque.ts +72 -4
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +12 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/types/data-structures/stack/stack.ts +1 -1
- package/src/utils/utils.ts +4 -2
package/dist/esm/index.mjs
CHANGED
|
@@ -22,6 +22,55 @@ var arrayRemove = /* @__PURE__ */ __name(function(array, predicate) {
|
|
|
22
22
|
return result;
|
|
23
23
|
}, "arrayRemove");
|
|
24
24
|
|
|
25
|
+
// src/common/error.ts
|
|
26
|
+
var ERR = {
|
|
27
|
+
// Range / index
|
|
28
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
29
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
30
|
+
// Type / argument
|
|
31
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
32
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
33
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
34
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
35
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
36
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
37
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
38
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
39
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
40
|
+
// State / operation
|
|
41
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
42
|
+
// Matrix
|
|
43
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
44
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
45
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
46
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
47
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/common/index.ts
|
|
51
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
52
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
53
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
54
|
+
return DFSOperation2;
|
|
55
|
+
})(DFSOperation || {});
|
|
56
|
+
var Range = class {
|
|
57
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
58
|
+
this.low = low;
|
|
59
|
+
this.high = high;
|
|
60
|
+
this.includeLow = includeLow;
|
|
61
|
+
this.includeHigh = includeHigh;
|
|
62
|
+
}
|
|
63
|
+
static {
|
|
64
|
+
__name(this, "Range");
|
|
65
|
+
}
|
|
66
|
+
// Determine whether a key is within the range
|
|
67
|
+
isInRange(key, comparator) {
|
|
68
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
69
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
70
|
+
return lowCheck && highCheck;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
25
74
|
// src/data-structures/base/iterable-entry-base.ts
|
|
26
75
|
var IterableEntryBase = class {
|
|
27
76
|
static {
|
|
@@ -222,7 +271,7 @@ var IterableElementBase = class {
|
|
|
222
271
|
if (options) {
|
|
223
272
|
const { toElementFn } = options;
|
|
224
273
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
225
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
274
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
226
275
|
}
|
|
227
276
|
}
|
|
228
277
|
/**
|
|
@@ -385,7 +434,7 @@ var IterableElementBase = class {
|
|
|
385
434
|
acc = initialValue;
|
|
386
435
|
} else {
|
|
387
436
|
const first = iter.next();
|
|
388
|
-
if (first.done) throw new TypeError(
|
|
437
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
389
438
|
acc = first.value;
|
|
390
439
|
index = 1;
|
|
391
440
|
}
|
|
@@ -745,7 +794,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
745
794
|
*/
|
|
746
795
|
map(callback, options, thisArg) {
|
|
747
796
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
748
|
-
if (!comparator) throw new TypeError("Heap.map
|
|
797
|
+
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|
|
749
798
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
750
799
|
let i = 0;
|
|
751
800
|
for (const x of this) {
|
|
@@ -772,18 +821,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
772
821
|
}
|
|
773
822
|
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
774
823
|
if (typeof a === "object" || typeof b === "object") {
|
|
775
|
-
throw TypeError(
|
|
824
|
+
throw new TypeError(ERR.comparatorRequired("Heap"));
|
|
776
825
|
}
|
|
777
826
|
if (a > b) return 1;
|
|
778
827
|
if (a < b) return -1;
|
|
779
828
|
return 0;
|
|
780
829
|
}, "_DEFAULT_COMPARATOR");
|
|
781
830
|
_comparator = this._DEFAULT_COMPARATOR;
|
|
782
|
-
/**
|
|
783
|
-
* Get the comparator used to order elements.
|
|
784
|
-
* @remarks Time O(1), Space O(1)
|
|
785
|
-
* @returns Comparator function.
|
|
786
|
-
*/
|
|
787
831
|
/**
|
|
788
832
|
* Get the comparator used to order elements.
|
|
789
833
|
* @remarks Time O(1), Space O(1)
|
|
@@ -832,8 +876,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
832
876
|
*/
|
|
833
877
|
_createInstance(options) {
|
|
834
878
|
const Ctor = this.constructor;
|
|
835
|
-
|
|
836
|
-
return next;
|
|
879
|
+
return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
|
|
837
880
|
}
|
|
838
881
|
/**
|
|
839
882
|
* (Protected) Create a like-kind instance seeded by elements.
|
|
@@ -1565,7 +1608,7 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
1565
1608
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
1566
1609
|
return this._addEdge(newEdge);
|
|
1567
1610
|
} else {
|
|
1568
|
-
throw new
|
|
1611
|
+
throw new TypeError(ERR.invalidArgument("dest must be a Vertex or vertex key when srcOrEdge is an Edge.", "Graph"));
|
|
1569
1612
|
}
|
|
1570
1613
|
}
|
|
1571
1614
|
}
|
|
@@ -2170,8 +2213,8 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
2170
2213
|
const Ctor = this.constructor;
|
|
2171
2214
|
const instance = new Ctor();
|
|
2172
2215
|
const graph = _options?.graph;
|
|
2173
|
-
if (graph) instance
|
|
2174
|
-
else instance
|
|
2216
|
+
if (graph) instance["_options"] = { ...instance["_options"], ...graph };
|
|
2217
|
+
else instance["_options"] = { ...instance["_options"], ...this._options };
|
|
2175
2218
|
return instance;
|
|
2176
2219
|
}
|
|
2177
2220
|
/**
|
|
@@ -2204,12 +2247,10 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
2204
2247
|
const [va, vb] = ends;
|
|
2205
2248
|
const ka = va.key;
|
|
2206
2249
|
const kb = vb.key;
|
|
2207
|
-
const hasA = g.hasVertex ? g.hasVertex(ka) : false;
|
|
2208
|
-
const hasB = g.hasVertex ? g.hasVertex(kb) : false;
|
|
2250
|
+
const hasA = typeof g.hasVertex === "function" ? g.hasVertex(ka) : false;
|
|
2251
|
+
const hasB = typeof g.hasVertex === "function" ? g.hasVertex(kb) : false;
|
|
2209
2252
|
if (hasA && hasB) {
|
|
2210
|
-
const
|
|
2211
|
-
const val = e.value;
|
|
2212
|
-
const newEdge = g.createEdge(ka, kb, w, val);
|
|
2253
|
+
const newEdge = g.createEdge(ka, kb, e.weight, e.value);
|
|
2213
2254
|
g._addEdge(newEdge);
|
|
2214
2255
|
}
|
|
2215
2256
|
}
|
|
@@ -2247,6 +2288,92 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
2247
2288
|
_getVertexKey(vertexOrKey) {
|
|
2248
2289
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
2249
2290
|
}
|
|
2291
|
+
/**
|
|
2292
|
+
* The edge connector string used in visual output.
|
|
2293
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
2294
|
+
*/
|
|
2295
|
+
get _edgeConnector() {
|
|
2296
|
+
return "--";
|
|
2297
|
+
}
|
|
2298
|
+
/**
|
|
2299
|
+
* Generate a text-based visual representation of the graph.
|
|
2300
|
+
*
|
|
2301
|
+
* **Adjacency list format:**
|
|
2302
|
+
* ```
|
|
2303
|
+
* Graph (5 vertices, 6 edges):
|
|
2304
|
+
* A -> B (1), C (2)
|
|
2305
|
+
* B -> D (3)
|
|
2306
|
+
* C -> (no outgoing edges)
|
|
2307
|
+
* D -> A (1)
|
|
2308
|
+
* E (isolated)
|
|
2309
|
+
* ```
|
|
2310
|
+
*
|
|
2311
|
+
* @param options - Optional display settings.
|
|
2312
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
2313
|
+
* @returns The visual string.
|
|
2314
|
+
*/
|
|
2315
|
+
toVisual(options) {
|
|
2316
|
+
const showWeight = options?.showWeight ?? true;
|
|
2317
|
+
const vertices = [...this._vertexMap.values()];
|
|
2318
|
+
const vertexCount = vertices.length;
|
|
2319
|
+
const edgeCount = this.edgeSet().length;
|
|
2320
|
+
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
2321
|
+
for (const vertex of vertices) {
|
|
2322
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2323
|
+
if (neighbors.length === 0) {
|
|
2324
|
+
lines.push(` ${vertex.key} (isolated)`);
|
|
2325
|
+
} else {
|
|
2326
|
+
const edgeStrs = neighbors.map((neighbor) => {
|
|
2327
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2328
|
+
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
2329
|
+
return `${neighbor.key} (${edge.weight})`;
|
|
2330
|
+
}
|
|
2331
|
+
return `${neighbor.key}`;
|
|
2332
|
+
});
|
|
2333
|
+
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
return lines.join("\n");
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* Generate DOT language representation for Graphviz.
|
|
2340
|
+
*
|
|
2341
|
+
* @param options - Optional display settings.
|
|
2342
|
+
* @param options.name - Graph name (default: 'G').
|
|
2343
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
2344
|
+
* @returns DOT format string.
|
|
2345
|
+
*/
|
|
2346
|
+
toDot(options) {
|
|
2347
|
+
const name = options?.name ?? "G";
|
|
2348
|
+
const showWeight = options?.showWeight ?? true;
|
|
2349
|
+
const isDirected = this._edgeConnector === "->";
|
|
2350
|
+
const graphType = isDirected ? "digraph" : "graph";
|
|
2351
|
+
const edgeOp = isDirected ? "->" : "--";
|
|
2352
|
+
const lines = [`${graphType} ${name} {`];
|
|
2353
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2354
|
+
lines.push(` "${vertex.key}";`);
|
|
2355
|
+
}
|
|
2356
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2357
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2358
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
2359
|
+
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
2360
|
+
if (visited.has(edgeId)) continue;
|
|
2361
|
+
visited.add(edgeId);
|
|
2362
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2363
|
+
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
2364
|
+
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
lines.push("}");
|
|
2368
|
+
return lines.join("\n");
|
|
2369
|
+
}
|
|
2370
|
+
/**
|
|
2371
|
+
* Print the graph to console.
|
|
2372
|
+
* @param options - Display settings passed to `toVisual`.
|
|
2373
|
+
*/
|
|
2374
|
+
print(options) {
|
|
2375
|
+
console.log(this.toVisual(options));
|
|
2376
|
+
}
|
|
2250
2377
|
};
|
|
2251
2378
|
|
|
2252
2379
|
// src/data-structures/graph/directed-graph.ts
|
|
@@ -2282,6 +2409,9 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2282
2409
|
constructor(options) {
|
|
2283
2410
|
super(options);
|
|
2284
2411
|
}
|
|
2412
|
+
get _edgeConnector() {
|
|
2413
|
+
return "->";
|
|
2414
|
+
}
|
|
2285
2415
|
_outEdgeMap = /* @__PURE__ */ new Map();
|
|
2286
2416
|
get outEdgeMap() {
|
|
2287
2417
|
return this._outEdgeMap;
|
|
@@ -2728,30 +2858,6 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
2728
2858
|
}
|
|
2729
2859
|
}
|
|
2730
2860
|
};
|
|
2731
|
-
|
|
2732
|
-
// src/common/index.ts
|
|
2733
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2734
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2735
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2736
|
-
return DFSOperation2;
|
|
2737
|
-
})(DFSOperation || {});
|
|
2738
|
-
var Range = class {
|
|
2739
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2740
|
-
this.low = low;
|
|
2741
|
-
this.high = high;
|
|
2742
|
-
this.includeLow = includeLow;
|
|
2743
|
-
this.includeHigh = includeHigh;
|
|
2744
|
-
}
|
|
2745
|
-
static {
|
|
2746
|
-
__name(this, "Range");
|
|
2747
|
-
}
|
|
2748
|
-
// Determine whether a key is within the range
|
|
2749
|
-
isInRange(key, comparator) {
|
|
2750
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2751
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2752
|
-
return lowCheck && highCheck;
|
|
2753
|
-
}
|
|
2754
|
-
};
|
|
2755
2861
|
/**
|
|
2756
2862
|
* data-structure-typed
|
|
2757
2863
|
*
|
|
@@ -2760,6 +2866,6 @@ var Range = class {
|
|
|
2760
2866
|
* @license MIT License
|
|
2761
2867
|
*/
|
|
2762
2868
|
|
|
2763
|
-
export { AbstractEdge, AbstractGraph, AbstractVertex, DFSOperation, DirectedEdge, DirectedGraph, DirectedVertex, Range };
|
|
2869
|
+
export { AbstractEdge, AbstractGraph, AbstractVertex, DFSOperation, DirectedEdge, DirectedGraph, DirectedVertex, ERR, Range };
|
|
2764
2870
|
//# sourceMappingURL=index.mjs.map
|
|
2765
2871
|
//# sourceMappingURL=index.mjs.map
|