feat: add pretty printer to CCMultiSet

This commit is contained in:
Fardale 2024-11-05 18:25:51 +01:00
parent 9bef25b6e2
commit 9cfedad7ba
2 changed files with 41 additions and 0 deletions

View file

@ -3,6 +3,7 @@
(** {1 Multiset} *) (** {1 Multiset} *)
type 'a iter = ('a -> unit) -> unit type 'a iter = ('a -> unit) -> unit
type 'a printer = Format.formatter -> 'a -> unit
let max_int = max let max_int = max
let min_int = min let min_int = min
@ -89,6 +90,15 @@ module type S = sig
val of_iter_mult : (elt * int) iter -> t val of_iter_mult : (elt * int) iter -> t
(** @since 0.19 *) (** @since 0.19 *)
val pp :
?pp_start:unit printer ->
?pp_stop:unit printer ->
?pp_sep:unit printer ->
elt printer ->
t printer
(** Print the multiset.
@since NEXT_RELEASE *)
end end
module Make (O : Set.OrderedType) = struct module Make (O : Set.OrderedType) = struct
@ -233,4 +243,25 @@ module Make (O : Set.OrderedType) = struct
let m = ref empty in let m = ref empty in
seq (fun (x, n) -> m := add_mult !m x n); seq (fun (x, n) -> m := add_mult !m x n);
!m !m
let pp ?(pp_start = fun _ () -> ()) ?(pp_stop = fun _ () -> ())
?(pp_sep = fun fmt () -> Format.fprintf fmt ",@ ") pp_x fmt m =
let rec pp_mult ?(first = true) x n =
if n = 0 then ()
else (
if not first then pp_sep fmt ();
pp_x fmt x;
pp_mult ~first:false x (n-1)
)
in
pp_start fmt ();
let first = ref true in
iter m
(fun x n ->
if !first then
first := false
else
pp_sep fmt ();
pp_mult n x);
pp_stop fmt ()
end end

View file

@ -3,6 +3,7 @@
(** Multiset *) (** Multiset *)
type 'a iter = ('a -> unit) -> unit type 'a iter = ('a -> unit) -> unit
type 'a printer = Format.formatter -> 'a -> unit
module type S = sig module type S = sig
type elt type elt
@ -86,6 +87,15 @@ module type S = sig
val of_iter_mult : (elt * int) iter -> t val of_iter_mult : (elt * int) iter -> t
(** @since 0.19 *) (** @since 0.19 *)
val pp :
?pp_start:unit printer ->
?pp_stop:unit printer ->
?pp_sep:unit printer ->
elt printer ->
t printer
(** Print the multiset.
@since NEXT_RELEASE *)
end end
module Make (O : Set.OrderedType) : S with type elt = O.t module Make (O : Set.OrderedType) : S with type elt = O.t