From 1e0669e1b0cca6528baef08dd4bd149ded0cdc18 Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 29 Oct 2019 12:48:52 +0100 Subject: [PATCH] add `CCHeap.to_string` --- 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 5afc9d2d..42d2ba82 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -202,6 +202,10 @@ module type S = sig val to_tree : t -> elt ktree (** Return a [ktree] of the elements of the heap. *) + val to_string : ?sep:string -> (elt -> string) -> t -> string + (** Print the heap in a string + @since NEXT_RELEASE *) + val pp : ?sep:string -> elt printer -> t printer (** @since 0.16 Renamed from {!print} @since 2.0 *) @@ -404,6 +408,22 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct | E -> `Nil | N (_, x, l, r) -> `Node(x, [to_tree l; to_tree r]) + let to_string ?(sep=",") elt_to_string h = + to_list_sorted h + |> List.map elt_to_string + |> String.concat sep + + (*$Q + Q.(list int) (fun l -> \ + let h = H.of_list l in \ + (H.to_string string_of_int h) \ + = (List.sort Stdlib.compare l |> List.map string_of_int |> String.concat ",")) + Q.(list int) (fun l -> \ + let h = H.of_list l in \ + (H.to_string ~sep:" " string_of_int h) \ + = (List.sort Stdlib.compare l |> List.map string_of_int |> String.concat " ")) + *) + let pp ?(sep=",") pp_elt out h = let first=ref true in iter diff --git a/src/core/CCHeap.mli b/src/core/CCHeap.mli index 5cd427d9..52c4077c 100644 --- a/src/core/CCHeap.mli +++ b/src/core/CCHeap.mli @@ -142,6 +142,10 @@ module type S = sig val to_tree : t -> elt ktree (** Return a [ktree] of the elements of the heap. *) + val to_string : ?sep:string -> (elt -> string) -> t -> string + (** Print the heap in a string + @since NEXT_RELEASE *) + val pp : ?sep:string -> elt printer -> t printer (** Printer. Renamed from {!print} since 2.0