Skip to content

Commit f01991b

Browse files
committed
Generic comparison based lattice node
1 parent 40d1ba8 commit f01991b

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

convex-core/src/main/java/convex/core/data/prim/AInteger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public static AInteger parse(Object o) {
8989
* @return Number of bytes
9090
*/
9191
public abstract int byteLength();
92+
9293

9394
@Override
9495
public ANumeric add(ANumeric b) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package convex.core.lattice;
2+
3+
import java.util.Comparator;
4+
5+
import convex.core.data.ACell;
6+
7+
/**
8+
* Lattice node implementing a comparison function.
9+
*
10+
* Suitable for lattice values where "greater" values replace previous ones,
11+
* e.g. taking a value with a more recent timestamp
12+
*
13+
* @param <V> Type of lattice values
14+
*/
15+
public class CompareNode<V extends ACell> extends ALattice<V> {
16+
17+
private Comparator<V> comparator;
18+
19+
private CompareNode(Comparator<V> comparator) {
20+
this.comparator = comparator;
21+
}
22+
23+
public static <V extends ACell> CompareNode<V> create(Comparator<V> comparator) {
24+
return new CompareNode<>(comparator);
25+
}
26+
27+
@Override
28+
public V merge(V ownValue, V otherValue) {
29+
if (otherValue==null) return ownValue;
30+
if (ownValue==null) return otherValue;
31+
32+
// retain own value if other value is lesser or equal
33+
if (comparator.compare(ownValue, otherValue)>=0) return ownValue;
34+
35+
return otherValue;
36+
}
37+
38+
@Override
39+
public V zero() {
40+
return null;
41+
}
42+
43+
}

convex-core/src/main/java/convex/core/lattice/KeyedNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static KeyedNode create(Object... keysAndValues) {
5757
ALattice<ACell> lattice=(ALattice<ACell>) lattices.get(i);
5858
Keyword key=keys.get(i);
5959

60+
if (!otherValue.containsKey(key)) continue;
61+
6062
ACell a=ownValue.get(key);
6163
ACell b=otherValue.get(key);
6264

convex-core/src/main/java/convex/core/lattice/MapNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static <K extends ACell,V extends ACell> MapNode<K,V> create(ALattice<V>
2626
@Override
2727
public AHashMap<K,V> merge(AHashMap<K, V> ownValue, AHashMap<K, V> otherValue) {
2828
if (otherValue==null) return ownValue;
29+
if (ownValue==null) return otherValue;
2930
return ownValue.mergeDifferences(otherValue, mergeFunction);
3031
}
3132

convex-core/src/test/java/convex/core/lattice/LatticeTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class LatticeTest {
4444

4545
doLatticeTest(KeyedNode.create("foo",MaxNode.create(),"bar",SetNode.create()),Maps.of(Keywords.FOO,CVMLong.ONE), Maps.of(Keywords.BAR,Sets.of(1,2)));
4646

47+
doLatticeTest(CompareNode.create((AInteger a,AInteger b)->a.compareTo(b)),CVMLong.ONE, CVMLong.MAX_VALUE);
4748
}
4849

4950

@@ -59,6 +60,11 @@ private <V extends ACell> void doLatticeTest(ALattice<V> lattice, V value, V val
5960
// Merges with zero
6061
assertEquals(value,lattice.merge(zero,value));
6162
assertEquals(value,lattice.merge(value,zero));
63+
64+
// Null merge
65+
assertEquals(value,lattice.merge(value,null));
66+
assertEquals(value,lattice.merge(null,value));
67+
6268

6369
assertEquals(value2,lattice.merge(zero,value2));
6470
assertEquals(value2,lattice.merge(value2,zero));

0 commit comments

Comments
 (0)