Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions core/src/main/java/fj/Equal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fj.data.*;
import fj.data.hamt.BitSet;
import fj.data.hlist.HList;
import fj.data.optic.Traversal;
import fj.data.vector.V2;
import fj.data.vector.V3;
import fj.data.vector.V4;
Expand Down Expand Up @@ -466,6 +467,25 @@ public static <A> Equal<Zipper<A>> zipperEqual(final Equal<A> ea) {
}

/**
* An equal instance for the {@link TreeZipper} type.
*
* @param ea Equality across the elements of the tree zipper.
* @return An equal instance for the {@link TreeZipper} type.
*/
public static <A> Equal<TreeZipper<A>> treeZipperEqual(final Equal<A> ea) {
final Equal<Tree<A>> te = Equal.treeEqual(ea);
final Equal<Stream<Tree<A>>> st = streamEqual(Equal.treeEqual(ea));
final Equal<Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>> sp =
streamEqual(p3Equal(streamEqual(treeEqual(ea)), ea, streamEqual(treeEqual(ea))));
return equalDef((a1, a2) ->
te.eq(a1.focus(), a2.focus()) &&
st.eq(a1.lefts(), a2.lefts()) &&
st.eq(a1.rights(), a2.rights()) &&
sp.eq(a1.parents(), a2.parents())
);
}

/**
* An equal instance for the {@link Array} type.
*
* @param ea Equality across the elements of the array.
Expand Down
26 changes: 25 additions & 1 deletion core/src/main/java/fj/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,31 @@ public static <A> Hash<Zipper<A>> zipperHash(final Hash<A> ha) {
});
}

/**
/**
* A hash instance for the {@link TreeZipper} type.
*
* @param ha A hash for the elements of the tree zipper.
* @return A hash instance for the {@link TreeZipper} type.
*/
public static <A> Hash<TreeZipper<A>> treeZipperHash(final Hash<A> ha) {
Hash<Tree<A>> th = treeHash(ha);
Hash<Stream<Tree<A>>> sth = streamHash(treeHash(ha));
Hash<Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>> tsp =
streamHash(p3Hash(streamHash(treeHash(ha)), ha, streamHash(treeHash(ha))));
return hash(as -> {
final int p = 419;
int r = 239;

r = p * r + th.hash(as.focus());
r = p * r + sth.hash(as.lefts());
r = p * r + sth.hash(as.rights());
r = p * r + tsp.hash(as.parents());

return r;
});
}

/**
* A hash instance for the {@link Tree} type.
*
* @param ha A hash for the elements of the tree.
Expand Down
25 changes: 22 additions & 3 deletions core/src/main/java/fj/data/TreeZipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ private TreeZipper(final Tree<A> tree,
this.parents = parents;
}

/**
@Override
public final boolean equals(Object other) {
return Equal.equals0(TreeZipper.class, this, other, () -> Equal.treeZipperEqual(Equal.anyEqual()));
}

@Override
public final int hashCode() {
return Hash.treeZipperHash(Hash.<A>anyHash()).hash(this);
}

/**
* Creates a new tree zipper given a currently selected tree, a forest on the left, a forest on the right,
* and a stream of parent contexts.
*
Expand Down Expand Up @@ -341,8 +351,17 @@ public Stream<Tree<A>> lefts() {
* @return the right siblings of the currently focused node.
*/
public Stream<Tree<A>> rights() {
return rights;
}
return rights;
}

/**
* Returns the parents of the currently focused node.
*
* @return the parents of the currently focused node.
*/
public Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>> parents() {
return parents;
}

/**
* Indicates whether the current node is at the top of the tree.
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/fj/FFunctionsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fj;

import fj.data.Tree;
import fj.data.TreeZipper;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
Expand All @@ -22,4 +24,20 @@ public void testApply() {
F<Integer, Integer> f1 = F2Functions.f(f2, 2);
assertThat(F1Functions.f(f1, 1).f(), is(36));
}

@Test
public void testTreeK() {
final Tree<Integer> t1 = F1Functions.<Integer, Integer>treeK(Function.identity()).f(1);
final Tree<Integer> t2 = F1Functions.<Integer, Integer>treeK(Function.identity()).f(2);
assertThat(F1Functions.<Integer, Integer>mapTree(i -> i + 1).f(t1),
is(F1Functions.<Integer, Integer>mapTree(i -> i * 1).f(t2)));
}

@Test
public void testTreeZipperK() {
final TreeZipper<Integer> tz1 = F1Functions.<Integer, Integer>treeZipperK(Function.identity()).f(1);
final TreeZipper<Integer> tz2 = F1Functions.<Integer, Integer>treeZipperK(Function.identity()).f(2);
assertThat(F1Functions.<Integer, Integer>mapTreeZipper(i -> i + 1).f(tz1),
is(F1Functions.<Integer, Integer>mapTreeZipper(i -> i * 1).f(tz2)));
}
}
32 changes: 32 additions & 0 deletions core/src/test/java/fj/data/TreeZipperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fj.data;

import org.junit.Test;

import static fj.data.Option.none;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class TreeZipperTest {
@Test
public void testDelete() {
final Tree<Integer> t = Tree.node(1, List.single(Tree.leaf(2)));
final TreeZipper<Integer> tz = TreeZipper.fromTree(t);
assertThat(tz.delete(), is(none()));
}

@Test
public void testDeleteForest() {
final Tree<Integer> t = Tree.node(1, List.single(Tree.leaf(2)));
final TreeZipper<Integer> tz = TreeZipper.fromForest(Stream.single(t)).some();
assertThat(tz.delete(), is(none()));

}

@Test
public void testHash() {
final Tree<Integer> t = Tree.node(1, List.single(Tree.leaf(2)));
final TreeZipper<Integer> tz1 = TreeZipper.fromForest(Stream.single(t)).some();
final TreeZipper<Integer> tz2 = TreeZipper.fromForest(Stream.single(t)).some();
assertThat(tz1.hashCode(), is(tz2.hashCode()));
}
}