Skip to content

Commit 398aaf5

Browse files
committed
share code between hashset and hashmap
1 parent 93c4a6e commit 398aaf5

16 files changed

+419
-375
lines changed

jscomp/others/.depend

+8-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ bs_internalAVLtree.cmj :
2222
bs_internalMutableAVLSet.cmj : bs_internalAVLset.cmj
2323
bs_Hash.cmj : bs_Hash.cmi
2424
bs_Queue.cmj : bs_Array.cmj bs_Queue.cmi
25-
bs_internalBuckets.cmj : bs_Array.cmj
26-
bs_HashMap.cmj : bs_internalBuckets.cmj bs_Hash.cmj bs_Bag.cmj bs_Array.cmj \
27-
bs_HashMap.cmi
25+
bs_internalBucketsType.cmj : bs_Array.cmj
26+
bs_internalBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj
27+
bs_HashMap.cmj : bs_internalBucketsType.cmj bs_internalBuckets.cmj \
28+
bs_Hash.cmj bs_Bag.cmj bs_Array.cmj bs_HashMap.cmi
2829
bs_Bag.cmj :
2930
bs_Cmp.cmj : bs_Cmp.cmi
3031
bs_Map.cmj : bs_internalAVLtree.cmj bs_Cmp.cmj bs_Bag.cmj bs_Array.cmj \
@@ -43,9 +44,10 @@ js_date.cmj :
4344
js_global.cmj :
4445
js_cast.cmj : js_cast.cmi
4546
js_promise.cmj :
46-
bs_HashMapInt.cmj : bs_internalBuckets.cmj bs_Array.cmj bs_HashMapInt.cmi
47-
bs_HashMapString.cmj : bs_internalBuckets.cmj bs_Array.cmj \
48-
bs_HashMapString.cmi
47+
bs_HashMapInt.cmj : bs_internalBucketsType.cmj bs_internalBuckets.cmj \
48+
bs_Array.cmj bs_HashMapInt.cmi
49+
bs_HashMapString.cmj : bs_internalBucketsType.cmj bs_internalBuckets.cmj \
50+
bs_Array.cmj bs_HashMapString.cmi
4951
node_process.cmi : js_dict.cmi
5052
js_re.cmi :
5153
js_null_undefined.cmi :

jscomp/others/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
1717
bs_internalMutableAVL\
1818
bs_Hash\
1919
bs_Queue\
20+
bs_internalBucketsType\
2021
bs_internalBuckets\
2122
bs_HashMap\
2223
bs_Bag\

jscomp/others/bs_Bag.ml

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
224

325
type (+ 'k, + 'v) bag = {
426
dict : 'k ;
527
data : 'v
6-
} [@@bs.deriving abstract]
28+
} [@@bs.deriving abstract]

jscomp/others/bs_HashMap.ml

+43-42
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
(** Adapted by Authors of BuckleScript 2017 *)
1414

