mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
Gen.lexico for lexicographic comparison
This commit is contained in:
parent
bc38851de6
commit
f992f279bc
3 changed files with 19 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,4 +1,5 @@
|
|||
.*.swp
|
||||
.*.swo
|
||||
_build
|
||||
*.native
|
||||
.session
|
||||
|
|
|
|||
15
gen.ml
15
gen.ml
|
|
@ -398,6 +398,21 @@ let max ?(lt=fun x y -> x < y) enum =
|
|||
let first = try gen () with EOG -> raise Not_found in
|
||||
Gen.fold (fun max x -> if lt max x then x else max) first gen
|
||||
|
||||
let lexico ?(cmp=compare) e1 e2 =
|
||||
let gen1 = e1() in
|
||||
let gen2 = e2() in
|
||||
let rec lexico () =
|
||||
let x1 = try Some (gen1 ()) with EOG -> None in
|
||||
let x2 = try Some (gen2 ()) with EOG -> None in
|
||||
match x1, x2 with
|
||||
| None, None -> 0
|
||||
| Some x1, Some x2 ->
|
||||
let c = cmp x1 x2 in
|
||||
if c <> 0 then c else lexico ()
|
||||
| Some _, None -> 1
|
||||
| None, Some _ -> -1
|
||||
in lexico ()
|
||||
|
||||
(** {2 Complex combinators} *)
|
||||
|
||||
(** Pick elements fairly in each sub-enum *)
|
||||
|
|
|
|||
3
gen.mli
3
gen.mli
|
|
@ -193,6 +193,9 @@ val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a
|
|||
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a
|
||||
(** Maximum element *)
|
||||
|
||||
val lexico : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int
|
||||
(** Lexicographic comparison of generators *)
|
||||
|
||||
(** {2 Complex combinators} *)
|
||||
|
||||
val merge : 'a t t -> 'a t
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue