Module Map.Make
Functor building an implementation of the map structure given a totally ordered type.
Parameters
Signature
type key
= Ord.t
The type of the map keys.
val empty : 'a t
The empty map.
val is_empty : 'a t -> bool
Test whether a map is empty or not.
val mem : key -> 'a t -> bool
mem x m
returnstrue
ifm
contains a binding forx
, andfalse
otherwise.
val add : key -> 'a -> 'a t -> 'a t
add x y m
returns a map containing the same bindings asm
, plus a binding ofx
toy
. Ifx
was already bound inm
to a value that is physically equal toy
,m
is returned unchanged (the result of the function is then physically equal tom
). Otherwise, the previous binding ofx
inm
disappears.- before 4.03
Physical equality was not ensured.
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
update x f m
returns a map containing the same bindings asm
, except for the binding ofx
. Depending on the value ofy
wherey
isf (find_opt x m)
, the binding ofx
is added, removed or updated. Ify
isNone
, the binding is removed if it exists; otherwise, ify
isSome z
thenx
is associated toz
in the resulting map. Ifx
was already bound inm
to a value that is physically equal toz
,m
is returned unchanged (the result of the function is then physically equal tom
).- since
- 4.06.0
val singleton : key -> 'a -> 'a t
singleton x y
returns the one-element map that contains a bindingy
forx
.- since
- 3.12.0
val remove : key -> 'a t -> 'a t
remove x m
returns a map containing the same bindings asm
, except forx
which is unbound in the returned map. Ifx
was not inm
,m
is returned unchanged (the result of the function is then physically equal tom
).- before 4.03
Physical equality was not ensured.
val merge : (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
merge f m1 m2
computes a map whose keys is a subset of keys ofm1
and ofm2
. The presence of each such binding, and the corresponding value, is determined with the functionf
. In terms of thefind_opt
operation, we havefind_opt x (merge f m1 m2) = f (find_opt x m1) (find_opt x m2)
for any keyx
, provided thatf None None = None
.- since
- 3.12.0
val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
union f m1 m2
computes a map whose keys is the union of keys ofm1
and ofm2
. When the same binding is defined in both arguments, the functionf
is used to combine them. This is a special case ofmerge
:union f m1 m2
is equivalent tomerge f' m1 m2
, wheref' _key None None = None
f' _key (Some v) None = Some v
f' _key None (Some v) = Some v
f' key (Some v1) (Some v2) = f key v1 v2
- since
- 4.03.0
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
Total ordering between maps. The first argument is a total ordering used to compare data associated with equal keys in the two maps.
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
equal cmp m1 m2
tests whether the mapsm1
andm2
are equal, that is, contain equal keys and associate them with equal data.cmp
is the equality predicate used to compare the data associated with the keys.
val iter : (key -> 'a -> unit) -> 'a t -> unit
iter f m
appliesf
to all bindings in mapm
.f
receives the key as first argument, and the associated value as second argument. The bindings are passed tof
in increasing order with respect to the ordering over the type of the keys.
val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
fold f m a
computes(f kN dN ... (f k1 d1 a)...)
, wherek1 ... kN
are the keys of all bindings inm
(in increasing order), andd1 ... dN
are the associated data.
val for_all : (key -> 'a -> bool) -> 'a t -> bool
for_all p m
checks if all the bindings of the map satisfy the predicatep
.- since
- 3.12.0
val exists : (key -> 'a -> bool) -> 'a t -> bool
exists p m
checks if at least one binding of the map satisfies the predicatep
.- since
- 3.12.0
val filter : (key -> 'a -> bool) -> 'a t -> 'a t
filter p m
returns the map with all the bindings inm
that satisfy predicatep
. Ifp
satisfies every binding inm
,m
is returned unchanged (the result of the function is then physically equal tom
)- since
- 3.12.0
- before 4.03
Physical equality was not ensured.
val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
partition p m
returns a pair of maps(m1, m2)
, wherem1
contains all the bindings ofs
that satisfy the predicatep
, andm2
is the map with all the bindings ofs
that do not satisfyp
.- since
- 3.12.0
val cardinal : 'a t -> int
Return the number of bindings of a map.
- since
- 3.12.0
val bindings : 'a t -> (key * 'a) list
Return the list of all bindings of the given map. The returned list is sorted in increasing order of keys with respect to the ordering
Ord.compare
, whereOrd
is the argument given toMap.Make
.- since
- 3.12.0
val min_binding : 'a t -> key * 'a
Return the binding with the smallest key in a given map (with respect to the
Ord.compare
ordering), or raiseNot_found
if the map is empty.- since
- 3.12.0
val min_binding_opt : 'a t -> (key * 'a) option
Return the binding with the smallest key in the given map (with respect to the
Ord.compare
ordering), orNone
if the map is empty.- since
- 4.05
val max_binding : 'a t -> key * 'a
Same as
Map.S.min_binding
, but returns the binding with the largest key in the given map.- since
- 3.12.0
val max_binding_opt : 'a t -> (key * 'a) option
Same as
Map.S.min_binding_opt
, but returns the binding with the largest key in the given map.- since
- 4.05
val choose : 'a t -> key * 'a
Return one binding of the given map, or raise
Not_found
if the map is empty. Which binding is chosen is unspecified, but equal bindings will be chosen for equal maps.- since
- 3.12.0
val choose_opt : 'a t -> (key * 'a) option
Return one binding of the given map, or
None
if the map is empty. Which binding is chosen is unspecified, but equal bindings will be chosen for equal maps.- since
- 4.05
val split : key -> 'a t -> 'a t * 'a option * 'a t
split x m
returns a triple(l, data, r)
, wherel
is the map with all the bindings ofm
whose key is strictly less thanx
;r
is the map with all the bindings ofm
whose key is strictly greater thanx
;data
isNone
ifm
contains no binding forx
, orSome v
ifm
bindsv
tox
.- since
- 3.12.0
val find : key -> 'a t -> 'a
find x m
returns the current binding ofx
inm
, or raisesNot_found
if no such binding exists.
val find_opt : key -> 'a t -> 'a option
find_opt x m
returnsSome v
if the current binding ofx
inm
isv
, orNone
if no such binding exists.- since
- 4.05
val find_first : (key -> bool) -> 'a t -> key * 'a
find_first f m
, wheref
is a monotonically increasing function, returns the binding ofm
with the lowest keyk
such thatf k
, or raisesNot_found
if no such key exists.For example,
find_first (fun k -> Ord.compare k x >= 0) m
will return the first bindingk, v
ofm
whereOrd.compare k x >= 0
(intuitively:k >= x
), or raiseNot_found
ifx
is greater than any element ofm
.- since
- 4.05
val find_first_opt : (key -> bool) -> 'a t -> (key * 'a) option
find_first_opt f m
, wheref
is a monotonically increasing function, returns an option containing the binding ofm
with the lowest keyk
such thatf k
, orNone
if no such key exists.- since
- 4.05
val find_last : (key -> bool) -> 'a t -> key * 'a
find_last f m
, wheref
is a monotonically decreasing function, returns the binding ofm
with the highest keyk
such thatf k
, or raisesNot_found
if no such key exists.- since
- 4.05
val find_last_opt : (key -> bool) -> 'a t -> (key * 'a) option
find_last_opt f m
, wheref
is a monotonically decreasing function, returns an option containing the binding ofm
with the highest keyk
such thatf k
, orNone
if no such key exists.- since
- 4.05
val map : ('a -> 'b) -> 'a t -> 'b t
map f m
returns a map with same domain asm
, where the associated valuea
of all bindings ofm
has been replaced by the result of the application off
toa
. The bindings are passed tof
in increasing order with respect to the ordering over the type of the keys.
val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
Same as
Map.S.map
, but the function receives as arguments both the key and the associated value for each binding of the map.
Iterators
val to_seq : 'a t -> (key * 'a) Stdlib.Seq.t
Iterate on the whole map, in ascending order of keys
- since
- 4.07
val to_seq_from : key -> 'a t -> (key * 'a) Stdlib.Seq.t
to_seq_from k m
iterates on a subset of the bindings ofm
, in ascending order of keys, from keyk
or above.- since
- 4.07
val add_seq : (key * 'a) Stdlib.Seq.t -> 'a t -> 'a t
Add the given bindings to the map, in order.
- since
- 4.07
val of_seq : (key * 'a) Stdlib.Seq.t -> 'a t
Build a map from the given bindings
- since
- 4.07