jon.recoil.org

Module Base.MapSource

Sourcemodule Or_duplicate : sig ... end
Sourcemodule Without_comparator : sig ... end
Sourcemodule With_comparator : sig ... end
Sourcemodule With_first_class_module : sig ... end
Sourcemodule Symmetric_diff_element : sig ... end
Sourcemodule When_unmatched : sig ... end
Sourcemodule When_matched : sig ... end
Sourcemodule Continue_or_stop : sig ... end
module type Accessors_generic = sig ... end
module type Transformers_generic = sig ... end
module type Creators_generic = sig ... end
module type S_poly = sig ... end
module type For_deriving = sig ... end

Map is a functional data structure (balanced binary tree) implementing finite maps over a totally-ordered domain, called a "key".

Sourcetype (!'key, !+'value, !'cmp) t
include sig ... end
Sourceval globalize : 'key 'value 'cmp. ('key @ local -> 'key) -> ('value @ local -> 'value) -> ('cmp @ local -> 'cmp) -> ('key, 'value, 'cmp) Base.Map.t @ local -> ('key, 'value, 'cmp) Base.Map.t
Sourcemodule Finished_or_unfinished : sig ... end
Sourcemodule Merge_element : sig ... end
Sourceval invariants : (_, _, _) Base.Map.t -> bool @@ portable

Test if the invariants of the internal weight-balanced search tree hold.

Sourceval comparator_s : ('a, _, 'cmp) Base.Map.t -> ('a, 'cmp) Base.Comparator.Module.t @@ portable

Returns a first-class module that can be used to build other map/set/etc. with the same notion of comparison.

Sourceval comparator : ('a, _, 'cmp) Base.Map.t -> ('a, 'cmp) Base.Comparator.t @@ portable
Sourceval globalize0 : ('key, 'data, 'cmp) Base.Map.t @ local -> ('key, 'data, 'cmp) Base.Map.t @@ portable
Sourceval empty : 'a 'b 'cmp. ('a, 'cmp) Base.Comparator.Module.t -> ('a, 'b, 'cmp) Base.Map.t @@ portable

The empty map.

Sourceval singleton : ('a, 'cmp) Base.Comparator.Module.t -> 'a -> 'b -> ('a, 'b, 'cmp) Base.Map.t @@ portable

A map with one (key, data) pair.

Sourceval of_alist : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) list -> [ `Ok of ('a, 'b, 'cmp) Base.Map.t | `Duplicate_key of 'a ] @@ portable

Creates a map from an association list with unique keys.

Sourceval of_alist_or_error : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) list -> ('a, 'b, 'cmp) Base.Map.t Base.Or_error.t @@ portable

Creates a map from an association list with unique keys, returning an error if duplicate 'a keys are found.

Sourceval of_alist_exn : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) list -> ('a, 'b, 'cmp) Base.Map.t @@ portable

Creates a map from an association list with unique keys, raising an exception if duplicate 'a keys are found.

Sourceval of_alist_multi : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) list -> ('a, 'b list, 'cmp) Base.Map.t @@ portable

Creates 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.

Sourceval of_alist_fold : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) list -> init:'c -> f:('c -> 'b -> 'c) @ local -> ('a, 'c, 'cmp) Base.Map.t @@ portable

Combines an association list into a map, folding together bound values with common keys. The accumulator is per-key.

Example:

  # (let map =
       String.Map.of_alist_fold
         [ "a", 1; "a", 10; "b", 2; "b", 20; "b", 200 ]
         ~init:Int.Set.empty
         ~f:Set.add
     in
     print_s [%sexp (map : Int.Set.t String.Map.t)]);;
  ((a (1 10)) (b (2 20 200)))
  - : unit = ()
Sourceval of_alist_reduce : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) list -> f:('b -> 'b -> 'b) @ local -> ('a, 'b, 'cmp) Base.Map.t @@ portable

Combines an association list into a map, reducing together bound values with common keys.

