Skip to content

Commit

Permalink
Make breadth-first iterators consume the successor iterators lazily, …
Browse files Browse the repository at this point in the history
…and refactor to cut about 150 lines of code.

RELNOTES=Lazier Traverser#breadthFirst().

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=321062203
  • Loading branch information
benyu authored and netdpb committed Jul 14, 2020
1 parent ea4e950 commit 32f2d77
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 490 deletions.
Expand Up @@ -184,6 +184,13 @@ public void forGraph_breadthFirstIterable_javadocExample_canBeIteratedMultipleTi
assertEqualCharNodes(result, "bfaecd");
}

@Test
public void forGraph_breadthFirst_infinite() {
Iterable<Integer> result =
Traverser.forGraph(fixedSuccessors(Iterables.cycle(1, 2, 3))).breadthFirst(0);
assertThat(Iterables.limit(result, 4)).containsExactly(0, 1, 2, 3).inOrder();
}

@Test
public void forGraph_breadthFirst_diamond() {
Traverser<Character> traverser = Traverser.forGraph(DIAMOND_GRAPH);
Expand Down Expand Up @@ -373,6 +380,13 @@ public void forGraph_depthFirstPreOrderIterable_javadocExample_canBeIteratedMult
assertEqualCharNodes(result, "bacefd");
}

@Test
public void forGraph_depthFirstPreOrder_infinite() {
Iterable<Integer> result =
Traverser.forGraph(fixedSuccessors(Iterables.cycle(1, 2, 3))).breadthFirst(0);
assertThat(Iterables.limit(result, 2)).containsExactly(0, 1).inOrder();
}

@Test
public void forGraph_depthFirstPreOrder_diamond() {
Traverser<Character> traverser = Traverser.forGraph(DIAMOND_GRAPH);
Expand Down Expand Up @@ -784,6 +798,13 @@ public void forTree_withUndirectedNetwork_throws() throws Exception {
}
}

@Test
public void forTree_breadthFirst_infinite() {
Iterable<Integer> result =
Traverser.forTree(fixedSuccessors(Iterables.cycle(1, 2, 3))).breadthFirst(0);
assertThat(Iterables.limit(result, 8)).containsExactly(0, 1, 2, 3, 1, 2, 3, 1).inOrder();
}

@Test
public void forTree_breadthFirst_tree() throws Exception {
Traverser<Character> traverser = Traverser.forTree(TREE);
Expand Down Expand Up @@ -912,6 +933,13 @@ public void forTree_breadthFirstIterable_iterableIsLazy() {
assertThat(graph.requestedNodes).containsExactly('a', 'a', 'd', 'd', 'd', 'g', 'g', 'g');
}

@Test
public void forTree_depthFirstPreOrder_infinite() {
Iterable<Integer> result =
Traverser.forTree(fixedSuccessors(Iterables.cycle(1, 2, 3))).depthFirstPreOrder(0);
assertThat(Iterables.limit(result, 3)).containsExactly(0, 1, 1).inOrder();
}

@Test
public void forTree_depthFirstPreOrderIterable_tree() throws Exception {
Traverser<Character> traverser = Traverser.forTree(TREE);
Expand Down Expand Up @@ -1238,4 +1266,13 @@ public Iterable<? extends Character> successors(Character node) {
return delegate.successors(node);
}
}

private static <N> SuccessorsFunction<N> fixedSuccessors(final Iterable<N> successors) {
return new SuccessorsFunction<N>() {
@Override
public Iterable<N> successors(N n) {
return successors;
}
};
}
}

1 comment on commit 32f2d77

@jbduncan
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Please sign in to comment.