Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PairFinding
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Guyslain Naves
PairFinding
Commits
4dbe8096
Commit
4dbe8096
authored
7 years ago
by
Guyslain Naves
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
treeIterators.ml
+71
-30
71 additions, 30 deletions
treeIterators.ml
with
71 additions
and
30 deletions
treeIterators.ml
+
71
−
30
View file @
4dbe8096
...
@@ -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
=
l
azy
(
l
et
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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment