mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add printer to CCHashtbl
This commit is contained in:
parent
caaecda7f7
commit
ff6157771e
2 changed files with 38 additions and 0 deletions
|
|
@ -29,6 +29,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||
type 'a sequence = ('a -> unit) -> unit
|
||||
type 'a eq = 'a -> 'a -> bool
|
||||
type 'a hash = 'a -> int
|
||||
type 'a printer = Format.formatter -> 'a -> unit
|
||||
|
||||
(** {2 Polymorphic tables} *)
|
||||
|
||||
|
|
@ -70,6 +71,19 @@ let of_list l =
|
|||
List.iter (fun (k,v) -> Hashtbl.add tbl k v) l;
|
||||
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} *)
|
||||
|
||||
module type S = sig
|
||||
|
|
@ -106,6 +120,8 @@ module type S = sig
|
|||
|
||||
val of_list : (key * 'a) list -> 'a t
|
||||
(** From the given list of bindings, added in order *)
|
||||
|
||||
val print : key printer -> 'a printer -> 'a t printer
|
||||
end
|
||||
|
||||
module Make(X : Hashtbl.HashedType) = struct
|
||||
|
|
@ -143,6 +159,19 @@ module Make(X : Hashtbl.HashedType) = struct
|
|||
let tbl = create 32 in
|
||||
List.iter (fun (k,v) -> add tbl k v) l;
|
||||
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
|
||||
|
||||
(** {2 Default Table} *)
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||
type 'a sequence = ('a -> unit) -> unit
|
||||
type 'a eq = 'a -> 'a -> bool
|
||||
type 'a hash = 'a -> int
|
||||
type 'a printer = Format.formatter -> 'a -> unit
|
||||
|
||||
(** {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
|
||||
(** 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} *)
|
||||
|
||||
module type S = sig
|
||||
|
|
@ -103,6 +108,10 @@ module type S = sig
|
|||
|
||||
val of_list : (key * 'a) list -> 'a t
|
||||
(** 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
|
||||
|
||||
module Make(X : Hashtbl.HashedType) :
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue