Skip to content

Commit 921d77d

Browse files
committed
hashset consistency check
1 parent ed02794 commit 921d77d

19 files changed

+222
-198
lines changed

jscomp/others/.depend

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bs_internalBucketsType.cmi :
111111
bs_internalSetBuckets.cmi : bs_internalBucketsType.cmi
112112
bs_HashMap.cmi : bs_Hash.cmi
113113
bs_HashMultiMap.cmi : bs_Hash.cmi bs_Bag.cmj
114-
bs_HashSet.cmi : bs_Hash.cmi bs_Bag.cmj
114+
bs_HashSet.cmi : bs_Hash.cmi
115115
bs_HashSetString.cmi :
116116
bs_HashSetInt.cmi :
117117
bs_Cmp.cmi :

jscomp/others/bs_HashSet.ml

+14-8
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ let create dict initialize_size =
173173
~dict
174174
let clear h = clear0 (B.data h)
175175
let reset h = reset0 (B.data h)
176-
let length h = length0 (B.data h)
177-
let iter h f = iter0 (B.data h) f
178-
let fold h init f = fold0 (B.data h) init f
176+
let size h = length0 (B.data h)
177+
let forEach h f = iter0 (B.data h) f
178+
let reduce h init f = N.fold0 (B.data h) init f
179179
let logStats h = logStats0 (B.data h)
180180

181181
let add (type a) (type id) (h : (a,id) t) (key:a) =
@@ -194,7 +194,7 @@ let replace (type a)(type id) (h : (a,id) t) (key : a) =
194194
let module M = (val dict) in
195195
add0 ~hash:M.hash ~eq:M.eq data key
196196

197-
let mem (type a) (type id) (h : (a,id) t) (key : a) =
197+
let has (type a) (type id) (h : (a,id) t) (key : a) =
198198
let dict,data = B.(dict h, data h) in
199199
let module M = (val dict) in
200200
mem0 ~hash:M.hash ~eq:M.eq data key
@@ -215,14 +215,20 @@ let addArray0 ~hash ~eq h arr =
215215
done
216216

217217
let ofArray (type a) (type id)
218-
~dict:(dict:(a,id) Bs_Hash.t) arr =
218+
arr
219+
~dict:(dict:(a,id) Bs_Hash.t) =
219220
let module M = (val dict) in
220221
B.bag ~dict
221222
~data:M.(ofArray0 ~eq~hash arr)
222223

223-
let addArray (type a) (type id)
224+
let mergeArrayDone (type a) (type id)
224225
(h : (a,id) t) arr =
225-
let dict,data = B.(dict h, data h) in
226-
let module M = (val dict) in
226+
let data = B.data h in
227+
let module M = (val B.dict h) in
227228
M.(addArray0 ~hash ~eq data arr)
228229

230+
let mergeArray h arr = mergeArrayDone h arr; h
231+
232+
let getData = B.data
233+
let getDict = B.dict
234+
let packDictData = B.bag

jscomp/others/bs_HashSet.mli

+40-62
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,76 @@
1-
type ('a, 'id) t0
21

3-
type ('a, 'id) t =
4-
(('a, 'id) Bs_Hash.t,
5-
('a, 'id) t0) Bs_Bag.bag
2+
3+
type ('a, 'id) t
64

75
(** The type of hash tables from type ['a] to type ['b]. *)
86

9-
val create0 : int -> ('a, 'id) t0
10-
val create : ('a,'id) Bs_Hash.t -> int -> ('a, 'id) t
117

8+
val create: ('a,'id) Bs_Hash.t -> int -> ('a, 'id) t
9+
val clear: ('a, 'id) t -> unit
10+
11+
val add: ('a, 'id) t -> 'a -> unit
12+
val has: ('a, 'id) t -> 'a -> bool
13+
val remove: ('a, 'id) t -> 'a -> unit
14+
15+
val forEach: ('a, 'id) t -> ('a -> unit [@bs]) -> unit
16+
(** Order unspecified. *)
17+
18+
19+
val reduce: ('a, 'id) t -> 'c -> ('c -> 'a -> 'c [@bs]) -> 'c
20+
(** Order unspecified. *)
21+
22+
val size: ('a, 'id) t -> int
1223

