Module BatHashtbl.Cap


module Cap: sig .. end
Capabilities for hashtables.

type ('a, 'b, [< `Read | `Write ]) t 
The type of a hashtable.

Constructors

val create : int -> ('a, 'b, [< `Read | `Write ]) 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 of_table : ('a, 'b) Hashtbl.t -> ('a, 'b, [< `Read | `Write ]) t
Adopt a regular hashtable as a capability hashtble, allowing to decrease capabilities if necessary.

This operation involves no copying. In other words, in let cap = of_table a in ..., any modification in a will also have effect on cap and reciprocally.

val to_table : ('a, 'b, [ `Read | `Write ]) t -> ('a, 'b) Hashtbl.t
Return a capability hashtable as a regular hashtable.

This operation requires both read and write permissions on the capability table and involves no copying. In other words, in let a = of_table cap in ..., any modification in a will also have effect on cap and reciprocally.

val read_only : ('a, 'b, [< `Read | `Write > `Read ]) t ->
('a, 'b, [ `Read ]) t
Drop to read-only permissions.

This operation involves no copying.

val write_only : ('a, 'b, [< `Read | `Write > `Write ]) t ->
('a, 'b, [ `Write ]) t
Drop to write-only permissions.

This operation involves no copying.

val length : ('a, 'b, [< `Read | `Write ]) t -> int
Base operations

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, [< `Read | `Write ]) t -> bool
Hashtbl.is_empty tbl returns true if there are no bindings in tbl, false otherwise.
val add : ('a, 'b, [< `Read | `Write > `Write ]) 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, [< `Read | `Write > `Write ]) 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, [< `Read | `Write > `Write ]) t -> 'a -> unit
Remove all bindings for the given key
val replace : ('a, 'b, [< `Read | `Write > `Write ]) 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 copy : ('a, 'b, [< `Read | `Write > `Read ]) t ->
('a, 'b, [< `Read | `Write ]) t
Return a copy of the given hashtable.
val clear : ('a, 'b, [< `Read | `Write > `Write ]) t -> unit
Empty a hash table.

Searching

val find : ('a, 'b, [< `Read | `Write > `Read ]) 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, [< `Read | `Write > `Read ]) 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, [< `Read | `Write > `Read ]) t -> 'a -> 'b -> 'b
Find a binding for the key, and return a default value if not found
val find_option : ('a, 'b, [< `Read | `Write > `Read ]) t -> 'a -> 'b option
Find a binding for the key, or return None if no value is found
val mem : ('a, 'b, [< `Read | `Write > `Read ]) t -> 'a -> bool
Hashtbl.mem tbl x checks if x is bound in tbl.

exists h k returns true is at least one item with key k is found in the hashtable.
val iter : ('a -> 'b -> unit) ->
('a, 'b, [< `Read | `Write > `Read ]) t -> unit
Traversing

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, [< `Read | `Write > `Read ]) t -> 'c -> 'c
Hashtbl.fold f tbl init computes (f kN dN ... (f k1 d1 init)...), where k1 ... kN are the keys of all bindings in tbl, and 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, [< `Read | `Write > `Read ]) t ->
('a, 'c, [< `Read | `Write ]) 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 filter : ('a -> bool) ->
('b, 'a, [< `Read | `Write > `Read ]) t ->
('b, 'a, [< `Read | `Write ]) t
filter f m returns a new hashtable where only the values a of m such that f a = true remain.
val filteri : ('a -> 'b -> bool) ->
('a, 'b, [< `Read | `Write > `Read ]) t ->
('a, 'b, [< `Read | `Write ]) t
filter f m returns a map where only the key, values pairs key, a of m such that f key a = true remain. The bindings are passed to f in increasing order with respect to the ordering over the type of the keys.
val filter_map : ('a -> 'b -> 'c option) ->
('a, 'b, [< `Read | `Write > `Read ]) t ->
('a, 'c, [< `Read | `Write ]) t
filter_map f m combines the features of filteri and map. It calls calls f key0 a0, f key1 a1, f keyn an where a0..an are the elements of m and key0..keyn the respective corresponding keys. It returns the map of pairs keyi,bi such as f keyi ai = Some bi (when f returns None, the corresponding element of m is discarded).

Conversions

val keys : ('a, 'b, [< `Read | `Write > `Read ]) 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, [< `Read | `Write > `Read ]) t -> 'b BatEnum.t
Return an enumeration of all the values of a hashtable.
val enum : ('a, 'b, [< `Read | `Write > `Read ]) t -> ('a * 'b) BatEnum.t
Return an enumeration of (key,value) pairs of a hashtable.
val of_enum : ('a * 'b) BatEnum.t -> ('a, 'b, [< `Read | `Write ]) t
Create a hashtable from a (key,value) enumeration.

Boilerplate code


Printing

val print : ?first:string ->
?last:string ->
?sep:string ->
('a BatInnerIO.output -> 'b -> unit) ->
('a BatInnerIO.output -> 'c -> unit) ->
'a BatInnerIO.output ->
('b, 'c, [< `Read | `Write > `Read ]) 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.
module Exceptionless: sig .. end
Operations on Hashtbl without exceptions.
module Labels: sig .. end
Operations on Hashtbl with labels.