Repository navigation
Expand file tree
/
Copy pathstdcompat__array.ml.in
More file actions
449 lines (372 loc) · 11.1 KB
/
Copy pathstdcompat__array.ml.in
File metadata and controls
449 lines (372 loc) · 11.1 KB
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
include Array
@BEGIN_BEFORE_5_2_0@
let init_matrix m n f =
init m (fun i -> init n (fun j -> f i j))
let shuffle ~rand array =
for i = length array - 1 downto 0 do
let j = rand (i + 1) in
let tmp = unsafe_get array i in
unsafe_set array i array.(j);
unsafe_set array j tmp
done
@END_BEFORE_5_2_0@
@BEGIN_BEFORE_4_08_0@
type 'a t = 'a array
@END_BEFORE_4_08_0@
@BEGIN_BEFORE_4_02_0@
let create_float l = Array.make l 0.
let make_float = create_float
@END_BEFORE_4_02_0@
@BEGIN_FROM_4_02_0@
@BEGIN_BEFORE_4_03_0@
let create_float = Array.make_float
@END_BEFORE_4_03_0@
@END_FROM_4_02_0@
@BEGIN_BEFORE_4_13_0@
let fold_left_map f init array =
let r = ref init in
let array' = Array.map (fun item ->
let (accu, item') = f !r item in
r := accu;
item') array in
!r, array'
let find_opt p a =
Stdcompat__generic_array.find_opt ~length ~unsafe_get p a
let find_map f a =
Stdcompat__generic_array.find_map ~length ~unsafe_get f a
let split array =
let l = length array in
if l = 0 then
([| |], [| |])
else
let (fst0, snd0) = unsafe_get array 0 in
let array_fst = Array.make l fst0 in
let array_snd = Array.make l snd0 in
for i = 1 to l - 1 do
let (fsti, sndi) = unsafe_get array i in
Array.unsafe_set array_fst i fsti;
Array.unsafe_set array_snd i sndi;
done;
(array_fst, array_snd)
let combine array_fst array_snd =
if length array_fst <> length array_snd then
invalid_arg "Array.combine";
Array.init (length array_fst) (fun i ->
(unsafe_get array_fst i, unsafe_get array_snd i))
@END_BEFORE_4_13_0@
@BEGIN_BEFORE_4_11_0@
let for_all2 p a1 a2 =
Stdcompat__generic_array.for_all2 ~caller:"Array.for_all2"
~length1:length ~length2:length
~unsafe_get1:unsafe_get ~unsafe_get2:unsafe_get
p a1 a2
let exists2 f array1 array2 =
Stdcompat__generic_array.exists2 ~caller:"Array.exists2"
~length1:length ~length2:length
~unsafe_get1:unsafe_get ~unsafe_get2:unsafe_get
p a1 a2
@END_BEFORE_4_11_0@
@BEGIN_BEFORE_4_03_0@
let iter2 f array1 array2 =
if length array1 <> length array2 then
invalid_arg "Array.iter2";
for i = 0 to length array1 - 1 do
f (unsafe_get array1 i) (unsafe_get array2 i)
done
let map2 f array1 array2 =
if length array1 <> length array2 then
invalid_arg "Array.map2";
init (length array1)
(fun i -> f (unsafe_get array1 i) (unsafe_get array2 i))
let for_all p a =
Stdcompat__generic_array.for_all ~length ~unsafe_get p a
let exists f array =
Stdcompat__generic_array.exists ~length ~unsafe_get p a
let mem item =
exists (( = ) item)
let memq item =
exists (( == ) item)
@END_BEFORE_4_03_0@
@BEGIN_BEFORE_4_06_0@
module Floatarray = struct
let create = create_float
let length : Stdcompat__root.floatarray -> int = length
let get : Stdcompat__root.floatarray -> int -> float = get
let set : Stdcompat__root.floatarray -> int -> float -> unit = set
let unsafe_get : Stdcompat__root.floatarray -> int -> float = unsafe_get
let unsafe_set : Stdcompat__root.floatarray -> int -> float -> unit
= unsafe_set
end
@END_BEFORE_4_06_0@
@BEGIN_BEFORE_4_07_0@
let to_seq s = Stdcompat__tools.vec_to_seq length unsafe_get s
let to_seqi s = Stdcompat__tools.vec_to_seqi length unsafe_get s
@END_BEFORE_4_07_0@
@BEGIN_BEFORE_4_07_1@
(* Array.of_seq is redefined in OCaml 4.07.0 to circumvent a bug in the
implementation of the standard library. See:
- https://caml.inria.fr/mantis/view.php?id=7820
- https://github.com/ocaml/ocaml/pull/1897 *)
let of_rev_list l =
match l with
| [] -> [||]
| hd :: tl ->
let len = List.length l in
let result = make len hd in
let rec fill i l =
match l with
| [] -> result
| hd :: tl ->
unsafe_set result i hd;
fill (pred i) tl in
fill (len - 2) tl
let of_seq g =
of_rev_list (Stdcompat__seq.fold_left (fun accu x -> x :: accu) [] g)
@END_BEFORE_4_07_1@
@BEGIN_BEFORE_5_1_0@
let map_inplace f a =
Stdcompat__generic_array.map_inplace ~length ~unsafe_get ~unsafe_set f a
let mapi_inplace f a =
Stdcompat__generic_array.mapi_inplace ~length ~unsafe_get ~unsafe_set f a
let find_index p a =
Stdcompat__generic_array.find_index ~length ~unsafe_get p a
let find_mapi f a =
Stdcompat__generic_array.find_mapi ~length ~unsafe_get f a
@END_BEFORE_5_1_0@
@BEGIN_BEFORE_5_4_0@
let equal eq a b =
Stdcompat__generic_array.equal ~length ~unsafe_get eq a b
let compare cmp a b =
Stdcompat__generic_array.compare ~length ~unsafe_get cmp a b
@END_BEFORE_5_4_0@
@BEGIN_BEFORE_5_5_0@
(* Generated with ChatGPT (Free version, 2026-08-08)
with the following prompt:
Implement powersort in OCaml.
The interface should be:
val stable_sort_sub : ('a -> 'a -> int) -> 'a array -> int -> int -> unit
*)
type run = {
mutable start : int;
mutable len : int;
mutable power : int;
}
let stable_sort_sub cmp a ofs len =
if len < 2 then
()
else begin
let last = ofs + len in
(* Powersort's minrun heuristic, with MAX_MINRUN = 64. *)
let minrun n =
let r = ref 0 in
let n = ref n in
while !n >= 64 do
r := !r lor (!n land 1);
n := !n lsr 1
done;
!n + !r
in
(* Stable binary insertion sort of a[lo .. hi), where
a[lo .. start) is already sorted. *)
let binary_sort lo start hi =
for i = start to hi - 1 do
let x = a.(i) in
let left = ref lo in
let right = ref i in
(* Upper bound: equal elements already in the sorted
prefix stay before x, hence stability. *)
while !left < !right do
let m = !left + ((!right - !left) lsr 1) in
if cmp x a.(m) < 0 then
right := m
else
left := m + 1
done;
let j = ref i in
while !j > !left do
a.(!j) <- a.(!j - 1);
decr j
done;
a.(!left) <- x
done
in
(* Find a natural run starting at lo.
Equal elements belong to ascending runs. A descending run is
necessarily strictly descending, so reversing it is stable. *)
let find_run lo =
let p = ref (lo + 1) in
if !p = last then
last
else if cmp a.(!p) a.(lo) < 0 then begin
(* Strictly descending. *)
incr p;
while !p < last && cmp a.(!p) a.(!p - 1) < 0 do
incr p
done;
let i = ref lo in
let j = ref (!p - 1) in
while !i < !j do
let x = a.(!i) in
a.(!i) <- a.(!j);
a.(!j) <- x;
incr i;
decr j
done;
!p
end else begin
(* Non-decreasing. *)
incr p;
while !p < last && cmp a.(!p) a.(!p - 1) >= 0 do
incr p
done;
!p
end
in
(*
Compute the Powersort node power of the boundary between
[s1, s1+n1)
[s1+n1, s1+n1+n2)
This is the number of common leading bits of the two run
midpoints, as fractions of the whole array.
We use doubled midpoints so everything remains integral.
*)
let node_power s1 n1 n2 n =
let rec loop result a b =
let result = result + 1 in
if a >= n then
loop result (a - n) (b - n)
else if b >= n then
result
else
loop result (a lsl 1) (b lsl 1)
in
loop 0 (2 * s1 + n1) (2 * s1 + n1 + n1 + n2)
in
(* The stack has logarithmic size in Powersort. *)
let stack = Array.make (max 32 (Sys.int_size * 2))
{ start = 0; len = 0; power = 0 } in
let top = ref 0 in
let ensure_stack () =
if !top = Array.length stack then
invalid_arg "stable_sort_sub: run stack overflow"
in
(* Temporary storage. We grow it to the size of the smaller
run whenever a merge requires it. *)
let tmp = ref [||] in
let ensure_tmp n =
if Array.length !tmp < n then
tmp := Array.make n a.(ofs)
in
(* Merge adjacent sorted runs [lo,mid) and [mid,hi).
Only the smaller run is copied. *)
let merge lo mid hi =
let n1 = mid - lo in
let n2 = hi - mid in
if n1 = 0 || n2 = 0 then
()
else if cmp a.(mid - 1) a.(mid) <= 0 then
(* Already merged. *)
()
else if n1 <= n2 then begin
(* Copy the left run and merge forwards. *)
ensure_tmp n1;
Array.blit a lo !tmp 0 n1;
let i = ref 0 in
let j = ref mid in
let k = ref lo in
while !i < n1 && !j < hi do
if cmp !tmp.(!i) a.(!j) <= 0 then begin
a.(!k) <- !tmp.(!i);
incr i
end else begin
a.(!k) <- a.(!j);
incr j
end;
incr k
done;
while !i < n1 do
a.(!k) <- !tmp.(!i);
incr i;
incr k
done
end else begin
(* Copy the right run and merge backwards.
On equality we take from the right run first, because
we're filling the result from right to left. *)
ensure_tmp n2;
Array.blit a mid !tmp 0 n2;
let i = ref (mid - 1) in
let j = ref (n2 - 1) in
let k = ref (hi - 1) in
while !i >= lo && !j >= 0 do
if cmp a.(!i) !tmp.(!j) > 0 then begin
a.(!k) <- a.(!i);
decr i
end else begin
a.(!k) <- !tmp.(!j);
decr j
end;
decr k
done;
while !j >= 0 do
a.(!k) <- !tmp.(!j);
decr j;
decr k
done
end
in
(* Merge runs at stack positions i and i+1. *)
let merge_at i =
let r1 = stack.(i) in
let r2 = stack.(i + 1) in
merge r1.start (r1.start + r1.len)
(r2.start + r2.len);
r1.len <- r1.len + r2.len;
(* Remove r2 from the stack. *)
for j = i + 1 to !top - 2 do
stack.(j) <- stack.(j + 1)
done;
decr top
in
let mr = minrun len in
let pos = ref ofs in
while !pos < last do
let start = !pos in
let natural_end = find_run start in
let natural_len = natural_end - start in
let run_len =
if natural_len < mr && natural_end < last then begin
let n = min mr (last - start) in
binary_sort start natural_end (start + n);
n
end else
natural_len
in
let run = { start; len = run_len; power = 0 } in
if !top > 0 then begin
let previous = stack.(!top - 1) in
let power =
node_power previous.start previous.len run.len len
in
(* Powers on the stack are strictly increasing.
Close every node whose power is larger than the
boundary we have just discovered. *)
while !top > 1 &&
stack.(!top - 2).power > power do
merge_at (!top - 2)
done;
(* The power belongs to the boundary after the current
top run, so it is stored in the current top entry. *)
stack.(!top - 1).power <- power
end;
ensure_stack ();
stack.(!top) <- run;
incr top;
pos := start + run_len
done;
(* Finish the merge tree. *)
while !top > 1 do
merge_at (!top - 2)
done
end
@END_BEFORE_5_5_0@