module BatHashtbl:`sig`

..`end`

Extra functions over hashtables.

Operations over hashtables.

This module replaces Stdlib's Hashtbl module. All functions and types are provided here.

type`('a, 'b)`

t =`('a, 'b) Stdlib.Hashtbl.t`

A Hashtable wth keys of type 'a and values 'b

`val create : ``int -> ('a, 'b) t`

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

`val length : ``('a, 'b) t -> int`

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

`val is_empty : ``('a, 'b) t -> bool`

`Hashtbl.is_empty tbl`

returns `true`

if there are no bindings
in `tbl`

, false otherwise.

`val add : ``('a, 'b) t -> 'a -> 'b -> unit`

`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.)

`val remove : ``('a, 'b) t -> 'a -> unit`

`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`

.

`val remove_all : ``('a, 'b) t -> 'a -> unit`

Remove all bindings for the given key

`val replace : ``('a, 'b) t -> 'a -> 'b -> unit`

`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`

.

`val modify : ``'a -> ('b -> 'b) -> ('a, 'b) t -> unit`

`Hashtbl.modify k f tbl`

replaces the first binding for `k`

in `tbl`

with `f`

applied to that value.

**Since**2.1**Raises**`Not_found`

if`k`

is unbound in`tbl`

.

`val modify_def : ``'b -> 'a -> ('b -> 'b) -> ('a, 'b) t -> unit`

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

**Since**2.1

`val modify_opt : ``'a -> ('b option -> 'b option) -> ('a, 'b) t -> unit`

`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`

.

**Since**2.1

`val copy : ``('a, 'b) t -> ('a, 'b) t`

Return a copy of the given hashtable.

`val clear : ``('a, 'b) t -> unit`

Empty a hash table.

`val keys : ``('a, 'b) t -> 'a BatEnum.t`

Return an enumeration of all the keys of a hashtable. If the key is in the Hashtable multiple times, all occurrences will be returned.

`val values : ``('a, 'b) t -> 'b BatEnum.t`

Return an enumeration of all the values of a hashtable.

`val enum : ``('a, 'b) t -> ('a * 'b) BatEnum.t`

Return an enumeration of (key,value) pairs of a hashtable.

`val of_enum : ``('a * 'b) BatEnum.t -> ('a, 'b) t`

Create a hashtable from a (key,value) enumeration.

`val of_list : ``('a * 'b) list -> ('a, 'b) t`

Create a hashtable from a list of (key,value) pairs.

**Since**2.6.0

`val to_list : ``('a, 'b) t -> ('a * 'b) list`

Return the list of (key,value) pairs.

**Since**2.6.0

`val bindings : ``('a, 'b) t -> ('a * 'b) list`

Alias for `to_list`

.

**Since**2.6.0

`val find : ``('a, 'b) t -> 'a -> 'b`

`Hashtbl.find tbl x`

returns the current binding of `x`

in `tbl`

,
or raises `Not_found`

if no such binding exists.

`val find_all : ``('a, 'b) t -> 'a -> 'b list`

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

`val find_default : ``('a, 'b) t -> 'a -> 'b -> 'b`

`Hashtbl.find_default tbl key default`

finds a binding for `key`

,
or return `default`

if `key`

is unbound in `tbl`

.

`val find_option : ``('a, 'b) Stdlib.Hashtbl.t -> 'a -> 'b option`

Find a binding for the key, or return `None`

if no
value is found

`val mem : ``('a, 'b) t -> 'a -> bool`

`Hashtbl.mem tbl x`

checks if `x`

is bound in `tbl`

.

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
`BatHashtbl.keys`

, `BatHashtbl.values`

, `BatHashtbl.enum`

and `BatHashtbl.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.

`val iter : ``('a -> 'b -> unit) -> ('a, 'b) t -> unit`

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

`val fold : ``('a -> 'b -> 'c -> 'c) -> ('a, 'b) t -> 'c -> 'c`

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

`val map : ``('a -> 'b -> 'c) -> ('a, 'b) t -> ('a, 'c) t`

`map f x`

creates a new hashtable with the same
keys as `x`

, but with the function `f`

applied to
all the values

`val map_inplace : ``('a -> 'b -> 'b) -> ('a, 'b) t -> unit`

`map_inplace f x`

replace all values currently bound in `x`

by `f`

applied to each value.

**Since**2.1

`val filter : ``('a -> bool) -> ('key, 'a) t -> ('key, 'a) t`

`filter f m`

returns a new hashtable where only the values `a`

of `m`

such that `f a = true`

remain.

`val filter_inplace : ``('a -> bool) -> ('key, 'a) t -> unit`

`filter_inplace f m`

removes from `m`

all bindings that does not
satisfy the predicate f.

**Since**2.1

`val filteri : ``('key -> 'a -> bool) -> ('key, 'a) t -> ('key, 'a) t`

`filteri f m`

returns a hashtbl where only the key, values pairs
`key`

, `a`

of `m`

such that `f key a = true`

remain.

`val filteri_inplace : ``('key -> 'a -> bool) -> ('key, 'a) t -> unit`

`filteri_inplace f m`

performs as filter_inplace but `f`

receive the value in additiuon to the key.

**Since**2.1

`val filter_map : ``('key -> 'a -> 'b option) ->`

('key, 'a) t -> ('key, 'b) t

`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 =`

. When

Some bi`f`

returns `None`

, the corresponding element of
`m`

is discarded.

`val filter_map_inplace : ``('key -> 'a -> 'a option) -> ('key, 'a) t -> unit`

`filter_map_inplace f m`

performs like filter_map but modify `m`

inplace instead of creating a new Hashtbl.

`val hash : ``'a -> int`

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

`val hash_param : ``int -> int -> 'a -> int`

`Hashtbl.hash_param n m x`

computes a hash value for `x`

, with the
same properties as for `hash`

. The two extra parameters `n`

and
`m`

give more precise control over hashing. Hashing performs a
depth-first, right-to-left traversal of the structure `x`

, stopping
after `n`

meaningful nodes were encountered, or `m`

nodes,
meaningful or not, were encountered. Meaningful nodes are: integers;
floating-point numbers; strings; characters; booleans; and constant
constructors. Larger values of `m`

and `n`

means that more
nodes are taken into account to compute the final hash
value, and therefore collisions are less likely to happen.
However, hashing takes longer. The parameters `m`

and `n`

govern the tradeoff between accuracy and speed.

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

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`

.

module Exceptionless:`sig`

..`end`

Operations on `Hashtbl`

without exceptions.

module Infix:`sig`

..`end`

Infix operators over a `BatHashtbl`

module Labels:`sig`

..`end`

Operations on `Hashtbl`

with labels.

module type HashedType =`sig`

..`end`

module type S =`sig`

..`end`

The output signature of the functor `Hashtbl.Make`

.

module Make:

Functor building an implementation of the hashtable structure.

module Cap:`sig`

..`end`

Capabilities for hashtables.