Skip to content

Commit f0fa9f3

Browse files
committed
Add ACursor accumulate functions
1 parent dfd6549 commit f0fa9f3

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package convex.lattice;
22

3+
import java.util.function.BinaryOperator;
34
import java.util.function.UnaryOperator;
45

56
import convex.core.data.ACell;
@@ -46,9 +47,12 @@ public void set(Object o) {
4647

4748
public abstract V getAndUpdate(UnaryOperator<V> updateFunction);
4849

49-
5050
public abstract V updateAndGet(UnaryOperator<V> updateFunction);
5151

52+
public abstract V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction);
53+
54+
public abstract V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction);
55+
5256
public String toString() {
5357
V v=get();
5458
if (v==null) return "nil";

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.lattice;
22

33
import java.util.concurrent.atomic.AtomicReference;
4+
import java.util.function.BinaryOperator;
45
import java.util.function.UnaryOperator;
56

67
import convex.core.data.ACell;
@@ -50,4 +51,14 @@ public V getAndUpdate(UnaryOperator<V> updateFunction) {
5051
public V updateAndGet(UnaryOperator<V> updateFunction) {
5152
return value.updateAndGet(updateFunction);
5253
}
54+
55+
@Override
56+
public V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction) {
57+
return value.getAndAccumulate(x,accumulatorFunction);
58+
}
59+
60+
@Override
61+
public V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction) {
62+
return value.accumulateAndGet(x,accumulatorFunction);
63+
}
5364
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ public class CursorTest {
3030
assertEquals(TWO,root.getAndUpdate(a->a.inc())); // value is now 3
3131
assertCVMEquals(4,root.updateAndGet(a->a.inc())); // value is now 4;
3232

33+
assertCVMEquals(4,root.getAndAccumulate(CVMLong.ONE,(a,b)->a.add(b))); // value is now 5;
34+
assertCVMEquals(7,root.accumulateAndGet(TWO,(a,b)->a.add(b))); // value is now 7;
35+
3336
}
3437
}

0 commit comments

Comments
 (0)