Module Multiset.Make

Parameters

module Ord : OrderedType

Signature

Types and Values

type elt = Ord.t

The type of the multiset elements.

type t

The type of multisets.

val empty : t

The empty set.

Comparison

val cmp : elt -> elt -> int

The comparison function for elt's.

val compare : t -> t -> int

compare is the version of compare for multisets.

Support

val support : t -> elt list

(support t) is the list of all the distinct (under Ord.compare) elements of the multiset t.

Note that each of the elements in the result is an arbitrary exemplar of its equivalent repetitions.

support is equivalent to (to_list >> sort_uniq Ord.compare) but is faster.

val support_size : t -> int

(support_size) is (len << support) but is faster.

Sizes

See also support_size.

val cardinal : t -> int

(cardinal t) is the sum of the multiplicities of all the elements of t.

val size : t -> int

size is cardinal.

val multiplicity : elt -> t -> int

(multiplicity elt t) is the number of repetitions of elt in t.

val count : elt -> t -> int

count is multiplicity.

Predicates

val is_empty : t -> bool

(is_empty t) is true iff t is empty.

val mem : elt -> t -> bool

(mem x t) is true iff x is a member of t

val disjoint : t -> t -> bool

(disjoint a b) is true iff a and b have no elements in common.

val equal : t -> t -> bool

(equal a b) is true iff both multisets contain the same elements, and the same repetitions of elements.

val subset : t -> t -> bool

(subset a b) is true iff all the distinct elements of a are contained in b, and the multiplicity of each element of a is <= the multiplicity of the corresponding element of b.

val for_all : (elt -> bool) -> t -> bool

(for_all p t) is true iff all elements of t satisfy the predicate p.

val exists : (elt -> bool) -> t -> bool

(exists p t) is true iff at least one element of t satisfies the predicate p.

Creating Multisets

See also of_seq and add_seq.

val singleton : elt -> t

(singleton elt) is the multiset that contains only elt.

val of_list : elt list -> t

(of_list xs) is the multiset containing containing all the elements of xs.

val split : elt -> t -> t * bool * t

(split x t) is a triple (l, present, r), where l is the multiset of elements of t that are strictly less than x; r is the multiset of elements of t that are strictly greater than x; present is false if t contains no element equal to x, or true if t contains an element equal to x.

If you also need the repetitions of x, you can call (find x t).

Adding Elements

val add : elt -> t -> t

(add elt t) is the multiset t with elt added.

Invariant: (multiplicity elt (add elt t)) = (1 + multiplicity elt t)