Sourceval of_iteri : ('a, 'cmp) Base.Comparator.Module.t -> iteri:(f:(key:'a -> data:'b -> unit) @ local -> unit) @ local -> [ `Ok of ('a, 'b, 'cmp) Base.Map.t | `Duplicate_key of 'a ] @@ portable

of_iteri ~iteri behaves like of_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.

Sourceval of_iteri_exn : ('a, 'cmp) Base.Comparator.Module.t -> iteri:(f:(key:'a -> data:'b -> unit) @ local -> unit) @ local -> ('a, 'b, 'cmp) Base.Map.t @@ portable

Like of_iteri except that it raises an exception if duplicate 'a keys are found.

Sourceval of_sorted_array : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) array -> ('a, 'b, 'cmp) Base.Map.t Base.Or_error.t @@ portable

Creates 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.

Sourceval of_sorted_array_unchecked : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) array -> ('a, 'b, 'cmp) Base.Map.t @@ portable

Like of_sorted_array except that it returns a map with broken invariants when an Error would have been returned.

Sourceval of_increasing_iterator_unchecked : ('a, 'cmp) Base.Comparator.Module.t -> len:int -> f:(int -> 'a * 'b) @ local -> ('a, 'b, 'cmp) Base.Map.t @@ portable

of_increasing_iterator_unchecked c ~len ~f behaves like of_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. f will be called with 0, 1, ... len - 1, in order.

Sourceval of_increasing_sequence : ('k, 'cmp) Base.Comparator.Module.t -> ('k * 'v) Base.Sequence.t -> ('k, 'v, 'cmp) Base.Map.t Base.Or_error.t @@ portable

of_increasing_sequence c seq behaves like of_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).

Sourceval of_sequence : ('k, 'cmp) Base.Comparator.Module.t -> ('k * 'v) Base.Sequence.t -> [ `Ok of ('k, 'v, 'cmp) Base.Map.t | `Duplicate_key of 'k ] @@ portable

Creates a map from an association sequence with unique keys.

of_sequence c seq behaves like of_alist c (Sequence.to_list seq) but does not allocate the intermediate list.

If your sequence is increasing, use of_increasing_sequence.

Sourceval of_sequence_or_error : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) Base.Sequence.t -> ('a, 'b, 'cmp) Base.Map.t Base.Or_error.t @@ portable

Creates a map from an association sequence with unique keys, returning an error if duplicate 'a keys are found.

of_sequence_or_error c seq behaves like of_alist_or_error c (Sequence.to_list seq) but does not allocate the intermediate list.

Sourceval of_sequence_exn : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) Base.Sequence.t -> ('a, 'b, 'cmp) Base.Map.t @@ portable

Creates a map from an association sequence with unique keys, raising an exception if duplicate 'a keys are found.

of_sequence_exn c seq behaves like of_alist_exn c (Sequence.to_list seq) but does not allocate the intermediate list.

Sourceval of_sequence_multi : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) Base.Sequence.t -> ('a, 'b list, 'cmp) Base.Map.t @@ portable

Creates a map from an association sequence 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.

of_sequence_multi c seq behaves like of_alist_exn c (Sequence.to_list seq) but does not allocate the intermediate list.

Sourceval of_sequence_fold : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) Base.Sequence.t -> init:'c -> f:('c -> 'b -> 'c) @ local -> ('a, 'c, 'cmp) Base.Map.t @@ portable

Combines an association sequence into a map, folding together bound values with common keys.

of_sequence_fold c seq ~init ~f behaves like of_alist_fold c (Sequence.to_list seq) ~init ~f but does not allocate the intermediate list.

Sourceval of_sequence_reduce : ('a, 'cmp) Base.Comparator.Module.t -> ('a * 'b) Base.Sequence.t -> f:('b -> 'b -> 'b) @ local -> ('a, 'b, 'cmp) Base.Map.t @@ portable

Combines an association sequence into a map, reducing together bound values with common keys.

of_sequence_reduce c seq ~f behaves like of_alist_reduce c (Sequence.to_list seq) ~f but does not allocate the intermediate list.

Sourceval of_list_with_key : ('k, 'cmp) Base.Comparator.Module.t -> 'v list -> get_key:('v -> 'k) @ local -> [ `Ok of ('k, 'v, 'cmp) Base.Map.t | `Duplicate_key of 'k ] @@ portable

Constructs a map from a list of values, where get_key extracts a key from a value.

Sourceval of_list_with_key_or_error : ('k, 'cmp) Base.Comparator.Module.t -> 'v list -> get_key:('v -> 'k) @ local -> ('k, 'v, 'cmp) Base.Map.t Base.Or_error.t @@ portable

Like of_list_with_key; returns Error on duplicate key.

Sourceval of_list_with_key_exn : ('k, 'cmp) Base.Comparator.Module.t -> 'v list -> get_key:('v -> 'k) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable

Like of_list_with_key; raises on duplicate key.

Sourceval of_list_with_key_multi : ('k, 'cmp) Base.Comparator.Module.t -> 'v list -> get_key:('v -> 'k) @ local -> ('k, 'v list, 'cmp) Base.Map.t @@ portable

Like of_list_with_key; produces lists of all values associated with each key.

Sourceval of_list_with_key_fold : ('k, 'cmp) Base.Comparator.Module.t -> 'v list -> get_key:('v -> 'k) @ local -> init:'acc -> f:('acc -> 'v -> 'acc) -> ('k, 'acc, 'cmp) Base.Map.t @@ portable

Like of_list_with_key; resolves duplicate keys the same way of_alist_fold does.

Sourceval of_list_with_key_reduce : ('k, 'cmp) Base.Comparator.Module.t -> 'v list -> get_key:('v -> 'k) @ local -> f:('v -> 'v -> 'v) -> ('k, 'v, 'cmp) Base.Map.t @@ portable

Like of_list_with_key; resolves duplicate keys the same way of_alist_reduce does.

Sourceval is_empty : (_, _, _) Base.Map.t @ local -> bool @@ portable

Tests whether a map is empty.

Sourceval length : (_, _, _) Base.Map.t @ local -> int @@ portable

length map returns the number of elements in map. O(1), but Tree.length is O(n).

Sourceval set : ('k, 'v, 'cmp) Base.Map.t -> key:'k -> data:'v -> ('k, 'v, 'cmp) Base.Map.t @@ portable

Returns a new map with the specified new binding; if the key was already bound, its previous binding disappears.

Sourceval add : ('k, 'v, 'cmp) Base.Map.t -> key:'k -> data:'v -> ('k, 'v, 'cmp) Base.Map.t Base.Map.Or_duplicate.t @@ portable

add t ~key ~data adds a new entry to t mapping key to data and returns `Ok with the new map, or if key is already present in t, returns `Duplicate.

Sourceval add_exn : ('k, 'v, 'cmp) Base.Map.t -> key:'k -> data:'v -> ('k, 'v, 'cmp) Base.Map.t @@ portable
Sourceval add_multi : ('k, 'v list, 'cmp) Base.Map.t -> key:'k -> data:'v -> ('k, 'v list, 'cmp) Base.Map.t @@ portable

If key is not present then add a singleton list, otherwise, cons data onto the head of the existing list.

Sourceval remove_multi : ('k, 'v list, 'cmp) Base.Map.t -> 'k -> ('k, 'v list, 'cmp) Base.Map.t @@ portable

If the key is present, then remove its head element; if the result is empty, remove the key.

Sourceval find_multi : ('k, 'v list, 'cmp) Base.Map.t -> 'k -> 'v list @@ portable

Returns the value bound to the given key, or the empty list if there is none.

Sourceval change : ('k, 'v, 'cmp) Base.Map.t -> 'k -> f:('v option -> 'v option) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable

change t key ~f returns a new map m that is the same as t on all keys except for key, and whose value for key is defined by f, i.e., find m key = f (find t key).

Sourceval update : ('k, 'v, 'cmp) Base.Map.t -> 'k -> f:('v option -> 'v) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable

update t key ~f returns a new map which is the same as t but sets the value corresponding to key to the result of f.

f is called with Some value if key is present in the map, otherwise None.

update can never remove an item from the map. See change if you need to remove values.

Sourceval update_and_return : ('k, 'v, 'cmp) Base.Map.t -> 'k -> f:('v option -> 'v) @ local -> 'v * ('k, 'v, 'cmp) Base.Map.t @@ portable

update_and_return t key ~f is like update t key ~f, but also returns the new value.

Sourceval find : ('k, 'v, 'cmp) Base.Map.t -> 'k -> 'v option @@ portable

Returns Some value bound to the given key, or None if none exists.

Sourceval find_exn : ('k, 'v, 'cmp) Base.Map.t -> 'k -> 'v @@ portable

Returns the value bound to the given key, raising Stdlib.Not_found or Not_found_s if none exists.

Sourceval remove : ('k, 'v, 'cmp) Base.Map.t -> 'k -> ('k, 'v, 'cmp) Base.Map.t @@ portable

Returns a new map with any binding for the key in question removed.

Sourceval mem : ('k, _, 'cmp) Base.Map.t -> 'k -> bool @@ portable

mem map key tests whether map contains a binding for key.

Sourceval iter_keys : ('k, _, _) Base.Map.t -> f:('k -> unit) @ local -> unit @@ portable
Sourceval iter : (_, 'v, _) Base.Map.t -> f:('v -> unit) @ local -> unit @@ portable
Sourceval iteri : ('k, 'v, _) Base.Map.t -> f:(key:'k -> data:'v -> unit) @ local -> unit @@ portable
Sourceval iteri_until : ('k, 'v, _) Base.Map.t -> f:(key:'k -> data:'v -> Continue_or_stop.t) @ local -> Base.Map.Finished_or_unfinished.t @@ portable

Iterates until the first time f returns Stop. If f returns Stop, the final result is Unfinished. Otherwise, the final result is Finished.

Sourceval iter2 : ('k, 'v1, 'cmp) Base.Map.t -> ('k, 'v2, 'cmp) Base.Map.t -> f:(key:'k -> data:('v1, 'v2) Base.Map.Merge_element.t -> unit) @ local -> unit @@ portable

Iterates 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)], f will be called with [(0, `Left a); (1, `Both (a, b)); (2, `Right b)].

