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.
Files changed (65) hide show
  1. package/dist/cjs/index.cjs +150 -43
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +151 -42
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +150 -44
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +151 -43
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +23 -0
  10. package/dist/types/common/index.d.ts +1 -0
  11. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
  15. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  16. package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
  17. package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
  18. package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
  19. package/dist/types/data-structures/heap/heap.d.ts +3 -7
  20. package/dist/types/data-structures/queue/deque.d.ts +41 -1
  21. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
  22. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  23. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  24. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  25. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  26. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  27. package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
  28. package/dist/umd/directed-graph-typed.js +149 -40
  29. package/dist/umd/directed-graph-typed.js.map +1 -1
  30. package/dist/umd/directed-graph-typed.min.js +3 -1
  31. package/dist/umd/directed-graph-typed.min.js.map +1 -1
  32. package/package.json +2 -2
  33. package/src/common/error.ts +60 -0
  34. package/src/common/index.ts +2 -0
  35. package/src/data-structures/base/iterable-element-base.ts +5 -4
  36. package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
  37. package/src/data-structures/binary-tree/binary-tree.ts +121 -49
  38. package/src/data-structures/binary-tree/bst.ts +12 -4
  39. package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
  40. package/src/data-structures/binary-tree/tree-map.ts +8 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
  42. package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
  43. package/src/data-structures/binary-tree/tree-set.ts +7 -6
  44. package/src/data-structures/graph/abstract-graph.ts +124 -19
  45. package/src/data-structures/graph/directed-graph.ts +8 -4
  46. package/src/data-structures/graph/map-graph.ts +1 -1
  47. package/src/data-structures/graph/undirected-graph.ts +99 -4
  48. package/src/data-structures/hash/hash-map.ts +19 -6
  49. package/src/data-structures/heap/heap.ts +21 -17
  50. package/src/data-structures/heap/max-heap.ts +2 -3
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  52. package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
  53. package/src/data-structures/matrix/matrix.ts +9 -10
  54. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
  55. package/src/data-structures/queue/deque.ts +72 -4
  56. package/src/data-structures/stack/stack.ts +1 -1
  57. package/src/data-structures/trie/trie.ts +12 -6
  58. package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
  59. package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
  60. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
  61. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  63. package/src/types/data-structures/queue/deque.ts +7 -0
  64. package/src/types/data-structures/stack/stack.ts +1 -1
  65. package/src/utils/utils.ts +4 -2
@@ -30,6 +30,7 @@ var directedGraphTyped = (() => {
30
30
  DirectedEdge: () => DirectedEdge,
31
31
  DirectedGraph: () => DirectedGraph,
32
32
  DirectedVertex: () => DirectedVertex,
33
+ ERR: () => ERR,
33
34
  Range: () => Range
34
35
  });
35
36
 
@@ -54,6 +55,52 @@ var directedGraphTyped = (() => {
54
55
  return result;
55
56
  };
56
57
 
58
+ // src/common/error.ts
59
+ var ERR = {
60
+ // Range / index
61
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
62
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
63
+ // Type / argument
64
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
65
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
66
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
67
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
68
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
69
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
70
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
71
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
72
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
73
+ // State / operation
74
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
75
+ // Matrix
76
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
77
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
78
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
79
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
80
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
81
+ };
82
+
83
+ // src/common/index.ts
84
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
85
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
86
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
87
+ return DFSOperation2;
88
+ })(DFSOperation || {});
89
+ var Range = class {
90
+ constructor(low, high, includeLow = true, includeHigh = true) {
91
+ this.low = low;
92
+ this.high = high;
93
+ this.includeLow = includeLow;
94
+ this.includeHigh = includeHigh;
95
+ }
96
+ // Determine whether a key is within the range
97
+ isInRange(key, comparator) {
98
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
99
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
100
+ return lowCheck && highCheck;
101
+ }
102
+ };
103
+
57
104
  // src/data-structures/base/iterable-entry-base.ts
