package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatHashtbl/index.html
Module BatHashtbl
Source
Extra functions over hashtables.
Operations over hashtables.
This module replaces Stdlib's Hashtbl module. All functions and types are provided here.
Base operations
Hashtbl.create n
creates a new, empty hash table, with initial size n
. For best results, n
should be on the order of the expected number of elements that will be in the table. The table grows as needed, so n
is just an initial guess.
Hashtbl.length tbl
returns the number of bindings in tbl
. Multiple bindings are counted multiply, so Hashtbl.length
gives the number of times Hashtbl.iter
calls its first argument.
Hashtbl.is_empty tbl
returns true
if there are no bindings in tbl
, false otherwise.
Hashtbl.add tbl x y
adds a binding of x
to y
in table tbl
. Previous bindings for x
are not removed, but simply hidden. That is, after performing Hashtbl.remove
tbl x
, the previous binding for x
, if any, is restored. (Same behavior as with association lists.)
Hashtbl.remove tbl x
removes the current binding of x
in tbl
, restoring the previous binding if it exists. It does nothing if x
is not bound in tbl
.
Hashtbl.replace tbl x y
replaces the current binding of x
in tbl
by a binding of x
to y
. If x
is unbound in tbl
, a binding of x
to y
is added to tbl
. This is functionally equivalent to Hashtbl.remove
tbl x
followed by Hashtbl.add
tbl x y
.
Hashtbl.modify k f tbl
replaces the first binding for k
in tbl
with f
applied to that value.
Hashtbl.modify_def v k f tbl
does the same as Hashtbl.modify k f tbl
but f v
is inserted in tbl
if k
was unbound.
Hashtbl.modify_opt k f tbl
allows to remove, modify or add a binding for k
in tbl
. f
will be called with None
if k
was unbound. first previous binding of k
in tbl
will be deleted if f
returns None
. Otherwise, the previous binding is replaced by the value produced by f
.
Empty a hash table and shrink the size of the bucket table to its initial size.
Hashtbl.stats tbl
returns statistics about the table tbl
: number of buckets, size of the biggest bucket, distribution of buckets by size.
Sequences
Iterate on the whole table. The order in which the bindings appear in the sequence is unspecified. However, if the table contains several bindings for the same key, they appear in reversed order of introduction, that is, the most recent binding appears first.
The behavior is not specified if the hash table is modified during the iteration.
Add the given bindings to the table, using add
Add the given bindings to the table, using replace
Build a table from the given bindings. The bindings are added in the same order they appear in the sequence, using replace_seq
, which means that if two pairs have the same key, only the latest one will appear in the table.
Enumerations
Return an enumeration of all the keys of a hashtable. If the key is in the Hashtable multiple times, all occurrences will be returned.
Return an enumeration of all the values of a hashtable.
Return an enumeration of (key,value) pairs of a hashtable.
Create a hashtable from a (key,value) enumeration.
Lists
Create a hashtable from a list of (key,value) pairs.
Searching
Hashtbl.find tbl x
returns the current binding of x
in tbl
, or raises Not_found
if no such binding exists.
Hashtbl.find_all tbl x
returns the list of all data associated with x
in tbl
. The current binding is returned first, then the previous bindings, in reverse order of introduction in the table.
Hashtbl.find_default tbl key default
finds a binding for key
, or return default
if key
is unbound in tbl
.
Find a binding for the key, or return None
if no value is found
Stdlib
-compatible alias for find_option
.
Check if at least one key-value pair satisfies p k v
Traversing
A number of higher-order functions are provided to allow purely functional traversal or transformation of hashtables. These functions are similar to their counterparts in module BatEnum
.
Whenever you wish to traverse or transfor a hashtable, you have the choice between using the more general functions of BatEnum
, with keys
, values
, enum
and of_enum
, or the more optimized functions of this section.
If you are new to OCaml or unsure about data structure, using the functions of BatEnum
is a safe bet. Should you wish to improve performance at the cost of generality, you will always be able to rewrite your code to make use of the functions of this section.
Hashtbl.iter f tbl
applies f
to all bindings in table tbl
. f
receives the key as first argument, and the associated value as second argument. Each binding is presented exactly once to f
. The order in which the bindings are passed to f
is unspecified. However, if the table contains several bindings for the same key, they are passed to f
in reverse order of introduction, that is, the most recent binding is passed first.
Hashtbl.for_all p tbl
check if the predicate p k v
holds for all bindings currently in tbl
.
Hashtbl.fold f tbl init
computes (f kN dN ... (f k1 d1 (f k0 d0 init))...)
, where k0,k1..kN
are the keys of all bindings in tbl
, and d0,d1..dN
are the associated values. Each binding is presented exactly once to f
. The order in which the bindings are passed to f
is unspecified. However, if the table contains several bindings for the same key, they are passed to f
in reverse order of introduction, that is, the most recent binding is passed first.
map f x
creates a new hashtable with the same keys as x
, but with the function f
applied to all the values
map_inplace f x
replace all values currently bound in x
by f
applied to each value.
filter f m
returns a new hashtable where only the values a
of m
such that f a = true
remain.
filter_inplace f m
removes from m
all bindings that does not satisfy the predicate f.
filteri f m
returns a hashtbl where only the key, values pairs key
, a
of m
such that f key a = true
remain.
filteri_inplace f m
performs as filter_inplace but f
receive the value in additiuon to the key.
filter_map f m
combines the features of filteri
and map
. It calls f key0 a0
, f key1 a1
, f keyn an
where a0,a1..an
are the elements of m
and key0..keyn
the corresponding keys. It returns a hashtbl with associations keyi
,bi
where f keyi ai = Some bi
. When f
returns None
, the corresponding element of m
is discarded.
filter_map_inplace f m
performs like filter_map but modify m
inplace instead of creating a new Hashtbl.
val merge :
('a -> 'b option -> 'c option -> 'd option) ->
('a, 'b) t ->
('a, 'c) t ->
('a, 'd) t
merge f a b
returns a new Hashtbl which is build from the bindings of a
and b
according to the function f
, that is given all defined keys one by one, along with the value from a
(if defined) and the value from b
(if defined), and has to return the (optional) resulting value.
It is assumed that each key is bound at most once in a
and b
. See merge_all
for a more general alternative if this is not the case.
val merge_all :
('a -> 'b list -> 'c list -> 'd list) ->
('a, 'b) t ->
('a, 'c) t ->
('a, 'd) t
merge_all f a b
is similar to merge
, but passes to f
all bindings for a key (most recent first, as returned by find_all
). f
must then return all the new bindings of the merged hashtable (or an empty list if that key should not be bound in the resulting hashtable). Those new bindings will be inserted in reverse, so that the head of the list will become the most recent binding in the merged hashtable.
The polymorphic hash primitive
Hashtbl.hash x
associates a positive integer to any value of any type. It is guaranteed that if x = y
or Pervasives.compare x y = 0
, then hash x = hash y
. Moreover, hash
always terminates, even on cyclic structures.
Boilerplate code
Printing
val print :
?first:string ->
?last:string ->
?sep:string ->
?kvsep:string ->
('a BatInnerIO.output -> 'b -> unit) ->
('a BatInnerIO.output -> 'c -> unit) ->
'a BatInnerIO.output ->
('b, 'c) t ->
unit
Override modules
The following modules replace functions defined in Hashtbl
with functions behaving slightly differently but having the same name. This is by design: the functions meant to override the corresponding functions of Hashtbl
.
Operations on Hashtbl
without exceptions.
Infix operators over a BatHashtbl
Functorial interface
The output signature of the functor Hashtbl.Make
.
Functor building an implementation of the hashtable structure. The functor Hashtbl.Make
returns a structure containing a type key
of keys and a type 'a t
of hash tables associating data of type 'a
to keys of type key
. The operations perform similarly to those of the generic interface, but use the hashing and equality functions specified in the functor argument H
instead of generic equality and hashing.