Sourceval map : ('k, 'v1, 'cmp) Base.Map.t -> f:('v1 -> 'v2) @ local -> ('k, 'v2, 'cmp) Base.Map.t @@ portable

Returns a new map with bound values replaced by f applied to the bound values.

Sourceval mapi : ('k, 'v1, 'cmp) Base.Map.t -> f:(key:'k -> data:'v1 -> 'v2) @ local -> ('k, 'v2, 'cmp) Base.Map.t @@ portable

Like map, but the passed function takes both key and data as arguments.

Sourceval map_keys : ('k2, 'cmp2) Base.Comparator.Module.t -> ('k1, 'v, 'cmp1) Base.Map.t -> f:('k1 -> 'k2) @ local -> [ `Ok of ('k2, 'v, 'cmp2) Base.Map.t | `Duplicate_key of 'k2 ] @@ portable

Convert map with keys of type 'k2 to a map with keys of type 'k2 using f.

Sourceval map_keys_exn : ('k2, 'cmp2) Base.Comparator.Module.t -> ('k1, 'v, 'cmp1) Base.Map.t -> f:('k1 -> 'k2) @ local -> ('k2, 'v, 'cmp2) Base.Map.t @@ portable

Like map_keys, but raises on duplicate key.

Sourceval fold : ('k, 'v, _) Base.Map.t -> init:'acc -> f:(key:'k -> data:'v -> 'acc -> 'acc) @ local -> 'acc @@ portable

Folds over keys and data in the map in increasing order of key.

Sourceval fold_until : ('k, 'v, _) Base.Map.t -> init:'acc -> f: (key:'k -> data:'v -> 'acc -> ('acc, 'final) Container.Continue_or_stop.t) @ local -> finish:('acc -> 'final) @ local -> 'final @@ portable

Folds over keys and data in the map in increasing order of key, until the first time that f returns Stop _. If f returns Stop final, this function returns immediately with the value final. If f never returns Stop _, and the final call to f returns Continue last, this function returns finish last.

Sourceval fold_right : ('k, 'v, _) Base.Map.t -> init:'acc -> f:(key:'k -> data:'v -> 'acc -> 'acc) @ local -> 'acc @@ portable

Folds over keys and data in the map in decreasing order of key.

Sourceval fold2 : ('k, 'v1, 'cmp) Base.Map.t -> ('k, 'v2, 'cmp) Base.Map.t -> init:'acc -> f: (key:'k -> data:('v1, 'v2) Base.Map.Merge_element.t -> 'acc -> 'acc) @ local -> 'acc @@ portable

Folds over two maps side by side, like iter2.

Sourceval filter_keys : ('k, 'v, 'cmp) Base.Map.t -> f:('k -> bool) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable

filter, filteri, filter_keys, filter_map, and filter_mapi run in O(n) time.

filter, filteri, filter_keys, partition_tf and partitioni_tf keep a lot of sharing between their result and the original map. Dropping or keeping a run of k consecutive elements costs O(log(k)) extra memory. Keeping the entire map costs no extra memory at all: filter ~f:(fun _ -> true) returns the original map.

Sourceval filter : ('k, 'v, 'cmp) Base.Map.t -> f:('v -> bool) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable
Sourceval filteri : ('k, 'v, 'cmp) Base.Map.t -> f:(key:'k -> data:'v -> bool) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable
Sourceval filter_map : ('k, 'v1, 'cmp) Base.Map.t -> f:('v1 -> 'v2 option) @ local -> ('k, 'v2, 'cmp) Base.Map.t @@ portable

Returns a new map with bound values filtered by f applied to the bound values.

Sourceval filter_mapi : ('k, 'v1, 'cmp) Base.Map.t -> f:(key:'k -> data:'v1 -> 'v2 option) @ local -> ('k, 'v2, 'cmp) Base.Map.t @@ portable

Like filter_map, but the passed function takes both key and data as arguments.

Sourceval partition_mapi : ('k, 'v1, 'cmp) Base.Map.t -> f:(key:'k -> data:'v1 -> ('v2, 'v3) Base.Either.t) @ local -> ('k, 'v2, 'cmp) Base.Map.t * ('k, 'v3, 'cmp) Base.Map.t @@ portable

partition_mapi t ~f returns two new ts, with each key in t appearing in exactly one of the resulting maps depending on its mapping in f.

Sourceval partition_map : ('k, 'v1, 'cmp) Base.Map.t -> f:('v1 -> ('v2, 'v3) Base.Either.t) @ local -> ('k, 'v2, 'cmp) Base.Map.t * ('k, 'v3, 'cmp) Base.Map.t @@ portable

partition_map t ~f = partition_mapi t ~f:(fun ~key:_ ~data -> f data)

Sourceval partition_result : ('k, ('ok, 'error) Base.Result.t, 'cmp) Base.Map.t -> ('k, 'ok, 'cmp) Base.Map.t * ('k, 'error, 'cmp) Base.Map.t @@ portable

partition_result t = partition_map t ~f:Result.to_either

Sourceval partitioni_tf : ('k, 'v, 'cmp) Base.Map.t -> f:(key:'k -> data:'v -> bool) @ local -> ('k, 'v, 'cmp) Base.Map.t * ('k, 'v, 'cmp) Base.Map.t @@ portable
  partitioni_tf t ~f
  = partition_mapi t ~f:(fun ~key ~data ->
    if f ~key ~data then First data else Second data)
Sourceval partition_tf : ('k, 'v, 'cmp) Base.Map.t -> f:('v -> bool) @ local -> ('k, 'v, 'cmp) Base.Map.t * ('k, 'v, 'cmp) Base.Map.t @@ portable

partition_tf t ~f = partitioni_tf t ~f:(fun ~key:_ ~data -> f data)

Sourceval combine_errors : ('k, 'v Base.Or_error.t, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t Base.Or_error.t @@ portable

Produces Ok of a map including all keys if all data is Ok, or an Error including all errors otherwise.

Sourceval unzip : ('k, 'v1 * 'v2, 'cmp) Base.Map.t -> ('k, 'v1, 'cmp) Base.Map.t * ('k, 'v2, 'cmp) Base.Map.t @@ portable

Given a map of tuples, produces a tuple of maps. Equivalent to: map t ~f:fst, map t ~f:snd

Sourceval compare_direct : ('v -> 'v -> int) -> ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> int @@ portable

Returns a total ordering between maps. The first argument is a total ordering used to compare data associated with equal keys in the two maps.

Hash function: a building block to use when hashing data structures containing maps in them. hash_fold_direct hash_fold_key is compatible with compare_direct iff hash_fold_key is compatible with (comparator m).compare of the map m being hashed.

Sourceval equal : ('v -> 'v -> bool) -> ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> bool @@ portable

equal cmp m1 m2 tests whether the maps m1 and m2 are equal, that is, contain the same keys and associate each key with the same value. cmp is the equality predicate used to compare the values associated with the keys.

Sourceval keys : ('k, _, _) Base.Map.t -> 'k list @@ portable

Returns a list of the keys in the given map.

Sourceval data : (_, 'v, _) Base.Map.t -> 'v list @@ portable

Returns a list of the data in the given map.

Sourceval to_alist : ?key_order:[ `Increasing | `Decreasing ] -> ('k, 'v, _) Base.Map.t -> ('k * 'v) list @@ portable

Creates an association list from the given map.

Additional operations on maps

Sourceval merge : ('k, 'v1, 'cmp) Base.Map.t -> ('k, 'v2, 'cmp) Base.Map.t -> f:(key:'k -> ('v1, 'v2) Base.Map.Merge_element.t -> 'v3 option) @ local -> ('k, 'v3, 'cmp) Base.Map.t @@ portable

Merges two maps. The runtime is O(length(t1) + length(t2)).

The merge_* functions immediately below perform better in cases where they are applicable. For merging a list of maps especially, use merge_disjoint_exn or merge_skewed instead. If you don't require the full generality of ~f's behavior, use merge_by_case.

Sourceval merge_disjoint_exn : ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t @@ portable

Merges two dictionaries with the same type of data and disjoint sets of keys. Raises if any keys overlap.

Sourceval merge_skewed : ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> combine:(key:'k -> 'v -> 'v -> 'v) @ local -> ('k, 'v, 'cmp) Base.Map.t @@ portable

A special case of merge, merge_skewed t1 t2 is a map containing all the bindings of t1 and t2. Bindings that appear in both t1 and t2 are combined into a single value using the combine function. In a call combine ~key v1 v2, the value v1 comes from t1 and v2 from t2.

The runtime of merge_skewed is O(min(l1, l2) * log(max(l1, l2))), where l1 is the length of t1 and l2 the length of t2. This is likely to be faster than merge when one of the maps is a lot smaller, or when you merge a list of maps.

Sourceval merge_by_case : ('k, 'v1, 'cmp) Base.Map.t -> ('k, 'v2, 'cmp) Base.Map.t -> first:('k, 'v1, 'v3) Base.Map.When_unmatched.t @ local -> second:('k, 'v2, 'v3) Base.Map.When_unmatched.t @ local -> both:('k, 'v1, 'v2, 'v3) Base.Map.When_matched.t @ local -> ('k, 'v3, 'cmp) Base.Map.t @@ portable

An alternative to merge that is more efficient when unmatched keys are unconditionally dropped or kept unchanged in the result.

Sourceval symmetric_diff : ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> data_equal:('v -> 'v -> bool) -> ('k, 'v) Base.Map.Symmetric_diff_element.t Base.Sequence.t @@ portable

symmetric_diff t1 t2 ~data_equal returns a list of changes between t1 and t2. It is intended to be efficient in the case where t1 and t2 share a large amount of structure. The keys in the output sequence will be in sorted order.

It is assumed that data_equal is at least as equating as physical equality: that phys_equal x y implies data_equal x y. Otherwise, symmetric_diff may behave in unexpected ways. For example, with ~data_equal:(fun _ _ -> false) it is NOT necessarily the case the resulting change sequence will contain an element (k, `Unequal _) for every key k shared by both maps.

