-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathbelt_Array.ml
525 lines (431 loc) · 13.3 KB
/
belt_Array.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Array operations *)
type 'a t = 'a array
external length : 'a t -> int = "%array_length"
external size : 'a t -> int = "%array_length"
external getUnsafe : 'a t -> int -> 'a = "%array_unsafe_get"
external setUnsafe : 'a t -> int -> 'a -> unit = "%array_unsafe_set"
external ( .!() ) : 'a t -> int -> 'a = "%array_unsafe_get"
external ( .!()<- ) : 'a t -> int -> 'a -> unit = "%array_unsafe_set"
external getUndefined : 'a t -> int -> 'a Js.undefined = "%array_unsafe_get"
(* external get: 'a t -> int -> 'a = "%array_safe_get" *)
let get arr i = if i >= 0 && i < length arr then Some arr.!(i) else None
let getExn arr i =
assert (i >= 0 && i < length arr);
arr.!(i)
let set arr i v =
if i >= 0 && i < length arr then (
arr.!(i) <- v;
true)
else false
let setExn arr i v =
assert (i >= 0 && i < length arr);
arr.!(i) <- v
external truncateToLengthUnsafe : 'a t -> int -> unit = "length" [@@bs.set]
external makeUninitialized : int -> 'a Js.undefined array = "Array" [@@bs.new]
external makeUninitializedUnsafe : int -> 'a array = "Array" [@@bs.new]
external copy : 'a t -> (_[@bs.as 0]) -> 'a t = "slice" [@@bs.send]
let swapUnsafe xs i j =
let tmp = xs.!(i) in
xs.!(i) <- xs.!(j);
xs.!(j) <- tmp
let shuffleInPlace xs =
let len = length xs in
for i = 0 to len - 1 do
swapUnsafe xs i (Js_math.random_int i len) (* [i,len)*)
done
let shuffle xs =
let result = copy xs in
shuffleInPlace result;
(* TODO: improve*)
result
let reverseAux xs ofs len =
for i = 0 to (len / 2) - 1 do
swapUnsafe xs (ofs + i) (ofs + len - i - 1)
done
let reverseInPlace xs =
let len = length xs in
reverseAux xs 0 len
let reverse xs =
let len = length xs in
let result = makeUninitializedUnsafe len in
for i = 0 to len - 1 do
result.!(i) <- xs.!(len - 1 - i)
done;
result
let make l f =
if l <= 0 then [||]
else
let res = makeUninitializedUnsafe l in
for i = 0 to l - 1 do
res.!(i) <- f
done;
res
(* See #6575. We could also check for maximum array size, but this depends
on whether we create a float array or a regular one... *)
let makeByU l f =
if l <= 0 then [||]
else
let res = makeUninitializedUnsafe l in
for i = 0 to l - 1 do
res.!(i) <- (f i [@bs])
done;
res
let makeBy l f = makeByU l (fun [@bs] a -> f a)
let makeByAndShuffleU l f =
let u = makeByU l f in
shuffleInPlace u;
u
let makeByAndShuffle l f = makeByAndShuffleU l (fun [@bs] a -> f a)
let range start finish =
let cut = finish - start in
if cut < 0 then [||]
else
let arr = makeUninitializedUnsafe (cut + 1) in
for i = 0 to cut do
arr.!(i) <- start + i
done;
arr
let rangeBy start finish ~step =
let cut = finish - start in
if cut < 0 || step <= 0 then [||]
else
let nb = (cut / step) + 1 in
let arr = makeUninitializedUnsafe nb in
let cur = ref start in
for i = 0 to nb - 1 do
arr.!(i) <- cur.contents;
cur.contents <- cur.contents + step
done;
arr
let zip xs ys =
let lenx, leny = (length xs, length ys) in
let len = Pervasives.min lenx leny in
let s = makeUninitializedUnsafe len in
for i = 0 to len - 1 do
s.!(i) <- (xs.!(i), ys.!(i))
done;
s
let zipByU xs ys f =
let lenx, leny = (length xs, length ys) in
let len = Pervasives.min lenx leny in
let s = makeUninitializedUnsafe len in
for i = 0 to len - 1 do
s.!(i) <- (f xs.!(i) ys.!(i) [@bs])
done;
s
let zipBy xs ys f = zipByU xs ys (fun [@bs] a b -> f a b)
let concat a1 a2 =
let l1 = length a1 in
let l2 = length a2 in
let a1a2 = makeUninitializedUnsafe (l1 + l2) in
for i = 0 to l1 - 1 do
a1a2.!(i) <- a1.!(i)
done;
for i = 0 to l2 - 1 do
a1a2.!(l1 + i) <- a2.!(i)
done;
a1a2
let concatMany arrs =
let lenArrs = length arrs in
let totalLen = ref 0 in
for i = 0 to lenArrs - 1 do
totalLen.contents <- totalLen.contents + length arrs.!(i)
done;
let result = makeUninitializedUnsafe totalLen.contents in
totalLen.contents <- 0;
for j = 0 to lenArrs - 1 do
let cur = arrs.!(j) in
for k = 0 to length cur - 1 do
result.!(totalLen.contents) <- cur.!(k);
totalLen.contents <- totalLen.contents + 1
done
done;
result
let slice a ~offset ~len =
if len <= 0 then [||]
else
let lena = length a in
let ofs = if offset < 0 then Pervasives.max (lena + offset) 0 else offset in
let hasLen = lena - ofs in
let copyLength = Pervasives.min hasLen len in
if copyLength <= 0 then [||]
else
let result = makeUninitializedUnsafe copyLength in
for i = 0 to copyLength - 1 do
result.!(i) <- a.!(ofs + i)
done;
result
let sliceToEnd a offset =
let lena = length a in
let ofs = if offset < 0 then Pervasives.max (lena + offset) 0 else offset in
let len = if lena > ofs then lena - ofs else 0 in
let result = makeUninitializedUnsafe len in
for i = 0 to len - 1 do
result.!(i) <- a.!(ofs + i)
done;
result
let fill a ~offset ~len v =
if len > 0 then
let lena = length a in
let ofs = if offset < 0 then Pervasives.max (lena + offset) 0 else offset in
let hasLen = lena - ofs in
let fillLength = Pervasives.min hasLen len in
if fillLength > 0 then
for i = ofs to ofs + fillLength - 1 do
a.!(i) <- v
done
let blitUnsafe ~src:a1 ~srcOffset:srcofs1 ~dst:a2 ~dstOffset:srcofs2
~len:blitLength =
if srcofs2 <= srcofs1 then
for j = 0 to blitLength - 1 do
a2.!(j + srcofs2) <- a1.!(j + srcofs1)
done
else
for j = blitLength - 1 downto 0 do
a2.!(j + srcofs2) <- a1.!(j + srcofs1)
done
(* We don't need check `blitLength` since when `blitLength < 0` the
for loop will be nop
*)
let blit ~src:a1 ~srcOffset:ofs1 ~dst:a2 ~dstOffset:ofs2 ~len =
let lena1 = length a1 in
let lena2 = length a2 in
let srcofs1 = if ofs1 < 0 then Pervasives.max (lena1 + ofs1) 0 else ofs1 in
let srcofs2 = if ofs2 < 0 then Pervasives.max (lena2 + ofs2) 0 else ofs2 in
let blitLength =
Pervasives.min len (Pervasives.min (lena1 - srcofs1) (lena2 - srcofs2))
in
(* blitUnsafe a1 srcofs1 a2 srcofs2 blitLength *)
if srcofs2 <= srcofs1 then
for j = 0 to blitLength - 1 do
a2.!(j + srcofs2) <- a1.!(j + srcofs1)
done
else
for j = blitLength - 1 downto 0 do
a2.!(j + srcofs2) <- a1.!(j + srcofs1)
done
let forEachU a f =
for i = 0 to length a - 1 do
(f a.!(i) [@bs])
done
let forEach a f = forEachU a (fun [@bs] a -> f a)
let mapU a f =
let l = length a in
let r = makeUninitializedUnsafe l in
for i = 0 to l - 1 do
r.!(i) <- (f a.!(i) [@bs])
done;
r
let map a f = mapU a (fun [@bs] a -> f a)
let flatMapU a f = concatMany (mapU a f)
let flatMap a f = flatMapU a (fun [@bs] a -> f a)
let getByU a p =
let l = length a in
let i = ref 0 in
let r = ref None in
while r.contents = None && i.contents < l do
let v = a.!(i.contents) in
if p v [@bs] then r.contents <- Some v;
i.contents <- i.contents + 1
done;
r.contents
let getBy a p = getByU a (fun [@bs] a -> p a)
let getIndexByU a p =
let l = length a in
let i = ref 0 in
let r = ref None in
while r.contents = None && i.contents < l do
let v = a.!(i.contents) in
if p v [@bs] then r.contents <- Some i.contents;
i.contents <- i.contents + 1
done;
r.contents
let getIndexBy a p = getIndexByU a (fun [@bs] a -> p a)
let keepU a f =
let l = length a in
let r = makeUninitializedUnsafe l in
let j = ref 0 in
for i = 0 to l - 1 do
let v = a.!(i) in
if f v [@bs] then (
r.!(j.contents) <- v;
j.contents <- j.contents + 1)
done;
truncateToLengthUnsafe r j.contents;
r
let keep a f = keepU a (fun [@bs] a -> f a)
let keepWithIndexU a f =
let l = length a in
let r = makeUninitializedUnsafe l in
let j = ref 0 in
for i = 0 to l - 1 do
let v = a.!(i) in
if f v i [@bs] then (
r.!(j.contents) <- v;
j.contents <- j.contents + 1)
done;
truncateToLengthUnsafe r j.contents;
r
let keepWithIndex a f = keepWithIndexU a (fun [@bs] a i -> f a i)
let keepMapU a f =
let l = length a in
let r = makeUninitializedUnsafe l in
let j = ref 0 in
for i = 0 to l - 1 do
let v = a.!(i) in
match f v [@bs] with
| None -> ()
| Some v ->
r.!(j.contents) <- v;
j.contents <- j.contents + 1
done;
truncateToLengthUnsafe r j.contents;
r
let keepMap a f = keepMapU a (fun [@bs] a -> f a)
let forEachWithIndexU a f =
for i = 0 to length a - 1 do
(f i a.!(i) [@bs])
done
let forEachWithIndex a f = forEachWithIndexU a (fun [@bs] a b -> f a b)
let mapWithIndexU a f =
let l = length a in
let r = makeUninitializedUnsafe l in
for i = 0 to l - 1 do
r.!(i) <- (f i a.!(i) [@bs])
done;
r
let mapWithIndex a f = mapWithIndexU a (fun [@bs] a b -> f a b)
let reduceU a x f =
let r = ref x in
for i = 0 to length a - 1 do
r.contents <- (f r.contents a.!(i) [@bs])
done;
r.contents
let reduce a x f = reduceU a x (fun [@bs] a b -> f a b)
let reduceReverseU a x f =
let r = ref x in
for i = length a - 1 downto 0 do
r.contents <- (f r.contents a.!(i) [@bs])
done;
r.contents
let reduceReverse a x f = reduceReverseU a x (fun [@bs] a b -> f a b)
let reduceReverse2U a b x f =
let r = ref x in
let len = Pervasives.min (length a) (length b) in
for i = len - 1 downto 0 do
r.contents <- (f r.contents a.!(i) b.!(i) [@bs])
done;
r.contents
let reduceReverse2 a b x f = reduceReverse2U a b x (fun [@bs] a b c -> f a b c)
let reduceWithIndexU a x f =
let r = ref x in
for i = 0 to length a - 1 do
r.contents <- (f r.contents a.!(i) i [@bs])
done;
r.contents
let reduceWithIndex a x f = reduceWithIndexU a x (fun [@bs] a b c -> f a b c)
let rec everyAux arr i b len =
if i = len then true
else if b arr.!(i) [@bs] then everyAux arr (i + 1) b len
else false
let rec someAux arr i b len =
if i = len then false
else if b arr.!(i) [@bs] then true
else someAux arr (i + 1) b len
let everyU arr b =
let len = length arr in
everyAux arr 0 b len
let every arr f = everyU arr (fun [@bs] b -> f b)
let someU arr b =
let len = length arr in
someAux arr 0 b len
let some arr f = someU arr (fun [@bs] b -> f b)
let rec everyAux2 arr1 arr2 i b len =
if i = len then true
else if b arr1.!(i) arr2.!(i) [@bs] then everyAux2 arr1 arr2 (i + 1) b len
else false
let rec someAux2 arr1 arr2 i b len =
if i = len then false
else if b arr1.!(i) arr2.!(i) [@bs] then true
else someAux2 arr1 arr2 (i + 1) b len
let every2U a b p = everyAux2 a b 0 p (Pervasives.min (length a) (length b))
let every2 a b p = every2U a b (fun [@bs] a b -> p a b)
let some2U a b p = someAux2 a b 0 p (Pervasives.min (length a) (length b))
let some2 a b p = some2U a b (fun [@bs] a b -> p a b)
let eqU a b p =
let lena = length a in
let lenb = length b in
if lena = lenb then everyAux2 a b 0 p lena else false
let eq a b p = eqU a b (fun [@bs] a b -> p a b)
let rec everyCmpAux2 arr1 arr2 i b len =
if i = len then 0
else
let c = (b arr1.!(i) arr2.!(i) [@bs]) in
if c = 0 then everyCmpAux2 arr1 arr2 (i + 1) b len else c
let cmpU a b p =
let lena = length a in
let lenb = length b in
if lena > lenb then 1
else if lena < lenb then -1
else everyCmpAux2 a b 0 p lena
let cmp a b p = cmpU a b (fun [@bs] a b -> p a b)
let partitionU a f =
let l = length a in
let i = ref 0 in
let j = ref 0 in
let a1 = makeUninitializedUnsafe l in
let a2 = makeUninitializedUnsafe l in
for ii = 0 to l - 1 do
let v = a.!(ii) in
if f v [@bs] then (
a1.!(i.contents) <- v;
i.contents <- i.contents + 1)
else (
a2.!(j.contents) <- v;
j.contents <- j.contents + 1)
done;
truncateToLengthUnsafe a1 i.contents;
truncateToLengthUnsafe a2 j.contents;
(a1, a2)
let partition a f = partitionU a (fun [@bs] x -> f x)
let unzip a =
let l = length a in
let a1 = makeUninitializedUnsafe l in
let a2 = makeUninitializedUnsafe l in
for i = 0 to l - 1 do
let v1, v2 = a.!(i) in
a1.!(i) <- v1;
a2.!(i) <- v2
done;
(a1, a2)
let joinWithU a sep toString =
match length a with
| 0 -> ""
| l ->
let lastIndex = l - 1 in
let rec aux i res =
if i = lastIndex then res ^ (toString a.!(i) [@bs])
else aux (i + 1) (res ^ (toString a.!(i) [@bs]) ^ sep)
in
aux 0 ""
let joinWith a sep toString = joinWithU a sep (fun [@bs] x -> toString x)
let initU n f =
let v = makeUninitializedUnsafe n in
for i = 0 to n - 1 do
v.!(i) <- (f i [@bs])
done;
v
let init n f = initU n (fun [@bs] i -> f i)
external push : 'a t -> 'a -> unit = "push" [@@send]