13-
val clear0 : ('a, 'id) t0 -> unit
14-
val clear : ('a, 'id) t -> unit
15-
(** Empty a hash table. Use [reset] instead of [clear] to shrink the
16-
size of the bucket table to its initial size. *)
1724

18-
val reset0 : ('a, 'id) t0 -> unit
19-
val reset : ('a, 'id) t -> unit
20-
(** Empty a hash table and shrink the size of the bucket table
21-
to its initial size.
22-
@since 4.00.0 *)
2325

26+
val logStats: _ t -> unit
27+
28+
29+
val toArray: ('a,'id) t -> 'a array
30+
31+
val ofArray:
32+
'a array ->
33+
dict:('a,'id) Bs_Hash.t ->
34+
('a,'id) t
35+
36+
37+
val mergeArrayDone: ('a,'id) t -> 'a array -> unit
38+
val mergeArray: ('a,'id) t -> 'a array -> ('a, 'id)t
39+
40+
(****************************************************************************)
41+
type ('a, 'id) t0
2442

43+
val getData: ('k, 'id) t -> ('k, 'id) t0
44+
val getDict: ('k, 'id) t -> ('k, 'id) Bs_Hash.t
45+
val packDictData: dict:('k, 'id) Bs_Hash.t -> data:('k, 'id) t0 -> ('k, 'id) t
2546

47+
val clear0 : ('a, 'id) t0 -> unit
48+
val create0 : int -> ('a, 'id) t0
49+
val reset0 : ('a, 'id) t0 -> unit
2650
val add0 :
2751
hash:('a,'id) Bs_Hash.hash ->
2852
eq:('a,'id) Bs_Hash.eq ->
2953
('a,'id) t0 -> 'a -> unit
30-
val add : ('a, 'id) t -> 'a -> unit
31-
3254
val mem0:
3355
hash:('a,'id) Bs_Hash.hash ->
3456
eq:('a,'id) Bs_Hash.eq ->
3557
('a, 'id) t0 -> 'a -> bool
36-
val mem:
37-
('a, 'id) t -> 'a -> bool
38-
39-
4058
val remove0:
4159
hash:('a,'id) Bs_Hash.hash ->
4260
eq:('a,'id) Bs_Hash.eq ->
4361
('a, 'id) t0 -> 'a -> unit
44-
val remove:
45-
('a, 'id) t -> 'a -> unit
46-
47-
48-
4962
val iter0 : ('a, 'id) t0 -> ('a -> unit [@bs]) -> unit
50-
val iter : ('a, 'id) t -> ('a -> unit [@bs]) -> unit
51-
(**
52-
If the hash table was created in non-randomized mode, the order
53-
in which the bindings are enumerated is reproducible between
54-
successive runs of the program, and even between minor versions
55-
of OCaml. For randomized hash tables, the order of enumeration
56-
is entirely random. *)
57-
58-
val fold0 : ('a, 'id) t0 -> 'c -> ('a -> 'c -> 'c [@bs]) -> 'c
59-
val fold : ('a, 'id) t -> 'c -> ('a -> 'c -> 'c [@bs]) -> 'c
60-
(**
61-
If the hash table was created in non-randomized mode, the order
62-
in which the bindings are enumerated is reproducible between
63-
successive runs of the program, and even between minor versions
64-
of OCaml. For randomized hash tables, the order of enumeration
65-
is entirely random. *)
66-
67-
63+
val fold0 : ('a, 'id) t0 -> 'c -> ('c -> 'a -> 'c [@bs]) -> 'c
6864
val length0 : ('a, 'id) t0 -> int
69-
val length : ('a, 'id) t -> int
70-
7165
val logStats0 : ('a, 'id) t0 -> unit
72-
val logStats : _ t -> unit
73-
(** [Hashtbl.stats tbl] returns statistics about the table [tbl]:
74-
number of buckets, size of the biggest bucket, distribution of
75-
buckets by size.
76-
@since 4.00.0 *)
77-
7866
val toArray0 : ('a,'id) t0 -> 'a array
79-
val toArray : ('a,'id) t -> 'a array
80-
8167
val ofArray0 :
8268
hash:('a,'id) Bs_Hash.hash ->
8369
eq:('a,'id) Bs_Hash.eq ->
8470
'a array ->
8571
('a, 'id) t0
8672