Warning: Float equality violates this property! phys_equal Float.nan Float.nan is true, but Float.(=) Float.nan Float.nan is false.

Sourceval fold_symmetric_diff : ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> data_equal:('v -> 'v -> bool) @ local -> init:'acc -> f:('acc -> ('k, 'v) Base.Map.Symmetric_diff_element.t -> 'acc) @ local -> 'acc @@ portable

fold_symmetric_diff t1 t2 ~data_equal folds across an implicit sequence of changes between t1 and t2, in sorted order by keys. Equivalent to Sequence.fold (symmetric_diff t1 t2 ~data_equal), and more efficient.

Sourceval min_elt : ('k, 'v, _) Base.Map.t -> ('k * 'v) option @@ portable

min_elt map returns Some (key, data) pair corresponding to the minimum key in map, or None if empty.

Sourceval min_elt_exn : ('k, 'v, _) Base.Map.t -> 'k * 'v @@ portable
Sourceval max_elt : ('k, 'v, _) Base.Map.t -> ('k * 'v) option @@ portable

max_elt map returns Some (key, data) pair corresponding to the maximum key in map, or None if map is empty.

Sourceval max_elt_exn : ('k, 'v, _) Base.Map.t -> 'k * 'v @@ portable
Sourceval transpose_keys : ('k2, 'cmp2) Base.Comparator.Module.t -> ('k1, ('k2, 'v, 'cmp2) Base.Map.t, 'cmp1) Base.Map.t -> ('k2, ('k1, 'v, 'cmp1) Base.Map.t, 'cmp2) Base.Map.t @@ portable

