summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/RoseTree.java
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-03 14:32:07 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-03 14:32:07 +1000
commit7b1a783b749ab04ab8219ef28f9b1abb0ded6ca4 (patch)
treea0d25f117dbf3af5adab6468e513c0b0c6ec4b34 /src/main/java/au/id/zancanaro/javacheck/RoseTree.java
parent7eabc41ad41e0da58fff2a6bb04212ea0cd3baa2 (diff)
Rename RoseTree to ShrinkTree (less confusing and more specific)
Diffstat (limited to 'src/main/java/au/id/zancanaro/javacheck/RoseTree.java')
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/RoseTree.java128
1 files changed, 0 insertions, 128 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java
deleted file mode 100644
index c735b46..0000000
--- a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java
+++ /dev/null
@@ -1,128 +0,0 @@
-package au.id.zancanaro.javacheck;
-
-import java.io.IOException;
-import java.io.Writer;
-import java.util.*;
-import java.util.function.Function;
-import java.util.function.Predicate;
-
-@SuppressWarnings("unused")
-public class RoseTree<T> {
- private final T value;
- private final Iterable<RoseTree<T>> children;
-
- public RoseTree(T value, Iterable<RoseTree<T>> children) {
- this.value = value;
- this.children = children;
- }
-
- public T getValue() {
- return value;
- }
-
- public Iterator<RoseTree<T>> getChildren() {
- return children.iterator();
- }
-
- public static <T> RoseTree<T> pure(T value) {
- return new RoseTree<>(value, Collections.emptyList());
- }
-
- public static <T> RoseTree<T> join(RoseTree<RoseTree<T>> tree) {
- return new RoseTree<>(
- tree.getValue().getValue(),
- () -> Iterators.concat(
- Iterators.mappingIterator(RoseTree::join, tree.children.iterator()),
- tree.getValue().children.iterator()));
- }
-
- private static <T> Iterator<RoseTree<T>[]> permutations(RoseTree<T>[] trees) {
- return Iterators.flatten(
- Iterators.rangeIterator(trees.length,
- index -> Iterators.mappingIterator(child -> {
- @SuppressWarnings("unchecked")
- RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[trees.length];
- for (int i = 0; i < trees.length; ++i) {
- result[i] = (i == index ? child : trees[i]);
- }
- return result;
- }, trees[index].getChildren())
- ));
- }
-
- private static <T> List<T> makeHeadList(RoseTree<T>[] trees) {
- List<T> heads = new ArrayList<>(trees.length);
- for (RoseTree<T> tree : trees) {
- heads.add(tree.getValue());
- }
- return heads;
- }
-
- public static <T, R> RoseTree<R> zip(Function<List<T>, R> fn, RoseTree<T>[] trees) {
- return new RoseTree<>(
- fn.apply(makeHeadList(trees)),
- () -> Iterators.mappingIterator(
- roses -> RoseTree.zip(fn, roses),
- RoseTree.permutations(trees)));
- }
-
- private static <T> Iterator<RoseTree<T>[]> removeEach(RoseTree<T>[] trees) {
- return Iterators.concat(
- Iterators.rangeIterator(trees.length, index -> {
- @SuppressWarnings("unchecked")
- RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[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 <T, R> RoseTree<R> shrink(Function<List<T>, R> fn, RoseTree<T>[] trees) {
- return new RoseTree<>(
- fn.apply(makeHeadList(trees)),
- () -> Iterators.mappingIterator(
- roses -> RoseTree.shrink(fn, roses),
- RoseTree.removeEach(trees)));
- }
-
- public <R> RoseTree<R> map(Function<T, R> f) {
- return new RoseTree<>(
- f.apply(this.value),
- () -> Iterators.mappingIterator(tree -> tree.map(f), this.children.iterator()));
- }
-
- public <R> RoseTree<R> flatMap(Function<T, RoseTree<R>> f) {
- return RoseTree.join(this.map(f));
- }
-
- public RoseTree<T> filter(Predicate<T> predicate) {
- if (predicate.test(this.getValue())) {
- return new RoseTree<>(
- this.getValue(),
- () -> Iterators.mappingIterator(tree -> tree.filter(predicate),
- Iterators.filteringIterator(
- tree -> predicate.test(tree.getValue()),
- this.getChildren())));
- } else {
- throw new IllegalArgumentException("Current value doesn't match predicate: whoops!");
- }
- }
-
- @SuppressWarnings("unused")
- public void print(Writer output) throws IOException {
- print(output, Object::toString);
- }
-
- @SuppressWarnings("unused")
- public void print(Writer output, Function<T, String> toString) throws IOException {
- output.write(toString.apply(this.getValue()));
- output.write('[');
- for (RoseTree<T> child : children) {
- child.print(output, toString);
- }
- output.write(']');
- output.flush();
- }
-}