1515
module N = Bs_internalBuckets
16+
module C = Bs_internalBucketsType
1617
module B = Bs_Bag
17-
type ('a, 'b,'id) t0 = ('a,'b,'id) N.t0
18+
type ('a, 'b,'id) t0 = ('a,'b) N.t0
1819

1920

2021
type ('a,'b) bucket = ('a,'b) N.bucket
@@ -26,12 +27,12 @@ type ('a,'b,'id) t =
2627

2728

2829
let rec insert_bucket ~hash ~h_buckets ~ndata_tail h old_bucket =
29-
match N.toOpt old_bucket with
30+
match C.toOpt old_bucket with
3031
| None -> ()
3132
| Some cell ->
3233
let nidx = (Bs_Hash.getHash hash) (N.key cell) [@bs] land (Array.length h_buckets - 1) in
33-
let v = N.return cell in
34-
begin match N.toOpt (Bs_Array.unsafe_get ndata_tail nidx) with
34+
let v = C.return cell in
35+
begin match C.toOpt (Bs_Array.unsafe_get ndata_tail nidx) with
3536
| None ->
3637
Bs_Array.unsafe_set h_buckets nidx v
3738
| Some tail ->
@@ -42,101 +43,101 @@ let rec insert_bucket ~hash ~h_buckets ~ndata_tail h old_bucket =
4243

4344

4445
let resize ~hash h =
45-
let odata = N.buckets h in
46+
let odata = C.buckets h in
4647
let osize = Array.length odata in
4748
let nsize = osize * 2 in
4849
if nsize >= osize then begin (* no overflow *)
49-
let h_buckets = N.makeSize nsize in
50-
let ndata_tail = N.makeSize nsize in (* keep track of tail *)
51-
N.bucketsSet h h_buckets; (* so that indexfun sees the new bucket count *)
50+
let h_buckets = C.makeSize nsize in
51+
let ndata_tail = C.makeSize nsize in (* keep track of tail *)
52+
C.bucketsSet h h_buckets; (* so that indexfun sees the new bucket count *)
5253
for i = 0 to osize - 1 do
5354
insert_bucket ~hash ~h_buckets ~ndata_tail h (Bs_Array.unsafe_get odata i)
5455
done;
5556
for i = 0 to nsize - 1 do
56-
match N.toOpt (Bs_Array.unsafe_get ndata_tail i) with
57+
match C.toOpt (Bs_Array.unsafe_get ndata_tail i) with
5758
| None -> ()
58-
| Some tail -> N.nextSet tail N.emptyOpt
59+
| Some tail -> N.nextSet tail C.emptyOpt
5960
done
6061
end
6162

6263

6364
let add0 ~hash h key value =
64-
let h_buckets = N.buckets h in
65+
let h_buckets = C.buckets h in
6566
let h_buckets_lenth = Array.length h_buckets in
6667
let i = (Bs_Hash.getHash hash) key [@bs] land (h_buckets_lenth - 1) in
6768
let bucket =
6869
N.bucket ~key ~value ~next:(Bs_Array.unsafe_get h_buckets i) in
69-
Bs_Array.unsafe_set h_buckets i (N.return bucket);
70-
let h_new_size = N.size h + 1 in
71-
N.sizeSet h h_new_size;
70+
Bs_Array.unsafe_set h_buckets i (C.return bucket);
71+
let h_new_size = C.size h + 1 in
72+
C.sizeSet h h_new_size;
7273
if h_new_size > h_buckets_lenth lsl 1 then resize ~hash h
7374

7475

7576
let rec remove_bucket ~eq h h_buckets i key prec buckets =
76-
match N.toOpt buckets with
77+
match C.toOpt buckets with
7778
| None -> ()
7879
| Some cell ->
7980
let cell_next = N.next cell in
8081
if (Bs_Hash.getEq eq) (N.key cell) key [@bs]
8182
then
8283
begin
83-
(match N.toOpt prec with
84+
(match C.toOpt prec with
8485
| None -> Bs_Array.unsafe_set h_buckets i cell_next
8586
| Some c -> N.nextSet c cell_next);
86-
N.sizeSet h (N.size h - 1);
87+
C.sizeSet h (C.size h - 1);
8788
end
8889
else remove_bucket ~eq h h_buckets i key buckets cell_next
8990

9091
let remove0 ~hash ~eq h key =
91-
let h_buckets = N.buckets h in
92+
let h_buckets = C.buckets h in
9293
let i = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
93-
remove_bucket ~eq h h_buckets i key N.emptyOpt (Bs_Array.unsafe_get h_buckets i)
94+
remove_bucket ~eq h h_buckets i key C.emptyOpt (Bs_Array.unsafe_get h_buckets i)
9495

9596
let rec removeAllBuckets ~eq h h_buckets i key prec buckets =
96-
match N.toOpt buckets with
97+
match C.toOpt buckets with
9798
| None -> ()
9899
| Some cell ->
99100
let cell_next = N.next cell in
100101
if (Bs_Hash.getEq eq) (N.key cell) key [@bs]
101102
then
102103
begin
103-
(match N.toOpt prec with
104+
(match C.toOpt prec with
104105
| None -> Bs_Array.unsafe_set h_buckets i cell_next
105106
| Some c -> N.nextSet c cell_next);
106-
N.sizeSet h (N.size h - 1);
107+
C.sizeSet h (C.size h - 1);
107108
end;
108109
removeAllBuckets ~eq h h_buckets i key buckets cell_next
109110

110111
let removeAll0 ~hash ~eq h key =
111-
let h_buckets = N.buckets h in
112+
let h_buckets = C.buckets h in
112113
let i = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
113-
removeAllBuckets ~eq h h_buckets i key N.emptyOpt (Bs_Array.unsafe_get h_buckets i)
114+
removeAllBuckets ~eq h h_buckets i key C.emptyOpt (Bs_Array.unsafe_get h_buckets i)
114115

115116

116117
let rec find_rec ~eq key buckets =
117-
match N.toOpt buckets with
118+
match C.toOpt buckets with
118119
| None ->
119120
None
120121
| Some cell ->
121122
if (Bs_Hash.getEq eq) key (N.key cell) [@bs] then Some (N.value cell)
122123
else find_rec ~eq key (N.next cell)
123124

124125
let findOpt0 ~hash ~eq h key =
125-
let h_buckets = N.buckets h in
126+
let h_buckets = C.buckets h in
126127
let nid = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
127-
match N.toOpt @@ Bs_Array.unsafe_get h_buckets nid with
128+
match C.toOpt @@ Bs_Array.unsafe_get h_buckets nid with
128129
| None -> None
129130
| Some cell1 ->
130131
if (Bs_Hash.getEq eq) key (N.key cell1) [@bs] then
131132
Some (N.value cell1)
132133
else
133-
match N.toOpt (N.next cell1) with
134+
match C.toOpt (N.next cell1) with
134135
| None -> None
135136
| Some cell2 ->
136137
if (Bs_Hash.getEq eq) key
137138
(N.key cell2) [@bs] then
138139
Some (N.value cell2) else
139-
match N.toOpt (N.next cell2) with
140+
match C.toOpt (N.next cell2) with
140141
| None -> None
141142
| Some cell3 ->
142143
if (Bs_Hash.getEq eq) key
@@ -148,20 +149,20 @@ let findOpt0 ~hash ~eq h key =
148149

149150
let findAll0 ~hash ~eq h key =
150151
let rec find_in_bucket buckets =
151-
match N.toOpt buckets with
152+
match C.toOpt buckets with
152153
| None ->
153154
[]
154155
| Some cell ->
155156
if (Bs_Hash.getEq eq)
156157
(N.key cell) key [@bs]
157158
then (N.value cell) :: find_in_bucket (N.next cell)
158159
else find_in_bucket (N.next cell) in
159-
let h_buckets = N.buckets h in
160+
let h_buckets = C.buckets h in
160161
let nid = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
161162
find_in_bucket (Bs_Array.unsafe_get h_buckets nid)
162163

163164
let rec replace_bucket ~eq key info buckets =
164-
match N.toOpt buckets with
165+
match C.toOpt buckets with
165166
| None ->
166167
true
167168
| Some cell ->
@@ -176,19 +177,19 @@ let rec replace_bucket ~eq key info buckets =
176177
replace_bucket ~eq key info (N.next cell)
177178

178179
let replace0 ~hash ~eq h key info =
179-
let h_buckets = N.buckets h in
180+
let h_buckets = C.buckets h in
180181
let i = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
181182
let l = Array.unsafe_get h_buckets i in
182183
if replace_bucket ~eq key info l then begin
183-
Bs_Array.unsafe_set h_buckets i (N.return
184+
Bs_Array.unsafe_set h_buckets i (C.return
184185
(N.bucket ~key ~value:info ~next:l));
185-
N.sizeSet h (N.size h + 1);
186-
if N.size h > Array.length (N.buckets h) lsl 1 then resize ~hash h
186+
C.sizeSet h (C.size h + 1);
187+
if C.size h > Array.length (C.buckets h) lsl 1 then resize ~hash h
187188
(* TODO: duplicate bucklets ? *)
188189
end
189190

190191
let rec mem_in_bucket ~eq key buckets =
191-
match N.toOpt buckets with
192+
match C.toOpt buckets with
192193
| None ->
193194
false
194195
| Some cell ->
@@ -197,15 +198,15 @@ let rec mem_in_bucket ~eq key buckets =
197198
mem_in_bucket ~eq key (N.next cell)
198199

199200
let mem0 ~hash ~eq h key =
200-
let h_buckets = N.buckets h in
201+
let h_buckets = C.buckets h in
201202
let nid = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
202203
mem_in_bucket ~eq key (Bs_Array.unsafe_get h_buckets nid)
203204

204205

205-
let create0 = N.create0
206-
let clear0 = N.clear0
207-
let reset0 = N.reset0
208-
let length0 = N.length0
206+
let create0 = C.create0
207+
let clear0 = C.clear0
208+
let reset0 = C.reset0
209+
let length0 = C.length0
209210
let iter0 = N.iter0
210211
let fold0 = N.fold0
211212
let logStats0 = N.logStats0

0 commit comments

Comments
 (0)