Swap the inner and outer keys of nested maps. If transpose_keys m a = b, then find_exn (find_exn a i) j = find_exn (find_exn b j) i.

These functions have the same semantics as similar functions in List.

Sourceval for_all : ('k, 'v, _) Base.Map.t -> f:('v -> bool) @ local -> bool @@ portable
Sourceval for_alli : ('k, 'v, _) Base.Map.t -> f:(key:'k -> data:'v -> bool) @ local -> bool @@ portable
Sourceval exists : ('k, 'v, _) Base.Map.t -> f:('v -> bool) @ local -> bool @@ portable
Sourceval existsi : ('k, 'v, _) Base.Map.t -> f:(key:'k -> data:'v -> bool) @ local -> bool @@ portable
Sourceval count : ('k, 'v, _) Base.Map.t -> f:('v -> bool) @ local -> int @@ portable
Sourceval counti : ('k, 'v, _) Base.Map.t -> f:(key:'k -> data:'v -> bool) @ local -> int @@ portable
Sourceval sum : (module Base.Container.Summable with type t = 'a) -> ('k, 'v, _) Base.Map.t -> f:('v -> 'a) @ local -> 'a @@ portable
Sourceval sumi : (module Base.Container.Summable with type t = 'a) -> ('k, 'v, _) Base.Map.t -> f:(key:'k -> data:'v -> 'a) @ local -> 'a @@ portable
Sourceval split : ('k, 'v, 'cmp) Base.Map.t -> 'k -> ('k, 'v, 'cmp) Base.Map.t * ('k * 'v) option * ('k, 'v, 'cmp) Base.Map.t @@ portable

split t key returns a map of keys strictly less than key, the mapping of key if any, and a map of keys strictly greater than key. Runtime is O(log n), where n is the size of the input map.

Sourceval split_le_gt : ('k, 'v, 'cmp) Base.Map.t -> 'k -> ('k, 'v, 'cmp) Base.Map.t * ('k, 'v, 'cmp) Base.Map.t @@ portable

split_le_gt t key returns a map of keys that are less or equal to key and a map of keys strictly greater than key.

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.

Sourceval split_lt_ge : ('k, 'v, 'cmp) Base.Map.t -> 'k -> ('k, 'v, 'cmp) Base.Map.t * ('k, 'v, 'cmp) Base.Map.t @@ portable

split_lt_ge t key returns a map of keys strictly less than key and a map of keys that are greater or equal to key.

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.

Sourceval split_n : ('k, 'v, 'cmp) Base.Map.t -> int -> ('k, 'v, 'cmp) Base.Map.t * ('k, 'v, 'cmp) Base.Map.t @@ portable

split_n t n = l, r such that append ~lower_part:l ~upper_part:r = `Ok t and length l = n, or as close as possible if n < 0 or n > length t.

Sourceval count_lt : ('k, 'v, 'cmp) Base.Map.t -> 'k -> int @@ portable

count_lt t key returns the number of keys in t that are strictly less than key.

Runtime is O(log n) where n is the size of the input map.

Sourceval count_le : ('k, 'v, 'cmp) Base.Map.t -> 'k -> int @@ portable

count_le t key returns the number of keys in t that are less than or equal to key.

Runtime is O(log n) where n is the size of the input map.

Sourceval count_gt : ('k, 'v, 'cmp) Base.Map.t -> 'k -> int @@ portable

count_gt t key returns the number of keys in t that are strictly greater than key.

Runtime is O(log n) where n is the size of the input map.

Sourceval count_ge : ('k, 'v, 'cmp) Base.Map.t -> 'k -> int @@ portable

count_ge t key returns the number of keys in t that are greater than or equal to key.

Runtime is O(log n) where n is the size of the input map.

Sourceval append : lower_part:('k, 'v, 'cmp) Base.Map.t -> upper_part:('k, 'v, 'cmp) Base.Map.t -> [ `Ok of ('k, 'v, 'cmp) Base.Map.t | `Overlapping_key_ranges ] @@ portable

append ~lower_part ~upper_part returns `Ok map where map contains all the (key, value) pairs from the two input maps if all the keys from lower_part are less than all the keys from upper_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.merge or repeated Map.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)
Sourceval subrange : ('k, 'v, 'cmp) Base.Map.t -> lower_bound:'k Base.Maybe_bound.t -> upper_bound:'k Base.Maybe_bound.t -> ('k, 'v, 'cmp) Base.Map.t @@ portable

subrange t ~lower_bound ~upper_bound returns a map containing all the entries from t whose keys lie inside the interval indicated by ~lower_bound and ~upper_bound. If this interval is empty, an empty map is returned.

Runtime is O(log n), where n is the size of the input map.

Sourceval fold_range_inclusive : ('k, 'v, 'cmp) Base.Map.t -> min:'k -> max:'k -> init:'acc -> f:(key:'k -> data:'v -> 'acc -> 'acc) @ local -> 'acc @@ portable

fold_range_inclusive t ~min ~max ~init ~f folds f (with initial value ~init) over all keys (and their associated values) that are in the range [min, max] (inclusive).

Sourceval range_to_alist : ('k, 'v, 'cmp) Base.Map.t -> min:'k -> max:'k -> ('k * 'v) list @@ portable

range_to_alist t ~min ~max returns an associative list of the elements whose keys lie in [min, max] (inclusive), with the smallest key being at the head of the list.

Sourceval closest_key : ('k, 'v, 'cmp) Base.Map.t -> [ `Greater_or_equal_to | `Greater_than | `Less_or_equal_to | `Less_than ] -> 'k -> ('k * 'v) option @@ portable

closest_key t dir k returns the (key, value) pair in t with key closest to k that satisfies the given inequality bound.

For example, closest_key t `Less_than k would be the pair with the closest key to k where key < k.

to_sequence can be used to get the same results as closest_key. It is less efficient for individual lookups but more efficient for finding many elements starting at some value.

Sourceval nth : ('k, 'v, _) Base.Map.t -> int -> ('k * 'v) option @@ portable

nth t n finds 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.

Sourceval nth_exn : ('k, 'v, _) Base.Map.t -> int -> 'k * 'v @@ portable
Sourceval rank : ('k, 'v, 'cmp) Base.Map.t -> 'k -> int option @@ portable

rank t k If k is in t, returns the number of keys strictly less than k in t, and None otherwise.

Sourceval to_sequence : ?order:[ `Increasing_key | `Decreasing_key ] -> ?keys_greater_or_equal_to:'k -> ?keys_less_or_equal_to:'k -> ('k, 'v, 'cmp) Base.Map.t -> ('k * 'v) Base.Sequence.t @@ portable

to_sequence ?order ?keys_greater_or_equal_to ?keys_less_or_equal_to t gives a sequence of key-value pairs between keys_less_or_equal_to and keys_greater_or_equal_to inclusive, presented in order. If keys_greater_or_equal_to > keys_less_or_equal_to, the sequence is empty.

When neither keys_greater_or_equal_to nor keys_less_or_equal_to are provided, the cost is O(log n) up front and amortized O(1) to produce each element. If either is provided (and is used by the order parameter provided), then the the cost is O(n) up front, and amortized O(1) to produce each element.

binary_search t ~compare which elt returns the (key, value) pair in t specified by compare and which, if one exists.

t must be sorted in increasing order according to compare, where compare and elt divide t into three (possibly empty) segments:

  |  < elt  |  = elt  |  > elt  |

binary_search returns an element on the boundary of segments as specified by which. See the diagram below next to the which variants.

binary_search does not check that compare orders t, and behavior is unspecified if compare doesn't order t. Behavior is also unspecified if compare mutates t.

Sourceval binary_search_segmented : ('k, 'v, 'cmp) Base.Map.t -> segment_of:(key:'k -> data:'v -> [ `Left | `Right ]) @ local -> [ `Last_on_left | `First_on_right ] -> ('k * 'v) option @@ portable

binary_search_segmented t ~segment_of which takes a segment_of function that divides t into two (possibly empty) segments:

  | segment_of elt = `Left | segment_of elt = `Right |

binary_search_segmented returns the (key, value) pair on the boundary of the segments as specified by which: `Last_on_left yields the last element of the left segment, while `First_on_right yields the first element of the right segment. It returns None if the segment is empty.

binary_search_segmented does not check that segment_of segments t as in the diagram, and behavior is unspecified if segment_of doesn't segment t. Behavior is also unspecified if segment_of mutates t.

Sourceval binary_search_subrange : ('k, 'v, 'cmp) Base.Map.t -> compare:(key:'k -> data:'v -> 'bound -> int) @ local -> lower_bound:'bound Base.Maybe_bound.t -> upper_bound:'bound Base.Maybe_bound.t -> ('k, 'v, 'cmp) Base.Map.t @@ portable

binary_search_subrange takes a compare function that divides t into three (possibly empty) segments with respect to lower_bound and upper_bound:

  | Below_lower_bound | In_range | Above_upper_bound |

and returns a map of the In_range segment.

Runtime is O(log m + n) where m is the length of the input map and n is the length of the output. The linear term in n is to compute the length of the output.

Behavior is undefined if compare does not segment t as shown above, or if compare mutates its inputs.

Sourcemodule Make_applicative_traversals (A : sig ... end) : sig ... end

Creates traversals to reconstruct a map within an applicative. Uses Lazy_applicative so that the map can be traversed within the applicative, rather than needing to be traversed all at once, outside the applicative.

Sourcemodule M (K : sig ... end) : sig ... end

M is meant to be used in combination with OCaml applicative functor types:

include Base.Map.For_deriving with type ('key, 'value, 'cmp) t := ('key, 'value, 'cmp) Base.Map.t
Sourcemodule type Sexp_of_m = Base.Sexpable.Sexp_of
Sourcemodule type M_of_sexp = sig ... end
Sourcemodule type M_sexp_grammar = sig ... end
Sourcemodule type Compare_m = sig ... end
Sourcemodule type Equal_m = sig ... end
Sourcemodule type Hash_fold_m = Base.Hasher.S
Sourcemodule type Globalize_m = sig ... end
Sourceval sexp_of_m__t : (module Base.Map.Sexp_of_m with type t = 'k) -> ('v -> Sexplib0.Sexp.t) -> ('k, 'v, 'cmp) Base.Map.t -> Sexplib0.Sexp.t
Sourceval m__t_of_sexp : (module Base.Map.M_of_sexp with type comparator_witness = 'cmp and type t = 'k) -> (Sexplib0.Sexp.t -> 'v) -> Sexplib0.Sexp.t -> ('k, 'v, 'cmp) Base.Map.t
Sourceval m__t_sexp_grammar : (module Base.Map.M_sexp_grammar with type t = 'k) -> 'v Sexplib0.Sexp_grammar.t -> ('k, 'v, 'cmp) Base.Map.t Sexplib0.Sexp_grammar.t @@ portable
Sourceval compare_m__t : (module Base.Map.Compare_m) -> ('v -> 'v -> int) -> ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> int
Sourceval compare_m__t__local : (module Base.Map.Compare_m) -> ('v @ local -> 'v @ local -> int) -> ('k, 'v, 'cmp) Base.Map.t @ local -> ('k, 'v, 'cmp) Base.Map.t @ local -> int
Sourceval equal_m__t : (module Base.Map.Equal_m) -> ('v -> 'v -> bool) -> ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Base.Map.t -> bool
Sourceval equal_m__t__local : (module Base.Map.Equal_m) -> ('v @ local -> 'v @ local -> bool) -> ('k, 'v, 'cmp) Base.Map.t @ local -> ('k, 'v, 'cmp) Base.Map.t @ local -> bool
Sourceval globalize_m__t : (module Base.Map.Globalize_m) -> _ -> ('k, 'v, 'cmp) Base.Map.t @ local -> ('k, 'v, 'cmp) Base.Map.t
Sourcemodule Tree : sig ... end
Sourcemodule Using_comparator : sig ... end

Using_comparator is a similar interface as the toplevel of Map, except the functions take a ~comparator:('k, 'cmp) Comparator.t, whereas the functions at the toplevel of Map take a ('k, 'cmp) comparator.

A polymorphic Map.

Sourceval of_tree : ('k, 'cmp) Base.Comparator.Module.t -> ('k, 'v, 'cmp) Using_comparator.Tree.t -> ('k, 'v, 'cmp) Base.Map.t @@ portable

Create a map from a tree using the given comparator.

Sourceval to_tree : ('k, 'v, 'cmp) Base.Map.t -> ('k, 'v, 'cmp) Using_comparator.Tree.t @@ portable

Extract a tree from a map.