11
11
(* *)
12
12
(* **********************************************************************)
13
13
(* * Adapted by Authors of BuckleScript 2017 *)
14
- type ('a, 'b,'id) t0 = ('a ,'b,'id) Bs_internalBuckets .t0 =
14
+
15
+ module N = Bs_internalBuckets
16
+
17
+ type ('a, 'b,'id) t0 = ('a ,'b,'id) N .t0 =
15
18
{
16
19
mutable size : int ; (* number of entries *)
17
- mutable buckets : ('a , 'b ) Bs_internalBuckets .bucketlist array ; (* the buckets *)
20
+ mutable buckets : ('a , 'b ) N .bucketlist array ; (* the buckets *)
18
21
initial_size : int ; (* initial array size *)
19
22
}
20
23
21
24
22
- type ('a,'b) buckets = ('a ,'b) Bs_internalBuckets .buckets
25
+ type ('a,'b) buckets = ('a ,'b) N .buckets
23
26
24
27
type ('a,'b,'id) t = {
25
28
dict : ('a , 'id ) Bs_Hash .t ;
@@ -28,36 +31,36 @@ type ('a,'b,'id) t = {
28
31
29
32
30
33
let rec insert_bucket ~hash ~h_buckets ~ndata_tail h old_bucket =
31
- match Bs_internalBuckets . toOpt old_bucket with
34
+ match N . toOpt old_bucket with
32
35
| None -> ()
33
36
| Some cell ->
34
- let nidx = (Bs_Hash. getHash hash) (Bs_internalBuckets . key cell) [@ bs] land (Array. length h_buckets - 1 ) in
35
- let v = Bs_internalBuckets . return cell in
36
- begin match Bs_internalBuckets . toOpt (Bs_Array. unsafe_get ndata_tail nidx) with
37
+ let nidx = (Bs_Hash. getHash hash) (N . key cell) [@ bs] land (Array. length h_buckets - 1 ) in
38
+ let v = N . return cell in
39
+ begin match N . toOpt (Bs_Array. unsafe_get ndata_tail nidx) with
37
40
| None ->
38
41
Bs_Array. unsafe_set h_buckets nidx v
39
42
| Some tail ->
40
- Bs_internalBuckets . nextSet tail v (* cell put at the end *)
43
+ N . nextSet tail v (* cell put at the end *)
41
44
end ;
42
45
Bs_Array. unsafe_set ndata_tail nidx v;
43
- insert_bucket ~hash ~h_buckets ~ndata_tail h (Bs_internalBuckets . next cell)
46
+ insert_bucket ~hash ~h_buckets ~ndata_tail h (N . next cell)
44
47
45
48
46
49
let resize ~hash h =
47
50
let odata = h.buckets in
48
51
let osize = Array. length odata in
49
52
let nsize = osize * 2 in
50
53
if nsize > = osize then begin (* no overflow *)
51
- let h_buckets = Bs_internalBuckets . makeSize nsize in
52
- let ndata_tail = Bs_internalBuckets . makeSize nsize in (* keep track of tail *)
54
+ let h_buckets = N . makeSize nsize in
55
+ let ndata_tail = N . makeSize nsize in (* keep track of tail *)
53
56
h.buckets < - h_buckets; (* so that indexfun sees the new bucket count *)
54
57
for i = 0 to osize - 1 do
55
58
insert_bucket ~hash ~h_buckets ~ndata_tail h (Bs_Array. unsafe_get odata i)
56
59
done ;
57
60
for i = 0 to nsize - 1 do
58
- match Bs_internalBuckets . toOpt (Bs_Array. unsafe_get ndata_tail i) with
61
+ match N . toOpt (Bs_Array. unsafe_get ndata_tail i) with
59
62
| None -> ()
60
- | Some tail -> Bs_internalBuckets . nextSet tail Bs_internalBuckets . emptyOpt
63
+ | Some tail -> N . nextSet tail N . emptyOpt
61
64
done
62
65
end
63
66
@@ -67,150 +70,150 @@ let add0 ~hash h key value =
67
70
let h_buckets_lenth = Array. length h_buckets in
68
71
let i = (Bs_Hash. getHash hash) key [@ bs] land (h_buckets_lenth - 1 ) in
69
72
let bucket =
70
- Bs_internalBuckets. newBuckets ~key ~value ~next: (Bs_Array. unsafe_get h_buckets i) in
71
- Bs_Array. unsafe_set h_buckets i (Bs_internalBuckets . return bucket);
73
+ N. buckets ~key ~value ~next: (Bs_Array. unsafe_get h_buckets i) in
74
+ Bs_Array. unsafe_set h_buckets i (N . return bucket);
72
75
let h_new_size = h.size + 1 in
73
76
h.size < - h_new_size;
74
77
if h_new_size > h_buckets_lenth lsl 1 then resize ~hash h
75
78
76
79
77
80
let rec remove_bucket ~eq h h_buckets i key prec buckets =
78
- match Bs_internalBuckets . toOpt buckets with
81
+ match N . toOpt buckets with
79
82
| None -> ()
80
83
| Some cell ->
81
- let cell_next = Bs_internalBuckets . next cell in
82
- if (Bs_Hash. getEq eq) (Bs_internalBuckets . key cell) key [@ bs]
84
+ let cell_next = N . next cell in
85
+ if (Bs_Hash. getEq eq) (N . key cell) key [@ bs]
83
86
then
84
87
begin
85
- (match Bs_internalBuckets . toOpt prec with
88
+ (match N . toOpt prec with
86
89
| None -> Bs_Array. unsafe_set h_buckets i cell_next
87
- | Some c -> Bs_internalBuckets . nextSet c cell_next);
90
+ | Some c -> N . nextSet c cell_next);
88
91
h.size < - h.size - 1 ;
89
92
end
90
93
else remove_bucket ~eq h h_buckets i key buckets cell_next
91
94
92
95
let remove0 ~hash ~eq h key =
93
96
let h_buckets = h.buckets in
94
97
let i = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
95
- remove_bucket ~eq h h_buckets i key Bs_internalBuckets . emptyOpt (Bs_Array. unsafe_get h_buckets i)
98
+ remove_bucket ~eq h h_buckets i key N . emptyOpt (Bs_Array. unsafe_get h_buckets i)
96
99
97
100
let rec removeAllBuckets ~eq h h_buckets i key prec buckets =
98
- match Bs_internalBuckets . toOpt buckets with
101
+ match N . toOpt buckets with
99
102
| None -> ()
100
103
| Some cell ->
101
- let cell_next = Bs_internalBuckets . next cell in
102
- if (Bs_Hash. getEq eq) (Bs_internalBuckets . key cell) key [@ bs]
104
+ let cell_next = N . next cell in
105
+ if (Bs_Hash. getEq eq) (N . key cell) key [@ bs]
103
106
then
104
107
begin
105
- (match Bs_internalBuckets . toOpt prec with
108
+ (match N . toOpt prec with
106
109
| None -> Bs_Array. unsafe_set h_buckets i cell_next
107
- | Some c -> Bs_internalBuckets . nextSet c cell_next);
110
+ | Some c -> N . nextSet c cell_next);
108
111
h.size < - h.size - 1 ;
109
112
end ;
110
113
removeAllBuckets ~eq h h_buckets i key buckets cell_next
111
114
112
115
let removeAll0 ~hash ~eq h key =
113
116
let h_buckets = h.buckets in
114
117
let i = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
115
- removeAllBuckets ~eq h h_buckets i key Bs_internalBuckets . emptyOpt (Bs_Array. unsafe_get h_buckets i)
118
+ removeAllBuckets ~eq h h_buckets i key N . emptyOpt (Bs_Array. unsafe_get h_buckets i)
116
119
117
120
118
121
let rec find_rec ~eq key buckets =
119
- match Bs_internalBuckets . toOpt buckets with
122
+ match N . toOpt buckets with
120
123
| None ->
121
124
None
122
125
| Some cell ->
123
- if (Bs_Hash. getEq eq) key (Bs_internalBuckets . key cell) [@ bs] then Some (Bs_internalBuckets . value cell)
124
- else find_rec ~eq key (Bs_internalBuckets . next cell)
126
+ if (Bs_Hash. getEq eq) key (N . key cell) [@ bs] then Some (N . value cell)
127
+ else find_rec ~eq key (N . next cell)
125
128
126
129
let findOpt0 ~hash ~eq h key =
127
130
let h_buckets = h.buckets in
128
131
let nid = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
129
- match Bs_internalBuckets . toOpt @@ Bs_Array. unsafe_get h_buckets nid with
132
+ match N . toOpt @@ Bs_Array. unsafe_get h_buckets nid with
130
133
| None -> None
131
134
| Some cell1 ->
132
- if (Bs_Hash. getEq eq) key (Bs_internalBuckets . key cell1) [@ bs] then
133
- Some (Bs_internalBuckets . value cell1)
135
+ if (Bs_Hash. getEq eq) key (N . key cell1) [@ bs] then
136
+ Some (N . value cell1)
134
137
else
135
- match Bs_internalBuckets . toOpt (Bs_internalBuckets . next cell1) with
138
+ match N . toOpt (N . next cell1) with
136
139
| None -> None
137
140
| Some cell2 ->
138
141
if (Bs_Hash. getEq eq) key
139
- (Bs_internalBuckets . key cell2) [@ bs] then
140
- Some (Bs_internalBuckets . value cell2) else
141
- match Bs_internalBuckets . toOpt (Bs_internalBuckets . next cell2) with
142
+ (N . key cell2) [@ bs] then
143
+ Some (N . value cell2) else
144
+ match N . toOpt (N . next cell2) with
142
145
| None -> None
143
146
| Some cell3 ->
144
147
if (Bs_Hash. getEq eq) key
145
- (Bs_internalBuckets . key cell3) [@ bs] then
146
- Some (Bs_internalBuckets . value cell3)
148
+ (N . key cell3) [@ bs] then
149
+ Some (N . value cell3)
147
150
else
148
- find_rec ~eq key (Bs_internalBuckets . next cell3)
151
+ find_rec ~eq key (N . next cell3)
149
152
150
153
151
154
let findAll0 ~hash ~eq h key =
152
155
let rec find_in_bucket buckets =
153
- match Bs_internalBuckets . toOpt buckets with
156
+ match N . toOpt buckets with
154
157
| None ->
155
158
[]
156
159
| Some cell ->
157
160
if (Bs_Hash. getEq eq)
158
- (Bs_internalBuckets . key cell) key [@ bs]
159
- then (Bs_internalBuckets . value cell) :: find_in_bucket (Bs_internalBuckets . next cell)
160
- else find_in_bucket (Bs_internalBuckets . next cell) in
161
+ (N . key cell) key [@ bs]
162
+ then (N . value cell) :: find_in_bucket (N . next cell)
163
+ else find_in_bucket (N . next cell) in
161
164
let h_buckets = h.buckets in
162
165
let nid = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
163
166
find_in_bucket (Bs_Array. unsafe_get h_buckets nid)
164
167
165
168
let rec replace_bucket ~eq key info buckets =
166
- match Bs_internalBuckets . toOpt buckets with
169
+ match N . toOpt buckets with
167
170
| None ->
168
171
true
169
172
| Some cell ->
170
- if (Bs_Hash. getEq eq) (Bs_internalBuckets . key cell) key [@ bs]
173
+ if (Bs_Hash. getEq eq) (N . key cell) key [@ bs]
171
174
then
172
175
begin
173
- Bs_internalBuckets . keySet cell key;
174
- Bs_internalBuckets . valueSet cell info;
176
+ N . keySet cell key;
177
+ N . valueSet cell info;
175
178
false
176
179
end
177
180
else
178
- replace_bucket ~eq key info (Bs_internalBuckets . next cell)
181
+ replace_bucket ~eq key info (N . next cell)
179
182
180
183
let replace0 ~hash ~eq h key info =
181
184
let h_buckets = h.buckets in
182
185
let i = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
183
186
let l = Array. unsafe_get h_buckets i in
184
187
if replace_bucket ~eq key info l then begin
185
- Bs_Array. unsafe_set h_buckets i (Bs_internalBuckets . return
186
- (Bs_internalBuckets. newBuckets ~key ~value: info ~next: l));
188
+ Bs_Array. unsafe_set h_buckets i (N . return
189
+ (N. buckets ~key ~value: info ~next: l));
187
190
h.size < - h.size + 1 ;
188
191
if h.size > Array. length h.buckets lsl 1 then resize ~hash h
189
192
end
190
193
191
194
let rec mem_in_bucket ~eq key buckets =
192
- match Bs_internalBuckets . toOpt buckets with
195
+ match N . toOpt buckets with
193
196
| None ->
194
197
false
195
198
| Some cell ->
196
199
(Bs_Hash. getEq eq)
197
- (Bs_internalBuckets . key cell) key [@ bs] ||
198
- mem_in_bucket ~eq key (Bs_internalBuckets . next cell)
200
+ (N . key cell) key [@ bs] ||
201
+ mem_in_bucket ~eq key (N . next cell)
199
202
200
203
let mem0 ~hash ~eq h key =
201
204
let h_buckets = h.buckets in
202
205
let nid = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
203
206
mem_in_bucket ~eq key (Bs_Array. unsafe_get h_buckets nid)
204
207
205
208
206
- let create0 = Bs_internalBuckets . create0
207
- let clear0 = Bs_internalBuckets . clear0
208
- let reset0 = Bs_internalBuckets . reset0
209
- let length0 = Bs_internalBuckets . length0
210
- let iter0 = Bs_internalBuckets . iter0
211
- let fold0 = Bs_internalBuckets . fold0
212
- let logStats0 = Bs_internalBuckets . logStats0
213
- let filterMapInplace0 = Bs_internalBuckets . filterMapInplace0
209
+ let create0 = N . create0
210
+ let clear0 = N . clear0
211
+ let reset0 = N . reset0
212
+ let length0 = N . length0
213
+ let iter0 = N . iter0
214
+ let fold0 = N . fold0
215
+ let logStats0 = N . logStats0
216
+ let filterMapInplace0 = N . filterMapInplace0
214
217
215
218
(* Wrapper *)
216
219
let create dict initialize_size =
0 commit comments