additional functions in CCMultiSet

This commit is contained in:
Simon Cruanes 2016-07-20 17:48:15 +02:00
parent 6784608496
commit e704020e35
2 changed files with 62 additions and 1 deletions

View file

@ -24,31 +24,53 @@ module type S = sig
val remove : t -> elt -> t val remove : t -> elt -> t
val add_mult : t -> elt -> int -> 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 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 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 val min : t -> elt
(** Minimal element w.r.t the total ordering on elements *)
val max : t -> elt val max : t -> elt
(** Maximal element w.r.t the total ordering on elements *)
val union : t -> t -> t 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 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 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 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 val contains : t -> t -> bool
(** [contains a x = (count m x > 0)] *)
val compare : t -> t -> int val compare : t -> t -> int
val equal : t -> t -> bool val equal : t -> t -> bool
val cardinal : t -> int val cardinal : t -> int
(** Number of distinct elements *) (** Number of distinct elements *)
val iter : t -> (int -> elt -> unit) -> unit val iter : t -> (int -> elt -> unit) -> unit
@ -61,6 +83,18 @@ module type S = sig
val to_seq : t -> elt sequence val to_seq : t -> elt sequence
val of_seq : elt sequence -> t 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 end
module Make(O : Set.OrderedType) = struct module Make(O : Set.OrderedType) = struct
@ -199,6 +233,21 @@ module Make(O : Set.OrderedType) = struct
let m = ref empty in let m = ref empty in
seq (fun x -> m := add !m x); seq (fun x -> m := add !m x);
!m !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 end
(*$T (*$T

View file

@ -83,6 +83,18 @@ module type S = sig
val to_seq : t -> elt sequence val to_seq : t -> elt sequence
val of_seq : elt sequence -> t 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 end
module Make(O : Set.OrderedType) : S with type elt = O.t module Make(O : Set.OrderedType) : S with type elt = O.t