From 8627838faf92b907d86a63853631e9ce09a027ff Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 23 Feb 2017 21:01:32 +0100 Subject: [PATCH] add `CCHeap.to_seq_sorted` --- src/core/CCHeap.ml | 20 ++++++++++++++++++++ src/core/CCHeap.mli | 4 ++++ 2 files changed, 24 insertions(+) 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