Skip to content

Commit 05804a3

Browse files
committed
Remove recursion - really weird graphs can cause a stack overflow. Instead, just loop until the vertexIterator is used up. Observed in #1461
1 parent 154fd14 commit 05804a3

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/edu/stanford/nlp/graph/DirectedMultiGraph.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -571,20 +571,22 @@ public E next() {
571571
}
572572

573573
private void primeIterator() {
574-
if (edgeIterator != null && edgeIterator.hasNext()) {
575-
hasNext = true; // technically, we shouldn't need to put this here, but let's be safe
576-
} else if (connectionIterator != null && connectionIterator.hasNext()) {
577-
Map.Entry<V, List<E>> nextConnection = connectionIterator.next();
578-
edgeIterator = nextConnection.getValue().iterator();
579-
currentTarget = nextConnection.getKey();
580-
primeIterator();
581-
} else if (vertexIterator != null && vertexIterator.hasNext()) {
582-
Map.Entry<V, Map<V, List<E>>> nextVertex = vertexIterator.next();
583-
connectionIterator = nextVertex.getValue().entrySet().iterator();
584-
currentSource = nextVertex.getKey();
585-
primeIterator();
586-
} else {
587-
hasNext = false;
574+
while (true) {
575+
if (edgeIterator != null && edgeIterator.hasNext()) {
576+
hasNext = true; // technically, we shouldn't need to put this here, but let's be safe
577+
return;
578+
} else if (connectionIterator != null && connectionIterator.hasNext()) {
579+
Map.Entry<V, List<E>> nextConnection = connectionIterator.next();
580+
edgeIterator = nextConnection.getValue().iterator();
581+
currentTarget = nextConnection.getKey();
582+
} else if (vertexIterator != null && vertexIterator.hasNext()) {
583+
Map.Entry<V, Map<V, List<E>>> nextVertex = vertexIterator.next();
584+
connectionIterator = nextVertex.getValue().entrySet().iterator();
585+
currentSource = nextVertex.getKey();
586+
} else {
587+
hasNext = false;
588+
return;
589+
}
588590
}
589591
}
590592

0 commit comments

Comments
 (0)