add CCHeap.to_seq_sorted

This commit is contained in:
Simon Cruanes 2017-02-23 21:01:32 +01:00
parent 12b38f4c31
commit 8627838faf
2 changed files with 24 additions and 0 deletions

View file

@ -69,6 +69,15 @@ end
) )
*) *)
(*$QR
Q.(list_of_size Gen.(return 1_000) int) (fun l ->
(* put elements into a heap *)
let h = H.of_seq (Sequence.of_list l) in
let l' = H.to_seq_sorted h |> Sequence.to_list in
is_sorted l'
)
*)
module type S = sig module type S = sig
type elt type elt
type t type t
@ -146,6 +155,10 @@ module type S = sig
val to_seq : t -> elt sequence val to_seq : t -> elt sequence
val to_seq_sorted : t -> elt sequence
(** Iterate on the elements, in increasing order
@since NEXT_RELEASE *)
val add_klist : t -> elt klist -> t (** @since 0.16 *) val add_klist : t -> elt klist -> t (** @since 0.16 *)
val of_klist : elt klist -> t val of_klist : elt klist -> t
@ -273,6 +286,13 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct
let to_seq h k = iter k h let to_seq h k = iter k h
let to_seq_sorted heap =
let rec recurse h k = match take h with
| None -> ()
| Some (h',x) -> k x; recurse h' k
in
fun k -> recurse heap k
let rec add_klist h l = match l() with let rec add_klist h l = match l() with
| `Nil -> h | `Nil -> h
| `Cons (x, l') -> | `Cons (x, l') ->

View file

@ -92,6 +92,10 @@ module type S = sig
val to_seq : t -> elt sequence val to_seq : t -> elt sequence
val to_seq_sorted : t -> elt sequence
(** Iterate on the elements, in increasing order
@since NEXT_RELEASE *)
val add_klist : t -> elt klist -> t (** @since 0.16 *) val add_klist : t -> elt klist -> t (** @since 0.16 *)
val of_klist : elt klist -> t val of_klist : elt klist -> t