58
105
  var IterableEntryBase = class {
59
106
  /**
@@ -255,7 +302,7 @@ var directedGraphTyped = (() => {
255
302
  if (options) {
256
303
  const { toElementFn } = options;
257
304
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
258
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
305
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
259
306
  }
260
307
  }
261
308
  /**
@@ -411,7 +458,7 @@ var directedGraphTyped = (() => {
411
458
  acc = initialValue;
412
459
  } else {
413
460
  const first = iter.next();
414
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
461
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
415
462
  acc = first.value;
416
463
  index = 1;
417
464
  }
@@ -468,7 +515,7 @@ var directedGraphTyped = (() => {
468
515
  __publicField(this, "_elements", []);
469
516
  __publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
470
517
  if (typeof a === "object" || typeof b === "object") {
471
- throw TypeError("When comparing object types, define a custom comparator in options.");
518
+ throw new TypeError(ERR.comparatorRequired("Heap"));
472
519
  }
473
520
  if (a > b) return 1;
474
521
  if (a < b) return -1;
@@ -778,7 +825,7 @@ var directedGraphTyped = (() => {
778
825
  */
779
826
  map(callback, options, thisArg) {
780
827
  const { comparator, toElementFn, ...rest } = options != null ? options : {};
781
- if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
828
+ if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
782
829
  const out = this._createLike([], { ...rest, comparator, toElementFn });
783
830
  let i = 0;
784
831
  for (const x of this) {
@@ -803,11 +850,6 @@ var directedGraphTyped = (() => {
803
850
  }
804
851
  return out;
805
852
  }
806
- /**
807
- * Get the comparator used to order elements.
808
- * @remarks Time O(1), Space O(1)
809
- * @returns Comparator function.
810
- */
811
853
  /**
812
854
  * Get the comparator used to order elements.
813
855
  * @remarks Time O(1), Space O(1)
@@ -856,8 +898,7 @@ var directedGraphTyped = (() => {
856
898
  */
857
899
  _createInstance(options) {
858
900
  const Ctor = this.constructor;
859
- const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options != null ? options : {} });
860
- return next;
901
+ return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options != null ? options : {} });
861
902
  }