87-
val ofArray :
88-
dict:('a,'id) Bs_Hash.t ->
89-
'a array ->
90-
('a,'id) t
91-
9273
val addArray0 :
9374
hash:('a,'id) Bs_Hash.hash ->
9475
eq:('a,'id) Bs_Hash.eq ->
9576
('a,'id) t0 -> 'a array -> unit
96-
97-
val addArray:
98-
('a,'id) t -> 'a array -> unit

jscomp/others/bs_HashSetInt.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ let rec mem_in_bucket (key : key) cell =
119119
| Some nextCell ->
120120
mem_in_bucket key nextCell)
121121

122-
let mem h key =
122+
let has h key =
123123
let h_buckets = C.buckets h in
124124
let nid = hash key land (Array.length h_buckets - 1) in
125125
let bucket = (Bs_Array.unsafe_get h_buckets nid) in
@@ -132,9 +132,9 @@ let mem h key =
132132
let create = C.create0
133133
let clear = C.clear0
134134
let reset = C.reset0
135-
let length = C.length0
136-
let iter = N.iter0
137-
let fold = N.fold0
135+
let size = C.length0
136+
let forEach = N.iter0
137+
let reduce = N.fold0
138138
let logStats = N.logStats0
139139
let toArray = N.toArray0
140140

@@ -147,8 +147,12 @@ let ofArray arr =
147147
v
148148

149149
(* TOOD: optimize heuristics for resizing *)
150-
let addArray h arr =
150+
let mergeArrayDone h arr =
151151
let len = Bs.Array.length arr in
152152
for i = 0 to len - 1 do
153153
add h (Bs_Array.unsafe_get arr i)
154-
done
154+
done
155+
156+
157+
let mergeArray h arr =
158+
mergeArrayDone h arr; h

jscomp/others/bs_HashSetInt.mli

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ type key = int
44

55
# 10
66
type t
7-
val create : int -> t
8-
val clear : t -> unit
9-
val reset : t -> unit
10-
val add : t -> key -> unit
11-
val mem:
7+
val create: int -> t
8+
val clear: t -> unit
9+
10+
val add: t -> key -> unit
11+
val has:
1212
t -> key -> bool
1313
val remove:
1414
t -> key -> unit
15-
val iter : t -> (key -> unit [@bs]) -> unit
16-
val fold : t -> 'c -> (key -> 'c -> 'c [@bs]) -> 'c
17-
val length : t -> int
18-
val logStats : t -> unit
19-
val toArray : t -> key array
20-
val ofArray : key array -> t
21-
val addArray : t -> key array -> unit
15+
val forEach: t -> (key -> unit [@bs]) -> unit
16+
val reduce: t -> 'c -> ( 'c -> key -> 'c [@bs]) -> 'c
17+
val size: t -> int
18+
val logStats: t -> unit
19+
val toArray: t -> key array
20+
val ofArray: key array -> t
21+
val mergeArrayDone: t -> key array -> unit
22+
val mergeArray: t -> key array -> t

jscomp/others/bs_HashSetString.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ let rec mem_in_bucket (key : key) cell =
119119
| Some nextCell ->
120120
mem_in_bucket key nextCell)
121121

122-
let mem h key =
122+
let has h key =
123123
let h_buckets = C.buckets h in
124124
let nid = hash key land (Array.length h_buckets - 1) in
125125
let bucket = (Bs_Array.unsafe_get h_buckets nid) in
@@ -132,9 +132,9 @@ let mem h key =
132132
let create = C.create0
133133
let clear = C.clear0
134134
let reset = C.reset0
135-
let length = C.length0
136-
let iter = N.iter0
137-
let fold = N.fold0
135+
let size = C.length0
136+
let forEach = N.iter0
137+
let reduce = N.fold0
138138
let logStats = N.logStats0
139139
let toArray = N.toArray0
140140

