Skip to content
This repository was archived by the owner on Jan 18, 2018. It is now read-only.

Commit 8e08eb6

Browse files
committed
experimental reversed flag
1 parent 0b9bfda commit 8e08eb6

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

lib/bem-graph.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BemGraph {
2323
naturalDependenciesOf(entities, tech) {
2424
return this.dependenciesOf(this._sortNaturally(entities), tech);
2525
}
26-
dependenciesOf(entities, tech) {
26+
dependenciesOf(entities, tech, reversed) {
2727
if (!Array.isArray(entities)) {
2828
entities = [entities];
2929
}
@@ -40,10 +40,7 @@ class BemGraph {
4040
}, []);
4141

4242
// Recommended order
43-
const _positions = vertices.reduce((res, e, pos) => { res[e.id] = pos + 1; return res; }, {});
44-
const _sort = (a, b) => _positions[a.id] - _positions[b.id];
45-
46-
const iter = resolve(this._mixedGraph, vertices, tech, _sort);
43+
const iter = resolve(this._mixedGraph, vertices, tech, reversed);
4744
const arr = Array.from(iter);
4845

4946
// TODO: returns iterator
@@ -61,6 +58,9 @@ class BemGraph {
6158
return obj;
6259
}).filter(Boolean);
6360
}
61+
reverseDependenciesOf(entities, tech) {
62+
return this.dependenciesOf(entities, tech, true);
63+
}
6464
naturalize() {
6565
const mixedGraph = this._mixedGraph;
6666

lib/mixed-graph-resolve.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ class TopoGroups {
6464
}
6565
}
6666

67-
function resolve(mixedGraph, startVertices, tech, backsort) {
67+
function resolve(mixedGraph, startVertices, tech, reverse) {
68+
const _positions = startVertices.reduce((res, e, pos) => { res[e.id] = pos + 1; return res; }, {});
69+
const backsort = (a, b) => _positions[a.id] - _positions[b.id];
70+
6871
const orderedSuccessors = []; // L ← Empty list that will contain the sorted nodes
6972
const _orderedVisits = {}; // Hash with visiting flags: temporary - false, permanently - true
7073
const unorderedSuccessors = new VertexSet(); // The rest nodes
@@ -74,7 +77,7 @@ function resolve(mixedGraph, startVertices, tech, backsort) {
7477
// ... while there are unmarked nodes do
7578
startVertices.forEach(v => visit(v, false));
7679

77-
const _orderedSuccessors = Array.from(new VertexSet(orderedSuccessors.reverse()));
80+
const _orderedSuccessors = reverse ? [] : Array.from(new VertexSet(orderedSuccessors.reverse()));
7881
const _unorderedSuccessors = Array.from(unorderedSuccessors).sort(backsort);
7982

8083
// console.log({topogroups, ordered: Array.from(_orderedSuccessors).map(v => v.id), unordered: Array.from(_unorderedSuccessors).map(v => v.id)});
@@ -115,7 +118,7 @@ function resolve(mixedGraph, startVertices, tech, backsort) {
115118
topo.lookupCreate(fromVertex.id);
116119

117120
// ... for each node m with an edge from n to m do
118-
const orderedDirectSuccessors = mixedGraph.directSuccessors(fromVertex, { ordered: true, tech });
121+
const orderedDirectSuccessors = reverse ? [] : mixedGraph.directSuccessors(fromVertex, { ordered: true, tech });
119122

120123
for (let successor of orderedDirectSuccessors) {
121124
if (successor.id === fromVertex.id) { // TODO: Filter loops earlier
@@ -146,7 +149,7 @@ function resolve(mixedGraph, startVertices, tech, backsort) {
146149
weak || orderedSuccessors.unshift(fromVertex);
147150
weak && unorderedSuccessors.add(fromVertex);
148151

149-
const unorderedDirectSuccessors = mixedGraph.directSuccessors(fromVertex, { ordered: false, tech });
152+
const unorderedDirectSuccessors = mixedGraph.directSuccessors(fromVertex, { ordered: false, tech, reversed: reverse });
150153

151154
for (let successor of unorderedDirectSuccessors) {
152155
// console.log('unordered', successor.id);

lib/mixed-graph.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = class MixedGraph {
1818
this._vertices = new VertexSet();
1919
this._orderedGraphMap = new Map();
2020
this._unorderedGraphMap = new Map();
21+
this._reverseGraphMap = new Map();
2122
}
2223
addVertex(vertex) {
2324
this._vertices.add(vertex);
@@ -51,6 +52,17 @@ module.exports = class MixedGraph {
5152

5253
subgraph.addEdge(fromVertex, toVertex);
5354

55+
let reverseSubgraph = this._getSubgraph({ tech, reversed: true });
56+
57+
// Create DirectedGraph for each tech
58+
if (!reverseSubgraph) {
59+
reverseSubgraph = new DirectedGraph();
60+
61+
this._reverseGraphMap.set(tech, reverseSubgraph);
62+
}
63+
64+
reverseSubgraph.addEdge(toVertex, fromVertex);
65+
5466
return this;
5567
}
5668
/**
@@ -86,7 +98,8 @@ module.exports = class MixedGraph {
8698
);
8799
}
88100
_getGraphMap(data) {
89-
return data.ordered ? this._orderedGraphMap : this._unorderedGraphMap;
101+
return data.reversed ? this._reverseGraphMap
102+
: data.ordered ? this._orderedGraphMap : this._unorderedGraphMap;
90103
}
91104
_getSubgraph(data) {
92105
return this._getGraphMap(data).get(data.tech);

0 commit comments

Comments
 (0)