A mutable Hash map which allows customized hash behavior.
All data are parameterized by not its only type but also a unique identity in the time of initialization, so that two HashMaps of ints initialized with different hash functions will have different type.
For example:
type t = int
module I0 =
(val Belt.Id.hashableU
~hash:(fun[\@bs] (a : t) -> a & 0xff_ff)
~eq:(fun[\@bs] a b -> a = b)
)
let s0 : (_, string,_) t = make ~hintSize:40 ~id:(module I0)
module I1 =
(val Belt.Id.hashableU
~hash:(fun[\@bs] (a : t) -> a & 0xff)
~eq:(fun[\@bs] a b -> a = b)
)
let s1 : (_, string,_) t = make ~hintSize:40 ~id:(module I1)
The invariant must be held: for two elements who are equal, their hashed value should be the same
Here the compiler would infer s0 and s1 having different type so that it would not mix.
val s0 : (int, I0.identity) t
val s1 : (int, I1.identity) t
We can add elements to the collection:
let () =
add s1 0 "3";
add s1 1 "3"
Since this is an mutable data strucure, s1 will contain two pairs.
module Int = Belt_HashMapInt
Specalized when key type is int, more efficient than the generic type
module String = Belt_HashMapString
Specalized when key type is string, more efficient than the generic type
type('key, 'value, 'id) t
The type of hash tables from type 'key to type 'value.
type('a, 'id) id =
(moduleBelt__.Belt_Id.Hashablewithtypeidentity = 'idandtypet = 'a)
val make : hintSize:int ->id:('key, 'id)id->('key, 'value, 'id)t
val forEachU : ('key, 'value, 'id)t->('key->'value-> unit)-> unit
val forEach : ('key, 'value, 'id)t->('key->'value-> unit)-> unit
forEach tbl f 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.
val reduceU : ('key, 'value, 'id)t->'c->('c->'key->'value->'c)->'c
val reduce : ('key, 'value, 'id)t->'c->('c->'key->'value->'c)->'c
reduce tbl init f 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 keepMapInPlaceU :
('key, 'value, 'id)t->('key->'value->'value option)->
unit
val keepMapInPlace :
('key, 'value, 'id)t->('key->'value->'value option)->
unit