123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300(** Hashtbl is a reimplementation of the standard {{:
https://caml.inria.fr/pub/docs/manual-ocaml/libref/MoreLabels.Hashtbl.html}[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:
{v
creation sexp-conv accessors
Hashtbl X
Hashtbl.Poly X X
Key.Table X X X'
v}
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].
{2:usage Usage}
For many students of OCaml, using hashtables is complicated by the
functors. Here are a few tips:
{ol
{- For a list of hashtable functions see {!Core.Hashtbl_intf.S}.}
{- To create a hashtable with string keys use [String.Table]:
{[
let table = String.Table.create () ~size:4 in
List.iter ~f:(fun (key, data) -> Hashtbl.set table ~key ~data)
[ ("A", 1); ("B", 2); ("C", 3); ];
Hashtbl.find table "C"
]}
Here 4 need only be a guess at the hashtable's future size. There are other similar
pre-made hashtables, e.g., [Int63.Table] or [Host_and_port.Table].
}
{- To create a hashtable with a custom key type use Hashable:
{[
module Key = struct
module T = struct
type t = String.t * Int63.t [@@deriving compare, hash, sexp]
end
include T
include Hashable.Make (T)
end
let table = Key.Table.create () ~size:4 in
List.iter ~f:(fun (key, data) -> Hashtbl.set table ~key ~data)
[ (("pi", Int63.zero), 3.14159);
(("e", Int63.minus_one), 2.71828);
(("Euler", Int63.one), 0.577215);
];
Hashtbl.find table ("pi", Int63.zero)
]}
Performance {i may} improve if you define [equal] and [hash] explicitly, e.g.:
{[
let equal (x, y) (x', y') = String.(=) x x' && Int63.(=) y y'
let hash (x, y) = String.hash x + Int63.hash y * 65599
]}
}
}
*)open!ImportmoduleBinable=Binable0moduleHashtbl=Base.HashtblmoduletypeKey_plain=Hashtbl.Key.SmoduleHashable=Base.HashablemoduleMerge_into_action=Base.Hashtbl.Merge_into_actionmoduletypeHashable=Base.Hashable.HashablemoduletypeKey=sigtypet[@@derivingsexp]includeKey_plainwithtypet:=tendmoduletypeKey_binable=sigtypet[@@derivingbin_io,sexp]includeKeywithtypet:=tendmoduletypeKey_stable=sigtypet[@@derivingbin_io,sexp,stable_witness]includeKey_binablewithtypet:=tendmoduletypeCreators=Hashtbl.Private.Creators_genericmoduletypeAccessors=sigincludeHashtbl.Accessorsvalvalidate:name:('akey->string)->'bValidate.check->('a,'b)tValidate.checkendmoduletypeMulti=Hashtbl.Multitype('key,'data,'z)create_options_with_first_class_module=('key,'data,'z)Hashtbl.create_optionstype('key,'data,'z)create_options_without_hashable=('key,'data,'z)Hashtbl.Private.create_options_without_first_class_moduletype('key,'data,'z)create_options_with_hashable=?growth_allowed:bool(** defaults to [true] *)->?size:int(** initial size -- default 0 *)->hashable:'keyHashable.t->'zmoduletypeM_quickcheck=sigtypet[@@derivingcompare,hash,quickcheck,sexp_of]endmoduletypeFor_deriving=sigincludeBase.Hashtbl.For_derivingmoduletypeM_quickcheck=M_quickcheckvalquickcheck_generator_m__t:(moduleM_quickcheckwithtypet='k)->'vBase_quickcheck.Generator.t->('k,'v)tQuickcheck.Generator.tvalquickcheck_observer_m__t:(moduleM_quickcheckwithtypet='k)->'vQuickcheck.Observer.t->('k,'v)tQuickcheck.Observer.tvalquickcheck_shrinker_m__t:(moduleM_quickcheckwithtypet='k)->'vQuickcheck.Shrinker.t->('k,'v)tQuickcheck.Shrinker.tendmoduletypeS_plain=sigtypekeytype('a,'b)hashtbltype'bt=(key,'b)hashtbl[@@derivingequal,sexp_of]type('a,'b)t_='bttype'akey_=keyvalhashable:keyHashable.tincludeInvariant.S1withtype'bt:='btincludeCreatorswithtype('a,'b)t:=('a,'b)t_withtype'akey:='akey_withtype('key,'data,'z)create_options:=('key,'data,'z)create_options_without_hashablemoduleProvide_of_sexp(Key:sigtypet[@@derivingof_sexp]endwithtypet:=key):sigtype_t[@@derivingof_sexp]endwithtype'at:='atmoduleProvide_bin_io(Key:sigtypet[@@derivingbin_io]endwithtypet:=key):sigtype'at[@@derivingbin_io]endwithtype'at:='atendmoduletypeS=sigincludeS_plainincludesigtype_t[@@derivingof_sexp]endwithtype'at:='atendmoduletypeS_binable=sigincludeSincludeBinable.S1withtype'vt:='vtendmoduletypeS_stable=sigincludeS_binabletypenonrec'at='at[@@derivingstable_witness]endmoduletypeHashtbl=sigincludeHashtbl.S_without_submodules(** @inline *)valvalidate:name:('akey->string)->'bValidate.check->('a,'b)tValidate.checkmoduleUsing_hashable:sigincludeCreatorswithtype('a,'b)t:=('a,'b)twithtype'akey:='akeywithtype('a,'b,'z)create_options:=('a,'b,'z)create_options_with_hashableendmodulePoly:sigtypenonrec('a,'b)t=('a,'b)t[@@derivingbin_io]includeHashtbl.S_polywithtype('a,'b)t:=('a,'b)tvalvalidate:name:('akey->string)->'bValidate.check->('a,'b)tValidate.checkendmoduletypeKey_plain=Key_plainmoduletypeKey=KeymoduletypeKey_binable=Key_binablemoduletypeKey_stable=Key_stablemoduletypeS_plain=S_plainwithtype('a,'b)hashtbl=('a,'b)tmoduletypeS=Swithtype('a,'b)hashtbl=('a,'b)tmoduletypeS_binable=S_binablewithtype('a,'b)hashtbl=('a,'b)tmoduletypeS_stable=S_stablewithtype('a,'b)hashtbl=('a,'b)tmoduleMake_plain(Key:Key_plain):S_plainwithtypekey=Key.tmoduleMake(Key:Key):Swithtypekey=Key.tmoduleMake_binable(Key:Key_binable):S_binablewithtypekey=Key.tmoduleMake_stable(Key:Key_stable):S_stablewithtypekey=Key.tmoduleMake_plain_with_hashable(T:sigmoduleKey:Key_plainvalhashable:Key.tHashable.tend):S_plainwithtypekey=T.Key.tmoduleMake_with_hashable(T:sigmoduleKey:Keyvalhashable:Key.tHashable.tend):Swithtypekey=T.Key.tmoduleMake_binable_with_hashable(T:sigmoduleKey:Key_binablevalhashable:Key.tHashable.tend):S_binablewithtypekey=T.Key.tmoduleMake_stable_with_hashable(T:sigmoduleKey:Key_stablevalhashable:Key.tHashable.tend):S_stablewithtypekey=T.Key.tmoduleM(K:T.T):sigtypenonrec'vt=(K.t,'v)tendmoduleHashable=HashablemoduleMerge_into_action=Merge_into_actionvalhashable:('key,_)t->'keyHashable.tmoduletypeFor_deriving=For_derivingincludeFor_derivingwithtype('a,'b)t:=('a,'b)tend