Skip to content

Commit

Permalink
Remove recursion - really weird graphs can cause a stack overflow. In…
Browse files Browse the repository at this point in the history
…stead, just loop until the vertexIterator is used up. Observed in #1461
  • Loading branch information
AngledLuffa committed Aug 15, 2024
1 parent 154fd14 commit 05804a3
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/edu/stanford/nlp/graph/DirectedMultiGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,20 +571,22 @@ public E next() {
}

private void primeIterator() {
if (edgeIterator != null && edgeIterator.hasNext()) {
hasNext = true; // technically, we shouldn't need to put this here, but let's be safe
} else if (connectionIterator != null && connectionIterator.hasNext()) {
Map.Entry<V, List<E>> nextConnection = connectionIterator.next();
edgeIterator = nextConnection.getValue().iterator();
currentTarget = nextConnection.getKey();
primeIterator();
} else if (vertexIterator != null && vertexIterator.hasNext()) {
Map.Entry<V, Map<V, List<E>>> nextVertex = vertexIterator.next();
connectionIterator = nextVertex.getValue().entrySet().iterator();
currentSource = nextVertex.getKey();
primeIterator();
} else {
hasNext = false;
while (true) {
if (edgeIterator != null && edgeIterator.hasNext()) {
hasNext = true; // technically, we shouldn't need to put this here, but let's be safe
return;
} else if (connectionIterator != null && connectionIterator.hasNext()) {
Map.Entry<V, List<E>> nextConnection = connectionIterator.next();
edgeIterator = nextConnection.getValue().iterator();
currentTarget = nextConnection.getKey();
} else if (vertexIterator != null && vertexIterator.hasNext()) {
Map.Entry<V, Map<V, List<E>>> nextVertex = vertexIterator.next();
connectionIterator = nextVertex.getValue().entrySet().iterator();
currentSource = nextVertex.getKey();
} else {
hasNext = false;
return;
}
}
}

Expand Down

0 comments on commit 05804a3

Please sign in to comment.