val addwith : ('a -> elt) -> 'a -> t -> t

(addwith f) is (fun f x t -> add (f x) t).

val addwhen : (elt -> bool) -> elt -> t -> t

(addwhen p x t) is (add x t) when p x = true and otherwise is t.

Removing Elements

val remove : elt -> t -> t

(remove elt t) is the multiset that contains all the elements of t excepting elt.

All repetitions of elt are removed.

Invariants:

  • (mem x (remove x t)) = false
  • (multiplicity x (remove x t)) = 0
val replace : elt -> elt list -> t -> t

(replace elt reps t) is t with all the repetitions of elt replaced by reps.

Functionals

val iter : (elt list -> unit) -> t -> unit

(iter f t) iterates f across each distinct element of t; the repetitions of each element are given to f as a list.

val flatiter : (elt -> unit) -> t -> unit

(flatiter f t) iterates f over all the repetitions of all the elements of t.

flatiter f is equivalent to (iter (List.iter f)).

val map : (elt -> elt) -> t -> t

(map f t) is a multiset where all repetitions of all elements have been replaced by the result of applying f to those elements.

val flatfold : (elt -> 'b -> 'b) -> t -> 'b -> 'b

(flatfold f t init) computes (f xn ... (f x2 (f x1 init))...), where x1 ... xn are the repetitions of all elements of t, in increasing order.

val fold : (elt list -> 'b -> 'b) -> t -> 'b -> 'b

(fold f t init) is like flatfold except that f is called once for each distinct distinct element of t, and f receives the repetitions of an element as a list.

val filter : (elt -> bool) -> t -> t

(filter p t) is the multiset of all elements that satisfy the predicate p.

p is applied to each repetition of each distinct element.

val partition : (elt -> bool) -> t -> t * t

(partition p t) returns a pair of multisets (t1, t2), where t1 is the multiset of all the elements of t that satisfy the predicate p, and t2 is the multiset of all the elements of t that do not satisfy p.

Set Theoretic Functions

val union : t -> t -> t

(union s t) is the maximum or lowest common multiple of multisets s and t.

See sum for what seems to me a more practical function.

val sum : t -> t -> t

(sum s t) is the disjoint union of s and t.

The sum contains the union of the distinct elements of s and t, and the repetitions of each element is the union of its repetitions in the two original sets..

Invariants:

  • (support (sum s t)) = List.union (support s) (support t)
  • (sort Ord.compare (elements (sum s t))) = sort Ord.compare (elements s @ elements t)
val inter : t -> t -> t

(inter s t) is the infimum or greatest common divisor of multisets s and t.

val diff : t -> t -> t

(diff s t) is the multiset that contains the distinct elements of s that are not in t.

Extracting Elements

val find : elt -> t -> elt list

(find elt t) is the list of the repetitions of elt in t.

Invariant: (len (default [] (find elt) t)) = (multiplicity elt t)

  • raises Not_found

    if is_empty t

val find_opt : elt -> t -> elt list option

(find_opt elt t) is (catch ~this:Not_found (find elt) t).

val elements : t -> elt list

(elements t) are all the repetitions of all the distinct elements of t, in ascending order of Ord.compare.

Invariant:

  • len (support t) <= len (elements t)
val to_list : t -> elt list

(to_list t) is elements.

val repetitions : t -> elt list list

(repetitions t) is the list of all repetitions of each distinct element, in ascending order of Ord.compare, in the form of a list of lists.

Invariants:

  • (len << repetitions) = (len << support)
  • (repetitions >> List.map len >> List.sum) = cardinal
  • flatten (repetitions t) = elements t
val choose : t -> elt list

(choose t) is the set of repetitions of one distinct element of the given multiset. Which element is chosen is unspecified, but equal elements will be chosen for equal multisets.

  • raises Not_found

    if is_empty t

val choose_opt : t -> elt list option

(choose t) is the set of repetitions of one distinct element of the given multiset; if the multiset is empty, the result is None. Which element is chosen is unspecified, but equal elements will be chosen for equal multisets.

val find_first : (elt -> bool) -> t -> elt list

(find_first f t) where f is a monotonically increasing function, returns the repetitions of the lowest element e of t such that (f e).

  • raises Not_found

    if no such element exists.

val find_first_opt : (elt -> bool) -> t -> elt list option

(find_first_opt f) is (catch (find_first_opt f)).

val find_last : (elt -> bool) -> t -> elt list

(find_last f t) where f is a monotonically decreasing function, returns the repetitions of the highest element e of t such that (f e).

  • raises Not_found

    if no such element exists.

val find_last_opt : (elt -> bool) -> t -> elt list option

(find_last_opt f) is (catch (find_last_opt f)).

val min_elt : t -> elt list

(min_elt_opt t) is the smallest element of t, per Ord.compare.

  • raises Not_found

    if no such element exists.

val min_elt_opt : t -> elt list option

(min_elt f) is (catch (min_elt f)).

val max_elt : t -> elt list

(max_elt_opt t) is the largest element of t, per Ord.compare.

  • raises Not_found

    if no such element exists.

val max_elt_opt : t -> elt list option

(max_elt_opt f) is (catch (max_elt f)).

Sequences

val to_seq : t -> elt list Seq.t

(to_seq t) is the Seq.t equivalent to (repetitions t).

val to_seq_from : elt -> t -> elt list Seq.t

(to_seq_from elt t) is (to_seq t) but containing only those elements from elt or above.

val of_seq : elt Seq.t -> t

(of_seq seq) is the multiset containing all the elements of seq.

val add_seq : elt Seq.t -> t -> t

(add_seq seq t) is t with the addition of all the elements in seq.

Development Utils

val to_string : ?sep:string -> (elt -> string) -> t -> string

(to_string ?(sep=", ") f t) returns a string representation of the multiset t, where the string representation of each elt is given by f.

val print : ?sep:string -> (elt -> string) -> t -> unit

(print ?sep f t) is (to_string ?sep f t |> print_endline).

val random : ?size:(unit -> int) -> (unit -> elt) -> unit -> t

(random ?size r ()) is a random multiset of size (size ()) (default: < 100), and each element is given by (r ()).