From e704020e3507fc5f781bc2db422a793355c72ff8 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 20 Jul 2016 17:48:15 +0200 Subject: [PATCH] additional functions in CCMultiSet --- src/data/CCMultiSet.ml | 51 ++++++++++++++++++++++++++++++++++++++++- src/data/CCMultiSet.mli | 12 ++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/data/CCMultiSet.ml b/src/data/CCMultiSet.ml index b9356cf7..c5c0bd3c 100644 --- a/src/data/CCMultiSet.ml +++ b/src/data/CCMultiSet.ml @@ -24,31 +24,53 @@ module type S = sig val remove : t -> elt -> t val add_mult : t -> elt -> int -> t + (** [add_mult set x n] adds [n] occurrences of [x] to [set] + @raise Invalid_argument if [n < 0] + @since 0.6 *) val remove_mult : t -> elt -> int -> t + (** [remove_mult set x n] removes at most [n] occurrences of [x] from [set] + @raise Invalid_argument if [n < 0] + @since 0.6 *) val update : t -> elt -> (int -> int) -> t + (** [update set x f] calls [f n] where [n] is the current multiplicity + of [x] in [set] ([0] to indicate its absence); the result of [f n] + is the new multiplicity of [x]. + @raise Invalid_argument if [f n < 0] + @since 0.6 *) val min : t -> elt + (** Minimal element w.r.t the total ordering on elements *) val max : t -> elt + (** Maximal element w.r.t the total ordering on elements *) val union : t -> t -> t + (** [union a b] contains as many occurrences of an element [x] + as [count a x + count b x]. *) val meet : t -> t -> t + (** [meet a b] is a multiset such that + [count (meet a b) x = max (count a x) (count b x)] *) val intersection : t -> t -> t + (** [intersection a b] is a multiset such that + [count (intersection a b) x = min (count a x) (count b x)] *) val diff : t -> t -> t + (** MultiSet difference. + [count (diff a b) x = max (count a x - count b x) 0] *) val contains : t -> t -> bool + (** [contains a x = (count m x > 0)] *) val compare : t -> t -> int val equal : t -> t -> bool val cardinal : t -> int - (** Number of distinct elements *) + (** Number of distinct elements *) val iter : t -> (int -> elt -> unit) -> unit @@ -61,6 +83,18 @@ module type S = sig val to_seq : t -> elt sequence val of_seq : elt sequence -> t + + val of_list_mult : (elt * int) list -> t + (** @since NEXT_RELEASE *) + + val to_list_mult : t -> (elt * int) list + (** @since NEXT_RELEASE *) + + val to_seq_mult : t -> (elt * int) sequence + (** @since NEXT_RELEASE *) + + val of_seq_mult : (elt * int) sequence -> t + (** @since NEXT_RELEASE *) end module Make(O : Set.OrderedType) = struct @@ -199,6 +233,21 @@ module Make(O : Set.OrderedType) = struct let m = ref empty in seq (fun x -> m := add !m x); !m + + let of_list_mult l = + List.fold_left + (fun s (x,i) -> add_mult s x i) + empty l + + let to_list_mult m = + fold m [] (fun acc n x -> (x,n) :: acc) + + let to_seq_mult m k = M.iter (fun x n -> k (x,n)) m + + let of_seq_mult seq = + let m = ref empty in + seq (fun (x,n) -> m := add_mult !m x n); + !m end (*$T diff --git a/src/data/CCMultiSet.mli b/src/data/CCMultiSet.mli index ef7174ce..4b83ee70 100644 --- a/src/data/CCMultiSet.mli +++ b/src/data/CCMultiSet.mli @@ -83,6 +83,18 @@ module type S = sig val to_seq : t -> elt sequence val of_seq : elt sequence -> t + + val of_list_mult : (elt * int) list -> t + (** @since NEXT_RELEASE *) + + val to_list_mult : t -> (elt * int) list + (** @since NEXT_RELEASE *) + + val to_seq_mult : t -> (elt * int) sequence + (** @since NEXT_RELEASE *) + + val of_seq_mult : (elt * int) sequence -> t + (** @since NEXT_RELEASE *) end module Make(O : Set.OrderedType) : S with type elt = O.t