Legend:
Library
Module
Module type
Parameter
Class
Class type
Hashtbl is a reimplementation of the standard MoreLabels.Hashtbl. Its worst case time complexity is O(log(N)) for lookups and additions, unlike the standard MoreLabels.Hashtbl, which is O(N).
A hash table is implemented as an array of AVL trees (see Avltree). If growth_allowed (default true) is false then size is the final size of the array; the table can always hold more elements than size, but they will all go into tree nodes. If it is true (default) then the array will double in size when the number of elements in the table reaches twice the size of the array. When this happens, all existing elements will be reinserted, which can take a long time. If you care about latency, set size and growth_allowed=false if possible.
In most cases, functions passed as arguments to hash table accessors must not mutate the hash table while it is being accessed, as this will result in an exception. For example, iter and change take a function f which must not modify t. In a few cases, mutation is allowed, such as in Hashtbl.find_and_call, where all access to t is finished before the ~if_found and ~if_not_found arguments are invoked.
We have three kinds of hash table modules:
Hashtbl
Hashtbl.Poly
Key.Table (a class of similar modules)
There are three kinds of hash-table functions:
creation from nothing (create, of_alist)
sexp converters (t_of_sexp, sexp_of_t, and bin_io too)
accessors and mappers (fold, mem, find, map, filter_map, ...)
Here is a table showing what classes of functions are available in each kind of hash-table module:
creation sexp-conv accessors
Hashtbl X
Hashtbl.Poly X X
Key.Table X X X'
The entry marked with X' is there for historical reasons, and may be eliminated at some point. The upshot is that one should use Hashtbl for accessors, Hashtbl.Poly for hash-table creation and sexp conversion using polymorphic compare/hash, and Key.Table for hash-table creation and sexp conversion using Key.compare and Key.hash.
Usage
For many students of OCaml, using hashtables is complicated by the functors. Here are a few tips:
val create_mapped :
('akey,
'b,
get_key:('r->'akey)->get_data:('r->'b)->'r list->[ `Duplicate_keys of 'akey list| `Ok of ('a, 'b)t ])Core_kernel__.Hashtbl_intf.Hashtbl_intf.create_options_with_first_class_module
val create_with_key :
('akey,
'r,
get_key:('r->'akey)->'r list->[ `Duplicate_keys of 'akey list| `Ok of ('a, 'r)t ])Core_kernel__.Hashtbl_intf.Hashtbl_intf.create_options_with_first_class_module
val create_with_key_or_error :
('akey,
'r,
get_key:('r->'akey)->'r list->('a, 'r)tBase__.Or_error.t)Core_kernel__.Hashtbl_intf.Hashtbl_intf.create_options_with_first_class_module
val create_with_key_exn :
('akey, 'r, get_key:('r->'akey)->'r list->('a, 'r)t)Core_kernel__.Hashtbl_intf.Hashtbl_intf.create_options_with_first_class_module
val group :
('akey,
'b,
get_key:('r->'akey)->get_data:('r->'b)->combine:('b->'b->'b)->'r list->('a, 'b)t)Core_kernel__.Hashtbl_intf.Hashtbl_intf.create_options_with_first_class_module