aboutsummaryrefslogtreecommitdiff
path: root/guava/src/com/google/common/graph/StandardMutableValueGraph.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava/src/com/google/common/graph/StandardMutableValueGraph.java')
-rw-r--r--guava/src/com/google/common/graph/StandardMutableValueGraph.java9
1 files changed, 7 insertions, 2 deletions
diff --git a/guava/src/com/google/common/graph/StandardMutableValueGraph.java b/guava/src/com/google/common/graph/StandardMutableValueGraph.java
index 0ea641a5b..1ad474083 100644
--- a/guava/src/com/google/common/graph/StandardMutableValueGraph.java
+++ b/guava/src/com/google/common/graph/StandardMutableValueGraph.java
@@ -24,6 +24,7 @@ import static com.google.common.graph.Graphs.checkNonNegative;
import static com.google.common.graph.Graphs.checkPositive;
import static java.util.Objects.requireNonNull;
+import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import javax.annotation.CheckForNull;
@@ -136,17 +137,21 @@ final class StandardMutableValueGraph<N, V> extends StandardValueGraph<N, V>
}
}
- for (N successor : connections.successors()) {
+ for (N successor : ImmutableList.copyOf(connections.successors())) {
// requireNonNull is safe because the node is a successor.
requireNonNull(nodeConnections.getWithoutCaching(successor)).removePredecessor(node);
+ requireNonNull(connections.removeSuccessor(successor));
--edgeCount;
}
if (isDirected()) { // In undirected graphs, the successor and predecessor sets are equal.
- for (N predecessor : connections.predecessors()) {
+ // Since views are returned, we need to copy the predecessors that will be removed.
+ // Thus we avoid modifying the underlying view while iterating over it.
+ for (N predecessor : ImmutableList.copyOf(connections.predecessors())) {
// requireNonNull is safe because the node is a predecessor.
checkState(
requireNonNull(nodeConnections.getWithoutCaching(predecessor)).removeSuccessor(node)
!= null);
+ connections.removePredecessor(predecessor);
--edgeCount;
}
}