From ac6900976a18a4585a3579ed80fff368f9844eb7 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 24 Feb 2016 21:46:01 +0100 Subject: [PATCH] add a printer into CCHeap --- src/core/CCHeap.ml | 12 ++++++++++++ src/core/CCHeap.mli | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/core/CCHeap.ml b/src/core/CCHeap.ml index 3cca9304..fb47a491 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -5,6 +5,7 @@ type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option +type 'a printer = Format.formatter -> 'a -> unit type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a ktree = unit -> [`Nil | `Node of 'a * 'a ktree list] @@ -127,6 +128,9 @@ module type S = sig val to_gen : t -> elt gen val to_tree : t -> elt ktree + + val print : ?sep:string -> elt printer -> t printer + (** @since NEXT_RELEASE *) end module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct @@ -273,4 +277,12 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct let rec to_tree h () = match h with | E -> `Nil | N (_, x, l, r) -> `Node(x, [to_tree l; to_tree r]) + + let print ?(sep=",") pp_elt out h = + let first=ref true in + iter + (fun x -> + if !first then first := false else Format.fprintf out "%s@," sep; + pp_elt out x) + h end diff --git a/src/core/CCHeap.mli b/src/core/CCHeap.mli index c8c2a076..2466ced5 100644 --- a/src/core/CCHeap.mli +++ b/src/core/CCHeap.mli @@ -7,6 +7,7 @@ type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a ktree = unit -> [`Nil | `Node of 'a * 'a ktree list] +type 'a printer = Format.formatter -> 'a -> unit module type PARTIAL_ORD = sig type t @@ -78,6 +79,9 @@ module type S = sig val to_gen : t -> elt gen val to_tree : t -> elt ktree + + val print : ?sep:string -> elt printer -> t printer + (** @since NEXT_RELEASE *) end module Make(E : PARTIAL_ORD) : S with type elt = E.t