Belt.HashSet
The top level provides generic mutable hash set operations.
It also has two specialized inner modules Belt.HashSet.Int
and Belt.HashSet.String
A mutable Hash set 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 HashSets of ints initialized with different hash functions will have different type.
For example:
type t = int
module I0 =
(val Belt.Id.hashableU
~hash:(fun[\@u] (a : t) -> a & 0xff_ff)
~eq:(fun[\@u] a b -> a = b)
)
let s0 = make ~id:(module I0) ~hintSize:40
module I1 =
(val Belt.Id.hashableU
~hash:(fun[\@u] (a : t) -> a & 0xff)
~eq:(fun[\@u] a b -> a = b)
)
let s1 = make ~id:(module I1) ~hintSize:40
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;
add s1 1
Since this is an mutable data strucure, s1
will contain two pairs.
module Int: { ... };
Specalized when key type is int
, more efficient than the generic type
module String: { ... };
Specalized when key type is string
, more efficient than the generic type
The type of hash tables from type 'a
to type 'b
.
let clear: t('a, 'id) => unit;
let isEmpty: t(_, _) => bool;
let add: t('a, 'id) => 'a => unit;
let has: t('a, 'id) => 'a => bool;
let remove: t('a, 'id) => 'a => unit;
let forEachU: t('a, 'id) => Js.Fn.arity1(('a => unit)) => unit;
let forEach: t('a, 'id) => ('a => unit) => unit;
Order unspecified.
let reduceU: t('a, 'id) => 'c => Js.Fn.arity2(('c => 'a => 'c)) => 'c;
let reduce: t('a, 'id) => 'c => ('c => 'a => 'c) => 'c;
Order unspecified.
let size: t('a, 'id) => int;
let logStats: t(_, _) => unit;
let toArray: t('a, 'id) => array('a);
let mergeMany: t('a, 'id) => array('a) => unit;
let getBucketHistogram: t(_, _) => array(int);