Gen.lexico for lexicographic comparison

This commit is contained in:
Simon Cruanes 2013-03-25 14:37:27 +01:00
parent bc38851de6
commit f992f279bc
3 changed files with 19 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.*.swp .*.swp
.*.swo
_build _build
*.native *.native
.session .session

15
gen.ml
View file

@ -398,6 +398,21 @@ let max ?(lt=fun x y -> x < y) enum =
let first = try gen () with EOG -> raise Not_found in 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 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} *) (** {2 Complex combinators} *)
(** Pick elements fairly in each sub-enum *) (** Pick elements fairly in each sub-enum *)

View file

@ -193,6 +193,9 @@ val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a
(** Maximum element *) (** Maximum element *)
val lexico : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int
(** Lexicographic comparison of generators *)
(** {2 Complex combinators} *) (** {2 Complex combinators} *)
val merge : 'a t t -> 'a t val merge : 'a t t -> 'a t