diff --git a/src/core/CCHeap.ml b/src/core/CCHeap.ml index 6201c6ee..d537f151 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -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 type elt type t @@ -146,6 +155,10 @@ module type S = sig 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 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_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 | `Nil -> h | `Cons (x, l') -> diff --git a/src/core/CCHeap.mli b/src/core/CCHeap.mli index e573cdc1..346a4e29 100644 --- a/src/core/CCHeap.mli +++ b/src/core/CCHeap.mli @@ -92,6 +92,10 @@ module type S = sig 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 of_klist : elt klist -> t