occam-directed-graphs 3.0.37 → 3.0.41

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.
@@ -1,422 +0,0 @@
1
- "use strict";
2
-
3
- import { arrayUtilities } from "necessary";
4
-
5
- import Edge from "./edge";
6
- import Vertex from "./vertex";
7
-
8
- import { vertexNamesFromVertices, orderVertices } from "./utilities/vertex";
9
-
10
- const { last } = arrayUtilities;
11
-
12
- export default class DirectedAcyclicGraph {
13
- constructor(vertexMap) {
14
- this.vertexMap = vertexMap;
15
- }
16
-
17
- isEmpty() {
18
- const vertices = this.getVertices(),
19
- verticesLength = vertices.length,
20
- empty = (verticesLength === 0);
21
-
22
- return empty;
23
- }
24
-
25
- getVertices() {
26
- const vertexMapValues = Object.values(this.vertexMap),
27
- vertices = vertexMapValues; ///
28
-
29
- return vertices;
30
- }
31
-
32
- getVertexNames() {
33
- const vertexMapKeys = Object.keys(this.vertexMap),
34
- vertexNames = vertexMapKeys; ///
35
-
36
- return vertexNames;
37
- }
38
-
39
- getVertexByVertexName(vertexName) {
40
- const vertexPresent = this.isVertexPresentByVertexName(vertexName),
41
- vertex = vertexPresent ?
42
- this.vertexMap[vertexName] :
43
- null;
44
-
45
- return vertex;
46
- }
47
-
48
- getImmediatePredecessorVertexNamesByVertexName(vertexName) {
49
- const vertex = this.getVertexByVertexName(vertexName),
50
- immediatePredecessorVertexNames = vertex.getImmediatePredecessorVertexNames();
51
-
52
- return immediatePredecessorVertexNames;
53
- }
54
-
55
- getImmediateSuccessorVertexNamesByVertexName(vertexName) {
56
- const vertex = this.getVertexByVertexName(vertexName),
57
- immediateSuccessorVertexNames = vertex.getImmediateSuccessorVertexNames();
58
-
59
- return immediateSuccessorVertexNames;
60
- }
61
-
62
- getPredecessorVertexNamesByVertexName(vertexName) {
63
- const vertex = this.getVertexByVertexName(vertexName),
64
- predecessorVertexNames = vertex.getPredecessorVertexNames();
65
-
66
- return predecessorVertexNames;
67
- }
68
-
69
- getSuccessorVertexNamesByVertexName(vertexName) {
70
- const vertex = this.getVertexByVertexName(vertexName),
71
- successorVertexNames = vertex.getSuccessorVertexNames();
72
-
73
- return successorVertexNames;
74
- }
75
-
76
- getEdgesByTargetVertexName(targetVertexName) {
77
- const edges = [],
78
- targetVertex = this.getVertexByVertexName(targetVertexName);
79
-
80
- if (targetVertex !== null) {
81
- const immediatePredecessorVertexNames = targetVertex.getImmediatePredecessorVertexNames(),
82
- sourceVertexNames = immediatePredecessorVertexNames; ///
83
-
84
- sourceVertexNames.forEach((sourceVertexName) => {
85
- const edge = Edge.fromSourceVertexNameAndTargetVertexName(sourceVertexName, targetVertexName);
86
-
87
- edges.push(edge);
88
- });
89
- }
90
-
91
- return edges;
92
- }
93
-
94
- getEdgesBySourceVertexName(sourceVertexName) {
95
- const edges = [],
96
- sourceVertex = this.getVertexByVertexName(sourceVertexName);
97
-
98
- if (sourceVertex !== null) {
99
- const immediateSuccessorVertexNames = sourceVertex.getImmediateSuccessorVertexNames(),
100
- targetVertexNames = immediateSuccessorVertexNames; ///
101
-
102
- targetVertexNames.forEach((targetVertexName) => {
103
- const edge = Edge.fromSourceVertexNameAndTargetVertexName(sourceVertexName, targetVertexName);
104
-
105
- edges.push(edge);
106
- });
107
- }
108
-
109
- return edges;
110
- }
111
-
112
- setVertexByVertexName(vertexName, vertex) {
113
- this.vertexMap[vertexName] = vertex;
114
- }
115
-
116
- deleteVertexByVertexName(vertexName) {
117
- delete this.vertexMap[vertexName];
118
- }
119
-
120
- isEdgePresent(edge) {
121
- const sourceVertexName = edge.getSourceVertexName(),
122
- targetVertexName = edge.getTargetVertexName(),
123
- edgePresent = this.isEdgePresentByVertexNames(sourceVertexName, targetVertexName);
124
-
125
- return edgePresent;
126
- }
127
-
128
- isEdgePresentByVertexNames(sourceVertexName, targetVertexName) {
129
- let edgePresent = false;
130
-
131
- const sourceVertex = this.getVertexByVertexName(sourceVertexName),
132
- targetVertex = this.getVertexByVertexName(targetVertexName),
133
- sourceVertexAndTargetVertexPresent = (sourceVertex !== null) && (targetVertex !== null);
134
-
135
- if (sourceVertexAndTargetVertexPresent) {
136
- edgePresent = sourceVertex.isEdgePresentByTargetVertex(targetVertex);
137
- }
138
-
139
- return edgePresent;
140
- }
141
-
142
- isVertexPresentByVertexName(vertexName) {
143
- const vertexNames = this.getVertexNames(),
144
- vertexNamesIncludesVertexName = vertexNames.includes(vertexName),
145
- vertexPresent = vertexNamesIncludesVertexName; ///
146
-
147
- return vertexPresent;
148
- }
149
-
150
- getOrderedVertexNames() {
151
- const vertices = this.getVertices();
152
-
153
- orderVertices(vertices);
154
-
155
- const orderedVertices = vertices, ///
156
- orderedVertexNames = vertexNamesFromVertices(orderedVertices);
157
-
158
- return orderedVertexNames;
159
- }
160
-
161
- addEdge(edge) {
162
- const sourceVertexName = edge.getSourceVertexName(),
163
- targetVertexName = edge.getTargetVertexName(),
164
- success = this.addEdgeByVertexNames(sourceVertexName, targetVertexName);
165
-
166
- return success;
167
- }
168
-
169
- removeEdge(edge) {
170
- const sourceVertexName = edge.getSourceVertexName(),
171
- targetVertexName = edge.getTargetVertexName();
172
-
173
- this.removeEdgeByVertexNames(sourceVertexName, targetVertexName);
174
- }
175
-
176
- addEdgeByVertexNames(sourceVertexName, targetVertexName) {
177
- let success = false;
178
-
179
- if (sourceVertexName !== targetVertexName) {
180
- const sourceVertex = this.addVertexByVertexName(sourceVertexName),
181
- targetVertex = this.addVertexByVertexName(targetVertexName),
182
- edgePresent = sourceVertex.isEdgePresentByTargetVertex(targetVertex);
183
-
184
- if (edgePresent) {
185
- success = true;
186
- } else {
187
- const sourceVertexIndex = sourceVertex.getIndex(),
188
- targetVertexIndex = targetVertex.getIndex(),
189
- invalidatingEdge = (sourceVertexIndex > targetVertexIndex);
190
-
191
- success = invalidatingEdge ?
192
- addInvalidatingEdgeByVertices(sourceVertex, targetVertex) :
193
- true;
194
-
195
- if (success) {
196
- const immediatePredecessorVertex = sourceVertex, ///
197
- immediateSuccessorVertex = targetVertex; ///
198
-
199
- immediatePredecessorVertex.addImmediateSuccessorVertex(immediateSuccessorVertex);
200
-
201
- immediateSuccessorVertex.addImmediatePredecessorVertex(immediatePredecessorVertex);
202
- }
203
- }
204
- }
205
-
206
- return success;
207
- }
208
-
209
- removeEdgeByVertexNames(sourceVertexName, targetVertexName) {
210
- const edgePresent = this.isEdgePresentByVertexNames(sourceVertexName, targetVertexName);
211
-
212
- if (edgePresent) {
213
- const sourceVertex = this.getVertexByVertexName(sourceVertexName),
214
- targetVertex = this.getVertexByVertexName(targetVertexName);
215
-
216
- sourceVertex.removeImmediateSuccessorVertex(targetVertex);
217
- targetVertex.removeImmediatePredecessorVertex(sourceVertex);
218
- }
219
- }
220
-
221
- removeEdgesBySourceVertexName(sourceVertexName) {
222
- const sourceVertexPresent = this.isVertexPresentByVertexName(sourceVertexName);
223
-
224
- if (sourceVertexPresent) {
225
- const sourceVertex = this.getVertexByVertexName(sourceVertexName);
226
-
227
- sourceVertex.removeOutgoingEdges();
228
- }
229
- }
230
-
231
- removeEdgesByTargetVertexName(targetVertexName) {
232
- const targetVertexPresent = this.isVertexPresentByVertexName(targetVertexName);
233
-
234
- if (targetVertexPresent) {
235
- const targetVertex = this.getVertexByVertexName(targetVertexName);
236
-
237
- targetVertex.removeIncomingEdges();
238
- }
239
- }
240
-
241
- addVertexByVertexName(vertexName) {
242
- const vertexPresent = this.isVertexPresentByVertexName(vertexName);
243
-
244
- if (!vertexPresent) {
245
- const vertexNames = this.getVertexNames(),
246
- vertexNamesLength = vertexNames.length,
247
- name = vertexName, ///
248
- index = vertexNamesLength, ///
249
- vertex = Vertex.fromNameAndIndex(name, index);
250
-
251
- this.setVertexByVertexName(vertexName, vertex);
252
- }
253
-
254
- const vertex = this.getVertexByVertexName(vertexName);
255
-
256
- return vertex;
257
- }
258
-
259
- removeVertexByVertexName(vertexName) {
260
- let removedEdges = null;
261
-
262
- const vertexPresent = this.isVertexPresentByVertexName(vertexName);
263
-
264
- if (vertexPresent) {
265
- removedEdges = [];
266
-
267
- const vertex = this.getVertexByVertexName(vertexName);
268
-
269
- vertex.forEachImmediateSuccessorVertex((immediateSuccessVertex) => {
270
- const immediatePredecessorVertex = vertex, ///
271
- immediatePredecessorVertexName = immediatePredecessorVertex.getName(),
272
- immediateSuccessVertexName = immediateSuccessVertex.getName(),
273
- removedEdgeSourceVertexName = immediatePredecessorVertexName, ///
274
- removedEdgeTargetVertexName = immediateSuccessVertexName, ///
275
- removedEdge = new Edge(removedEdgeSourceVertexName, removedEdgeTargetVertexName);
276
-
277
- removedEdges.push(removedEdge);
278
-
279
- immediateSuccessVertex.removeImmediatePredecessorVertex(immediatePredecessorVertex);
280
- });
281
-
282
- vertex.forEachImmediatePredecessorVertex((immediatePredecessorVertex) => {
283
- const immediateSuccessVertex = vertex, ///
284
- immediatePredecessorVertexName = immediatePredecessorVertex.getName(),
285
- immediateSuccessVertexName = immediateSuccessVertex.getName(), ///
286
- removedEdgeSourceVertexName = immediatePredecessorVertexName, ///
287
- removedEdgeTargetVertexName = immediateSuccessVertexName, ///
288
- removedEdge = new Edge(removedEdgeSourceVertexName, removedEdgeTargetVertexName);
289
-
290
- removedEdges.push(removedEdge);
291
-
292
- immediatePredecessorVertex.removeImmediateSuccessorVertex(immediateSuccessVertex);
293
- });
294
-
295
- this.deleteVertexByVertexName(vertexName);
296
-
297
- const deletedVertex = vertex, ///
298
- deletedVertexIndex = deletedVertex.getIndex(),
299
- vertices = this.getVertices(),
300
- affectedVertices = vertices.reduce((affectedVertices, vertex) => {
301
- const vertexIndex = vertex.getIndex(),
302
- vertexAffected = (vertexIndex > deletedVertexIndex);
303
-
304
- if (vertexAffected) {
305
- const affectedVertex = vertex; ///
306
-
307
- affectedVertices.push(affectedVertex);
308
- }
309
-
310
- return affectedVertices;
311
- }, []);
312
-
313
- affectedVertices.forEach((affectedVertex) => affectedVertex.decrementIndex());
314
- }
315
-
316
- return removedEdges;
317
- }
318
-
319
- static fromNothing() {
320
- const vertexMap = {},
321
- directedAcyclicGraph = new DirectedAcyclicGraph(vertexMap);
322
-
323
- return directedAcyclicGraph;
324
- }
325
-
326
- static fromVertexNames(vertexNames) {
327
- const vertexMap = vertexMapFromVertexNames(vertexNames);
328
-
329
- const directedAcyclicGraph = new DirectedAcyclicGraph(vertexMap);
330
-
331
- return directedAcyclicGraph;
332
- }
333
-
334
- static fromOrderedVertices(orderedVertices) {
335
- const vertexMap = vertexMapFromOrderedVertices(orderedVertices);
336
-
337
- addEdgesToVertices(orderedVertices, vertexMap);
338
-
339
- const directedAcyclicGraph = new DirectedAcyclicGraph(vertexMap);
340
-
341
- return directedAcyclicGraph;
342
- }
343
- }
344
-
345
- function addInvalidatingEdgeByVertices(sourceVertex, targetVertex) {
346
- let success = false;
347
-
348
- const forwardsAffectedVertices = targetVertex.retrieveForwardsAffectedVertices(sourceVertex),
349
- lastForwardsAffectedVertex = last(forwardsAffectedVertices),
350
- resultsInCycle = (lastForwardsAffectedVertex === sourceVertex);
351
-
352
- if (!resultsInCycle) {
353
- const backwardsAffectedVertices = sourceVertex.retrieveBackwardsAffectedVertices();
354
-
355
- orderVertices(backwardsAffectedVertices);
356
-
357
- orderVertices(forwardsAffectedVertices);
358
-
359
- const affectedVertices = [].concat(backwardsAffectedVertices).concat(forwardsAffectedVertices),
360
- affectedVertexIndices = affectedVertices.map((affectedVertex) => {
361
- const affectedVertexIndex = affectedVertex.getIndex();
362
-
363
- return affectedVertexIndex;
364
- });
365
-
366
- affectedVertexIndices.sort((indexA, indexB) => (indexA - indexB));
367
-
368
- affectedVertices.forEach((affectedVertex, index) => {
369
- const affectedVertexIndex = affectedVertexIndices[index];
370
-
371
- affectedVertex.setIndex(affectedVertexIndex);
372
- });
373
-
374
- success = true;
375
- }
376
-
377
- return success;
378
- }
379
-
380
- function vertexMapFromVertexNames(vertexNames) {
381
- const vertexMap = {};
382
-
383
- vertexNames.forEach((vertexName, index) => {
384
- const name = vertexName, ///
385
- vertex = Vertex.fromNameAndIndex(name, index);
386
-
387
- vertexMap[vertexName] = vertex;
388
- });
389
-
390
- return vertexMap;
391
- }
392
-
393
- function vertexMapFromOrderedVertices(orderedVertices) {
394
- const vertexMap = {};
395
-
396
- orderedVertices.forEach((orderedVertex, index) => {
397
- const name = orderedVertex.getName(),
398
- vertex = Vertex.fromNameAndIndex(name, index),
399
- vertexName = name; ///
400
-
401
- vertexMap[vertexName] = vertex;
402
- });
403
-
404
- return vertexMap;
405
- }
406
-
407
- function addEdgesToVertices(orderedVertices, vertexMap) {
408
- orderedVertices.forEach((orderedVertex) => {
409
- orderedVertex.forEachOutgoingEdge((outgoingEdge) => {
410
- const sourceVertexName = outgoingEdge.getSourceVertexName(),
411
- targetVertexName = outgoingEdge.getTargetVertexName(),
412
- immediatePredecessorVertexName = sourceVertexName, ///
413
- immediateSuccessorVertexName = targetVertexName,
414
- immediatePredecessorVertex = vertexMap[immediatePredecessorVertexName], ///
415
- immediateSuccessorVertex = vertexMap[immediateSuccessorVertexName]; ///
416
-
417
- immediatePredecessorVertex.addImmediateSuccessorVertex(immediateSuccessorVertex);
418
-
419
- immediateSuccessorVertex.addImmediatePredecessorVertex(immediatePredecessorVertex);
420
- });
421
- });
422
- }
package/src/example.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
-
3
- import { DirectedGraph } from "./index"; ///
4
-
5
- const directedGraph = DirectedGraph.fromNothing();
6
-
7
- directedGraph.addVerticesByVertexNames([
8
- "./easy-layout",
9
- "./easy-with-style",
10
- "./occam-lexers",
11
- "./with-style"
12
- ]);
13
-
14
- directedGraph.addEdgeByVertexNames("./easy-with-style", "./easy-layout");
15
- directedGraph.addEdgeByVertexNames("./with-style", "./easy-with-style");
16
- directedGraph.addEdgeByVertexNames("./easy-layout", "./occam-lexers");
17
- directedGraph.addEdgeByVertexNames("./easy-with-style", "./easy-layout");
18
- directedGraph.addEdgeByVertexNames("./occam-lexers", "./with-style");
19
-
20
- directedGraph.removeVertexByVertexName("./occam-lexers");
21
-
22
- debugger
@@ -1,69 +0,0 @@
1
- "use strict";
2
-
3
- import { arrayUtilities } from "necessary";
4
-
5
- const { first } = arrayUtilities;
6
-
7
- export default class PartialCycle {
8
- constructor(predecessorVertices, cyclicEdge) {
9
- this.predecessorVertices = predecessorVertices;
10
- this.cyclicEdge = cyclicEdge;
11
- }
12
-
13
- getPredecessorVertices() {
14
- return this.predecessorVertices;
15
- }
16
-
17
- getCyclicEdge() {
18
- return this.cyclicEdge;
19
- }
20
-
21
- getTargetVertexName() {
22
- const cyclicEdgeTargetVertexName = this.cyclicEdge.getTargetVertexName(),
23
- targetVertexName = cyclicEdgeTargetVertexName; ///
24
-
25
- return targetVertexName;
26
- }
27
-
28
- getPredecessorVertexNames() {
29
- const predecessorVertexNames = this.predecessorVertices.map((predecessorVertex) => {
30
- const predecessorVertexName = predecessorVertex.getName();
31
-
32
- return predecessorVertexName;
33
- });
34
-
35
- return predecessorVertexNames;
36
- }
37
-
38
- getCyclicEdgeSourceVertexName() {
39
- const cyclicEdgeSourceVertexName = this.cyclicEdge.getSourceVertexName();
40
-
41
- return cyclicEdgeSourceVertexName;
42
- }
43
-
44
- getCyclicEdgeTargetVertexName() {
45
- const cyclicEdgeTargetVertexName = this.cyclicEdge.getTargetVertexName();
46
-
47
- return cyclicEdgeTargetVertexName;
48
- }
49
-
50
- static fromCyclicEdgeAndPredecessorVertices(cyclicEdge, predecessorVertices) {
51
- predecessorVertices = predecessorVertices.slice(); ///
52
-
53
- const predecessorVerticesLength = predecessorVertices.length;
54
-
55
- if (predecessorVerticesLength > 0) {
56
- const firstPredecessorVertex = first(predecessorVertices),
57
- firstPredecessorVertexName = firstPredecessorVertex.getName(),
58
- cyclicEdgeTargetVertexName = cyclicEdge.getTargetVertexName();
59
-
60
- if (firstPredecessorVertexName === cyclicEdgeTargetVertexName) {
61
- predecessorVertices.shift();
62
- }
63
- }
64
-
65
- const partialCycle = new PartialCycle(predecessorVertices, cyclicEdge);
66
-
67
- return partialCycle;
68
- }
69
- }
@@ -1,58 +0,0 @@
1
- "use strict";
2
-
3
- import { arrayUtilities } from "necessary";
4
-
5
- const { prune } = arrayUtilities;
6
-
7
- const remove = prune; ///
8
-
9
- export function checkEdgesIncludesEdge(edge, edges) {
10
- const edge1 = edge, ///
11
- edgesIncludesEdge = edges.some((edge) => {
12
- const edge2 = edge, ///
13
- matches = edge1.match(edge2);
14
-
15
- if (matches) {
16
- return true;
17
- }
18
- });
19
-
20
- return edgesIncludesEdge;
21
- }
22
-
23
- export function removeEdgeFromEdges(edge, edges) {
24
- const edge1 = edge; ///
25
-
26
- remove(edges, (edge) => {
27
- const edge2 = edge, ///
28
- matches = edge1.match(edge2);
29
-
30
- if (!matches) { ///
31
- return true;
32
- }
33
- });
34
- }
35
-
36
- export function edgesBySourceVertexName(sourceVertexName, edges) {
37
- edges = edges.filter((edge) => { ///
38
- const matches = edge.matchSourceVertexName(sourceVertexName);
39
-
40
- if (matches) {
41
- return true;
42
- }
43
- });
44
-
45
- return edges;
46
- }
47
-
48
- export function edgesByTargetVertexName(targetVertexName, edges) {
49
- edges = edges.filter((edge) => { ///
50
- const matches = edge.matchTargetVertexName(targetVertexName);
51
-
52
- if (matches) {
53
- return true;
54
- }
55
- });
56
-
57
- return edges;
58
- }