File tree 6 files changed +36
-18
lines changed
guava/src/com/google/common/graph
guava-tests/test/com/google/common/graph
guava/src/com/google/common/graph
guava-tests/test/com/google/common/graph
6 files changed +36
-18
lines changed Original file line number Diff line number Diff line change @@ -385,10 +385,13 @@ public void removeNode_nodeNotPresent() {
385
385
public void removeNode_queryAfterRemoval () {
386
386
assume ().that (graphIsMutable ()).isTrue ();
387
387
388
- addNode (N1 );
389
- @ SuppressWarnings ("unused" )
390
- Set <Integer > unused = graph .adjacentNodes (N1 ); // ensure cache (if any) is populated
388
+ putEdge (N1 , N2 );
389
+ putEdge (N2 , N1 );
390
+ Set <Integer > n1AdjacentNodes = graph .adjacentNodes (N1 );
391
+ Set <Integer > n2AdjacentNodes = graph .adjacentNodes (N2 );
391
392
assertThat (graphAsMutableGraph .removeNode (N1 )).isTrue ();
393
+ assertThat (n1AdjacentNodes ).isEmpty ();
394
+ assertThat (n2AdjacentNodes ).isEmpty ();
392
395
IllegalArgumentException e =
393
396
assertThrows (IllegalArgumentException .class , () -> graph .adjacentNodes (N1 ));
394
397
assertNodeNotInGraphErrorMessage (e );
Original file line number Diff line number Diff line change @@ -670,11 +670,12 @@ public void removeNode_nodeNotPresent() {
670
670
public void removeNode_queryAfterRemoval () {
671
671
assume ().that (graphIsMutable ()).isTrue ();
672
672
673
- addNode (N1 );
674
- @ SuppressWarnings ("unused" )
675
- Set <Integer > unused =
676
- networkAsMutableNetwork .adjacentNodes (N1 ); // ensure cache (if any) is populated
673
+ addEdge (N1 , N2 , E12 );
674
+ Set <Integer > n1AdjacentNodes = networkAsMutableNetwork .adjacentNodes (N1 );
675
+ Set <Integer > n2AdjacentNodes = networkAsMutableNetwork .adjacentNodes (N2 );
677
676
assertTrue (networkAsMutableNetwork .removeNode (N1 ));
677
+ assertThat (n1AdjacentNodes ).isEmpty ();
678
+ assertThat (n2AdjacentNodes ).isEmpty ();
678
679
IllegalArgumentException e =
679
680
assertThrows (
680
681
IllegalArgumentException .class , () -> networkAsMutableNetwork .adjacentNodes (N1 ));
Original file line number Diff line number Diff line change 24
24
import static com .google .common .graph .Graphs .checkPositive ;
25
25
import static java .util .Objects .requireNonNull ;
26
26
27
+ import com .google .common .collect .ImmutableList ;
27
28
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
28
29
import javax .annotation .CheckForNull ;
29
30
@@ -136,17 +137,21 @@ public boolean removeNode(N node) {
136
137
}
137
138
}
138
139
139
- for (N successor : connections .successors ()) {
140
+ for (N successor : ImmutableList . copyOf ( connections .successors () )) {
140
141
// requireNonNull is safe because the node is a successor.
141
142
requireNonNull (nodeConnections .getWithoutCaching (successor )).removePredecessor (node );
143
+ requireNonNull (connections .removeSuccessor (successor ));
142
144
--edgeCount ;
143
145
}
144
146
if (isDirected ()) { // In undirected graphs, the successor and predecessor sets are equal.
145
- for (N predecessor : connections .predecessors ()) {
147
+ // Since views are returned, we need to copy the predecessors that will be removed.
148
+ // Thus we avoid modifying the underlying view while iterating over it.
149
+ for (N predecessor : ImmutableList .copyOf (connections .predecessors ())) {
146
150
// requireNonNull is safe because the node is a predecessor.
147
151
checkState (
148
152
requireNonNull (nodeConnections .getWithoutCaching (predecessor )).removeSuccessor (node )
149
153
!= null );
154
+ connections .removePredecessor (predecessor );
150
155
--edgeCount ;
151
156
}
152
157
}
Original file line number Diff line number Diff line change @@ -385,10 +385,13 @@ public void removeNode_nodeNotPresent() {
385
385
public void removeNode_queryAfterRemoval () {
386
386
assume ().that (graphIsMutable ()).isTrue ();
387
387
388
- addNode (N1 );
389
- @ SuppressWarnings ("unused" )
390
- Set <Integer > unused = graph .adjacentNodes (N1 ); // ensure cache (if any) is populated
388
+ putEdge (N1 , N2 );
389
+ putEdge (N2 , N1 );
390
+ Set <Integer > n1AdjacentNodes = graph .adjacentNodes (N1 );
391
+ Set <Integer > n2AdjacentNodes = graph .adjacentNodes (N2 );
391
392
assertThat (graphAsMutableGraph .removeNode (N1 )).isTrue ();
393
+ assertThat (n1AdjacentNodes ).isEmpty ();
394
+ assertThat (n2AdjacentNodes ).isEmpty ();
392
395
IllegalArgumentException e =
393
396
assertThrows (IllegalArgumentException .class , () -> graph .adjacentNodes (N1 ));
394
397
assertNodeNotInGraphErrorMessage (e );
Original file line number Diff line number Diff line change @@ -677,11 +677,12 @@ public void removeNode_nodeNotPresent() {
677
677
public void removeNode_queryAfterRemoval () {
678
678
assume ().that (graphIsMutable ()).isTrue ();
679
679
680
- addNode (N1 );
681
- @ SuppressWarnings ("unused" )
682
- Set <Integer > unused =
683
- networkAsMutableNetwork .adjacentNodes (N1 ); // ensure cache (if any) is populated
680
+ addEdge (N1 , N2 , E12 );
681
+ Set <Integer > n1AdjacentNodes = networkAsMutableNetwork .adjacentNodes (N1 );
682
+ Set <Integer > n2AdjacentNodes = networkAsMutableNetwork .adjacentNodes (N2 );
684
683
assertTrue (networkAsMutableNetwork .removeNode (N1 ));
684
+ assertThat (n1AdjacentNodes ).isEmpty ();
685
+ assertThat (n2AdjacentNodes ).isEmpty ();
685
686
IllegalArgumentException e =
686
687
assertThrows (
687
688
IllegalArgumentException .class , () -> networkAsMutableNetwork .adjacentNodes (N1 ));
Original file line number Diff line number Diff line change 24
24
import static com .google .common .graph .Graphs .checkPositive ;
25
25
import static java .util .Objects .requireNonNull ;
26
26
27
+ import com .google .common .collect .ImmutableList ;
27
28
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
28
29
import javax .annotation .CheckForNull ;
29
30
@@ -136,17 +137,21 @@ public boolean removeNode(N node) {
136
137
}
137
138
}
138
139
139
- for (N successor : connections .successors ()) {
140
+ for (N successor : ImmutableList . copyOf ( connections .successors () )) {
140
141
// requireNonNull is safe because the node is a successor.
141
142
requireNonNull (nodeConnections .getWithoutCaching (successor )).removePredecessor (node );
143
+ requireNonNull (connections .removeSuccessor (successor ));
142
144
--edgeCount ;
143
145
}
144
146
if (isDirected ()) { // In undirected graphs, the successor and predecessor sets are equal.
145
- for (N predecessor : connections .predecessors ()) {
147
+ // Since views are returned, we need to copy the predecessors that will be removed.
148
+ // Thus we avoid modifying the underlying view while iterating over it.
149
+ for (N predecessor : ImmutableList .copyOf (connections .predecessors ())) {
146
150
// requireNonNull is safe because the node is a predecessor.
147
151
checkState (
148
152
requireNonNull (nodeConnections .getWithoutCaching (predecessor )).removeSuccessor (node )
149
153
!= null );
154
+ connections .removePredecessor (predecessor );
150
155
--edgeCount ;
151
156
}
152
157
}
You can’t perform that action at this time.
0 commit comments