@@ -147,8 +147,12 @@ let ofArray arr =
147147
v
148148

149149
(* TOOD: optimize heuristics for resizing *)
150-
let addArray h arr =
150+
let mergeArrayDone h arr =
151151
let len = Bs.Array.length arr in
152152
for i = 0 to len - 1 do
153153
add h (Bs_Array.unsafe_get arr i)
154-
done
154+
done
155+
156+
157+
let mergeArray h arr =
158+
mergeArrayDone h arr; h

jscomp/others/bs_HashSetString.mli

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ type key = string
44

55
# 10
66
type t
7-
val create : int -> t
8-
val clear : t -> unit
9-
val reset : t -> unit
10-
val add : t -> key -> unit
11-
val mem:
7+
val create: int -> t
8+
val clear: t -> unit
9+
10+
val add: t -> key -> unit
11+
val has:
1212
t -> key -> bool
1313
val remove:
1414
t -> key -> unit
15-
val iter : t -> (key -> unit [@bs]) -> unit
16-
val fold : t -> 'c -> (key -> 'c -> 'c [@bs]) -> 'c
17-
val length : t -> int
18-
val logStats : t -> unit
19-
val toArray : t -> key array
20-
val ofArray : key array -> t
21-
val addArray : t -> key array -> unit
15+
val forEach: t -> (key -> unit [@bs]) -> unit
16+
val reduce: t -> 'c -> ( 'c -> key -> 'c [@bs]) -> 'c
17+
val size: t -> int
18+
val logStats: t -> unit
19+
val toArray: t -> key array
20+
val ofArray: key array -> t
21+
val mergeArrayDone: t -> key array -> unit
22+
val mergeArray: t -> key array -> t

jscomp/others/bs_internalSetBuckets.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let rec doBucketFold ~f b accu =
8484
| None ->
8585
accu
8686
| Some cell ->
87-
doBucketFold ~f (next cell) (f (key cell) accu [@bs])
87+
doBucketFold ~f (next cell) (f accu (key cell) [@bs])
8888

8989
let fold0 h init f =
9090
let d = C.buckets h in

jscomp/others/bs_internalSetBuckets.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ val iter0 : 'a bucket C.container -> ('a -> 'b [@bs]) -> unit
3636
val fillArray : int -> 'a array -> 'a bucket -> int
3737
val toArray0 : 'a bucket C.container -> 'a array
3838

39-
val fold0 : 'a bucket C.container -> 'b -> ('a -> 'b -> 'b [@bs]) -> 'b
39+
val fold0 : 'a bucket C.container -> 'b -> ('b -> 'a -> 'b [@bs]) -> 'b
4040
val logStats0 : 'a bucket C.container -> unit

jscomp/others/hashset.cppo.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ let rec mem_in_bucket (key : key) cell =
128128
| Some nextCell ->
129129
mem_in_bucket key nextCell)
130130

131-
let mem h key =
131+
let has h key =
132132
let h_buckets = C.buckets h in
133133
let nid = hash key land (Array.length h_buckets - 1) in
134134
let bucket = (Bs_Array.unsafe_get h_buckets nid) in
@@ -141,9 +141,9 @@ let mem h key =
141141
let create = C.create0
142142
let clear = C.clear0
143143
let reset = C.reset0
144-
let length = C.length0
145-
let iter = N.iter0
146-
let fold = N.fold0
144+
let size = C.length0
145+
let forEach = N.iter0
146+
let reduce = N.fold0
147147
let logStats = N.logStats0
148148
let toArray = N.toArray0
149149

@@ -156,8 +156,12 @@ let ofArray arr =
156156
v
157157

158158
(* TOOD: optimize heuristics for resizing *)
159-
let addArray h arr =
159+
let mergeArrayDone h arr =
160160
let len = Bs.Array.length arr in
161161
for i = 0 to len - 1 do
162162
add h (Bs_Array.unsafe_get arr i)
163-
done
163+
done
164+
165+
166+
let mergeArray h arr =
167+
mergeArrayDone h arr; h

0 commit comments

Comments
 (0)