@hpcc-js/util 2.45.0 → 2.46.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.
Files changed (79) hide show
  1. package/LICENSE +43 -43
  2. package/dist/index.js +2290 -2289
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/lib-es6/__package__.js +3 -3
  7. package/lib-es6/array.js +84 -84
  8. package/lib-es6/array.js.map +1 -1
  9. package/lib-es6/cache.js +60 -60
  10. package/lib-es6/debounce.js +85 -85
  11. package/lib-es6/dictionary.js +61 -61
  12. package/lib-es6/dispatch.js +101 -101
  13. package/lib-es6/esp.js +32 -32
  14. package/lib-es6/graph.js +348 -348
  15. package/lib-es6/graph.js.map +1 -1
  16. package/lib-es6/graph2.js +603 -603
  17. package/lib-es6/graph2.js.map +1 -1
  18. package/lib-es6/hashSum.js +49 -49
  19. package/lib-es6/immutable.js +54 -54
  20. package/lib-es6/index.js +21 -21
  21. package/lib-es6/logging.js +177 -177
  22. package/lib-es6/logging.js.map +1 -1
  23. package/lib-es6/math.js +90 -90
  24. package/lib-es6/object.js +121 -121
  25. package/lib-es6/object.js.map +1 -1
  26. package/lib-es6/observer.js +79 -79
  27. package/lib-es6/platform.js +17 -17
  28. package/lib-es6/saxParser.js +125 -125
  29. package/lib-es6/stack.js +41 -41
  30. package/lib-es6/stateful.js +170 -170
  31. package/lib-es6/string.js +22 -22
  32. package/lib-es6/url.js +32 -32
  33. package/lib-es6/url.js.map +1 -1
  34. package/package.json +6 -21
  35. package/src/__package__.ts +2 -2
  36. package/src/array.ts +98 -98
  37. package/src/cache.ts +65 -65
  38. package/src/debounce.ts +88 -88
  39. package/src/dictionary.ts +69 -69
  40. package/src/dispatch.ts +119 -119
  41. package/src/esp.ts +32 -32
  42. package/src/graph.ts +353 -353
  43. package/src/graph2.ts +661 -661
  44. package/src/hashSum.ts +55 -55
  45. package/src/immutable.ts +57 -57
  46. package/src/index.ts +21 -21
  47. package/src/logging.ts +211 -211
  48. package/src/math.ts +92 -92
  49. package/src/object.ts +117 -117
  50. package/src/observer.ts +91 -91
  51. package/src/platform.ts +20 -20
  52. package/src/saxParser.ts +135 -135
  53. package/src/stack.ts +41 -41
  54. package/src/stateful.ts +178 -178
  55. package/src/string.ts +21 -21
  56. package/src/url.ts +27 -27
  57. package/types/__package__.d.ts +3 -3
  58. package/types/array.d.ts +13 -13
  59. package/types/cache.d.ts +20 -20
  60. package/types/debounce.d.ts +12 -12
  61. package/types/dictionary.d.ts +20 -20
  62. package/types/dispatch.d.ts +26 -26
  63. package/types/esp.d.ts +1 -1
  64. package/types/graph.d.ts +73 -73
  65. package/types/graph2.d.ts +111 -111
  66. package/types/hashSum.d.ts +1 -1
  67. package/types/immutable.d.ts +2 -2
  68. package/types/index.d.ts +21 -21
  69. package/types/logging.d.ts +57 -57
  70. package/types/math.d.ts +70 -70
  71. package/types/object.d.ts +48 -48
  72. package/types/observer.d.ts +18 -18
  73. package/types/platform.d.ts +5 -5
  74. package/types/saxParser.d.ts +28 -28
  75. package/types/stack.d.ts +28 -28
  76. package/types/stateful.d.ts +33 -33
  77. package/types/string.d.ts +2 -2
  78. package/types/url.d.ts +2 -2
  79. package/types-3.4/__package__.d.ts +2 -2
