@@ -67,14 +67,23 @@ module A = Belt_Array
67
67
68
68
external mutableCell :
69
69
'a -> 'a t -> 'a t = " #makemutablelist"
70
+
71
+
72
+ type poly = { unsafeMutateTail : 'a . 'a t -> 'a t -> unit } [@@ unboxed]
73
+
74
+
70
75
(*
71
76
[mutableCell x []] == [x]
72
77
but tell the compiler that is a mutable cell, so it wont
73
78
be mis-inlined in the future
74
79
dont inline a binding to mutable cell, it is mutable
75
80
*)
76
- external unsafeMutateTail :
77
- 'a t -> 'a t -> unit = " #setfield1"
81
+ (* relies on list internal representation *)
82
+ let m : poly = {unsafeMutateTail = [% raw{| function(xs,ys){
83
+ xs[1 ] = ys
84
+ }| }]}
85
+
86
+
78
87
79
88
(*
80
89
- the cell is not empty
@@ -128,12 +137,12 @@ let rec partitionAux p cell precX precY =
128
137
let next = mutableCell h [] in
129
138
if p h [@ bs] then
130
139
begin
131
- unsafeMutateTail precX next ;
140
+ m. unsafeMutateTail precX next ;
132
141
partitionAux p t next precY
133
142
end
134
143
else
135
144
begin
136
- unsafeMutateTail precY next ;
145
+ m. unsafeMutateTail precY next ;
137
146
partitionAux p t precX next
138
147
end
139
148
@@ -143,8 +152,8 @@ let rec splitAux cell precX precY =
143
152
| (a ,b )::t ->
144
153
let nextA = mutableCell a [] in
145
154
let nextB = mutableCell b [] in
146
- unsafeMutateTail precX nextA;
147
- unsafeMutateTail precY nextB;
155
+ m. unsafeMutateTail precX nextA;
156
+ m. unsafeMutateTail precY nextB;
148
157
splitAux t nextA nextB
149
158
150
159
(* return the tail pointer so it can continue copy other
@@ -155,7 +164,7 @@ let rec copyAuxCont cellX prec =
155
164
| [] -> prec
156
165
| h ::t ->
157
166
let next = mutableCell h [] in
158
- unsafeMutateTail prec next ;
167
+ m. unsafeMutateTail prec next ;
159
168
copyAuxCont t next
160
169
161
170
let rec copyAuxWitFilter f cellX prec =
@@ -166,7 +175,7 @@ let rec copyAuxWitFilter f cellX prec =
166
175
if f h [@ bs] then
167
176
begin
168
177
let next = mutableCell h [] in
169
- unsafeMutateTail prec next ;
178
+ m. unsafeMutateTail prec next ;
170
179
copyAuxWitFilter f t next
171
180
end
172
181
else copyAuxWitFilter f t prec
@@ -179,7 +188,7 @@ let rec copyAuxWithFilterIndex f cellX prec i =
179
188
if f h i [@ bs] then
180
189
begin
181
190
let next = mutableCell h [] in
182
- unsafeMutateTail prec next ;
191
+ m. unsafeMutateTail prec next ;
183
192
copyAuxWithFilterIndex f t next (i + 1 )
184
193
end
185
194
else copyAuxWithFilterIndex f t prec (i + 1 )
@@ -193,7 +202,7 @@ let rec copyAuxWitFilterMap f cellX prec =
193
202
| Some h ->
194
203
begin
195
204
let next = mutableCell h [] in
196
- unsafeMutateTail prec next ;
205
+ m. unsafeMutateTail prec next ;
197
206
copyAuxWitFilterMap f t next
198
207
end
199
208
| None -> copyAuxWitFilterMap f t prec
@@ -203,21 +212,21 @@ let rec removeAssocAuxWithMap cellX x prec f =
203
212
| [] -> false
204
213
| ((a ,_ ) as h ):: t ->
205
214
if f a x [@ bs] then
206
- (unsafeMutateTail prec t ; true )
215
+ (m. unsafeMutateTail prec t ; true )
207
216
else
208
217
let next = mutableCell h [] in
209
- unsafeMutateTail prec next ;
218
+ m. unsafeMutateTail prec next ;
210
219
removeAssocAuxWithMap t x next f
211
220
212
221
let rec setAssocAuxWithMap cellX x k prec eq =
213
222
match cellX with
214
223
| [] -> false
215
224
| ((a ,_ ) as h ) :: t ->
216
225
if eq a x [@ bs] then
217
- (unsafeMutateTail prec ( (x,k)::t); true )
226
+ (m. unsafeMutateTail prec ( (x,k)::t); true )
218
227
else
219
228
let next = mutableCell h [] in
220
- unsafeMutateTail prec next ;
229
+ m. unsafeMutateTail prec next ;
221
230
setAssocAuxWithMap t x k next eq
222
231
223
232
@@ -227,15 +236,15 @@ let rec copyAuxWithMap cellX prec f =
227
236
()
228
237
| h ::t ->
229
238
let next = mutableCell (f h [@ bs]) [] in
230
- unsafeMutateTail prec next ;
239
+ m. unsafeMutateTail prec next ;
231
240
copyAuxWithMap t next f
232
241
233
242
234
243
let rec zipAux cellX cellY prec =
235
244
match cellX, cellY with
236
245
| h1 ::t1 , h2 ::t2 ->
237
246
let next = mutableCell ( h1, h2) [] in
238
- unsafeMutateTail prec next ;
247
+ m. unsafeMutateTail prec next ;
239
248
zipAux t1 t2 next
240
249
| [] ,_ | _ ,[] ->
241
250
()
@@ -244,7 +253,7 @@ let rec copyAuxWithMap2 f cellX cellY prec =
244
253
match cellX, cellY with
245
254
| h1 ::t1 , h2 ::t2 ->
246
255
let next = mutableCell (f h1 h2 [@ bs]) [] in
247
- unsafeMutateTail prec next ;
256
+ m. unsafeMutateTail prec next ;
248
257
copyAuxWithMap2 f t1 t2 next
249
258
| [] ,_ | _ ,[] ->
250
259
()
@@ -253,7 +262,7 @@ let rec copyAuxWithMapI f i cellX prec =
253
262
match cellX with
254
263
| h ::t ->
255
264
let next = mutableCell (f i h [@ bs]) [] in
256
- unsafeMutateTail prec next ;
265
+ m. unsafeMutateTail prec next ;
257
266
copyAuxWithMapI f (i + 1 ) t next
258
267
| [] ->
259
268
()
@@ -265,7 +274,7 @@ let rec takeAux n cell prec =
265
274
| [] -> false
266
275
| x ::xs ->
267
276
let cell = mutableCell x [] in
268
- unsafeMutateTail prec cell;
277
+ m. unsafeMutateTail prec cell;
269
278
takeAux (n - 1 ) xs cell
270
279
271
280
let rec splitAtAux n cell prec =
@@ -275,7 +284,7 @@ let rec splitAtAux n cell prec =
275
284
| [] -> None
276
285
| x ::xs ->
277
286
let cell = mutableCell x [] in
278
- unsafeMutateTail prec cell;
287
+ m. unsafeMutateTail prec cell;
279
288
splitAtAux (n - 1 ) xs cell
280
289
281
290
(* invarint [n >= 0] *)
@@ -323,7 +332,7 @@ let concat xs ys =
323
332
| [] -> ys
324
333
| h ::t ->
325
334
let cell = mutableCell h [] in
326
- unsafeMutateTail (copyAuxCont t cell) ys;
335
+ m. unsafeMutateTail (copyAuxCont t cell) ys;
327
336
cell
328
337
329
338
let mapU xs f =
@@ -365,7 +374,7 @@ let makeByU n f =
365
374
let i = ref 1 in
366
375
while i.contents < n do
367
376
let v = mutableCell (f i.contents [@ bs]) [] in
368
- unsafeMutateTail cur.contents v ;
377
+ m. unsafeMutateTail cur.contents v ;
369
378
cur.contents< - v ;
370
379
i.contents < - i.contents + 1 ;
371
380
done
@@ -374,15 +383,15 @@ let makeByU n f =
374
383
375
384
let makeBy n f = makeByU n (fun[@ bs] x -> f x)
376
385
377
- let make n v =
386
+ let make ( type a ) n ( v : a ) : a list =
378
387
if n < = 0 then []
379
388
else
380
389
let headX = mutableCell v [] in
381
390
let cur = ref headX in
382
391
let i = ref 1 in
383
392
while i.contents < n do
384
393
let v = mutableCell v [] in
385
- unsafeMutateTail cur.contents v ;
394
+ m. unsafeMutateTail cur.contents v ;
386
395
cur.contents< - v ;
387
396
i.contents < - i.contents + 1 ;
388
397
done
@@ -452,7 +461,7 @@ let shuffle xs =
452
461
(* let cell = ref head in *)
453
462
(* for i = 1 to len - 1 do *)
454
463
(* let next = mutableCell (f (A.getUnsafe arr i) [@bs]) [] in *)
455
- (* unsafeMutateTail !cell next ; *)
464
+ (* unsafeMutateTail.unsafeMutateTail !cell next ; *)
456
465
(* cell .contents<- next *)
457
466
(* done ; *)
458
467
(* head *)
@@ -468,7 +477,7 @@ let reverse l = reverseConcat l []
468
477
469
478
let rec flattenAux prec xs =
470
479
match xs with
471
- | [] -> unsafeMutateTail prec []
480
+ | [] -> m. unsafeMutateTail prec []
472
481
| h ::r -> flattenAux (copyAuxCont h prec) r
473
482
474
483
0 commit comments