add printer to CCHashtbl

This commit is contained in:
Simon Cruanes 2015-08-11 22:54:12 +02:00
parent caaecda7f7
commit ff6157771e
2 changed files with 38 additions and 0 deletions

View file

@ -29,6 +29,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a eq = 'a -> 'a -> bool type 'a eq = 'a -> 'a -> bool
type 'a hash = 'a -> int type 'a hash = 'a -> int
type 'a printer = Format.formatter -> 'a -> unit
(** {2 Polymorphic tables} *) (** {2 Polymorphic tables} *)
@ -70,6 +71,19 @@ let of_list l =
List.iter (fun (k,v) -> Hashtbl.add tbl k v) l; List.iter (fun (k,v) -> Hashtbl.add tbl k v) l;
tbl tbl
let print pp_k pp_v fmt m =
Format.fprintf fmt "@[<hov2>tbl {@,";
let first = ref true in
Hashtbl.iter
(fun k v ->
if !first then first := false else Format.pp_print_string fmt ", ";
pp_k fmt k;
Format.pp_print_string fmt " -> ";
pp_v fmt v;
Format.pp_print_cut fmt ()
) m;
Format.fprintf fmt "}@]"
(** {2 Functor} *) (** {2 Functor} *)
module type S = sig module type S = sig
@ -106,6 +120,8 @@ module type S = sig
val of_list : (key * 'a) list -> 'a t val of_list : (key * 'a) list -> 'a t
(** From the given list of bindings, added in order *) (** From the given list of bindings, added in order *)
val print : key printer -> 'a printer -> 'a t printer
end end
module Make(X : Hashtbl.HashedType) = struct module Make(X : Hashtbl.HashedType) = struct
@ -143,6 +159,19 @@ module Make(X : Hashtbl.HashedType) = struct
let tbl = create 32 in let tbl = create 32 in
List.iter (fun (k,v) -> add tbl k v) l; List.iter (fun (k,v) -> add tbl k v) l;
tbl tbl
let print pp_k pp_v fmt m =
Format.pp_print_string fmt "@[<hov2>tbl {@,";
let first = ref true in
iter
(fun k v ->
if !first then first := false else Format.pp_print_string fmt ", ";
pp_k fmt k;
Format.pp_print_string fmt " -> ";
pp_v fmt v;
Format.pp_print_cut fmt ()
) m;
Format.pp_print_string fmt "}@]"
end end
(** {2 Default Table} *) (** {2 Default Table} *)

View file

@ -32,6 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a eq = 'a -> 'a -> bool type 'a eq = 'a -> 'a -> bool
type 'a hash = 'a -> int type 'a hash = 'a -> int
type 'a printer = Format.formatter -> 'a -> unit
(** {2 Polymorphic tables} *) (** {2 Polymorphic tables} *)
@ -67,6 +68,10 @@ val to_list : ('a,'b) Hashtbl.t -> ('a * 'b) list
val of_list : ('a * 'b) list -> ('a,'b) Hashtbl.t val of_list : ('a * 'b) list -> ('a,'b) Hashtbl.t
(** From the given list of bindings, added in order *) (** From the given list of bindings, added in order *)
val print : 'a printer -> 'b printer -> ('a, 'b) Hashtbl.t printer
(** Printer for table
@since NEXT_RELEASE *)
(** {2 Functor} *) (** {2 Functor} *)
module type S = sig module type S = sig
@ -103,6 +108,10 @@ module type S = sig
val of_list : (key * 'a) list -> 'a t val of_list : (key * 'a) list -> 'a t
(** From the given list of bindings, added in order *) (** From the given list of bindings, added in order *)
val print : key printer -> 'a printer -> 'a t printer
(** Printer for tables
@since NEXT_RELEASE *)
end end
module Make(X : Hashtbl.HashedType) : module Make(X : Hashtbl.HashedType) :