Module Bag.Make
Parameters
X : sig ... end
Signature
type elt
= X.t
type t
The immutable type of bags.
val empty : t
The empty bag.
val is_empty : t -> bool
Test for emptiness.
val occ : elt -> t -> int
occ x b
is the number of occurrences ofx
in bagb
. It returns 0 whenx
is not inb
.
val mem : elt -> t -> bool
mem x b
checks whetherx
belongs tob
, i.e. has a multiplicty greater than 0.
val add : elt -> ?mult:int -> t -> t
add x ?mult b
returns a new bag where the multiplicity ofx
is increased bymult
(defaults to one). RaisesInvalid_argument
ifmult
is negative.
val update : elt -> (int -> int) -> t -> t
update x f b
returns a bag containing the same elements asb
, except for elementx
whose multiplicity isf (occ x b)
. RaisesInvalid_argument
iff
returns a negative value.
val remove : elt -> ?mult:int -> t -> t
remove x ?mult b
returns a new bag where the multiplicity ofx
is decreased bymult
(defaults to one). RaisesInvalid_argument
ifmult
is negative.
val merge : (elt -> int -> int -> int) -> t -> t -> t
merge f b1 b2
computes a bag whose elements are a subset of the elements ofb1
and ofb2
. The presence of each such element, and the corresponding multiplicity, is determined with the functionf
. In terms of theocc
operation, we haveocc x (merge f b1 b2) = f x (occ x b1) (occ x b2)
for any elementx
, provided thatf x 0 0 = 0
. RaisesInvalid_argument
iff
returns a negative value.
val cardinal : t -> int
cardinal b
is the sum of the multiplicities.
val elements : t -> (elt * int) list
Returns the list of all elements of the given bag. Each element is given with its multiplicity. The returned list is sorted in increasing order of elements with respect to the ordering over the type of the elements.
val min_elt : t -> elt * int
Returns the smallest element in the given bag (with respect to the ordering) with its multiplicity, or raises
Not_found
if the bag is empty.
val min_elt_opt : t -> (elt * int) option
Returns the smallest element of the given bag (with respect to the ordering) with its multiplicity, or
None
if the bag is empty.
val max_elt : t -> elt * int
Returns the largest element in the given bag (with respect to the ordering) with its multiplicity, or raises
Not_found
if the bag is empty.
val max_elt_opt : t -> (elt * int) option
Returns the largest element of the given bag (with respect to the ordering) with its multiplicity, or
None
if the bag is empty.
val choose : t -> elt * int
Returns one element of the given bag with its multiplicity, or raises
Not_found
if the bag is empty. Which binding is chosen is unspecified, but equal elements will be chosen for equal bags.
val choose_opt : t -> (elt * int) option
Returns one element of the given bag, or
None
if the set is empty. Which element is chosen is unspecified, but equal elements will be chosen for equal bags.
val union : t -> t -> t
union b1 b2
returns a new bagb
where, for all element x,occ x b = max (occ x b1) (occ x b2)
.
val sum : t -> t -> t
sum b1 b2
returns a new bagb
where, for all element x,occ x b = occ x b1 + occ x b2
.
val inter : t -> t -> t
inter b1 b2
returns a new bagb
where, for all element x,occ x b = min (occ x b1) (occ x b2)
.
val diff : t -> t -> t
diff b1 b2
returns a new bagb
where, for all element x,occ x b = max 0 (occ x b1 - occ x b2)
.
val included : t -> t -> bool
included b1 b2
returns true if and only if, for all element x,occ x b1 <= occ x b2
.
val iter : (elt -> int -> unit) -> t -> unit
iter f b
appliesf
to all elements in bagb
.f
receives the element as first argument, and its multiplicity as second argument. The elements are passed tof
in increasing order with respect to the ordering over the type of the elements.
val fold : (elt -> int -> 'a -> 'a) -> t -> 'a -> 'a
fold f b a
computes(f xN mN ... (f x1 m1 a)...)
, wherex1 ... xN
are the elements in bagb
(in increasing order), andm1 ... mN
are their multiplicities.
val for_all : (elt -> int -> bool) -> t -> bool
for_all p b
checks if all the elements of the bag satisfy the predicatep
.
val exists : (elt -> int -> bool) -> t -> bool
exists p b
checks if at least one element of the bag satisfies the predicatep
.
val filter : (elt -> int -> bool) -> t -> t
filter p b
returns the bag with all the elements inb
that satisfy predicatep
. Multiplicities are unchanged.
val partition : (elt -> int -> bool) -> t -> t * t
partition p b
returns a pair of bags(b1, b2)
, whereb1
contains all the elements ofb
that satisfy the predicatep
, andb2
is the bag with all the elements ofb
that do not satisfyp
.
val split : elt -> t -> t * int * t
split x b
returns a triple(l, m, r)
, wherel
is the bag with all the elements ofb
that are strictly less thanx
;r
is the bag with all the elements ofb
that are strictly greater thanx
;m
is the multiplicity ofx
inb
.
val find_first : (elt -> bool) -> t -> elt * int
find_first f b
, wheref
is a monotonically increasing function, returns the lowest elementx
ofb
such thatf x
, or raisesNot_found
if no such key exists.
val find_first_opt : (elt -> bool) -> t -> (elt * int) option
find_first_opt f b
, wheref
is a monotonically increasing function, returns an option containing the lowest elementx
ofb
such thatf x
, orNone
if no such key exists.
val find_last : (elt -> bool) -> t -> elt * int
find_last f b
, wheref
is a monotonically decreasing function, returns the largest elementx
ofb
such thatf x
, or raisesNot_found
if no such key exists.
val find_last_opt : (elt -> bool) -> t -> (elt * int) option
find_last_opt f b
, wheref
is a monotonically decreasing function, returns an option containing the largest elementx
ofm
such thatf x
, orNone
if no such key exists.
val map : (int -> int) -> t -> t
map f b
returns a bag with same elements asb
, where the multiplicitym
of each element ofb
has been updated by the result of the application off
tom
. The elements are passed tof
in increasing order with respect to the ordering over the type of the elements. RaisesInvalid_argument
iff
returns a negative value.
val mapi : (elt -> int -> int) -> t -> t
Same as
Bag
.map, but the function receives as arguments both the element and the associated multiplicity. RaisesInvalid_argument
iff
returns a negative value.
val equal : t -> t -> bool
equal b1 b2
tests whether the bagsb1
andb2
are equal, that is, contain equal elements with equal multiplicities.
Iterators
val to_seq : t -> (elt * int) Stdlib.Seq.t
Iterates on the whole bag, in ascending order of elements.
val to_seq_from : elt -> t -> (elt * int) Stdlib.Seq.t
to_seq_from x b
iterates on a subset ofb
, in ascending order of elements, from elementx
or above.