Skip to content

Commit

Permalink
ImmutableValueGraph: Support incidentEdgeOrder=stable
Browse files Browse the repository at this point in the history
RELNOTES=N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=288467276
  • Loading branch information
nymanjens authored and netdpb committed Jan 7, 2020
1 parent 845c974 commit 6c3fe55
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
Expand Up @@ -122,4 +122,20 @@ public void immutableValueGraphBuilder_putEdgeFromEndpointPair() {
assertThat(graph.edges()).containsExactly(EndpointPair.ordered("A", "B"));
assertThat(graph.edgeValueOrDefault("A", "B", null)).isEqualTo(10);
}

@Test
public void immutableValueGraphBuilder_incidentEdges_preservesIncidentEdgesOrder() {
ImmutableValueGraph<Integer, String> graph =
ValueGraphBuilder.directed()
.<Integer, String>immutable()
.putEdgeValue(2, 1, "2-1")
.putEdgeValue(2, 3, "2-3")
.putEdgeValue(1, 2, "1-2")
.build();

assertThat(graph.incidentEdges(2))
.containsExactly(
EndpointPair.ordered(2, 1), EndpointPair.ordered(2, 3), EndpointPair.ordered(1, 2))
.inOrder();
}
}
Expand Up @@ -125,7 +125,10 @@ public static class Builder<N, V> {
private final MutableValueGraph<N, V> mutableValueGraph;

Builder(ValueGraphBuilder<N, V> graphBuilder) {
this.mutableValueGraph = graphBuilder.build();
// The incidentEdgeOrder for immutable graphs is always stable. However, we don't want to
// modify this builder, so we make a copy instead.
this.mutableValueGraph =
graphBuilder.copy().incidentEdgeOrder(ElementOrder.<N>stable()).build();
}

/**
Expand Down
Expand Up @@ -179,6 +179,15 @@ public <N1 extends N, V1 extends V> MutableValueGraph<N1, V1> build() {
return new ConfigurableMutableValueGraph<>(this);
}

ValueGraphBuilder<N, V> copy() {
ValueGraphBuilder<N, V> newBuilder = new ValueGraphBuilder<>(directed);
newBuilder.allowsSelfLoops = allowsSelfLoops;
newBuilder.nodeOrder = nodeOrder;
newBuilder.expectedNodeCount = expectedNodeCount;
newBuilder.incidentEdgeOrder = incidentEdgeOrder;
return newBuilder;
}

@SuppressWarnings("unchecked")
private <N1 extends N, V1 extends V> ValueGraphBuilder<N1, V1> cast() {
return (ValueGraphBuilder<N1, V1>) this;
Expand Down
Expand Up @@ -122,4 +122,20 @@ public void immutableValueGraphBuilder_putEdgeFromEndpointPair() {
assertThat(graph.edges()).containsExactly(EndpointPair.ordered("A", "B"));
assertThat(graph.edgeValueOrDefault("A", "B", null)).isEqualTo(10);
}

@Test
public void immutableValueGraphBuilder_incidentEdges_preservesIncidentEdgesOrder() {
ImmutableValueGraph<Integer, String> graph =
ValueGraphBuilder.directed()
.<Integer, String>immutable()
.putEdgeValue(2, 1, "2-1")
.putEdgeValue(2, 3, "2-3")
.putEdgeValue(1, 2, "1-2")
.build();

assertThat(graph.incidentEdges(2))
.containsExactly(
EndpointPair.ordered(2, 1), EndpointPair.ordered(2, 3), EndpointPair.ordered(1, 2))
.inOrder();
}
}
5 changes: 4 additions & 1 deletion guava/src/com/google/common/graph/ImmutableValueGraph.java
Expand Up @@ -125,7 +125,10 @@ public static class Builder<N, V> {
private final MutableValueGraph<N, V> mutableValueGraph;

Builder(ValueGraphBuilder<N, V> graphBuilder) {
this.mutableValueGraph = graphBuilder.build();
// The incidentEdgeOrder for immutable graphs is always stable. However, we don't want to
// modify this builder, so we make a copy instead.
this.mutableValueGraph =
graphBuilder.copy().incidentEdgeOrder(ElementOrder.<N>stable()).build();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions guava/src/com/google/common/graph/ValueGraphBuilder.java
Expand Up @@ -179,6 +179,15 @@ public <N1 extends N, V1 extends V> MutableValueGraph<N1, V1> build() {
return new ConfigurableMutableValueGraph<>(this);
}

ValueGraphBuilder<N, V> copy() {
ValueGraphBuilder<N, V> newBuilder = new ValueGraphBuilder<>(directed);
newBuilder.allowsSelfLoops = allowsSelfLoops;
newBuilder.nodeOrder = nodeOrder;
newBuilder.expectedNodeCount = expectedNodeCount;
newBuilder.incidentEdgeOrder = incidentEdgeOrder;
return newBuilder;
}

@SuppressWarnings("unchecked")
private <N1 extends N, V1 extends V> ValueGraphBuilder<N1, V1> cast() {
return (ValueGraphBuilder<N1, V1>) this;
Expand Down

0 comments on commit 6c3fe55

Please sign in to comment.