mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-11 13:38:32 -05:00
Add CCHeap.Make_from_compare (#225)
Allow to pass modules implementing `compare` without implementing `leq`
This commit is contained in:
parent
1a4919af29
commit
ba633d5d3c
2 changed files with 38 additions and 0 deletions
|
|
@ -15,6 +15,15 @@ module type PARTIAL_ORD = sig
|
||||||
(** [leq x y] shall return [true] iff [x] is lower or equal to [y]. *)
|
(** [leq x y] shall return [true] iff [x] is lower or equal to [y]. *)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module type TOTAL_ORD = sig
|
||||||
|
type t
|
||||||
|
val compare : t -> t -> int
|
||||||
|
(** [compare a b] shall return
|
||||||
|
a negative value if [a] is smaller than [b],
|
||||||
|
[0] if [a] and [b] are equal or
|
||||||
|
a positive value if [a] is greater than [b] *)
|
||||||
|
end
|
||||||
|
|
||||||
(*$inject
|
(*$inject
|
||||||
module H = CCHeap.Make(struct
|
module H = CCHeap.Make(struct
|
||||||
type t = int
|
type t = int
|
||||||
|
|
@ -403,3 +412,18 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct
|
||||||
pp_elt out x)
|
pp_elt out x)
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Make_from_compare(E : TOTAL_ORD) =
|
||||||
|
Make(struct
|
||||||
|
type t = E.t
|
||||||
|
let leq a b = E.compare a b <= 0
|
||||||
|
end)
|
||||||
|
|
||||||
|
(*$QR
|
||||||
|
Q.(list_of_size Gen.(return 1_000) int) (fun l ->
|
||||||
|
let module H' = Make_from_compare(Int) in
|
||||||
|
let h = H'.of_list l in
|
||||||
|
let l' = H'.to_list_sorted h in
|
||||||
|
is_sorted l'
|
||||||
|
)
|
||||||
|
*)
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,15 @@ module type PARTIAL_ORD = sig
|
||||||
(** [leq x y] shall return [true] iff [x] is lower or equal to [y]. *)
|
(** [leq x y] shall return [true] iff [x] is lower or equal to [y]. *)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module type TOTAL_ORD = sig
|
||||||
|
type t
|
||||||
|
val compare : t -> t -> int
|
||||||
|
(** [compare a b] shall return
|
||||||
|
a negative value if [a] is smaller than [b],
|
||||||
|
[0] if [a] and [b] are equal or
|
||||||
|
a positive value if [a] is greater than [b] *)
|
||||||
|
end
|
||||||
|
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type elt
|
type elt
|
||||||
type t
|
type t
|
||||||
|
|
@ -138,3 +147,8 @@ module type S = sig
|
||||||
end
|
end
|
||||||
|
|
||||||
module Make(E : PARTIAL_ORD) : S with type elt = E.t
|
module Make(E : PARTIAL_ORD) : S with type elt = E.t
|
||||||
|
|
||||||
|
(** A convenient version Make that take a TOTAL_ORD instead
|
||||||
|
It allow to directly pass modules that implement [compare]
|
||||||
|
without implementing [leq] *)
|
||||||
|
module Make_from_compare(E : TOTAL_ORD) : S with type elt = E.t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue