From 0aa99a5f941b886597e7366be763dadc8c6db6dd Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 4 Jun 2015 11:41:19 +1000 Subject: Clean up a bit of the ShrinkTree stuff, particularly for shrinking --- .../java/au/id/zancanaro/javacheck/ShrinkTree.java | 65 ++++++++++------------ 1 file changed, 29 insertions(+), 36 deletions(-) (limited to 'src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java') diff --git a/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java index 7dff917..bdc7604 100644 --- a/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java @@ -41,55 +41,50 @@ public class ShrinkTree { tree.getValue().children.iterator())); } - private static Iterator[]> permutations(ShrinkTree[] trees) { + private static List makeHeadList(ShrinkTree[] trees) { + List heads = new ArrayList<>(trees.length); + for (ShrinkTree tree : trees) { + heads.add(tree.getValue()); + } + return heads; + } + + public static Iterator[]> promoteChildren(ShrinkTree[] trees) { return flatten( - rangeIterator(trees.length, - index -> mappingIterator(child -> { + rangeIterator(trees.length, index -> + mappingIterator(child -> { @SuppressWarnings("unchecked") ShrinkTree[] result = (ShrinkTree[]) new ShrinkTree[trees.length]; for (int i = 0; i < trees.length; ++i) { result[i] = (i == index ? child : trees[i]); } return result; - }, trees[index].getChildren()) - )); - } - - private static List makeHeadList(ShrinkTree[] trees) { - List heads = new ArrayList<>(trees.length); - for (ShrinkTree tree : trees) { - heads.add(tree.getValue()); - } - return heads; + }, trees[index].getChildren()))); } - public static ShrinkTree zip(Function, R> fn, ShrinkTree[] trees) { - return new ShrinkTree<>( - fn.apply(makeHeadList(trees)), - () -> mappingIterator( - shrinks -> ShrinkTree.zip(fn, shrinks), - ShrinkTree.permutations(trees))); + public static Iterator[]> removeChildren(ShrinkTree[] trees) { + return rangeIterator(trees.length, index -> { + @SuppressWarnings("unchecked") + ShrinkTree[] result = (ShrinkTree[]) new ShrinkTree[trees.length - 1]; + for (int i = 0; i < trees.length - 1; ++i) { + result[i] = trees[(i >= index ? i + 1 : i)]; + } + return result; + }); } - private static Iterator[]> removeEach(ShrinkTree[] trees) { - return concat( - rangeIterator(trees.length, index -> { - @SuppressWarnings("unchecked") - ShrinkTree[] result = (ShrinkTree[]) new ShrinkTree[trees.length - 1]; - for (int i = 0; i < trees.length - 1; ++i) { - result[i] = trees[(i >= index ? i + 1 : i)]; - } - return result; - }), - permutations(trees)); + public static Iterator[]> removeAndPromoteChildren(ShrinkTree[] trees) { + return concat(removeChildren(trees), promoteChildren(trees)); } - public static ShrinkTree shrink(Function, R> fn, ShrinkTree[] trees) { + public static ShrinkTree> combine( + ShrinkTree[] trees, + Function[], Iterator[]>> processChildren) { return new ShrinkTree<>( - fn.apply(makeHeadList(trees)), + makeHeadList(trees), () -> mappingIterator( - shrinks -> ShrinkTree.shrink(fn, shrinks), - ShrinkTree.removeEach(trees))); + shrinks -> ShrinkTree.combine(shrinks, processChildren), + processChildren.apply(trees))); } public ShrinkTree map(Function f) { @@ -126,12 +121,10 @@ public class ShrinkTree { strategy.shrink(value)); } - @SuppressWarnings("unused") public void print(Writer output) throws IOException { print(output, Object::toString); } - @SuppressWarnings("unused") public void print(Writer output, Function toString) throws IOException { output.write(toString.apply(this.getValue())); output.write('['); -- cgit v1.2.3