Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Graph.adjacentNodes more transparent when a node gets deleted. #6553

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions guava-tests/test/com/google/common/graph/AbstractGraphTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,20 @@ public void removeNode_nodeNotPresent() {
public void removeNode_queryAfterRemoval() {
assume().that(graphIsMutable()).isTrue();

addNode(N1);
@SuppressWarnings("unused")
Set<Integer> unused = graph.adjacentNodes(N1); // ensure cache (if any) is populated
putEdge(N1, N2);
putEdge(N2, N1);
Set<Integer> n1AdjacentNodes = graph.adjacentNodes(N1);
Set<Integer> n2AdjacentNodes = graph.adjacentNodes(N2);

assertThat(graphAsMutableGraph.removeNode(N1)).isTrue();
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(N1));
assertNodeNotInGraphErrorMessage(e);
assertThat(n1AdjacentNodes).isEmpty();
assertThat(n2AdjacentNodes).isEmpty();
try {
graph.adjacentNodes(N1);
fail(ERROR_NODE_NOT_IN_GRAPH);
} catch (IllegalArgumentException e) {
assertNodeNotInGraphErrorMessage(e);
}
}

@Test
Expand Down
20 changes: 12 additions & 8 deletions guava-tests/test/com/google/common/graph/AbstractNetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,19 @@ public void removeNode_nodeNotPresent() {
public void removeNode_queryAfterRemoval() {
assume().that(graphIsMutable()).isTrue();

addNode(N1);
@SuppressWarnings("unused")
Set<Integer> unused =
networkAsMutableNetwork.adjacentNodes(N1); // ensure cache (if any) is populated
addEdge(N1, N2, E12);
Set<Integer> n1AdjacentNodes = networkAsMutableNetwork.adjacentNodes(N1);
Set<Integer> n2AdjacentNodes = networkAsMutableNetwork.adjacentNodes(N2);

assertTrue(networkAsMutableNetwork.removeNode(N1));
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class, () -> networkAsMutableNetwork.adjacentNodes(N1));
assertNodeNotInGraphErrorMessage(e);
assertThat(n1AdjacentNodes).isEmpty();
assertThat(n2AdjacentNodes).isEmpty();
try {
networkAsMutableNetwork.adjacentNodes(N1);
fail(ERROR_NODE_NOT_IN_GRAPH);
} catch (IllegalArgumentException e) {
assertNodeNotInGraphErrorMessage(e);
}
}

@Test
Expand Down
11 changes: 9 additions & 2 deletions guava/src/com/google/common/graph/StandardMutableValueGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;

Expand Down Expand Up @@ -136,17 +137,23 @@ public boolean removeNode(N node) {
}
}

for (N successor : connections.successors()) {
// Since views are returned, we need to copy the successors that will be removed.
// Thus we avoid modifying the underlying view while iterating over it.
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while since I've looked at this code, but I'm a bit suspicious of the three different ways in which we're now accessing the connections of a node in this method:

  • direct reference (or alias) for the internal data structure: nodeConnections.get(node) (via connections, line 127; nodeConnections is declared in the superclass as MapIteratorCache<N, GraphConnections<N, V>> nodeConnections)
  • direct reference without caching: nodeConnections.getWithoutCaching() (line 144)
  • (immutable) copy of the output of that structure (line 142)

We should (a) validate that we want those three different mechanisms, or use only the ones we actually want, and (b) document which one(s) we do want, and why.

There's a lot of clever stuff going on in this code, which makes it easy to make mistakes in updating it.

(To be clear, I think you've explained why you want the copy of the set in the places that you've made it, but the reason why we're using each of the above two mechanisms in different places is less clear.)

--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;
}
}
Expand Down
Loading