Add java ocaml and swift tests
This commit is contained in:
22
koka_bench/java/CMakeLists.txt
Normal file
22
koka_bench/java/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
set(java_wrapper "../../java_wrapper.sh")
|
||||
|
||||
set(sources rbtree.java nqueens.java deriv.java cfold.java)
|
||||
foreach (source IN LISTS sources)
|
||||
|
||||
get_filename_component(name "${source}" NAME_WE)
|
||||
|
||||
set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/out/bench")
|
||||
set(out_path "${out_dir}/${name}")
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${out_path}
|
||||
COMMAND ${java_wrapper} "${CMAKE_CURRENT_SOURCE_DIR}/${source}" ${out_dir} ${name}
|
||||
DEPENDS ${source}
|
||||
VERBATIM)
|
||||
|
||||
add_custom_target(update-${name} ALL DEPENDS "${out_path}")
|
||||
add_executable(${name}-exe IMPORTED)
|
||||
set_target_properties(${name}-exe PROPERTIES IMPORTED_LOCATION "${out_path}")
|
||||
endforeach ()
|
||||
|
||||
156
koka_bench/java/cfold.java
Normal file
156
koka_bench/java/cfold.java
Normal file
@@ -0,0 +1,156 @@
|
||||
interface XExpr {
|
||||
}
|
||||
|
||||
final class ValXExpr implements XExpr {
|
||||
long value;
|
||||
ValXExpr(long i) {
|
||||
value = i;
|
||||
}
|
||||
}
|
||||
|
||||
final class VarXExpr implements XExpr {
|
||||
long name;
|
||||
VarXExpr(long i) {
|
||||
name = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
final class AddXExpr implements XExpr {
|
||||
XExpr left;
|
||||
XExpr right;
|
||||
AddXExpr(XExpr l, XExpr r) {
|
||||
left = l;
|
||||
right = r;
|
||||
}
|
||||
}
|
||||
|
||||
final class MulXExpr implements XExpr {
|
||||
XExpr left;
|
||||
XExpr right;
|
||||
MulXExpr(XExpr l, XExpr r) {
|
||||
left = l;
|
||||
right = r;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class cfold {
|
||||
static XExpr mk_expr( long n, long v ) {
|
||||
if (n == 0) {
|
||||
return (v==0 ? new VarXExpr(1) : new ValXExpr(v));
|
||||
}
|
||||
else {
|
||||
return new AddXExpr( mk_expr(n-1, v+1), mk_expr(n - 1, v == 0 ? 0 : v - 1));
|
||||
}
|
||||
}
|
||||
|
||||
static XExpr append_add( XExpr e1, XExpr e2 ) {
|
||||
if (e1 instanceof AddXExpr a) {
|
||||
return new AddXExpr(a.left, append_add(a.right, e2));
|
||||
}
|
||||
else {
|
||||
return new AddXExpr(e1,e2);
|
||||
}
|
||||
}
|
||||
|
||||
static XExpr tail_append_add( XExpr e1, XExpr e2 ) {
|
||||
AddXExpr hd = null;
|
||||
AddXExpr acc = null;
|
||||
while(e1 instanceof AddXExpr x) {
|
||||
if (acc==null) {
|
||||
hd = acc = new AddXExpr(x.left,null);
|
||||
}
|
||||
else {
|
||||
AddXExpr y = new AddXExpr(x.left,null);
|
||||
acc.right = y;
|
||||
acc = y;
|
||||
}
|
||||
e1 = x.right;
|
||||
}
|
||||
if (acc==null) hd = acc = new AddXExpr(e1,e2);
|
||||
else acc.right = new AddXExpr(e1,e2);
|
||||
return hd;
|
||||
}
|
||||
|
||||
static XExpr append_mul( XExpr e1, XExpr e2 ) {
|
||||
if (e1 instanceof MulXExpr a) {
|
||||
return new MulXExpr(a.left, append_mul(a.right, e2));
|
||||
}
|
||||
else {
|
||||
return new MulXExpr(e1,e2);
|
||||
}
|
||||
}
|
||||
|
||||
static XExpr reassoc( XExpr e ) {
|
||||
if (e instanceof AddXExpr a) {
|
||||
return append_add( reassoc(a.left), reassoc(a.right) );
|
||||
}
|
||||
else if (e instanceof MulXExpr m) {
|
||||
return append_mul( reassoc(m.left), reassoc(m.right) );
|
||||
}
|
||||
else return e;
|
||||
}
|
||||
|
||||
static XExpr const_folding( XExpr e ) {
|
||||
if (e instanceof AddXExpr x) {
|
||||
XExpr e1 = const_folding(x.left);
|
||||
XExpr e2 = const_folding(x.right);
|
||||
if (e1 instanceof ValXExpr a && e2 instanceof ValXExpr b) {
|
||||
return new ValXExpr(a.value + b.value );
|
||||
}
|
||||
else if (e1 instanceof ValXExpr a && e2 instanceof AddXExpr b && b.right instanceof ValXExpr br) {
|
||||
return new AddXExpr( new ValXExpr(a.value + br.value), b.left );
|
||||
}
|
||||
else if (e1 instanceof ValXExpr a && e2 instanceof AddXExpr b && b.left instanceof ValXExpr bl) {
|
||||
return new AddXExpr( new ValXExpr(a.value + bl.value), b.right );
|
||||
}
|
||||
else {
|
||||
return new AddXExpr(e1,e2);
|
||||
}
|
||||
}
|
||||
else if (e instanceof MulXExpr x) {
|
||||
XExpr e1 = const_folding(x.left);
|
||||
XExpr e2 = const_folding(x.right);
|
||||
if (e1 instanceof ValXExpr a && e2 instanceof ValXExpr b) {
|
||||
return new ValXExpr(a.value * b.value );
|
||||
}
|
||||
else if (e1 instanceof ValXExpr a && e2 instanceof MulXExpr b && b.right instanceof ValXExpr br) {
|
||||
return new MulXExpr( new ValXExpr(a.value * br.value), b.left );
|
||||
}
|
||||
else if (e1 instanceof ValXExpr a && e2 instanceof MulXExpr b && b.left instanceof ValXExpr bl) {
|
||||
return new MulXExpr( new ValXExpr(a.value * bl.value), b.right );
|
||||
}
|
||||
else {
|
||||
return new MulXExpr(e1,e2);
|
||||
}
|
||||
}
|
||||
else return e;
|
||||
}
|
||||
|
||||
static long eval( XExpr e ) {
|
||||
if (e instanceof VarXExpr x) {
|
||||
return 0;
|
||||
}
|
||||
else if (e instanceof ValXExpr x) {
|
||||
return x.value;
|
||||
}
|
||||
else if (e instanceof AddXExpr x) {
|
||||
return eval(x.left) + eval(x.right);
|
||||
}
|
||||
else if (e instanceof MulXExpr x) {
|
||||
return eval(x.left) * eval(x.right);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
XExpr e = mk_expr(Integer.parseInt(args[0]),1);
|
||||
long v1 = eval(e);
|
||||
long v2 = eval(const_folding(reassoc(e)));
|
||||
System.out.println( v1 + ", " + v2 );
|
||||
}
|
||||
}
|
||||
235
koka_bench/java/deriv.java
Normal file
235
koka_bench/java/deriv.java
Normal file
@@ -0,0 +1,235 @@
|
||||
interface Expr {
|
||||
}
|
||||
|
||||
final class ValExpr implements Expr {
|
||||
long value;
|
||||
ValExpr(long i) {
|
||||
value = i;
|
||||
}
|
||||
}
|
||||
|
||||
final class VarExpr implements Expr {
|
||||
String name;
|
||||
VarExpr(String s) {
|
||||
name = s;
|
||||
}
|
||||
}
|
||||
|
||||
final class LnExpr implements Expr {
|
||||
Expr expr;
|
||||
LnExpr(Expr e) {
|
||||
expr = e;
|
||||
}
|
||||
}
|
||||
|
||||
final class AddExpr implements Expr {
|
||||
Expr left;
|
||||
Expr right;
|
||||
AddExpr(Expr l, Expr r) {
|
||||
left = l;
|
||||
right = r;
|
||||
}
|
||||
}
|
||||
|
||||
final class MulExpr implements Expr {
|
||||
Expr left;
|
||||
Expr right;
|
||||
MulExpr(Expr l, Expr r) {
|
||||
left = l;
|
||||
right = r;
|
||||
}
|
||||
}
|
||||
|
||||
final class PowExpr implements Expr {
|
||||
Expr left;
|
||||
Expr right;
|
||||
PowExpr(Expr l, Expr r) {
|
||||
left = l;
|
||||
right = r;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class deriv {
|
||||
static long pown(long x, long n) {
|
||||
if (n==0) return 1;
|
||||
else if (n == 1) return x;
|
||||
else {
|
||||
long y = pown(x, n/2);
|
||||
return (y * y * (n%2 == 0 ? 1 : x));
|
||||
}
|
||||
}
|
||||
|
||||
static Expr add( Expr x, Expr y ) {
|
||||
if (x instanceof ValExpr a && y instanceof ValExpr b) {
|
||||
return new ValExpr( a.value + b.value );
|
||||
}
|
||||
else if (x instanceof ValExpr a && a.value == 0) {
|
||||
return y;
|
||||
}
|
||||
else if (y instanceof ValExpr b && b.value == 0) {
|
||||
return x;
|
||||
}
|
||||
else if (y instanceof ValExpr b) {
|
||||
return add(y,x);
|
||||
}
|
||||
else if (x instanceof ValExpr a && y instanceof AddExpr b && b.left instanceof ValExpr bl) {
|
||||
return add(new ValExpr(a.value + bl.value), b.right);
|
||||
}
|
||||
else if (y instanceof AddExpr b && b.left instanceof ValExpr) {
|
||||
return add(b.left, add(x,b.right));
|
||||
}
|
||||
else if (x instanceof AddExpr a) {
|
||||
return add(a.left, add(a.right,y));
|
||||
}
|
||||
else {
|
||||
return new AddExpr(x,y);
|
||||
}
|
||||
}
|
||||
|
||||
static Expr mul( Expr x, Expr y ) {
|
||||
if (x instanceof ValExpr a && y instanceof ValExpr b) {
|
||||
return new ValExpr( a.value * b.value );
|
||||
}
|
||||
else if (x instanceof ValExpr a && a.value == 0) {
|
||||
return x;
|
||||
}
|
||||
else if (y instanceof ValExpr b && b.value == 0) {
|
||||
return y;
|
||||
}
|
||||
else if (x instanceof ValExpr a && a.value == 1) {
|
||||
return y;
|
||||
}
|
||||
else if (y instanceof ValExpr b && b.value == 1) {
|
||||
return x;
|
||||
}
|
||||
else if (y instanceof ValExpr b) {
|
||||
return mul(y,x);
|
||||
}
|
||||
else if (x instanceof ValExpr a && y instanceof MulExpr b && b.left instanceof ValExpr bl) {
|
||||
return mul(new ValExpr(a.value * bl.value), b.right);
|
||||
}
|
||||
else if (y instanceof MulExpr b && b.left instanceof MulExpr) {
|
||||
return mul(b.left, mul(x,b.right));
|
||||
}
|
||||
else if (x instanceof MulExpr a) {
|
||||
return mul(a.left, mul(a.right,y));
|
||||
}
|
||||
else {
|
||||
return new MulExpr(x,y);
|
||||
}
|
||||
}
|
||||
|
||||
static Expr powr( Expr x, Expr y ) {
|
||||
if (x instanceof ValExpr a && y instanceof ValExpr b) {
|
||||
return new ValExpr(pown(a.value,b.value));
|
||||
}
|
||||
else if (y instanceof ValExpr b && b.value == 0) {
|
||||
return new ValExpr(1);
|
||||
}
|
||||
else if (y instanceof ValExpr b && b.value == 1) {
|
||||
return x;
|
||||
}
|
||||
else if (x instanceof ValExpr a && a.value == 0) {
|
||||
return new ValExpr(0);
|
||||
}
|
||||
else {
|
||||
return new PowExpr(x,y);
|
||||
}
|
||||
}
|
||||
|
||||
static Expr ln( Expr x ) {
|
||||
if (x instanceof ValExpr a && a.value == 1) {
|
||||
return new ValExpr(0);
|
||||
}
|
||||
else {
|
||||
return new LnExpr(x);
|
||||
}
|
||||
}
|
||||
|
||||
static Expr d( String x, Expr e ) {
|
||||
if (e instanceof ValExpr) {
|
||||
return new ValExpr(0);
|
||||
}
|
||||
else if (e instanceof VarExpr a) {
|
||||
return new ValExpr(a.name == x ? 1 : 0);
|
||||
}
|
||||
else if (e instanceof AddExpr a) {
|
||||
Expr f = a.left;
|
||||
Expr g = a.right;
|
||||
return add(d(x,f),d(x,g));
|
||||
}
|
||||
else if (e instanceof MulExpr a) {
|
||||
Expr f = a.left;
|
||||
Expr g = a.right;
|
||||
return add(mul(f,d(x,g)),mul(g,d(x,f)));
|
||||
}
|
||||
else if (e instanceof PowExpr a) {
|
||||
Expr f = a.left;
|
||||
Expr g = a.right;
|
||||
return mul(powr(f,g),add(mul(mul(g,d(x,f)),powr(f,new ValExpr(-1))),mul(ln(f),d(x,g))));
|
||||
}
|
||||
else if (e instanceof LnExpr a) {
|
||||
Expr f = a.expr;
|
||||
return mul(d(x,f),powr(f,new ValExpr(-1)));
|
||||
}
|
||||
else {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
static long count( Expr e ) {
|
||||
if (e instanceof ValExpr) {
|
||||
return 1;
|
||||
}
|
||||
else if (e instanceof VarExpr) {
|
||||
return 1;
|
||||
}
|
||||
else if (e instanceof AddExpr a) {
|
||||
Expr f = a.left;
|
||||
Expr g = a.right;
|
||||
return count(f) + count(g);
|
||||
}
|
||||
else if (e instanceof MulExpr a) {
|
||||
Expr f = a.left;
|
||||
Expr g = a.right;
|
||||
return count(f) + count(g);
|
||||
}
|
||||
else if (e instanceof PowExpr a) {
|
||||
Expr f = a.left;
|
||||
Expr g = a.right;
|
||||
return count(f) + count(g);
|
||||
}
|
||||
else if (e instanceof LnExpr a) {
|
||||
Expr f = a.expr;
|
||||
return count(f);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static Expr deriv( long i, Expr e) {
|
||||
Expr f = d("x",e);
|
||||
System.out.println( (i+1) + " count: " + count(f) );
|
||||
return f;
|
||||
}
|
||||
|
||||
static Expr nest( long s, Expr e) {
|
||||
long n = s;
|
||||
while(n > 0) {
|
||||
e = deriv(s - n, e);
|
||||
n--;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
Expr x = new VarExpr("x");
|
||||
Expr e = powr(x,x);
|
||||
nest(Integer.parseInt(args[0]),e);
|
||||
System.out.println( "done" );
|
||||
}
|
||||
}
|
||||
78
koka_bench/java/nqueens.java
Normal file
78
koka_bench/java/nqueens.java
Normal file
@@ -0,0 +1,78 @@
|
||||
class List<T> {
|
||||
T head;
|
||||
List<T> tail;
|
||||
|
||||
List(T h, List<T> t) {
|
||||
head = h;
|
||||
tail = t;
|
||||
}
|
||||
|
||||
static <T> int len( List<T> xs ) {
|
||||
int n = 0;
|
||||
while(xs != null) {
|
||||
n++;
|
||||
xs = xs.tail;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static<T> List<T> Cons( T h, List<T> t ) {
|
||||
return new List<T>(h,t);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class nqueens {
|
||||
static boolean safe( int queen, List<Integer> xs ) {
|
||||
int diag = 1;
|
||||
while(xs != null) {
|
||||
int q = xs.head;
|
||||
if (queen == q || queen == (q + diag) || queen == (q - diag)) {
|
||||
return false;
|
||||
}
|
||||
diag++;
|
||||
xs = xs.tail;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static List<List<Integer>> appendSafe( int k, List<Integer> soln, List<List<Integer>> solns ) {
|
||||
List<List<Integer>> acc = solns;
|
||||
while(k > 0) {
|
||||
if (safe(k,soln)) {
|
||||
acc = List.Cons(List.Cons(k,soln),acc);
|
||||
}
|
||||
k--;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
static List<List<Integer>> extend( int n, List<List<Integer>> solns ) {
|
||||
List<List<Integer>> acc = null;
|
||||
List<List<Integer>> cur = solns;
|
||||
while(cur != null) {
|
||||
acc = appendSafe(n, cur.head, acc);
|
||||
cur = cur.tail;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
static List<List<Integer>> findSolutions( int n ) {
|
||||
int k = 0;
|
||||
List<List<Integer>> acc = List.Cons(null,null);
|
||||
while(k < n) {
|
||||
acc = extend(n,acc);
|
||||
k++;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
|
||||
static int nqueens(int n) {
|
||||
return List.len(findSolutions(n));
|
||||
}
|
||||
public static void main(String args[])
|
||||
{
|
||||
System.out.println( nqueens(Integer.parseInt(args[0])) );
|
||||
}
|
||||
}
|
||||
159
koka_bench/java/rbtree.java
Normal file
159
koka_bench/java/rbtree.java
Normal file
@@ -0,0 +1,159 @@
|
||||
|
||||
enum Color {
|
||||
Red,
|
||||
Black
|
||||
}
|
||||
|
||||
|
||||
interface FoldFun {
|
||||
int Apply(int k, boolean v, int acc);
|
||||
}
|
||||
|
||||
class Tree {
|
||||
Color color;
|
||||
Tree left;
|
||||
int key;
|
||||
boolean val;
|
||||
Tree right;
|
||||
|
||||
Tree( Color c, Tree l, int k, boolean v, Tree r) {
|
||||
color = c;
|
||||
left = l;
|
||||
key = k;
|
||||
val = v;
|
||||
right = r;
|
||||
}
|
||||
|
||||
static Tree Node( Color c, Tree l, int k, boolean v, Tree r) {
|
||||
return new Tree(c,l,k,v,r);
|
||||
}
|
||||
|
||||
static boolean isRed( Tree t ) {
|
||||
return (t != null && t.color == Color.Red);
|
||||
}
|
||||
|
||||
static Tree balanceRight( int kv, boolean vv, Tree t, Tree n ) {
|
||||
if (n == null) {
|
||||
return null;
|
||||
}
|
||||
else if (n.left != null && n.left.color == Color.Red) {
|
||||
//case let .Node(_, .Node(.Red, l, kx, vx, r1), ky, vy, r2):
|
||||
// return .Node(.Red, .Node(.Black, l, kx, vx, r1), ky, vy, .Node(.Black, r2, kv, vv, t))
|
||||
Tree l = n.left;
|
||||
return Node( Color.Red, Node( Color.Black, l.left, l.key, l.val, l.right), n.key, n.val, Node(Color.Black, n.right, kv, vv, t));
|
||||
}
|
||||
else if (n.right != null && n.right.color == Color.Red) {
|
||||
//case let .Node(_, l1, ky, vy, .Node(.Red, l2, kx, vx, r)):
|
||||
// return .Node(.Red, .Node(.Black, l1, ky, vy, l2), kx, vx, .Node(.Black, r, kv, vv, t))
|
||||
Tree r = n.right;
|
||||
return Node( Color.Red, Node( Color.Black, n.left, n.key, n.val, r.left), r.key, r.val, Node(Color.Black, r.right, kv, vv, t));
|
||||
}
|
||||
else {
|
||||
//case let .Node(_, l, ky, vy, r):
|
||||
// return .Node(.Black, .Node(.Red, l, ky, vy, r), kv, vv, t)
|
||||
return Node(Color.Black, Node(Color.Red, n.left, n.key, n.val, n.right), kv, vv, t);
|
||||
}
|
||||
}
|
||||
|
||||
static Tree balanceLeft( Tree t, int kv, boolean vv, Tree n ) {
|
||||
if (n == null) {
|
||||
return null;
|
||||
}
|
||||
else if (n.left != null && n.left.color == Color.Red) {
|
||||
//case let .Node(_, .Node(.Red, l, kx1, vx1, r1), ky, vy, r2):
|
||||
// return .Node(.Red, .Node(.Black, t, kv, vv, l), kx1, vx1, .Node(.Black, r1, ky, vy, r2))
|
||||
Tree l = n.left;
|
||||
return Node( Color.Red, Node( Color.Black, t, kv, vv, l.left), l.key, l.val, Node(Color.Black, l.right, n.key, n.val, n.right));
|
||||
}
|
||||
else if (n.right != null && n.right.color == Color.Red) {
|
||||
//case let .Node(_, l1, ky, vy, .Node(.Red, l2, kx2, vx2, r2)):
|
||||
// return .Node(.Red, .Node(.Black, t, kv, vv, l1), ky, vy, .Node(.Black, l2, kx2, vx2, r2))
|
||||
Tree r = n.right;
|
||||
return Node( Color.Red, Node( Color.Black, t, kv, vv, n.left), n.key, n.val, Node(Color.Black, r.left, r.key, r.val, r.right));
|
||||
}
|
||||
else {
|
||||
//case let .Node (_, l, ky, vy, r):
|
||||
// return .Node(.Black, t, kv, vv, .Node(.Red, l, ky, vy, r))
|
||||
return Node(Color.Black, t, kv, vv, Node(Color.Red, n.left, n.key, n.val, n.right));
|
||||
}
|
||||
}
|
||||
|
||||
static Tree ins(Tree t, int kx, boolean vx ) {
|
||||
if (t==null) {
|
||||
return Node(Color.Red, null, kx, vx, null);
|
||||
}
|
||||
else if (t.color == Color.Red) {
|
||||
//case let .Node(.Red, a, ky, vy, b):
|
||||
if (kx < t.key) {
|
||||
return Node(Color.Red, ins(t.left, kx, vx), t.key, t.val, t.right);
|
||||
} else if (t.key == kx) {
|
||||
return Node(Color.Red, t.left, kx, vx, t.right);
|
||||
} else {
|
||||
return Node(Color.Red, t.left, t.key, t.val, ins(t.right, kx, vx));
|
||||
}
|
||||
}
|
||||
else { // t.color == Black
|
||||
if (kx < t.key) {
|
||||
if (isRed(t.left)) {
|
||||
return balanceRight(t.key, t.val, t.right, ins(t.left, kx, vx));
|
||||
} else {
|
||||
return Node(Color.Black, ins(t.left, kx, vx), t.key, t.val, t.right);
|
||||
}
|
||||
} else if (kx == t.key) {
|
||||
return Node(Color.Black, t.left, kx, vx, t.right);
|
||||
} else {
|
||||
if (isRed(t.right)) {
|
||||
return balanceLeft(t.left, t.key, t.val, ins(t.right, kx, vx));
|
||||
} else {
|
||||
return Node(Color.Black, t.left, t.key, t.val, ins(t.right, kx, vx));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Tree setBlack( Tree t ) {
|
||||
if (t == null) return t;
|
||||
return Node(Color.Black, t.left, t.key, t.val, t.right);
|
||||
}
|
||||
|
||||
static Tree insert (Tree t, int k, boolean v) {
|
||||
if (isRed(t)) {
|
||||
return setBlack(ins(t, k, v));
|
||||
} else {
|
||||
return ins(t, k, v);
|
||||
}
|
||||
}
|
||||
|
||||
static int Fold( FoldFun f, Tree t, int acc ) {
|
||||
while(t != null) {
|
||||
acc = Fold(f,t.left,acc);
|
||||
acc = f.Apply(t.key,t.val,acc);
|
||||
t = t.right;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class rbtree
|
||||
{
|
||||
static Tree mkMap( int n ) {
|
||||
Tree t = null;
|
||||
while(n > 0) {
|
||||
n--;
|
||||
t = Tree.insert(t, n, (n%10)==0);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
static int Test(int n ) {
|
||||
Tree t = mkMap(n);
|
||||
return Tree.Fold( (k,v,acc) -> { return (v ? acc + 1 : acc); }, t, 0);
|
||||
}
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
System.out.println( Test(Integer.parseInt(args[0])) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user