Skip to content

Commit bc667a5

Browse files
committed
algorithm improvements
1 parent 59e67d8 commit bc667a5

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

lib/util/findGraphRoots.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ module.exports = (items, getDependencies) => {
113113
cycle = new Cycle();
114114
cycle.nodes.add(dependency);
115115
dependency.cycle = cycle;
116-
// Assume this is a root cycle
117-
// It will removed if not on node leaving
118-
rootCycles.add(cycle);
119116
}
120117
// set cycle property for each node in the cycle
121118
// if nodes are already part of a cycle
@@ -129,7 +126,6 @@ module.exports = (items, getDependencies) => {
129126
if (node.cycle) {
130127
if (node.cycle !== cycle) {
131128
// merge cycles
132-
rootCycles.delete(node.cycle);
133129
for (const cycleNode of node.cycle.nodes) {
134130
cycleNode.cycle = cycle;
135131
cycle.nodes.add(cycleNode);
@@ -153,7 +149,8 @@ module.exports = (items, getDependencies) => {
153149
roots.delete(dependency);
154150
break;
155151
case DONE_MAYBE_ROOT_CYCLE_MARKER:
156-
// This node has be visited yet and is currently part of a completed root cycle
152+
// This node has be visited yet and
153+
// is maybe currently part of a completed root cycle
157154
// we found a new reference to the cycle
158155
// so it's not really a root cycle
159156
// remove the cycle from the root cycles
@@ -167,28 +164,16 @@ module.exports = (items, getDependencies) => {
167164
// All dependencies of the current node has been visited
168165
// we leave the node
169166
stack.pop();
170-
const node = topOfStack.node;
171-
// If the leaving node is part of a cycle
172-
// and parent node is not part of the same cycle
173-
// we have a parent-child-relationship between these cycles
174-
// and remove the child cycle from the rootCycles
175-
const cycle = node.cycle;
176-
if (cycle) {
177-
if (stack.length > 0) {
178-
const parent = stack[stack.length - 1];
179-
if (parent.node.cycle !== cycle) {
180-
rootCycles.delete(cycle);
181-
node.marker = DONE_MARKER;
182-
continue;
183-
}
184-
}
185-
node.marker = DONE_MAYBE_ROOT_CYCLE_MARKER;
186-
} else {
187-
node.marker = DONE_MARKER;
188-
}
167+
topOfStack.node.marker = DONE_MARKER;
189168
}
190169
}
191-
if (selectedNode.marker === DONE_MARKER) {
170+
const cycle = selectedNode.cycle;
171+
if (cycle) {
172+
for (const node of cycle.nodes) {
173+
node.marker = DONE_MAYBE_ROOT_CYCLE_MARKER;
174+
}
175+
rootCycles.add(cycle);
176+
} else {
192177
selectedNode.marker = DONE_AND_ROOT_MARKER;
193178
roots.add(selectedNode);
194179
}

0 commit comments

Comments
 (0)