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

Update AbstractBaseGraph.java #7641

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
73 changes: 45 additions & 28 deletions guava/src/com/google/common/graph/AbstractBaseGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import com.google.common.primitives.Ints;
import java.util.AbstractSet;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -63,6 +66,9 @@ protected long edgeCount() {
* An implementation of {@link BaseGraph#edges()} defined in terms of {@link Graph#nodes()} and
* {@link #successors(Object)}.
*/
// Cache for edges
private transient Set<EndpointPair<N>> cachedEdges;

@Override
public Set<EndpointPair<N>> edges() {
return new AbstractSet<EndpointPair<N>>() {
Expand Down Expand Up @@ -104,34 +110,45 @@ public ElementOrder<N> incidentEdgeOrder() {
}

@Override
public Set<EndpointPair<N>> incidentEdges(N node) {
checkNotNull(node);
checkArgument(nodes().contains(node), "Node %s is not an element of this graph.", node);
IncidentEdgeSet<N> incident =
new IncidentEdgeSet<N>(this, node) {
@Override
public UnmodifiableIterator<EndpointPair<N>> iterator() {
if (graph.isDirected()) {
return Iterators.unmodifiableIterator(
Iterators.concat(
Iterators.transform(
graph.predecessors(node).iterator(),
(N predecessor) -> EndpointPair.ordered(predecessor, node)),
Iterators.transform(
// filter out 'node' from successors (already covered by predecessors,
// above)
Sets.difference(graph.successors(node), ImmutableSet.of(node)).iterator(),
(N successor) -> EndpointPair.ordered(node, successor))));
} else {
return Iterators.unmodifiableIterator(
Iterators.transform(
graph.adjacentNodes(node).iterator(),
(N adjacentNode) -> EndpointPair.unordered(node, adjacentNode)));
}
}
};
return nodeInvalidatableSet(incident, node);
}
public Set<EndpointPair<N>> edges() {
if (cachedEdges == null) {
cachedEdges = new AbstractSet<EndpointPair<N>>() {
@Override
public Iterator<EndpointPair<N>> iterator() {
return endpointPairIterator();
}

@Override
public int size() {
return edgeCount();
}

@Override
public boolean contains(Object obj) {
if (!(obj instanceof EndpointPair)) {
return false;
}
EndpointPair<?> endpointPair = (EndpointPair<?>) obj;
return isDirected() == endpointPair.isOrdered()
&& nodes().contains(endpointPair.nodeU())
&& successors((N) endpointPair.nodeU()).contains(endpointPair.nodeV());
}
};
}
return cachedEdges;
}

// Method to clear the cache when the graph is modified
protected void clearCache() {
cachedEdges = null;
}

// Abstract methods that subclasses must implement
protected abstract Iterator<EndpointPair<N>> endpointPairIterator();
protected abstract int edgeCount();
protected abstract boolean isDirected();
protected abstract Set<N> nodes();
protected abstract Set<N> successors(N node);

@Override
public int degree(N node) {
Expand Down