Skip to content
Snippets Groups Projects
Commit 4dbe8096 authored by Guyslain Naves's avatar Guyslain Naves
Browse files

Improved the way lazy lists are generated.

Add implementation using () -> 'a instead of 'a Lazy.t (no need for memo)
parent 46acf767
Branches master
No related tags found
No related merge requests found
...@@ -62,37 +62,25 @@ struct ...@@ -62,37 +62,25 @@ struct
| Cons of 'elt * 'elt lazy_list | Cons of 'elt * 'elt lazy_list
and 'elt lazy_list = 'elt lazy_cell Lazy.t and 'elt lazy_list = 'elt lazy_cell Lazy.t
let cons elt lazy_list = lazy (Cons (elt, lazy_list))
let rec append llist1 llist2 =
lazy (match Lazy.force llist1 with
| Nil -> Lazy.force llist2
| Cons (first, tail) -> Cons (first, append tail llist2)
)
let lazy_increasing_elements tree =
let rec lazy_increasing_elements tree = let rec loop accu = function
lazy ( | Empty ->
match tree with accu
| Empty -> Nil
| Node node -> | Node node ->
Lazy.force loop (Cons (node.key, lazy (loop accu node.right))) node.left
(append in
(lazy_increasing_elements node.left) lazy (loop Nil tree)
(cons node.key (lazy_increasing_elements node.right))
)
)
let rec lazy_decreasing_elements tree = let rec lazy_decreasing_elements tree =
lazy ( let rec loop accu = function
match tree with | Empty ->
| Empty -> Nil accu
| Node node -> | Node node ->
Lazy.force loop (Cons (node.key, lazy (loop accu node.left))) node.right
(append in
(lazy_decreasing_elements node.right) lazy (loop Nil tree)
(cons node.key (lazy_decreasing_elements node.left))
)
)
let rec find_pair sum increasing decreasing = let rec find_pair sum increasing decreasing =
...@@ -116,6 +104,58 @@ struct ...@@ -116,6 +104,58 @@ struct
end end
module UsingSimiliLazyLists : PairFinder =
struct
let name = "simili lazy lists"
type 'elt lazy_cell =
| Nil
| Cons of 'elt * 'elt lazy_list
and 'elt lazy_list = unit -> 'elt lazy_cell
let lazy_increasing_elements tree =
let rec loop accu = function
| Empty ->
accu
| Node node ->
loop (Cons (node.key, (fun () -> loop accu node.right))) node.left
in
fun () -> loop Nil tree
let rec lazy_decreasing_elements tree =
let rec loop accu = function
| Empty ->
accu
| Node node ->
loop (Cons (node.key, (fun () -> loop accu node.left))) node.right
in
fun () -> loop Nil tree
let rec find_pair sum increasing decreasing =
match increasing (), decreasing () with
| Cons (elt1,tail1), Cons (elt2,tail2) when elt2 < elt1 ->
None
| Cons (elt1,tail1), Cons (elt2,tail2) when elt1 + elt2 = sum ->
Some (elt1,elt2)
| Cons (elt1,tail1), Cons (elt2,tail2) when elt1 + elt2 > sum ->
find_pair sum increasing tail2
| Cons (elt1, tail1), Cons (elt2,tail2) (* when elt1 + elt2 < sum *) ->
find_pair sum tail1 decreasing
| _ -> None
let solve sum tree =
find_pair sum
(lazy_increasing_elements tree)
(lazy_decreasing_elements tree)
end
module UsingGenerators : PairFinder = module UsingGenerators : PairFinder =
struct struct
let name = "generators" let name = "generators"
...@@ -330,9 +370,9 @@ struct ...@@ -330,9 +370,9 @@ struct
list_init (fun _ -> mini + Random.int (maxi - mini)) n list_init (fun _ -> mini + Random.int (maxi - mini)) n
let satisfiable_instance ~ratio ~n = let satisfiable_instance ~ratio ~n =
assert (ratio > 0. && ratio < 0.5); assert (ratio > 0. && ratio <= 0.5);
let nb_sides = int_of_float (ratio *. float n) in let nb_sides = int_of_float (ratio *. float n) in
let nb_middle = n - 2 * nb_sides in let nb_middle = max 0 (n - 2 * nb_sides) in
let below = let below =
fully_random_list ~mini:0 ~maxi:n nb_sides fully_random_list ~mini:0 ~maxi:n nb_sides
|> List.map (( * ) 4) |> List.map (( * ) 4)
...@@ -357,6 +397,7 @@ end ...@@ -357,6 +397,7 @@ end
let implementations = let implementations =
[ (module UsingLists : PairFinder); [ (module UsingLists : PairFinder);
(module UsingLazyLists : PairFinder); (module UsingLazyLists : PairFinder);
(module UsingSimiliLazyLists : PairFinder);
(module UsingGenerators : PairFinder); (module UsingGenerators : PairFinder);
(module UsingZippers : PairFinder) (module UsingZippers : PairFinder)
] ]
...@@ -385,7 +426,7 @@ let benchmarks ~ratio ~size ~nb_instances = ...@@ -385,7 +426,7 @@ let benchmarks ~ratio ~size ~nb_instances =
let main = let main =
let size = 10000 in let size = 10000 in
let ratio = 0.1 in let ratio = 0.2 in
let nb_instances = 2 in let nb_instances = 2 in
benchmarks ~ratio ~size ~nb_instances benchmarks ~ratio ~size ~nb_instances
|> Core_bench.Bench.make_command |> Core_bench.Bench.make_command
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment