Module Base.Map
module Or_duplicate = Base__.Map_intf.Or_duplicatetype ('k, 'cmp) comparator= (module Comparator.S with type comparator_witness = 'cmp and type t = 'k)
val invariants : (_, _, _) t -> boolTest if the invariants of the internal AVL search tree hold.
val comparator_s : ('a, _, 'cmp) t -> ('a, 'cmp) comparatorReturns a first-class module that can be used to build other map/set/etc. with the same notion of comparison.
val comparator : ('a, _, 'cmp) t -> ('a, 'cmp) Comparator.tval empty : ('a, 'cmp) comparator -> ('a, 'b, 'cmp) tThe empty map.
val singleton : ('a, 'cmp) comparator -> 'a -> 'b -> ('a, 'b, 'cmp) tA map with one (key, data) pair.
val of_alist : ('a, 'cmp) comparator -> ('a * 'b) list -> [ `Ok of ('a, 'b, 'cmp) t | `Duplicate_key of 'a ]Creates a map from an association list with unique keys.
val of_alist_or_error : ('a, 'cmp) comparator -> ('a * 'b) list -> ('a, 'b, 'cmp) t Or_error.tCreates a map from an association list with unique keys, returning an error if duplicate
'akeys are found.
val of_alist_exn : ('a, 'cmp) comparator -> ('a * 'b) list -> ('a, 'b, 'cmp) tCreates a map from an association list with unique keys, raising an exception if duplicate
'akeys are found.
val of_alist_multi : ('a, 'cmp) comparator -> ('a * 'b) list -> ('a, 'b list, 'cmp) tCreates a map from an association list with possibly repeated keys. The values in the map for a given key appear in the same order as they did in the association list.
val of_alist_fold : ('a, 'cmp) comparator -> ('a * 'b) list -> init:'c -> f:('c -> 'b -> 'c) -> ('a, 'c, 'cmp) tCombines an association list into a map, folding together bound values with common keys.
val of_alist_reduce : ('a, 'cmp) comparator -> ('a * 'b) list -> f:('b -> 'b -> 'b) -> ('a, 'b, 'cmp) tCombines an association list into a map, reducing together bound values with common keys.
val of_iteri : ('a, 'cmp) comparator -> iteri:(f:(key:'a -> data:'b -> unit) -> unit) -> [ `Ok of ('a, 'b, 'cmp) t | `Duplicate_key of 'a ]of_iteri ~iteribehaves likeof_alist, except that instead of taking a concrete data structure, it takes an iteration function. For instance, to convert a string table into a map:of_iteri (module String) ~f:(Hashtbl.iteri table). It is faster than adding the elements one by one.
val of_sorted_array : ('a, 'cmp) comparator -> ('a * 'b) array -> ('a, 'b, 'cmp) t Or_error.tCreates a map from a sorted array of key-data pairs. The input array must be sorted (either in ascending or descending order), as given by the relevant comparator, and must not contain duplicate keys. If either of these conditions does not hold, an error is returned.
val of_sorted_array_unchecked : ('a, 'cmp) comparator -> ('a * 'b) array -> ('a, 'b, 'cmp) tLike
of_sorted_arrayexcept that it returns a map with broken invariants when anErrorwould have been returned.
val of_increasing_iterator_unchecked : ('a, 'cmp) comparator -> len:int -> f:(int -> 'a * 'b) -> ('a, 'b, 'cmp) tof_increasing_iterator_unchecked c ~len ~fbehaves likeof_sorted_array_unchecked c (Array.init len ~f), with the additional restriction that a decreasing order is not supported. The advantage is not requiring you to allocate an intermediate array.fwill be called with 0, 1, ...len - 1, in order.
val of_increasing_sequence : ('k, 'cmp) comparator -> ('k * 'v) Sequence.t -> ('k, 'v, 'cmp) t Or_error.tof_increasing_sequence c seqbehaves likeof_sorted_array c (Sequence.to_array seq), but does not allocate the intermediate array.The sequence will be folded over once, and the additional time complexity is O(n).
val is_empty : (_, _, _) t -> boolTests whether a map is empty.
val length : (_, _, _) t -> intlength mapreturns the number of elements inmap. O(1), butTree.lengthis O(n).
val set : ('k, 'v, 'cmp) t -> key:'k -> data:'v -> ('k, 'v, 'cmp) tReturns a new map with the specified new binding; if the key was already bound, its previous binding disappears.
val add : ('k, 'v, 'cmp) t -> key:'k -> data:'v -> ('k, 'v, 'cmp) t Or_duplicate.tadd t ~key ~dataadds a new entry totmappingkeytodataand returns`Okwith the new map, or ifkeyis already present int, returns`Duplicate.
val add_exn : ('k, 'v, 'cmp) t -> key:'k -> data:'v -> ('k, 'v, 'cmp) tval add_multi : ('k, 'v list, 'cmp) t -> key:'k -> data:'v -> ('k, 'v list, 'cmp) tIf
keyis not present then add a singleton list, otherwise, cons data onto the head of the existing list.
val remove_multi : ('k, 'v list, 'cmp) t -> 'k -> ('k, 'v list, 'cmp) tIf the key is present, then remove its head element; if the result is empty, remove the key.
val find_multi : ('k, 'v list, 'cmp) t -> 'k -> 'v listReturns the value bound to the given key, or the empty list if there is none.
val change : ('k, 'v, 'cmp) t -> 'k -> f:('v option -> 'v option) -> ('k, 'v, 'cmp) tchange t key ~freturns a new mapmthat is the same aston all keys except forkey, and whose value forkeyis defined byf, i.e.,find m key = f (find t key).
val update : ('k, 'v, 'cmp) t -> 'k -> f:('v option -> 'v) -> ('k, 'v, 'cmp) tupdate t key ~fischange t key ~f:(fun o -> Some (f o)).
val find : ('k, 'v, 'cmp) t -> 'k -> 'v optionReturns
Some valuebound to the given key, orNoneif none exists.
val find_exn : ('k, 'v, 'cmp) t -> 'k -> 'vReturns the value bound to the given key, raising
Caml.Not_foundofNot_found_sif none exists.
val remove : ('k, 'v, 'cmp) t -> 'k -> ('k, 'v, 'cmp) tReturns a new map with any binding for the key in question removed.
val mem : ('k, _, 'cmp) t -> 'k -> boolmem map keytests whethermapcontains a binding forkey.
val iter_keys : ('k, _, _) t -> f:('k -> unit) -> unitval iter : (_, 'v, _) t -> f:('v -> unit) -> unitval iteri : ('k, 'v, _) t -> f:(key:'k -> data:'v -> unit) -> unitval iter2 : ('k, 'v1, 'cmp) t -> ('k, 'v2, 'cmp) t -> f:(key:'k -> data:[ `Left of 'v1 | `Right of 'v2 | `Both of 'v1 * 'v2 ] -> unit) -> unitIterates two maps side by side. The complexity of this function is O(M + N). If two inputs are
[(0, a); (1, a)]and[(1, b); (2, b)],fwill be called with[(0, `Left a); (1, `Both (a, b)); (2, `Right b)].
val map : ('k, 'v1, 'cmp) t -> f:('v1 -> 'v2) -> ('k, 'v2, 'cmp) tReturns a new map with bound values replaced by
fapplied to the bound values.
val mapi : ('k, 'v1, 'cmp) t -> f:(key:'k -> data:'v1 -> 'v2) -> ('k, 'v2, 'cmp) tLike
map, but the passed function takes bothkeyanddataas arguments.
val fold : ('k, 'v, _) t -> init:'a -> f:(key:'k -> data:'v -> 'a -> 'a) -> 'aFolds over keys and data in the map in increasing order of
key.
val fold_right : ('k, 'v, _) t -> init:'a -> f:(key:'k -> data:'v -> 'a -> 'a) -> 'aFolds over keys and data in the map in decreasing order of
key.
val fold2 : ('k, 'v1, 'cmp) t -> ('k, 'v2, 'cmp) t -> init:'a -> f:(key:'k -> data:[ `Left of 'v1 | `Right of 'v2 | `Both of 'v1 * 'v2 ] -> 'a -> 'a) -> 'aFolds over two maps side by side, like
iter2.
val filter_keys : ('k, 'v, 'cmp) t -> f:('k -> bool) -> ('k, 'v, 'cmp) tfilter,filteri,filter_keys,filter_map, andfilter_mapirun in O(n * lg n) time; they simply accumulate each key & data pair retained byfinto a new map usingadd.
val filter : ('k, 'v, 'cmp) t -> f:('v -> bool) -> ('k, 'v, 'cmp) tval filteri : ('k, 'v, 'cmp) t -> f:(key:'k -> data:'v -> bool) -> ('k, 'v, 'cmp) tval filter_map : ('k, 'v1, 'cmp) t -> f:('v1 -> 'v2 option) -> ('k, 'v2, 'cmp) tReturns a new map with bound values filtered by
fapplied to the bound values.
val filter_mapi : ('k, 'v1, 'cmp) t -> f:(key:'k -> data:'v1 -> 'v2 option) -> ('k, 'v2, 'cmp) tLike
filter_map, but the passed function takes bothkeyanddataas arguments.
val partition_mapi : ('k, 'v1, 'cmp) t -> f:(key:'k -> data:'v1 -> [ `Fst of 'v2 | `Snd of 'v3 ]) -> ('k, 'v2, 'cmp) t * ('k, 'v3, 'cmp) tpartition_mapi t ~freturns two newts, with each key intappearing in exactly one of the resulting maps depending on its mapping inf.
val partition_map : ('k, 'v1, 'cmp) t -> f:('v1 -> [ `Fst of 'v2 | `Snd of 'v3 ]) -> ('k, 'v2, 'cmp) t * ('k, 'v3, 'cmp) tpartition_map t ~f = partition_mapi t ~f:(fun ~key:_ ~data -> f data)
val partitioni_tf : ('k, 'v, 'cmp) t -> f:(key:'k -> data:'v -> bool) -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) tpartitioni_tf t ~f = partition_mapi t ~f:(fun ~key ~data -> if f ~key ~data then `Fst data else `Snd data)
val partition_tf : ('k, 'v, 'cmp) t -> f:('v -> bool) -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) tpartition_tf t ~f = partitioni_tf t ~f:(fun ~key:_ ~data -> f data)
val compare_direct : ('v -> 'v -> int) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> intReturns a total ordering between maps. The first argument is a total ordering used to compare data associated with equal keys in the two maps.
val hash_fold_direct : 'k Hash.folder -> 'v Hash.folder -> ('k, 'v, 'cmp) t Hash.folderHash function: a building block to use when hashing data structures containing maps in them.
hash_fold_direct hash_fold_keyis compatible withcompare_directiffhash_fold_keyis compatible with(comparator m).compareof the mapmbeing hashed.
val equal : ('v -> 'v -> bool) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> boolequal cmp m1 m2tests whether the mapsm1andm2are equal, that is, contain the same keys and associate each key with the same value.cmpis the equality predicate used to compare the values associated with the keys.
val keys : ('k, _, _) t -> 'k listReturns a list of the keys in the given map.
val data : (_, 'v, _) t -> 'v listReturns a list of the data in the given map.
val to_alist : ?key_order:[ `Increasing | `Decreasing ] -> ('k, 'v, _) t -> ('k * 'v) listCreates an association list from the given map.
val validate : name:('k -> string) -> 'v Validate.check -> ('k, 'v, _) t Validate.check
Additional operations on maps
val merge : ('k, 'v1, 'cmp) t -> ('k, 'v2, 'cmp) t -> f:(key:'k -> [ `Left of 'v1 | `Right of 'v2 | `Both of 'v1 * 'v2 ] -> 'v3 option) -> ('k, 'v3, 'cmp) tMerges two maps. The runtime is O(length(t1) + length(t2)). You shouldn't use this function to merge a list of maps; consider using
merge_skewedinstead.
val merge_skewed : ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> combine:(key:'k -> 'v -> 'v -> 'v) -> ('k, 'v, 'cmp) tA special case of
merge,merge_skewed t1 t2is a map containing all the bindings oft1andt2. Bindings that appear in botht1andt2are combined into a single value using thecombinefunction. In a callcombine ~key v1 v2, the valuev1comes fromt1andv2fromt2.The runtime of
merge_skewedisO(l1 * log(l2)), wherel1is the length of the smaller map andl2the length of the larger map. This is likely to be faster thanmergewhen one of the maps is a lot smaller, or when you merge a list of maps.
module Symmetric_diff_element : sig ... endval symmetric_diff : ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> data_equal:('v -> 'v -> bool) -> ('k, 'v) Symmetric_diff_element.t Sequence.tsymmetric_diff t1 t2 ~data_equalreturns a list of changes betweent1andt2. It is intended to be efficient in the case wheret1andt2share a large amount of structure. The keys in the output sequence will be in sorted order.
val min_elt : ('k, 'v, _) t -> ('k * 'v) optionmin_elt mapreturnsSome (key, data)pair corresponding to the minimum key inmap, orNoneif empty.
val min_elt_exn : ('k, 'v, _) t -> 'k * 'vval max_elt : ('k, 'v, _) t -> ('k * 'v) optionmax_elt mapreturnsSome (key, data)pair corresponding to the maximum key inmap, orNoneifmapis empty.
val max_elt_exn : ('k, 'v, _) t -> 'k * 'v
val for_all : ('k, 'v, _) t -> f:('v -> bool) -> boolval for_alli : ('k, 'v, _) t -> f:(key:'k -> data:'v -> bool) -> boolval exists : ('k, 'v, _) t -> f:('v -> bool) -> boolval existsi : ('k, 'v, _) t -> f:(key:'k -> data:'v -> bool) -> boolval count : ('k, 'v, _) t -> f:('v -> bool) -> intval counti : ('k, 'v, _) t -> f:(key:'k -> data:'v -> bool) -> intval split : ('k, 'v, 'cmp) t -> 'k -> ('k, 'v, 'cmp) t * ('k * 'v) option * ('k, 'v, 'cmp) tsplit t keyreturns a map of keys strictly less thankey, the mapping ofkeyif any, and a map of keys strictly greater thankey.Runtime is O(m + log n), where n is the size of the input map and m is the size of the smaller of the two output maps. The O(m) term is due to the need to calculate the length of the output maps.
val append : lower_part:('k, 'v, 'cmp) t -> upper_part:('k, 'v, 'cmp) t -> [ `Ok of ('k, 'v, 'cmp) t | `Overlapping_key_ranges ]append ~lower_part ~upper_partreturns`Ok mapwheremapcontains all the(key, value)pairs from the two input maps if all the keys fromlower_partare less than all the keys fromupper_part. Otherwise it returns`Overlapping_key_ranges.Runtime is O(log n) where n is the size of the larger input map. This can be significantly faster than
Map.mergeor repeatedMap.add.assert (match Map.append ~lower_part ~upper_part with | `Ok whole_map -> Map.to_alist whole_map = List.append (to_alist lower_part) (to_alist upper_part) | `Overlapping_key_ranges -> true);
val subrange : ('k, 'v, 'cmp) t -> lower_bound:'k Maybe_bound.t -> upper_bound:'k Maybe_bound.t -> ('k, 'v, 'cmp) tsubrange t ~lower_bound ~upper_boundreturns a map containing all the entries fromtwhose keys lie inside the interval indicated by~lower_boundand~upper_bound. If this interval is empty, an empty map is returned.Runtime is O(m + log n), where n is the size of the input map and m is the size of the output map. The O(m) term is due to the need to calculate the length of the output map.
val fold_range_inclusive : ('k, 'v, 'cmp) t -> min:'k -> max:'k -> init:'a -> f:(key:'k -> data:'v -> 'a -> 'a) -> 'afold_range_inclusive t ~min ~max ~init ~ffoldsf(with initial value~init) over all keys (and their associated values) that are in the range[min, max](inclusive).
val range_to_alist : ('k, 'v, 'cmp) t -> min:'k -> max:'k -> ('k * 'v) listrange_to_alist t ~min ~maxreturns an associative list of the elements whose keys lie in[min, max](inclusive), with the smallest key being at the head of the list.
val closest_key : ('k, 'v, 'cmp) t -> [ `Greater_or_equal_to | `Greater_than | `Less_or_equal_to | `Less_than ] -> 'k -> ('k * 'v) optionclosest_key t dir kreturns the(key, value)pair intwithkeyclosest tokthat satisfies the given inequality bound.For example,
closest_key t `Less_than kwould be the pair with the closest key tokwherekey < k.to_sequencecan be used to get the same results asclosest_key. It is less efficient for individual lookups but more efficient for finding many elements starting at some value.
val nth : ('k, 'v, _) t -> int -> ('k * 'v) optionnth t nfinds the (key, value) pair of rank n (i.e., such that there are exactly n keys strictly less than the found key), if one exists. O(log(length t) + n) time.
val nth_exn : ('k, 'v, _) t -> int -> 'k * 'vval rank : ('k, 'v, 'cmp) t -> 'k -> int optionrank t kIfkis int, returns the number of keys strictly less thankint, andNoneotherwise.
val to_sequence : ?order:[ `Increasing_key | `Decreasing_key ] -> ?keys_greater_or_equal_to:'k -> ?keys_less_or_equal_to:'k -> ('k, 'v, 'cmp) t -> ('k * 'v) Sequence.tto_sequence ?order ?keys_greater_or_equal_to ?keys_less_or_equal_to tgives a sequence of key-value pairs betweenkeys_less_or_equal_toandkeys_greater_or_equal_toinclusive, presented inorder. Ifkeys_greater_or_equal_to > keys_less_or_equal_to, the sequence is empty. Cost is O(log n) up front and amortized O(1) to produce each element.
module M : functor (K : sig ... end) -> sig ... endMis meant to be used in combination with OCaml applicative functor types:
include Base__.Map_intf.For_deriving with type ('key, 'value, 'cmp) t := ('key, 'value, 'cmp) t
module type Sexp_of_m = sig ... endmodule type M_of_sexp = sig ... endmodule type Compare_m = sig ... endmodule type Hash_fold_m = Hasher.Sval sexp_of_m__t : (module Sexp_of_m with type t = 'k) -> ('v -> Sexp.t) -> ('k, 'v, 'cmp) t -> Sexp.tval m__t_of_sexp : (module M_of_sexp with type comparator_witness = 'cmp and type t = 'k) -> (Sexp.t -> 'v) -> Sexp.t -> ('k, 'v, 'cmp) tval compare_m__t : (module Compare_m) -> ('v -> 'v -> int) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> intval hash_fold_m__t : (module Hash_fold_m with type t = 'k) -> (Hash.state -> 'v -> Hash.state) -> Hash.state -> ('k, 'v, _) t -> Hash.state
module Poly : Base__.Map_intf.S_poly with type ('key, +'value) t = ('key, 'value, Comparator.Poly.comparator_witness) tA polymorphic Map.
module Using_comparator : sig ... endUsing_comparatoris a similar interface as the toplevel ofMap, except the functions take a~comparator:('k, 'cmp) Comparator.t, whereas the functions at the toplevel ofMaptake a('k, 'cmp) comparator.
Modules and module types for extending Map
For use in extensions of Base, like Core_kernel.
module With_comparator = Base__.Map_intf.With_comparatormodule With_first_class_module = Base__.Map_intf.With_first_class_modulemodule Without_comparator = Base__.Map_intf.Without_comparatormodule type For_deriving = Base__.Map_intf.For_derivingmodule type S_poly = Base__.Map_intf.S_polymodule type Accessors1 = Base__.Map_intf.Accessors1module type Accessors2 = Base__.Map_intf.Accessors2module type Accessors3 = Base__.Map_intf.Accessors3module type Accessors_generic = Base__.Map_intf.Accessors_genericmodule type Creators1 = Base__.Map_intf.Creators1module type Creators2 = Base__.Map_intf.Creators2module type Creators_and_accessors3_with_comparator = Base__.Map_intf.Creators_and_accessors3_with_comparatormodule type Creators_generic = Base__.Map_intf.Creators_generic