13
13
(* * Adapted by Authors of BuckleScript 2017 *)
14
14
15
15
module N = Bs_internalBuckets
16
+ module C = Bs_internalBucketsType
16
17
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
18
19
19
20
20
21
type ('a,'b) bucket = ('a ,'b) N .bucket
@@ -26,12 +27,12 @@ type ('a,'b,'id) t =
26
27
27
28
28
29
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
30
31
| None -> ()
31
32
| Some cell ->
32
33
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
35
36
| None ->
36
37
Bs_Array. unsafe_set h_buckets nidx v
37
38
| Some tail ->
@@ -42,101 +43,101 @@ let rec insert_bucket ~hash ~h_buckets ~ndata_tail h old_bucket =
42
43
43
44
44
45
let resize ~hash h =
45
- let odata = N . buckets h in
46
+ let odata = C . buckets h in
46
47
let osize = Array. length odata in
47
48
let nsize = osize * 2 in
48
49
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 *)
52
53
for i = 0 to osize - 1 do
53
54
insert_bucket ~hash ~h_buckets ~ndata_tail h (Bs_Array. unsafe_get odata i)
54
55
done ;
55
56
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
57
58
| None -> ()
58
- | Some tail -> N. nextSet tail N . emptyOpt
59
+ | Some tail -> N. nextSet tail C . emptyOpt
59
60
done
60
61
end
61
62
62
63
63
64
let add0 ~hash h key value =
64
- let h_buckets = N . buckets h in
65
+ let h_buckets = C . buckets h in
65
66
let h_buckets_lenth = Array. length h_buckets in
66
67
let i = (Bs_Hash. getHash hash) key [@ bs] land (h_buckets_lenth - 1 ) in
67
68
let bucket =
68
69
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;
72
73
if h_new_size > h_buckets_lenth lsl 1 then resize ~hash h
73
74
74
75
75
76
let rec remove_bucket ~eq h h_buckets i key prec buckets =
76
- match N . toOpt buckets with
77
+ match C . toOpt buckets with
77
78
| None -> ()
78
79
| Some cell ->
79
80
let cell_next = N. next cell in
80
81
if (Bs_Hash. getEq eq) (N. key cell) key [@ bs]
81
82
then
82
83
begin
83
- (match N . toOpt prec with
84
+ (match C . toOpt prec with
84
85
| None -> Bs_Array. unsafe_set h_buckets i cell_next
85
86
| Some c -> N. nextSet c cell_next);
86
- N . sizeSet h (N . size h - 1 );
87
+ C . sizeSet h (C . size h - 1 );
87
88
end
88
89
else remove_bucket ~eq h h_buckets i key buckets cell_next
89
90
90
91
let remove0 ~hash ~eq h key =
91
- let h_buckets = N . buckets h in
92
+ let h_buckets = C . buckets h in
92
93
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)
94
95
95
96
let rec removeAllBuckets ~eq h h_buckets i key prec buckets =
96
- match N . toOpt buckets with
97
+ match C . toOpt buckets with
97
98
| None -> ()
98
99
| Some cell ->
99
100
let cell_next = N. next cell in
100
101
if (Bs_Hash. getEq eq) (N. key cell) key [@ bs]
101
102
then
102
103
begin
103
- (match N . toOpt prec with
104
+ (match C . toOpt prec with
104
105
| None -> Bs_Array. unsafe_set h_buckets i cell_next
105
106
| Some c -> N. nextSet c cell_next);
106
- N . sizeSet h (N . size h - 1 );
107
+ C . sizeSet h (C . size h - 1 );
107
108
end ;
108
109
removeAllBuckets ~eq h h_buckets i key buckets cell_next
109
110
110
111
let removeAll0 ~hash ~eq h key =
111
- let h_buckets = N . buckets h in
112
+ let h_buckets = C . buckets h in
112
113
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)
114
115
115
116
116
117
let rec find_rec ~eq key buckets =
117
- match N . toOpt buckets with
118
+ match C . toOpt buckets with
118
119
| None ->
119
120
None
120
121
| Some cell ->
121
122
if (Bs_Hash. getEq eq) key (N. key cell) [@ bs] then Some (N. value cell)
122
123
else find_rec ~eq key (N. next cell)
123
124
124
125
let findOpt0 ~hash ~eq h key =
125
- let h_buckets = N . buckets h in
126
+ let h_buckets = C . buckets h in
126
127
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
128
129
| None -> None
129
130
| Some cell1 ->
130
131
if (Bs_Hash. getEq eq) key (N. key cell1) [@ bs] then
131
132
Some (N. value cell1)
132
133
else
133
- match N . toOpt (N. next cell1) with
134
+ match C . toOpt (N. next cell1) with
134
135
| None -> None
135
136
| Some cell2 ->
136
137
if (Bs_Hash. getEq eq) key
137
138
(N. key cell2) [@ bs] then
138
139
Some (N. value cell2) else
139
- match N . toOpt (N. next cell2) with
140
+ match C . toOpt (N. next cell2) with
140
141
| None -> None
141
142
| Some cell3 ->
142
143
if (Bs_Hash. getEq eq) key
@@ -148,20 +149,20 @@ let findOpt0 ~hash ~eq h key =
148
149
149
150
let findAll0 ~hash ~eq h key =
150
151
let rec find_in_bucket buckets =
151
- match N . toOpt buckets with
152
+ match C . toOpt buckets with
152
153
| None ->
153
154
[]
154
155
| Some cell ->
155
156
if (Bs_Hash. getEq eq)
156
157
(N. key cell) key [@ bs]
157
158
then (N. value cell) :: find_in_bucket (N. next cell)
158
159
else find_in_bucket (N. next cell) in
159
- let h_buckets = N . buckets h in
160
+ let h_buckets = C . buckets h in
160
161
let nid = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
161
162
find_in_bucket (Bs_Array. unsafe_get h_buckets nid)
162
163
163
164
let rec replace_bucket ~eq key info buckets =
164
- match N . toOpt buckets with
165
+ match C . toOpt buckets with
165
166
| None ->
166
167
true
167
168
| Some cell ->
@@ -176,19 +177,19 @@ let rec replace_bucket ~eq key info buckets =
176
177
replace_bucket ~eq key info (N. next cell)
177
178
178
179
let replace0 ~hash ~eq h key info =
179
- let h_buckets = N . buckets h in
180
+ let h_buckets = C . buckets h in
180
181
let i = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
181
182
let l = Array. unsafe_get h_buckets i in
182
183
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
184
185
(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
187
188
(* TODO: duplicate bucklets ? *)
188
189
end
189
190
190
191
let rec mem_in_bucket ~eq key buckets =
191
- match N . toOpt buckets with
192
+ match C . toOpt buckets with
192
193
| None ->
193
194
false
194
195
| Some cell ->
@@ -197,15 +198,15 @@ let rec mem_in_bucket ~eq key buckets =
197
198
mem_in_bucket ~eq key (N. next cell)
198
199
199
200
let mem0 ~hash ~eq h key =
200
- let h_buckets = N . buckets h in
201
+ let h_buckets = C . buckets h in
201
202
let nid = (Bs_Hash. getHash hash) key [@ bs] land (Array. length h_buckets - 1 ) in
202
203
mem_in_bucket ~eq key (Bs_Array. unsafe_get h_buckets nid)
203
204
204
205
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
209
210
let iter0 = N. iter0
210
211
let fold0 = N. fold0
211
212
let logStats0 = N. logStats0
0 commit comments