This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbintree.ml
76 lines (67 loc) · 1.86 KB
/
bintree.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module T = Domainslib.Task
type tree = Leaf of int
| Node of int * tree * tree
let rec buildtree_seq n =
if n <= 0
then Leaf 1
else
let x = buildtree_seq (n-1) in
let y = buildtree_seq (n-1) in
Node (n,x,y)
let rec buildtree_par pool cutoff n =
if n <= cutoff
then buildtree_seq n
else if n <= 0
then Leaf 1
else
let x_f = T.async pool (fun _ -> buildtree_par pool cutoff (n-1)) in
let y = buildtree_par pool cutoff (n-1) in
Node (n, T.await pool x_f, y)
let rec add1tree_seq tr =
match tr with
Leaf n -> Leaf (n+1)
| Node (n,x,y) ->
let x1 = add1tree_seq x in
let y1 = add1tree_seq y in
Node (n, x1, y1)
let rec add1tree_par pool cutoff tr =
match tr with
Leaf n -> Leaf (n+1)
| Node (n,x,y) ->
if n <= cutoff
then let x1 = add1tree_seq x in
let y1 = add1tree_seq y in
Node (n+1, x1, y1)
else let x1_f = T.async pool (fun _ -> add1tree_par pool cutoff x) in
let y1 = add1tree_par pool cutoff y in
Node (n, T.await pool x1_f, y1)
let rec sumtree_seq tr =
match tr with
Leaf n -> n
| Node (n,x,y) -> sumtree_seq x + sumtree_seq y
let rec sumtree_par pool cutoff tr =
match tr with
Leaf n -> n
| Node (n,x,y) ->
if n <= cutoff
then sumtree_seq tr
else
let n1_f = T.async pool (fun _ -> sumtree_par pool cutoff x) in
let n2 = sumtree_par pool cutoff y in
T.await pool n1_f + n2
let rec buildfib_seq n =
if n <= 0
then Leaf (Fib.fib 20)
else
let x = buildfib_seq (n-1) in
let y = buildfib_seq (n-1) in
Node (n,x,y)
let rec buildfib_par pool cutoff n =
if n <= cutoff
then buildfib_seq n
else if n <= 0
then Leaf (Fib.fib 20)
else
let x_f = T.async pool (fun _ -> buildfib_par pool cutoff (n-1)) in
let y = buildfib_par pool cutoff (n-1) in
Node (n, T.await pool x_f, y)