23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24
24
25
25
(* *
26
- A **mutable** Hash set which allows customized [ `hash`]() behavior.
26
+ A **mutable** Hash set which allows customized `hash` behavior.
27
27
28
28
All data are parameterized by not its only type but also a unique identity in
29
- the time of initialization, so that two _HashSets of ints_ initialized with different
30
- _hash_ functions will have different type.
29
+ the time of initialization, so that two _HashSets of ints_ initialized with
30
+ different _hash_ functions will have different type.
31
31
32
32
For example:
33
33
34
- ```
35
- type t = int
36
- module I0 =
37
- (val Belt.Id.hashableU
38
- ~hash:(fun[@bs] (a : t) -> a & 0xff_ff)
39
- ~eq:(fun[@bs] a b -> a = b)
40
- )
41
- let s0 = make ~id:(module I0) ~hintSize:40
42
- module I1 =
43
- (val Belt.Id.hashableU
44
- ~hash:(fun[@bs] (a : t) -> a & 0xff)
45
- ~eq:(fun[@bs] a b -> a = b)
34
+ ```res prelude
35
+ module I0 = unpack(
36
+ Belt.Id.hashableU(
37
+ ~hash=(. a: int) => land(a, 65535),
38
+ ~eq=(. a, b) => a == b,
46
39
)
47
- let s1 = make ~id:(module I1) ~hintSize:40
48
- ```
40
+ )
49
41
50
- The invariant must be held: for two elements who are _equal_,
51
- their hashed value should be the same
42
+ let s0 = Belt.HashSet.make(~id=module(I0), ~hintSize=40)
52
43
53
- Here the compiler would infer `s0` and `s1` having different type so that
54
- it would not mix.
44
+ module I1 = unpack(
45
+ Belt.Id.hashableU(
46
+ ~hash=(. a: int) => land(a, 255),
47
+ ~eq=(. a, b) => a == b,
48
+ )
49
+ )
55
50
56
- ```
57
- val s0 : (int, I0.identity) t
58
- val s1 : (int, I1.identity) t
51
+ let s1 = Belt.HashSet.make(~id=module(I1), ~hintSize=40)
52
+
53
+ Belt.HashSet.add(s1, 0)
54
+ Belt.HashSet.add(s1, 1)
59
55
```
60
56
61
- We can add elements to the collection:
57
+ The invariant must be held: for two elements who are equal, their hashed
58
+ value should be the same.
62
59
63
- ```
64
- let () =
65
- add s1 0;
66
- add s1 1
60
+ Here the compiler would infer `s0` and `s1` having different type so that it
61
+ would not mix.
62
+
63
+ ```res sig
64
+ let s0: Belt.HashSet.t<int, I0.identity>
65
+ let s1: Belt.HashSet.t<int, I1.identity>
67
66
```
68
67
69
- Since this is an mutable data strucure, `s1` will contain two pairs.
68
+ We can add elements to the collection (see last two lines in the example
69
+ above). Since this is an mutable data structure, `s1` will contain two pairs.
70
70
*)
71
71
72
72
@@ -88,7 +88,6 @@ module String = Belt_HashSetString
88
88
type ('a, 'id) t
89
89
90
90
(* * The type of hash tables from type `'a` to type `'b`. *)
91
-
92
91
type ('a, 'id) id = ('a , 'id ) Belt_Id .hashable
93
92
94
93
val make : hintSize :int -> id :('a ,'id ) id -> ('a , 'id ) t
0 commit comments