This is documentation for version v0.9.0 of patricia-tree,
but the latest version is
v0.11.0.
Click here
to redirect to the latest version.
Patricia Tree API - Map2
include NODE with type 'a key = 'a key
Types
type 'a key = 'a keyThe type of keys.
The type of value, which depends on the type of the key and the type of the map.
Constructors: build values
val empty : 'map tThe empty map
A singleton leaf, similar to BASE_MAP.singleton
A branch node. This shouldn't be called externally unless you know what you're doing! Doing so could easily break the data structure's invariants.
When called, it assumes that:
- Neither
tree0nortree1should be empty. branching_bitshould have a single bit setprefixshould be normalized (bits belowbranching_bitset to zero)- All elements of
tree0should have theirto_intstart byprefixfollowed by 0 at positionbranching_bit). - All elements of
tree1should have theirto_intstart byprefixfollowed by 0 at positionbranching_bit).
Destructors: access the value
type 'map view = private | Empty : 'map view(*Can happen only at the toplevel: there is no empty interior node.
*)| Branch : {} -> 'map view(*Branching bit contains only one bit set; the corresponding mask is (branching_bit - 1). The prefixes are normalized: the bits below the branching bit are set to zero (i.e. prefix & (branching_bit - 1) = 0).
*)| Leaf : {} -> 'map view(*A key -> value mapping.
*)
This makes the map nodes accessible to the pattern matching algorithm; this corresponds 1:1 to the SimpleNode implementation. This just needs to be copy-and-pasted for every node type.
val is_empty : 'map t -> boolCheck if the map is empty. Should be constant time.
Existential wrapper for the 'a parameter in a 'a key, ('a,'map) value pair
Basic functions
val min_binding : 'a t -> 'a key_value_pairval max_binding : 'a t -> 'a key_value_pairval cardinal : 'a t -> intThe size of the map, O(n) complexity
val is_singleton : 'a t -> 'a key_value_pair optionis_singleton m returns Some(KeyValue(k,v)) if and only if m contains a unique binding k->v.
Same as find, but returns None for Not_found
mem key map returns true iff key is bound in map, O(log(n)) complexity.
Returns a map with the element removed, O(log(n)) complexity. Returns a physically equal map if the element is absent.
val pop_minimum : 'map t -> ('map key_value_pair * 'map t) optionpop_minimum m returns None if is_empty m, or Some(key,value,m') where (key,value) = min_binding m and m' = remove m key. O(log(n)) complexity.
val pop_maximum : 'map t -> ('map key_value_pair * 'map t) optionpop_maximum m returns None if is_empty m, or Some(key,value,m') where (key,value) = max_binding m and m' = remove m key. O(log(n)) complexity.
insert key f map modifies or insert an element of the map; f takes None if the value was not previously bound, and Some old where old is the previously bound value otherwise. The function preserves physical equality when possible. O(log(n)) complexity. Preserves physical equality if the new value is physically equal to the old.
update key f map modifies, insert, or remove an element from the map; f takes None if the value was not previously bound, and Some old where old is the previously bound value otherwise. The function preserves physical equality when possible. It returns None if the element should be removed O(log(n)) complexity. Preserves physical equality if the new value is physically equal to the old.
Unconditionally adds a value in the map (independently from whether the old value existed). O(log(n)) complexity. Preserves physical equality if the new value is physically equal to the old.
Iterators
split key map splits the map into:
- submap of
mapwhose keys are smaller thankey - value associated to
key(if present) - submap of
mapwhose keys are bigger thankeyWhere the order is given byKey.to_int.
iter f m calls f.f on all bindings of m, in the order given by Key.to_int
fold f m acc returns f.f key_n value_n (... (f.f key_1 value_1 acc)) where (key_1, value_1) ... (key_n, value_n) are the bindings of m, in the order given by Key.to_int.
val filter : 'map polypredicate -> 'map t -> 'map tfilter f m returns the submap of m containing the bindings k->v such that f.f k v = true. f.f is called in the order given by Key.to_int
val for_all : 'map polypredicate -> 'map t -> boolfor_all f m checks that f holds on all bindings of m7 Short-circuiting.
In the following, the *no_share function allows taking arguments of different types (but cannot share subtrees of the map), while the default functions attempt to preserve and benefit from sharing the subtrees (using physical equality to detect sharing).
map f m and map_no_share f m replace all bindings (k,v) by (k, f.f v). Bindings are examined in the order given by Key.to_int.
mapi f m and mapi_no_share f m replace all bindings (k,v) by (k, f.f k v). Bindings are examined in the order given by Key.to_int.
val filter_map : ('map, 'map) polyfilter_map -> 'map t -> 'map tfilter_map m f and filter_map_no_share m f remove the bindings (k,v) for which f.f k v is None, and replaces the bindings (k,v) for which f.f k v is Some v' by (k,v'). Bindings are examined in the order given by Key.to_int.
val pretty :
?pp_sep:(Stdlib.Format.formatter -> unit -> unit) ->
'map polypretty ->
Stdlib.Format.formatter ->
'map t ->
unitPretty-prints a map using the given formatter. pp_sep is called once between each binding, it defaults to Format.pp_print_cut. Bindings are printed in the order given by Key.to_int
Functions on pairs of maps
val reflexive_same_domain_for_all2 :
('map, 'map) polysame_domain_for_all2 ->
'map t ->
'map t ->
boolreflexive_same_domain_for_all2 f m1 m2 is true if and only if
m1andm2have the same domain (set of keys)- for all bindings
(k, v1)inm1and(k, v2)inm2,f.f k v1 v2holds @assumesf.fis reflexive, i.e.f.f k v v = trueto skip calls to equal subtrees. Callsf.fin ascending order ofKey.to_int. Exits early if the domains mismatch.
It is useful to implement equality on maps:
let equal m1 m2 = reflexive_same_domain_for_all2
{ f = fun _ v1 v2 -> Value.equal v1 v2}
m1 m2val nonreflexive_same_domain_for_all2 :
('map1, 'map2) polysame_domain_for_all2 ->
'map1 t ->
'map2 t ->
boolnonreflexive_same_domain_for_all2 f m1 m2 is the same as reflexive_same_domain_for_all2, but doesn't assume f.f is reflexive. It thus calls f.f on every binding, in ascending order of Key.to_int. Exits early if the domains mismatch.
val reflexive_subset_domain_for_all2 :
('map, 'map) polysame_domain_for_all2 ->
'map t ->
'map t ->
boolreflexive_subset_domain_for_all2 f m1 m2 is true if and only if
m1's domain is a subset ofm2's. (all keys defined inm1are also defined inm2)- for all bindings
(k, v1)inm1and(k, v2)inm2,f.f k v1 v2holds @assumesf.fis reflexive, i.e.f.f k v v = trueto skip calls to equal subtrees. Callsf.fin ascending order ofKey.to_int. Exits early if the domains mismatch.
idempotent_union f map1 map2 returns a map whose keys is the union of the keys of map1 and map2. f.f is used to combine the values of keys mapped in both maps. @assumes f.f idempotent (i.e. f key value value == value) f.f is called in the order given by Key.to_int. f.f is never called on physically equal values. Preserves physical equality as much as possible. Complexity is O(log(n)*Delta) where Delta is the number of different keys between map1 and map2.
idempotent_inter f map1 map2 returns a map whose keys is the intersection of the keys of map1 and map2. f.f is used to combine the values a key is mapped in both maps. @assumes f.f idempotent (i.e. f key value value == value) f.f is called in the order given by Key.to_int. f.f is never called on physically equal values. Preserves physical equality as much as possible. Complexity is O(log(n)*Delta) where Delta is the number of different keys between map1 and map2.
nonidempotent_inter_no_share f map1 map2 is the same as idempotent_inter but doesn't preverse physical equality, doesn't assume f.f is idempotent, and can change the type of values. f.f is called on every shared binding. f.f is called in increasing order of keys. O(n) complexity
val idempotent_inter_filter :
('a, 'a, 'a) polyinterfilter ->
'a t ->
'a t ->
'a tidempotent_inter_filter f map1 map2 is the same as idempotent_inter but f.f can return None to remove a binding from the resutling map.
This is the same as Stdlib.Map.S.merge
Conversion functions
val to_seq : 'a t -> 'a key_value_pair Stdlib.Seq.tto_seq m iterates the whole map, in increasing order of Key.to_int
val to_rev_seq : 'a t -> 'a key_value_pair Stdlib.Seq.tto_rev_seq m iterates the whole map, in decreasing order of Key.to_int
val add_seq : 'a key_value_pair Stdlib.Seq.t -> 'a t -> 'a tadd_seq s m adds all bindings of the sequence s to m in order.
val of_seq : 'a key_value_pair Stdlib.Seq.t -> 'a tof_seq s creates a new map from the bindings of s. If a key is bound multiple times in s, the latest binding is kept
val of_list : 'a key_value_pair list -> 'a tof_list l creates a new map from the bindings of l. If a key is bound multiple times in l, the latest binding is kept
val to_list : 'a t -> 'a key_value_pair listto_list m returns the bindings of m as a list, in increasing order of Key.to_int