862
903
  /**
863
904
  * (Protected) Create a like-kind instance seeded by elements.
@@ -1576,7 +1617,7 @@ var directedGraphTyped = (() => {
1576
1617
  const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
1577
1618
  return this._addEdge(newEdge);
1578
1619
  } else {
1579
- throw new Error("dest must be a Vertex or vertex key while srcOrEdge is an Edge");
1620
+ throw new TypeError(ERR.invalidArgument("dest must be a Vertex or vertex key when srcOrEdge is an Edge.", "Graph"));
1580
1621
  }
1581
1622
  }
1582
1623
  }
@@ -2185,8 +2226,8 @@ var directedGraphTyped = (() => {
2185
2226
  const Ctor = this.constructor;
2186
2227
  const instance = new Ctor();
2187
2228
  const graph = _options == null ? void 0 : _options.graph;
2188
- if (graph) instance._options = { ...instance._options, ...graph };
2189
- else instance._options = { ...instance._options, ...this._options };
2229
+ if (graph) instance["_options"] = { ...instance["_options"], ...graph };
2230
+ else instance["_options"] = { ...instance["_options"], ...this._options };
2190
2231
  return instance;
2191
2232
  }
2192
2233
  /**
@@ -2219,12 +2260,10 @@ var directedGraphTyped = (() => {
2219
2260
  const [va, vb] = ends;
2220
2261
  const ka = va.key;
2221
2262
  const kb = vb.key;
2222
- const hasA = g.hasVertex ? g.hasVertex(ka) : false;
2223
- const hasB = g.hasVertex ? g.hasVertex(kb) : false;
2263
+ const hasA = typeof g.hasVertex === "function" ? g.hasVertex(ka) : false;
2264
+ const hasB = typeof g.hasVertex === "function" ? g.hasVertex(kb) : false;
2224
2265
  if (hasA && hasB) {
2225
- const w = e.weight;
2226
- const val = e.value;
2227
- const newEdge = g.createEdge(ka, kb, w, val);
2266
+ const newEdge = g.createEdge(ka, kb, e.weight, e.value);
2228
2267
  g._addEdge(newEdge);
2229
2268
  }
2230
2269
  }
@@ -2262,6 +2301,94 @@ var directedGraphTyped = (() => {
2262
2301
  _getVertexKey(vertexOrKey) {
2263
2302
  return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
2264
2303
  }
2304
+ /**
2305
+ * The edge connector string used in visual output.
2306
+ * Override in subclasses (e.g., '--' for undirected, '->' for directed).
2307
+ */
2308
+ get _edgeConnector() {
2309
+ return "--";
2310
+ }
2311
+ /**
2312
+ * Generate a text-based visual representation of the graph.
2313
+ *
2314
+ * **Adjacency list format:**
2315
+ * ```
2316
+ * Graph (5 vertices, 6 edges):
2317
+ * A -> B (1), C (2)
2318
+ * B -> D (3)
2319
+ * C -> (no outgoing edges)
2320
+ * D -> A (1)
2321
+ * E (isolated)
2322
+ * ```
2323
+ *
2324
+ * @param options - Optional display settings.
2325
+ * @param options.showWeight - Whether to show edge weights (default: true).
2326
+ * @returns The visual string.
2327
+ */
2328
+ toVisual(options) {
2329
+ var _a;
2330
+ const showWeight = (_a = options == null ? void 0 : options.showWeight) != null ? _a : true;
2331
+ const vertices = [...this._vertexMap.values()];
2332
+ const vertexCount = vertices.length;
2333
+ const edgeCount = this.edgeSet().length;
2334
+ const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
2335
+ for (const vertex of vertices) {
2336
+ const neighbors = this.getNeighbors(vertex);
2337
+ if (neighbors.length === 0) {
2338
+ lines.push(` ${vertex.key} (isolated)`);
2339
+ } else {
2340
+ const edgeStrs = neighbors.map((neighbor) => {
2341
+ const edge = this.getEdge(vertex, neighbor);
2342
+ if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
2343
+ return `${neighbor.key} (${edge.weight})`;
2344
+ }
2345
+ return `${neighbor.key}`;
2346
+ });
2347
+ lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
2348
+ }
2349
+ }
2350
+ return lines.join("\n");
2351
+ }
2352
+ /**
2353
+ * Generate DOT language representation for Graphviz.
2354
+ *
2355
+ * @param options - Optional display settings.
2356
+ * @param options.name - Graph name (default: 'G').
2357
+ * @param options.showWeight - Whether to label edges with weight (default: true).
2358
+ * @returns DOT format string.
2359
+ */
2360
+ toDot(options) {
2361
+ var _a, _b;
2362
+ const name = (_a = options == null ? void 0 : options.name) != null ? _a : "G";
2363
+ const showWeight = (_b = options == null ? void 0 : options.showWeight) != null ? _b : true;
2364
+ const isDirected = this._edgeConnector === "->";
2365
+ const graphType = isDirected ? "digraph" : "graph";
2366
+ const edgeOp = isDirected ? "->" : "--";
2367
+ const lines = [`${graphType} ${name} {`];
2368
+ for (const vertex of this._vertexMap.values()) {
2369
+ lines.push(` "${vertex.key}";`);
2370
+ }
2371
+ const visited = /* @__PURE__ */ new Set();
2372
+ for (const vertex of this._vertexMap.values()) {
2373
+ for (const neighbor of this.getNeighbors(vertex)) {
2374
+ const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
2375
+ if (visited.has(edgeId)) continue;
2376
+ visited.add(edgeId);
2377
+ const edge = this.getEdge(vertex, neighbor);
2378
+ const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
2379
+ lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
2380
+ }
2381
+ }
2382
+ lines.push("}");
2383
+ return lines.join("\n");
2384
+ }
2385
+ /**
2386
+ * Print the graph to console.
2387
+ * @param options - Display settings passed to `toVisual`.
2388
+ */
2389
+ print(options) {
2390
+ console.log(this.toVisual(options));
2391
+ }
2265
2392
  };
2266
2393
 
2267
2394
  // src/data-structures/graph/directed-graph.ts
@@ -2290,6 +2417,9 @@ var directedGraphTyped = (() => {
2290
2417
  __publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
2291
2418
  __publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
2292
2419
  }
2420
+ get _edgeConnector() {
2421
+ return "->";
2422
+ }
2293
2423
  get outEdgeMap() {
2294
2424
  return this._outEdgeMap;
2295
2425
  }
@@ -2735,27 +2865,6 @@ var directedGraphTyped = (() => {
2735
2865
  }
2736
2866
  }
2737
2867
  };
2738
-
2739
- // src/common/index.ts
2740
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
2741
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
2742
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
2743
- return DFSOperation2;
2744
- })(DFSOperation || {});
2745
- var Range = class {
2746
- constructor(low, high, includeLow = true, includeHigh = true) {
2747
- this.low = low;
2748
- this.high = high;
2749
- this.includeLow = includeLow;
2750
- this.includeHigh = includeHigh;
2751
- }
2752
- // Determine whether a key is within the range
2753
- isInRange(key, comparator) {
2754
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
2755
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
2756
- return lowCheck && highCheck;
2757
- }
2758
- };
2759
2868
  return __toCommonJS(src_exports);
2760
2869
  })();
2761
2870
  /**