package/lib-es6/graph2.js CHANGED
@@ -1,604 +1,604 @@
1
- import { __extends, __spreadArray } from "tslib";
2
- import { compare2 } from "./array";
3
- var GraphItem = /** @class */ (function () {
4
- function GraphItem(g, _) {
5
- this._graph = g;
6
- this._ = _;
7
- }
8
- GraphItem.prototype.id = function () {
9
- return this._graph.id(this._);
10
- };
11
- return GraphItem;
12
- }());
13
- var ChildGraphItem = /** @class */ (function (_super) {
14
- __extends(ChildGraphItem, _super);
15
- function ChildGraphItem(g, _) {
16
- return _super.call(this, g, _) || this;
17
- }
18
- ChildGraphItem.prototype.clearParent = function () {
19
- if (this._parent) {
20
- this._parent.removeChild(this);
21
- delete this._parent;
22
- }
23
- return this;
24
- };
25
- ChildGraphItem.prototype.parent = function (_) {
26
- if (arguments.length === 0)
27
- return this._parent;
28
- if (this._parent !== _) {
29
- if (this._parent) {
30
- this._parent.removeChild(this);
31
- }
32
- this._parent = _;
33
- if (this._parent) {
34
- this._parent.addChild(this);
35
- }
36
- }
37
- return this;
38
- };
39
- return ChildGraphItem;
40
- }(GraphItem));
41
- var Subgraph = /** @class */ (function (_super) {
42
- __extends(Subgraph, _super);
43
- function Subgraph(g, _) {
44
- var _this = _super.call(this, g, _) || this;
45
- _this._children = [];
46
- return _this;
47
- }
48
- Subgraph.prototype.children = function () {
49
- return this._children;
50
- };
51
- Subgraph.prototype.addChild = function (_) {
52
- this._children.push(_);
53
- };
54
- Subgraph.prototype.removeChild = function (_) {
55
- this._children = this._children.filter(function (row) { return row.id !== _.id; });
56
- };
57
- return Subgraph;
58
- }(ChildGraphItem));
59
- var Vertex = /** @class */ (function (_super) {
60
- __extends(Vertex, _super);
61
- function Vertex(g, _) {
62
- var _this = _super.call(this, g, _) || this;
63
- _this._inEdges = [];
64
- _this._outEdges = [];
65
- return _this;
66
- }
67
- Vertex.prototype.edges = function () {
68
- return __spreadArray(__spreadArray([], this._inEdges), this._outEdges);
69
- };
70
- Vertex.prototype.edgeCount = function () {
71
- return this._outEdges.length + this._inEdges.length;
72
- };
73
- Vertex.prototype.inEdges = function () {
74
- return this._inEdges;
75
- };
76
- Vertex.prototype.addInEdge = function (e) {
77
- this._inEdges.push(e);
78
- };
79
- Vertex.prototype.removeInEdge = function (id) {
80
- this._inEdges = this._inEdges.filter(function (e) { return e._.id !== id; });
81
- };
82
- Vertex.prototype.outEdges = function () {
83
- return this._outEdges;
84
- };
85
- Vertex.prototype.addOutEdge = function (e) {
86
- this._outEdges.push(e);
87
- };
88
- Vertex.prototype.removeOutEdge = function (id) {
89
- this._outEdges = this._outEdges.filter(function (e) { return e._.id !== id; });
90
- };
91
- return Vertex;
92
- }(ChildGraphItem));
93
- var Edge = /** @class */ (function (_super) {
94
- __extends(Edge, _super);
95
- function Edge(g, _, source, target) {
96
- var _this = _super.call(this, g, _) || this;
97
- _this._source = source;
98
- _this._target = target;
99
- return _this;
100
- }
101
- return Edge;
102
- }(ChildGraphItem));
103
- var Graph2 = /** @class */ (function () {
104
- function Graph2(directed) {
105
- if (directed === void 0) { directed = true; }
106
- this._subgraphMap = {};
107
- this._vertexMap = {};
108
- this._edgeMap = {};
109
- this._idFunc = function (_) { return typeof _.id === "function" ? _.id() : _.id; };
110
- this._sourceFunc = function (_) { return typeof _.source === "function" ? _.source() : _.source; };
111
- this._targetFunc = function (_) { return typeof _.target === "function" ? _.target() : _.target; };
112
- this._updateFunc = function (before, after) { return after; };
113
- this._directed = directed;
114
- }
115
- Graph2.prototype.clear = function () {
116
- this._subgraphMap = {};
117
- this._vertexMap = {};
118
- this._edgeMap = {};
119
- return this;
120
- };
121
- Graph2.prototype.clearParents = function () {
122
- for (var key in this._subgraphMap) {
123
- this._subgraphMap[key].clearParent();
124
- }
125
- for (var key in this._vertexMap) {
126
- this._vertexMap[key].clearParent();
127
- }
128
- return this;
129
- };
130
- Graph2.prototype.isDirected = function () {
131
- return this._directed;
132
- };
133
- Graph2.prototype.idFunc = function (_) {
134
- this._idFunc = _;
135
- return this;
136
- };
137
- Graph2.prototype.sourceFunc = function (_) {
138
- this._sourceFunc = _;
139
- return this;
140
- };
141
- Graph2.prototype.targetFunc = function (_) {
142
- this._targetFunc = _;
143
- return this;
144
- };
145
- Graph2.prototype.updateFunc = function (_) {
146
- this._updateFunc = _;
147
- return this;
148
- };
149
- Graph2.prototype.id = function (_) {
150
- return this._idFunc(_);
151
- };
152
- Graph2.prototype.type = function (id) {
153
- if (this.subgraphExists(id))
154
- return "S";
155
- if (this.vertexExists(id))
156
- return "V";
157
- if (this.edgeExists(id))
158
- return "E";
159
- return "";
160
- };
161
- Graph2.prototype.isSubgraph = function (_) {
162
- return this.subgraphExists(this.id(_));
163
- };
164
- Graph2.prototype.isVertex = function (_) {
165
- return this.vertexExists(this.id(_));
166
- };
167
- Graph2.prototype.isEdge = function (_) {
168
- return this.edgeExists(this.id(_));
169
- };
170
- Graph2.prototype.allItems = function () {
171
- return __spreadArray(__spreadArray(__spreadArray([], this.allSubgraphs()), this.allVertices()), this.allEdges());
172
- };
173
- Graph2.prototype.item = function (id) {
174
- if (this.subgraphExists(id))
175
- return this.subgraph(id);
176
- if (this.vertexExists(id))
177
- return this.vertex(id);
178
- if (this.edgeExists(id))
179
- return this.edge(id);
180
- return undefined;
181
- };
182
- Graph2.prototype.itemExists = function (id) {
183
- return this.edgeExists(id) || this.vertexExists(id) || this.subgraphExists(id);
184
- };
185
- // Subgraphs ---
186
- Graph2.prototype.allSubgraphs = function () {
187
- var retVal = [];
188
- for (var key in this._subgraphMap) {
189
- retVal.push(this._subgraphMap[key]._);
190
- }
191
- return retVal;
192
- };
193
- Graph2.prototype.subgraphs = function () {
194
- var retVal = [];
195
- for (var key in this._subgraphMap) {
196
- if (this._subgraphMap[key].parent() === undefined) {
197
- retVal.push(this._subgraphMap[key]._);
198
- }
199
- }
200
- return retVal;
201
- };
202
- Graph2.prototype.subgraphExists = function (id) {
203
- return !!this._subgraphMap[id];
204
- };
205
- Graph2.prototype.subgraph = function (id) {
206
- return this._subgraphMap[id]._;
207
- };
208
- Graph2.prototype.subgraphSubgraphs = function (id) {
209
- var _this = this;
210
- return this._subgraphMap[id].children().filter(function (child) { return _this.isSubgraph(child._); }).map(function (child) { return child._; });
211
- };
212
- Graph2.prototype.subgraphVertices = function (id) {
213
- var _this = this;
214
- return this._subgraphMap[id].children().filter(function (child) { return _this.isVertex(child._); }).map(function (child) { return child._; });
215
- };
216
- Graph2.prototype.subgraphEdges = function (id) {
217
- var _this = this;
218
- return this._subgraphMap[id].children().filter(function (child) { return _this.isEdge(child._); }).map(function (child) { return child._; });
219
- };
220
- Graph2.prototype.addSubgraph = function (s, parent) {
221
- var s_id = this._idFunc(s);
222
- if (this._subgraphMap[s_id])
223
- throw new Error("Subgraph '" + s_id + "' already exists.");
224
- var subgraph = new Subgraph(this, s);
225
- if (parent) {
226
- var p_id = this._idFunc(parent);
227
- if (!this._subgraphMap[p_id])
228
- throw new Error("Subgraph '" + p_id + "' does not exist.");
229
- subgraph.parent(this._subgraphMap[p_id]);
230
- }
231
- this._subgraphMap[s_id] = subgraph;
232
- return this;
233
- };
234
- Graph2.prototype.mergeSubgraphs = function (_subgraphs) {
235
- var _this = this;
236
- if (_subgraphs === void 0) { _subgraphs = []; }
237
- var sgDiff = compare2(this.allSubgraphs(), _subgraphs, function (sg) { return _this._idFunc(sg); }, this._updateFunc);
238
- sgDiff.exit.forEach(function (sg) { return _this.removeSubgraph(_this._idFunc(sg)); });
239
- sgDiff.enter.forEach(function (sg) { return _this.addSubgraph(sg); });
240
- sgDiff.update.forEach(function (sg) { return _this.updateSubgraph(sg); });
241
- return this;
242
- };
243
- Graph2.prototype.updateSubgraph = function (sg) {
244
- var sg_id = this._idFunc(sg);
245
- var subgraph = this._subgraphMap[sg_id];
246
- if (!subgraph)
247
- throw new Error("Subgraph '" + sg_id + "' does not exist.");
248
- subgraph._ = sg;
249
- return this;
250
- };
251
- Graph2.prototype.removeSubgraph = function (id, promoteChildren) {
252
- var _this = this;
253
- if (promoteChildren === void 0) { promoteChildren = true; }
254
- var sg = this._subgraphMap[id];
255
- if (!sg)
256
- throw new Error("Subgraph '" + id + "' does not exist.");
257
- sg.children().forEach(function (child) {
258
- if (promoteChildren) {
259
- child.parent(sg.parent());
260
- }
261
- else {
262
- if (child instanceof Subgraph) {
263
- _this.removeSubgraph(child.id());
264
- }
265
- else {
266
- _this.removeVertex(child.id());
267
- }
268
- }
269
- });
270
- delete this._subgraphMap[id];
271
- return this;
272
- };
273
- Graph2.prototype.subgraphParent = function (id, parentID) {
274
- var item = this._subgraphMap[id];
275
- if (!item)
276
- throw new Error("Subgraph '" + id + "' does not exist.");
277
- if (parentID === void 0) {
278
- var parent_1 = item.parent();
279
- return parent_1 ? parent_1._ : undefined;
280
- }
281
- var parent = this._subgraphMap[parentID];
282
- if (!parent)
283
- throw new Error("Vertex parent '" + parent + "' does not exist.");
284
- item.parent(parent);
285
- return this;
286
- };
287
- // Vertices ---
288
- Graph2.prototype.allVertices = function () {
289
- var retVal = [];
290
- for (var key in this._vertexMap) {
291
- retVal.push(this._vertexMap[key]._);
292
- }
293
- return retVal;
294
- };
295
- Graph2.prototype.vertices = function () {
296
- var retVal = [];
297
- for (var key in this._vertexMap) {
298
- if (this._vertexMap[key].parent() === undefined) {
299
- retVal.push(this._vertexMap[key]._);
300
- }
301
- }
302
- return retVal;
303
- };
304
- Graph2.prototype.vertexExists = function (id) {
305
- return !!this._vertexMap[id];
306
- };
307
- Graph2.prototype.vertex = function (id) {
308
- return this._vertexMap[id]._;
309
- };
310
- Graph2.prototype.allEdges = function () {
311
- var retVal = [];
312
- for (var key in this._edgeMap) {
313
- retVal.push(this._edgeMap[key]._);
314
- }
315
- return retVal;
316
- };
317
- Graph2.prototype.edges = function () {
318
- var retVal = [];
319
- for (var key in this._edgeMap) {
320
- if (this._edgeMap[key].parent() === undefined) {
321
- retVal.push(this._edgeMap[key]._);
322
- }
323
- }
324
- return retVal;
325
- };
326
- Graph2.prototype.vertexEdges = function (vertexID) {
327
- return this._vertexMap[vertexID].edges().map(function (e) { return e._; });
328
- };
329
- Graph2.prototype.inEdges = function (vertexID) {
330
- return this._vertexMap[vertexID].inEdges().map(function (e) { return e._; });
331
- };
332
- Graph2.prototype.outEdges = function (vertexID) {
333
- return this._vertexMap[vertexID].outEdges().map(function (e) { return e._; });
334
- };
335
- Graph2.prototype._neighbors = function (id) {
336
- return __spreadArray(__spreadArray([], this._vertexMap[id].outEdges().map(function (e) { return e._target; })), this._vertexMap[id].inEdges().map(function (e) { return e._source; }));
337
- };
338
- Graph2.prototype.neighbors = function (id) {
339
- return this._neighbors(id).map(function (n) { return n._; });
340
- };
341
- Graph2.prototype.singleNeighbors = function (id) {
342
- return this._neighbors(id).filter(function (n) { return n.edgeCount() === 1; }).map(function (n) { return n._; });
343
- };
344
- Graph2.prototype.addVertex = function (v, parent) {
345
- var v_id = this._idFunc(v);
346
- if (this._vertexMap[v_id])
347
- throw new Error("Vertex '" + v_id + "' already exists.");
348
- var vertex = new Vertex(this, v);
349
- if (parent) {
350
- var p_id = this._idFunc(parent);
351
- if (!this.subgraphExists(p_id))
352
- throw new Error("Subgraph '" + p_id + "' does not exist.");
353
- vertex.parent(this._subgraphMap[p_id]);
354
- }
355
- this._vertexMap[v_id] = vertex;
356
- return this;
357
- };
358
- Graph2.prototype.mergeVertices = function (_vertices) {
359
- var _this = this;
360
- var vDiff = compare2(this.allVertices(), _vertices, function (v) { return _this._idFunc(v); }, this._updateFunc);
361
- vDiff.exit.forEach(function (v) { return _this.removeVertex(_this._idFunc(v)); });
362
- vDiff.enter.forEach(function (v) { return _this.addVertex(v); });
363
- vDiff.update.forEach(function (v) { return _this.updateVertex(v); });
364
- return this;
365
- };
366
- Graph2.prototype.updateVertex = function (v) {
367
- var v_id = this._idFunc(v);
368
- var vertex = this._vertexMap[v_id];
369
- if (!vertex)
370
- throw new Error("Vertex '" + v_id + "' does not exist.");
371
- vertex._ = v;
372
- return this;
373
- };
374
- Graph2.prototype.removeVertex = function (id) {
375
- var _this = this;
376
- var v = this._vertexMap[id];
377
- if (!v)
378
- throw new Error("Vertex '" + id + "' does not exist.");
379
- v.edges().forEach(function (e) {
380
- _this.removeEdge(e.id());
381
- });
382
- delete this._vertexMap[id];
383
- return this;
384
- };
385
- Graph2.prototype.vertexParent = function (id, parentID) {
386
- var item = this._vertexMap[id];
387
- if (!item)
388
- throw new Error("Vertex '" + id + "' does not exist.");
389
- if (parentID === void 0) {
390
- var parent_2 = item.parent();
391
- return parent_2 ? parent_2._ : undefined;
392
- }
393
- var parent = this._subgraphMap[parentID];
394
- if (!parent)
395
- throw new Error("Vertex parent '" + parent + "' does not exist.");
396
- item.parent(parent);
397
- return this;
398
- };
399
- // Edges ---
400
- Graph2.prototype.edgeExists = function (id) {
401
- return !!this._edgeMap[id];
402
- };
403
- Graph2.prototype.edge = function (id) {
404
- return this._edgeMap[id]._;
405
- };
406
- Graph2.prototype.addEdge = function (e, parent) {
407
- var e_id = this._idFunc(e);
408
- var e_source = this._sourceFunc(e);
409
- var e_target = this._targetFunc(e);
410
- if (this._edgeMap[e_id])
411
- throw new Error("Edge '" + e_id + "' already exists.");
412
- if (!this.vertexExists(e_source))
413
- throw new Error("Edge Source '" + e_source + "' does not exist.");
414
- if (!this.vertexExists(e_target))
415
- throw new Error("Edge Target '" + e_target + "' does not exist.");
416
- var edge = new Edge(this, e, this._vertexMap[e_source], this._vertexMap[e_target]);
417
- if (parent) {
418
- var p_id = this._idFunc(parent);
419
- if (!this.subgraphExists(p_id))
420
- throw new Error("Subgraph '" + p_id + "' does not exist.");
421
- edge.parent(this._subgraphMap[p_id]);
422
- }
423
- this._edgeMap[e_id] = edge;
424
- this._vertexMap[e_source].addOutEdge(edge);
425
- this._vertexMap[e_target].addInEdge(edge);
426
- return this;
427
- };
428
- Graph2.prototype.mergeEdges = function (_edges) {
429
- var _this = this;
430
- var eDiff = compare2(this.allEdges(), _edges, function (e) { return _this._idFunc(e); }, this._updateFunc);
431
- eDiff.exit.forEach(function (e) { return _this.removeEdge(_this._idFunc(e)); });
432
- eDiff.enter.forEach(function (e) { return _this.addEdge(e); });
433
- eDiff.update.forEach(function (e) { return _this.updateEdge(e); });
434
- return this;
435
- };
436
- Graph2.prototype.updateEdge = function (e) {
437
- var e_id = this._idFunc(e);
438
- var edge = this._edgeMap[e_id];
439
- if (!edge)
440
- throw new Error("Edge '" + e_id + "' does not exist.");
441
- edge._ = e;
442
- return this;
443
- };
444
- Graph2.prototype.removeEdge = function (id) {
445
- var e = this._edgeMap[id];
446
- if (!e)
447
- throw new Error("Edge '" + id + "' does not exist.");
448
- var e_sourceID = this._idFunc(e._source._);
449
- if (!this.vertexExists(e_sourceID))
450
- throw new Error("Edge Source'" + e_sourceID + "' does not exist.");
451
- this._vertexMap[e_sourceID].removeOutEdge(id);
452
- var e_targetID = this._idFunc(e._target._);
453
- if (!this.vertexExists(e_targetID))
454
- throw new Error("Edge Target'" + e_targetID + "' does not exist.");
455
- this._vertexMap[e_targetID].removeInEdge(id);
456
- delete this._edgeMap[id];
457
- return this;
458
- };
459
- Graph2.prototype._hwalk = function (item, formatter) {
460
- var _this = this;
461
- if (item instanceof Subgraph) {
462
- return formatter("subgraph", item._, item.children().map(function (child) { return _this._hwalk(child, formatter); }));
463
- }
464
- else {
465
- return formatter("vertex", item._);
466
- }
467
- };
468
- Graph2.prototype.hierarchy = function (formatter) {
469
- var retVal = [];
470
- for (var id in this._subgraphMap) {
471
- var sg = this._subgraphMap[id];
472
- if (sg.parent() === undefined) {
473
- retVal.push(this._hwalk(sg, formatter));
474
- }
475
- }
476
- for (var id in this._vertexMap) {
477
- var v = this._vertexMap[id];
478
- if (v.parent() === undefined) {
479
- retVal.push(this._hwalk(v, formatter));
480
- }
481
- }
482
- return retVal;
483
- };
484
- Graph2.prototype.dijkstra = function (source, target) {
485
- var edges = this.allEdges();
486
- var Q = new Set();
487
- var prev = {};
488
- var dist = {};
489
- var adj = {};
490
- function vertex_with_min_dist(Q, dist) {
491
- var min_distance = Infinity;
492
- var u = null;
493
- Q.forEach(function (v) {
494
- if (dist[v] < min_distance) {
495
- min_distance = dist[v];
496
- u = v;
497
- }
498
- });
499
- return u;
500
- }
501
- for (var i = 0; i < edges.length; i++) {
502
- var v1 = this._sourceFunc(edges[i]);
503
- var v2 = this._targetFunc(edges[i]);
504
- var len_1 = 1;
505
- Q.add(v1);
506
- Q.add(v2);
507
- dist[v1] = Infinity;
508
- dist[v2] = Infinity;
509
- if (adj[v1] === undefined)
510
- adj[v1] = {};
511
- if (adj[v2] === undefined)
512
- adj[v2] = {};
513
- adj[v1][v2] = len_1;
514
- adj[v2][v1] = len_1;
515
- }
516
- dist[source] = 0;
517
- while (Q.size) {
518
- var u_1 = vertex_with_min_dist(Q, dist);
519
- if (u_1 === null)
520
- break;
521
- var neighbors = Object.keys(adj[u_1]).filter(function (v) { return Q.has(v); }); // Neighbor still in Q
522
- Q.delete(u_1);
523
- if (u_1 === target)
524
- break; // Break when the target has been found
525
- for (var _i = 0, neighbors_1 = neighbors; _i < neighbors_1.length; _i++) {
526
- var v = neighbors_1[_i];
527
- var alt = dist[u_1] + adj[u_1][v];
528
- if (alt < dist[v]) {
529
- dist[v] = alt;
530
- prev[v] = u_1;
531
- }
532
- }
533
- }
534
- var u = target;
535
- var ids = [u];
536
- var len = 0;
537
- while (prev[u] !== undefined) {
538
- ids.unshift(prev[u]);
539
- len += adj[u][prev[u]];
540
- u = prev[u];
541
- }
542
- return { ids: ids, len: len };
543
- };
544
- Graph2.prototype.sort = function (v_id) {
545
- var retVal = [];
546
- var visited = {};
547
- var visit = function (vertex, ancestors) {
548
- if (ancestors === void 0) { ancestors = []; }
549
- var v_id = vertex.id();
550
- if (visited[v_id])
551
- return;
552
- visited[v_id] = true;
553
- ancestors.push(vertex);
554
- vertex.outEdges().forEach(function (e) {
555
- if (ancestors.indexOf(e._target) < 0) {
556
- visit(e._target, __spreadArray([], ancestors));
557
- }
558
- });
559
- retVal.unshift(vertex._);
560
- };
561
- if (v_id) {
562
- visit(this._vertexMap[v_id]);
563
- }
564
- else {
565
- for (var key in this._vertexMap) {
566
- visit(this._vertexMap[key]);
567
- }
568
- }
569
- return retVal;
570
- };
571
- return Graph2;
572
- }());
573
- export { Graph2 };
574
- var Set = /** @class */ (function () {
575
- function Set() {
576
- this._content = [];
577
- }
578
- Object.defineProperty(Set.prototype, "size", {
579
- get: function () {
580
- return this._content.length;
581
- },
582
- enumerable: false,
583
- configurable: true
584
- });
585
- Set.prototype.has = function (_) {
586
- return this._content.indexOf(_) >= 0;
587
- };
588
- Set.prototype.add = function (_) {
589
- if (!this.has(_)) {
590
- this._content.push(_);
591
- }
592
- };
593
- Set.prototype.delete = function (_) {
594
- var idx = this._content.indexOf(_);
595
- if (idx >= 0) {
596
- this._content.splice(idx, 1);
597
- }
598
- };
599
- Set.prototype.forEach = function (_) {
600
- this._content.forEach(_);
601
- };
602
- return Set;
603
- }());
1
+ import { __extends, __spreadArray } from "tslib";
2
+ import { compare2 } from "./array";
3
+ var GraphItem = /** @class */ (function () {
4
+ function GraphItem(g, _) {
5
+ this._graph = g;
6
+ this._ = _;
7
+ }
8
+ GraphItem.prototype.id = function () {
9
+ return this._graph.id(this._);
10
+ };
11
+ return GraphItem;
12
+ }());
13
+ var ChildGraphItem = /** @class */ (function (_super) {
14
+ __extends(ChildGraphItem, _super);
15
+ function ChildGraphItem(g, _) {
16
+ return _super.call(this, g, _) || this;
17
+ }
18
+ ChildGraphItem.prototype.clearParent = function () {
19
+ if (this._parent) {
20
+ this._parent.removeChild(this);
21
+ delete this._parent;
22
+ }
23
+ return this;
24
+ };
25
+ ChildGraphItem.prototype.parent = function (_) {
26
+ if (arguments.length === 0)
27
+ return this._parent;
28
+ if (this._parent !== _) {
29
+ if (this._parent) {
30
+ this._parent.removeChild(this);
31
+ }
32
+ this._parent = _;
33
+ if (this._parent) {
34
+ this._parent.addChild(this);
35
+ }
36
+ }
37
+ return this;
38
+ };
39
+ return ChildGraphItem;
40
+ }(GraphItem));
41
+ var Subgraph = /** @class */ (function (_super) {
42
+ __extends(Subgraph, _super);
43
+ function Subgraph(g, _) {
44
+ var _this = _super.call(this, g, _) || this;
45
+ _this._children = [];
46
+ return _this;
47
+ }
48
+ Subgraph.prototype.children = function () {
49
+ return this._children;
50
+ };
51
+ Subgraph.prototype.addChild = function (_) {
52
+ this._children.push(_);
53
+ };
54
+ Subgraph.prototype.removeChild = function (_) {
55
+ this._children = this._children.filter(function (row) { return row.id !== _.id; });
56
+ };
57
+ return Subgraph;
58
+ }(ChildGraphItem));
59
+ var Vertex = /** @class */ (function (_super) {
60
+ __extends(Vertex, _super);
61
+ function Vertex(g, _) {
62
+ var _this = _super.call(this, g, _) || this;
63
+ _this._inEdges = [];
64
+ _this._outEdges = [];
65
+ return _this;
66
+ }
67
+ Vertex.prototype.edges = function () {
68
+ return __spreadArray(__spreadArray([], this._inEdges, true), this._outEdges, true);
69
+ };
70
+ Vertex.prototype.edgeCount = function () {
71
+ return this._outEdges.length + this._inEdges.length;
72
+ };
73
+ Vertex.prototype.inEdges = function () {
74
+ return this._inEdges;
75
+ };
76
+ Vertex.prototype.addInEdge = function (e) {
77
+ this._inEdges.push(e);
78
+ };
79
+ Vertex.prototype.removeInEdge = function (id) {
80
+ this._inEdges = this._inEdges.filter(function (e) { return e._.id !== id; });
81
+ };
82
+ Vertex.prototype.outEdges = function () {
83
+ return this._outEdges;
84
+ };
85
+ Vertex.prototype.addOutEdge = function (e) {
86
+ this._outEdges.push(e);
87
+ };
88
+ Vertex.prototype.removeOutEdge = function (id) {
89
+ this._outEdges = this._outEdges.filter(function (e) { return e._.id !== id; });
90
+ };
91
+ return Vertex;
92
+ }(ChildGraphItem));
93
+ var Edge = /** @class */ (function (_super) {
94
+ __extends(Edge, _super);
95
+ function Edge(g, _, source, target) {
96
+ var _this = _super.call(this, g, _) || this;
97
+ _this._source = source;
98
+ _this._target = target;
99
+ return _this;
100
+ }
101
+ return Edge;
102
+ }(ChildGraphItem));
103
+ var Graph2 = /** @class */ (function () {
104
+ function Graph2(directed) {
105
+ if (directed === void 0) { directed = true; }
106
+ this._subgraphMap = {};
107
+ this._vertexMap = {};
108
+ this._edgeMap = {};
109
+ this._idFunc = function (_) { return typeof _.id === "function" ? _.id() : _.id; };
110
+ this._sourceFunc = function (_) { return typeof _.source === "function" ? _.source() : _.source; };
111
+ this._targetFunc = function (_) { return typeof _.target === "function" ? _.target() : _.target; };
112
+ this._updateFunc = function (before, after) { return after; };
113
+ this._directed = directed;
114
+ }
115
+ Graph2.prototype.clear = function () {
116
+ this._subgraphMap = {};
117
+ this._vertexMap = {};
118
+ this._edgeMap = {};
119
+ return this;
120
+ };
121
+ Graph2.prototype.clearParents = function () {
122
+ for (var key in this._subgraphMap) {
123
+ this._subgraphMap[key].clearParent();
124
+ }
125
+ for (var key in this._vertexMap) {
126
+ this._vertexMap[key].clearParent();
127
+ }
128
+ return this;
129
+ };
130
+ Graph2.prototype.isDirected = function () {
131
+ return this._directed;
132
+ };
133
+ Graph2.prototype.idFunc = function (_) {
134
+ this._idFunc = _;
135
+ return this;
136
+ };
137
+ Graph2.prototype.sourceFunc = function (_) {
138
+ this._sourceFunc = _;
139
+ return this;
140
+ };
141
+ Graph2.prototype.targetFunc = function (_) {
142
+ this._targetFunc = _;
143
+ return this;
144
+ };
145
+ Graph2.prototype.updateFunc = function (_) {
146
+ this._updateFunc = _;
147
+ return this;
148
+ };
149
+ Graph2.prototype.id = function (_) {
150
+ return this._idFunc(_);
151
+ };
152
+ Graph2.prototype.type = function (id) {
153
+ if (this.subgraphExists(id))
154
+ return "S";
155
+ if (this.vertexExists(id))
156
+ return "V";
157
+ if (this.edgeExists(id))
158
+ return "E";
159
+ return "";
160
+ };
161
+ Graph2.prototype.isSubgraph = function (_) {
162
+ return this.subgraphExists(this.id(_));
163
+ };
164
+ Graph2.prototype.isVertex = function (_) {
165
+ return this.vertexExists(this.id(_));
166
+ };
167
+ Graph2.prototype.isEdge = function (_) {
168
+ return this.edgeExists(this.id(_));
169
+ };
170
+ Graph2.prototype.allItems = function () {
171
+ return __spreadArray(__spreadArray(__spreadArray([], this.allSubgraphs(), true), this.allVertices(), true), this.allEdges(), true);
172
+ };
173
+ Graph2.prototype.item = function (id) {
174
+ if (this.subgraphExists(id))
175
+ return this.subgraph(id);
176
+ if (this.vertexExists(id))
177
+ return this.vertex(id);
178
+ if (this.edgeExists(id))
179
+ return this.edge(id);
180
+ return undefined;
181
+ };
182
+ Graph2.prototype.itemExists = function (id) {
183
+ return this.edgeExists(id) || this.vertexExists(id) || this.subgraphExists(id);
184
+ };
185
+ // Subgraphs ---
186
+ Graph2.prototype.allSubgraphs = function () {
187
+ var retVal = [];
188
+ for (var key in this._subgraphMap) {
189
+ retVal.push(this._subgraphMap[key]._);
190
+ }
191
+ return retVal;
192
+ };
193
+ Graph2.prototype.subgraphs = function () {
194
+ var retVal = [];
195
+ for (var key in this._subgraphMap) {
196
+ if (this._subgraphMap[key].parent() === undefined) {
197
+ retVal.push(this._subgraphMap[key]._);
198
+ }
199
+ }
200
+ return retVal;
201
+ };
202
+ Graph2.prototype.subgraphExists = function (id) {
203
+ return !!this._subgraphMap[id];
204
+ };
205
+ Graph2.prototype.subgraph = function (id) {
206
+ return this._subgraphMap[id]._;
207
+ };
208
+ Graph2.prototype.subgraphSubgraphs = function (id) {
209
+ var _this = this;
210
+ return this._subgraphMap[id].children().filter(function (child) { return _this.isSubgraph(child._); }).map(function (child) { return child._; });
211
+ };
212
+ Graph2.prototype.subgraphVertices = function (id) {
213
+ var _this = this;
214
+ return this._subgraphMap[id].children().filter(function (child) { return _this.isVertex(child._); }).map(function (child) { return child._; });
215
+ };
216
+ Graph2.prototype.subgraphEdges = function (id) {
217
+ var _this = this;
218
+ return this._subgraphMap[id].children().filter(function (child) { return _this.isEdge(child._); }).map(function (child) { return child._; });
219
+ };
220
+ Graph2.prototype.addSubgraph = function (s, parent) {
221
+ var s_id = this._idFunc(s);
222
+ if (this._subgraphMap[s_id])
223
+ throw new Error("Subgraph '".concat(s_id, "' already exists."));
224
+ var subgraph = new Subgraph(this, s);
225
+ if (parent) {
226
+ var p_id = this._idFunc(parent);
227
+ if (!this._subgraphMap[p_id])
228
+ throw new Error("Subgraph '".concat(p_id, "' does not exist."));
229
+ subgraph.parent(this._subgraphMap[p_id]);
230
+ }
231
+ this._subgraphMap[s_id] = subgraph;
232
+ return this;
233
+ };
234
+ Graph2.prototype.mergeSubgraphs = function (_subgraphs) {
235
+ var _this = this;
236
+ if (_subgraphs === void 0) { _subgraphs = []; }
237
+ var sgDiff = compare2(this.allSubgraphs(), _subgraphs, function (sg) { return _this._idFunc(sg); }, this._updateFunc);
238
+ sgDiff.exit.forEach(function (sg) { return _this.removeSubgraph(_this._idFunc(sg)); });
239
+ sgDiff.enter.forEach(function (sg) { return _this.addSubgraph(sg); });
240
+ sgDiff.update.forEach(function (sg) { return _this.updateSubgraph(sg); });
241
+ return this;
242
+ };
243
+ Graph2.prototype.updateSubgraph = function (sg) {
244
+ var sg_id = this._idFunc(sg);
245
+ var subgraph = this._subgraphMap[sg_id];
246
+ if (!subgraph)
247
+ throw new Error("Subgraph '".concat(sg_id, "' does not exist."));
248
+ subgraph._ = sg;
249
+ return this;
250
+ };
251
+ Graph2.prototype.removeSubgraph = function (id, promoteChildren) {
252
+ var _this = this;
253
+ if (promoteChildren === void 0) { promoteChildren = true; }
254
+ var sg = this._subgraphMap[id];
255
+ if (!sg)
256
+ throw new Error("Subgraph '".concat(id, "' does not exist."));
257
+ sg.children().forEach(function (child) {
258
+ if (promoteChildren) {
259
+ child.parent(sg.parent());
260
+ }
261
+ else {
262
+ if (child instanceof Subgraph) {
263
+ _this.removeSubgraph(child.id());
264
+ }
265
+ else {
266
+ _this.removeVertex(child.id());
267
+ }
268
+ }
269
+ });
270
+ delete this._subgraphMap[id];
271
+ return this;
272
+ };
273
+ Graph2.prototype.subgraphParent = function (id, parentID) {
274
+ var item = this._subgraphMap[id];
275
+ if (!item)
276
+ throw new Error("Subgraph '".concat(id, "' does not exist."));
277
+ if (parentID === void 0) {
278
+ var parent_1 = item.parent();
279
+ return parent_1 ? parent_1._ : undefined;
280
+ }
281
+ var parent = this._subgraphMap[parentID];
282
+ if (!parent)
283
+ throw new Error("Vertex parent '".concat(parent, "' does not exist."));
284
+ item.parent(parent);
285
+ return this;
286
+ };
287
+ // Vertices ---
288
+ Graph2.prototype.allVertices = function () {
289
+ var retVal = [];
290
+ for (var key in this._vertexMap) {
291
+ retVal.push(this._vertexMap[key]._);
292
+ }
293
+ return retVal;
294
+ };
295
+ Graph2.prototype.vertices = function () {
296
+ var retVal = [];
297
+ for (var key in this._vertexMap) {
298
+ if (this._vertexMap[key].parent() === undefined) {
299
+ retVal.push(this._vertexMap[key]._);
300
+ }
301
+ }
302
+ return retVal;
303
+ };
304
+ Graph2.prototype.vertexExists = function (id) {
305
+ return !!this._vertexMap[id];
306
+ };
307
+ Graph2.prototype.vertex = function (id) {
308
+ return this._vertexMap[id]._;
309
+ };
310
+ Graph2.prototype.allEdges = function () {
311
+ var retVal = [];
312
+ for (var key in this._edgeMap) {
313
+ retVal.push(this._edgeMap[key]._);
314
+ }
315
+ return retVal;
316
+ };
317
+ Graph2.prototype.edges = function () {
318
+ var retVal = [];
319
+ for (var key in this._edgeMap) {
320
+ if (this._edgeMap[key].parent() === undefined) {
321
+ retVal.push(this._edgeMap[key]._);
322
+ }
323
+ }
324
+ return retVal;
325
+ };
326
+ Graph2.prototype.vertexEdges = function (vertexID) {
327
+ return this._vertexMap[vertexID].edges().map(function (e) { return e._; });
328
+ };
329
+ Graph2.prototype.inEdges = function (vertexID) {
330
+ return this._vertexMap[vertexID].inEdges().map(function (e) { return e._; });
331
+ };
332
+ Graph2.prototype.outEdges = function (vertexID) {
333
+ return this._vertexMap[vertexID].outEdges().map(function (e) { return e._; });
334
+ };
335
+ Graph2.prototype._neighbors = function (id) {
336
+ return __spreadArray(__spreadArray([], this._vertexMap[id].outEdges().map(function (e) { return e._target; }), true), this._vertexMap[id].inEdges().map(function (e) { return e._source; }), true);
337
+ };
338
+ Graph2.prototype.neighbors = function (id) {
339
+ return this._neighbors(id).map(function (n) { return n._; });
340
+ };
341
+ Graph2.prototype.singleNeighbors = function (id) {
342
+ return this._neighbors(id).filter(function (n) { return n.edgeCount() === 1; }).map(function (n) { return n._; });
343
+ };
344
+ Graph2.prototype.addVertex = function (v, parent) {
345
+ var v_id = this._idFunc(v);
346
+ if (this._vertexMap[v_id])
347
+ throw new Error("Vertex '".concat(v_id, "' already exists."));
348
+ var vertex = new Vertex(this, v);
349
+ if (parent) {
350
+ var p_id = this._idFunc(parent);
351
+ if (!this.subgraphExists(p_id))
352
+ throw new Error("Subgraph '".concat(p_id, "' does not exist."));
353
+ vertex.parent(this._subgraphMap[p_id]);
354
+ }
355
+ this._vertexMap[v_id] = vertex;
356
+ return this;
357
+ };
358
+ Graph2.prototype.mergeVertices = function (_vertices) {
359
+ var _this = this;
360
+ var vDiff = compare2(this.allVertices(), _vertices, function (v) { return _this._idFunc(v); }, this._updateFunc);
361
+ vDiff.exit.forEach(function (v) { return _this.removeVertex(_this._idFunc(v)); });
362
+ vDiff.enter.forEach(function (v) { return _this.addVertex(v); });
363
+ vDiff.update.forEach(function (v) { return _this.updateVertex(v); });
364
+ return this;
365
+ };
366
+ Graph2.prototype.updateVertex = function (v) {
367
+ var v_id = this._idFunc(v);
368
+ var vertex = this._vertexMap[v_id];
369
+ if (!vertex)
370
+ throw new Error("Vertex '".concat(v_id, "' does not exist."));
371
+ vertex._ = v;
372
+ return this;
373
+ };
374
+ Graph2.prototype.removeVertex = function (id) {
375
+ var _this = this;
376
+ var v = this._vertexMap[id];
377
+ if (!v)
378
+ throw new Error("Vertex '".concat(id, "' does not exist."));
379
+ v.edges().forEach(function (e) {
380
+ _this.removeEdge(e.id());
381
+ });
382
+ delete this._vertexMap[id];
383
+ return this;
384
+ };
385
+ Graph2.prototype.vertexParent = function (id, parentID) {
386
+ var item = this._vertexMap[id];
387
+ if (!item)
388
+ throw new Error("Vertex '".concat(id, "' does not exist."));
389
+ if (parentID === void 0) {
390
+ var parent_2 = item.parent();
391
+ return parent_2 ? parent_2._ : undefined;
392
+ }
393
+ var parent = this._subgraphMap[parentID];
394
+ if (!parent)
395
+ throw new Error("Vertex parent '".concat(parent, "' does not exist."));
396
+ item.parent(parent);
397
+ return this;
398
+ };
399
+ // Edges ---
400
+ Graph2.prototype.edgeExists = function (id) {
401
+ return !!this._edgeMap[id];
402
+ };
403
+ Graph2.prototype.edge = function (id) {
404
+ return this._edgeMap[id]._;
405
+ };
406
+ Graph2.prototype.addEdge = function (e, parent) {
407
+ var e_id = this._idFunc(e);
408
+ var e_source = this._sourceFunc(e);
409
+ var e_target = this._targetFunc(e);
410
+ if (this._edgeMap[e_id])
411
+ throw new Error("Edge '".concat(e_id, "' already exists."));
412
+ if (!this.vertexExists(e_source))
413
+ throw new Error("Edge Source '".concat(e_source, "' does not exist."));
414
+ if (!this.vertexExists(e_target))
415
+ throw new Error("Edge Target '".concat(e_target, "' does not exist."));
416
+ var edge = new Edge(this, e, this._vertexMap[e_source], this._vertexMap[e_target]);
417
+ if (parent) {
418
+ var p_id = this._idFunc(parent);
419
+ if (!this.subgraphExists(p_id))
420
+ throw new Error("Subgraph '".concat(p_id, "' does not exist."));
421
+ edge.parent(this._subgraphMap[p_id]);
422
+ }
423
+ this._edgeMap[e_id] = edge;
424
+ this._vertexMap[e_source].addOutEdge(edge);
425
+ this._vertexMap[e_target].addInEdge(edge);
426
+ return this;
427
+ };
428
+ Graph2.prototype.mergeEdges = function (_edges) {
429
+ var _this = this;
430
+ var eDiff = compare2(this.allEdges(), _edges, function (e) { return _this._idFunc(e); }, this._updateFunc);
431
+ eDiff.exit.forEach(function (e) { return _this.removeEdge(_this._idFunc(e)); });
432
+ eDiff.enter.forEach(function (e) { return _this.addEdge(e); });
433
+ eDiff.update.forEach(function (e) { return _this.updateEdge(e); });
434
+ return this;
435
+ };
436
+ Graph2.prototype.updateEdge = function (e) {
437
+ var e_id = this._idFunc(e);
438
+ var edge = this._edgeMap[e_id];
439
+ if (!edge)
440
+ throw new Error("Edge '".concat(e_id, "' does not exist."));
441
+ edge._ = e;
442
+ return this;
443
+ };
444
+ Graph2.prototype.removeEdge = function (id) {
445
+ var e = this._edgeMap[id];
446
+ if (!e)
447
+ throw new Error("Edge '".concat(id, "' does not exist."));
448
+ var e_sourceID = this._idFunc(e._source._);
449
+ if (!this.vertexExists(e_sourceID))
450
+ throw new Error("Edge Source'".concat(e_sourceID, "' does not exist."));
451
+ this._vertexMap[e_sourceID].removeOutEdge(id);
452
+ var e_targetID = this._idFunc(e._target._);
453
+ if (!this.vertexExists(e_targetID))
454
+ throw new Error("Edge Target'".concat(e_targetID, "' does not exist."));
455
+ this._vertexMap[e_targetID].removeInEdge(id);
456
+ delete this._edgeMap[id];
457
+ return this;
458
+ };
459
+ Graph2.prototype._hwalk = function (item, formatter) {
460
+ var _this = this;
461
+ if (item instanceof Subgraph) {
462
+ return formatter("subgraph", item._, item.children().map(function (child) { return _this._hwalk(child, formatter); }));
463
+ }
464
+ else {
465
+ return formatter("vertex", item._);
466
+ }
467
+ };
468
+ Graph2.prototype.hierarchy = function (formatter) {
469
+ var retVal = [];
470
+ for (var id in this._subgraphMap) {
471
+ var sg = this._subgraphMap[id];
472
+ if (sg.parent() === undefined) {
473
+ retVal.push(this._hwalk(sg, formatter));
474
+ }
475
+ }
476
+ for (var id in this._vertexMap) {
477
+ var v = this._vertexMap[id];
478
+ if (v.parent() === undefined) {
479
+ retVal.push(this._hwalk(v, formatter));
480
+ }
481
+ }
482
+ return retVal;
483
+ };
484
+ Graph2.prototype.dijkstra = function (source, target) {
485
+ var edges = this.allEdges();
486
+ var Q = new Set();
487
+ var prev = {};
488
+ var dist = {};
489
+ var adj = {};
490
+ function vertex_with_min_dist(Q, dist) {
491
+ var min_distance = Infinity;
492
+ var u = null;
493
+ Q.forEach(function (v) {
494
+ if (dist[v] < min_distance) {
495
+ min_distance = dist[v];
496
+ u = v;
497
+ }
498
+ });
499
+ return u;
500
+ }
501
+ for (var i = 0; i < edges.length; i++) {
502
+ var v1 = this._sourceFunc(edges[i]);
503
+ var v2 = this._targetFunc(edges[i]);
504
+ var len_1 = 1;
505
+ Q.add(v1);
506
+ Q.add(v2);
507
+ dist[v1] = Infinity;
508
+ dist[v2] = Infinity;
509
+ if (adj[v1] === undefined)
510
+ adj[v1] = {};
511
+ if (adj[v2] === undefined)
512
+ adj[v2] = {};
513
+ adj[v1][v2] = len_1;
514
+ adj[v2][v1] = len_1;
515
+ }
516
+ dist[source] = 0;
517
+ while (Q.size) {
518
+ var u_1 = vertex_with_min_dist(Q, dist);
519
+ if (u_1 === null)
520
+ break;
521
+ var neighbors = Object.keys(adj[u_1]).filter(function (v) { return Q.has(v); }); // Neighbor still in Q
522
+ Q.delete(u_1);
523
+ if (u_1 === target)
524
+ break; // Break when the target has been found
525
+ for (var _i = 0, neighbors_1 = neighbors; _i < neighbors_1.length; _i++) {
526
+ var v = neighbors_1[_i];
527
+ var alt = dist[u_1] + adj[u_1][v];
528
+ if (alt < dist[v]) {
529
+ dist[v] = alt;
530
+ prev[v] = u_1;
531
+ }
532
+ }
533
+ }
534
+ var u = target;
535
+ var ids = [u];
536
+ var len = 0;
537
+ while (prev[u] !== undefined) {
538
+ ids.unshift(prev[u]);
539
+ len += adj[u][prev[u]];
540
+ u = prev[u];
541
+ }
542
+ return { ids: ids, len: len };
543
+ };
544
+ Graph2.prototype.sort = function (v_id) {
545
+ var retVal = [];
546
+ var visited = {};
547
+ var visit = function (vertex, ancestors) {
548
+ if (ancestors === void 0) { ancestors = []; }
549
+ var v_id = vertex.id();
550
+ if (visited[v_id])
551
+ return;
552
+ visited[v_id] = true;
553
+ ancestors.push(vertex);
554
+ vertex.outEdges().forEach(function (e) {
555
+ if (ancestors.indexOf(e._target) < 0) {
556
+ visit(e._target, __spreadArray([], ancestors, true));
557
+ }
558
+ });
559
+ retVal.unshift(vertex._);
560
+ };
561
+ if (v_id) {
562
+ visit(this._vertexMap[v_id]);
563
+ }
564
+ else {
565
+ for (var key in this._vertexMap) {
566
+ visit(this._vertexMap[key]);
567
+ }
568
+ }
569
+ return retVal;
570
+ };
571
+ return Graph2;
572
+ }());
573
+ export { Graph2 };
574
+ var Set = /** @class */ (function () {
575
+ function Set() {
576
+ this._content = [];
577
+ }
578
+ Object.defineProperty(Set.prototype, "size", {
579
+ get: function () {
580
+ return this._content.length;
581
+ },
582
+ enumerable: false,
583
+ configurable: true
584
+ });
585
+ Set.prototype.has = function (_) {
586
+ return this._content.indexOf(_) >= 0;
587
+ };
588
+ Set.prototype.add = function (_) {
589
+ if (!this.has(_)) {
590
+ this._content.push(_);
591
+ }
592
+ };
593
+ Set.prototype.delete = function (_) {
594
+ var idx = this._content.indexOf(_);
595
+ if (idx >= 0) {
596
+ this._content.splice(idx, 1);
597
+ }
598
+ };
599
+ Set.prototype.forEach = function (_) {
600
+ this._content.forEach(_);
601
+ };
602
+ return Set;
603
+ }());
604
604
  //# sourceMappingURL=graph2.js.map