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
type ('a, 'b) t = {
mutable r : int;
mutable w : int;
mutable c : int;
k : ('a, 'b) Bigarray.kind;
mutable v : ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t;
}
exception Empty
external ( = ) : 'a -> 'a -> bool = "%equal"
let ( = ) (a : int) b = a = b
let[@inline always] mask t v = v land (t.c - 1)
let[@inline always] empty t = t.r = t.w
let[@inline always] size t = t.w - t.r
let[@inline always] available t = t.c - (t.w - t.r)
let[@inline always] full t = size t = t.c
let length q = size q
let[@inline always] to_power_of_two v =
let res = ref (pred v) in
res := !res lor (!res lsr 1);
res := !res lor (!res lsr 2);
res := !res lor (!res lsr 4);
res := !res lor (!res lsr 8);
res := !res lor (!res lsr 16);
succ !res
let[@inline always] is_power_of_two v = v <> 0 && v land (lnot v + 1) = v
let is_empty t = (empty [@inlined]) t
let create ?capacity kind =
let capacity =
match capacity with
| None | Some 0 -> 1
| Some n ->
if n < 0 then Fmt.invalid_arg "Rke.create" else to_power_of_two n
in
{
r = 0;
w = 0;
c = capacity;
k = kind;
v = Bigarray.Array1.create kind Bigarray.c_layout capacity;
}
let capacity { c; _ } = c
let copy t =
let v = Bigarray.Array1.create t.k Bigarray.c_layout t.c in
Bigarray.Array1.blit t.v v;
{ r = t.r; w = t.w; c = t.c; v; k = t.k }
let grow t want =
let max : int -> int -> int = max in
let c = to_power_of_two (max 1 (max want (size t))) in
if c <> Bigarray.Array1.dim t.v then (
let dst = Bigarray.Array1.create t.k Bigarray.c_layout c in
let sze = (size [@inlined]) t in
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = sze - pre in
(if rst > 0 then (
Bigarray.Array1.(blit (sub t.v msk pre) (sub dst 0 pre));
Bigarray.Array1.(blit (sub t.v 0 rst) (sub dst pre rst)))
else Bigarray.Array1.(blit (sub t.v msk sze) (sub dst 0 sze)));
t.v <- dst;
t.w <- sze;
t.c <- c;
t.r <- 0)
let push t v =
if (full [@inlined]) t then grow t (2 * (size [@inlined]) t);
Bigarray.Array1.unsafe_set t.v ((mask [@inlined]) t t.w) v;
t.w <- t.w + 1
let cons t v =
if (full [@inlined]) t then grow t (2 * (size [@inlined]) t);
let i = t.r - 1 in
Bigarray.Array1.unsafe_set t.v ((mask [@inlined]) t i) v;
t.r <- i
let pop_exn t =
if (empty [@inlined]) t then raise Empty;
let r = Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t t.r) in
t.r <- t.r + 1;
r
let pop t = try Some (pop_exn t) with Empty -> None
let peek_exn t =
if (empty [@inlined]) t then raise Empty;
Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t t.r)
let peek t = try Some (peek_exn t) with Empty -> None
let blit src src_off dst dst_off len =
let a = Bigarray.Array1.sub src src_off len in
let b = Bigarray.Array1.sub dst dst_off len in
Bigarray.Array1.blit a b
let compress t =
let len = length t in
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then (
if (available [@inlined]) t >= pre then (
blit t.v 0 t.v pre rst;
blit t.v msk t.v 0 pre)
else
let tmp = Bigarray.Array1.create t.k Bigarray.c_layout pre in
blit t.v msk tmp 0 pre;
blit t.v 0 t.v pre rst;
blit tmp 0 t.v 0 pre)
else blit t.v msk t.v 0 len;
t.r <- 0;
t.w <- len
module N = struct
type ('a, 'b) bigarray = ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t
type ('a, 'b) blit = 'a -> int -> 'b -> int -> int -> unit
type 'a length = 'a -> int
let push t ~blit ~length ?(off = 0) ?len v =
let len = match len with None -> length v - off | Some len -> len in
if (available [@inlined]) t < len then grow t (len + (size [@inlined]) t);
let msk = (mask [@inlined]) t t.w in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then (
blit v off t.v msk pre;
blit v (off + pre) t.v 0 rst)
else blit v off t.v msk len;
t.w <- t.w + len
let keep_exn t ~blit ~length ?(off = 0) ?len v =
let len = match len with None -> length v - off | Some len -> len in
if (size [@inlined]) t < len then raise Empty;
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then (
blit t.v msk v off pre;
blit t.v 0 v (off + pre) rst)
else blit t.v msk v off len
let keep t ~blit ~length ?off ?len v =
try Some (keep_exn t ~blit ~length ?off ?len v) with Empty -> None
let peek t =
let len = (size [@inlined]) t in
if len == 0 then []
else
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then
[ Bigarray.Array1.sub t.v msk pre; Bigarray.Array1.sub t.v 0 rst ]
else [ Bigarray.Array1.sub t.v msk len ]
let unsafe_shift t len = t.r <- t.r + len
let shift_exn t len =
if (size [@inlined]) t < len then raise Empty;
unsafe_shift t len
let shift t len = try Some (shift_exn t len) with Empty -> None
end
let iter f t =
let idx = ref t.r in
let max = t.w in
while !idx <> max do
f (Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t !idx));
incr idx
done
let rev_iter f t =
if t.r == t.w then ()
else
let idx = ref (pred t.w) in
let min = t.r in
while
f (Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t !idx));
!idx <> min
do
decr idx
done
let fold f a t =
let a = ref a in
iter (fun x -> a := f !a x) t;
!a
let pp ?sep pp_elt = Fmt.iter ?sep iter pp_elt
let dump pp_elt = Fmt.Dump.iter iter (Fmt.any "rke") pp_elt
let clear q =
q.r <- 0;
q.w <- 0
module Weighted = struct
type ('a, 'b) t = {
mutable r : int;
mutable w : int;
c : int;
k : ('a, 'b) Bigarray.kind;
v : ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t;
}
exception Empty
exception Full
let[@inline always] mask t v = v land (t.c - 1)
let[@inline always] empty t = t.r = t.w
let[@inline always] size t = t.w - t.r
let[@inline always] full t = size t = t.c
let[@inline always] available t = t.c - (t.w - t.r)
let is_empty t = (empty [@inlined]) t
let length q = size q
let create ?capacity kind =
let capacity =
match capacity with
| None | Some 0 -> 1
| Some n ->
if n < 0 then Fmt.invalid_arg "Rke.Weighted.create"
else to_power_of_two n
in
( {
r = 0;
w = 0;
c = capacity;
k = kind;
v = Bigarray.Array1.create kind Bigarray.c_layout capacity;
},
capacity )
let copy t =
let v = Bigarray.Array1.create t.k Bigarray.c_layout t.c in
Bigarray.Array1.blit t.v v;
{ r = t.r; w = t.w; c = t.c; v; k = t.k }
let from v =
if not (is_power_of_two (Bigarray.Array1.dim v)) then
Fmt.invalid_arg "RBA.from";
let c = Bigarray.Array1.dim v in
let k = Bigarray.Array1.kind v in
{ r = 0; w = 0; c; k; v }
let push_exn t v =
if (full [@inlined]) t then raise Full;
Bigarray.Array1.unsafe_set t.v ((mask [@inlined]) t t.w) v;
t.w <- t.w + 1
let push t v = try Some (push_exn t v) with Full -> None
let cons_exn t v =
if (full [@inlined]) t then raise Full;
let i = t.r - 1 in
Bigarray.Array1.unsafe_set t.v ((mask [@inlined]) t i) v;
t.r <- i
let cons t v = try Some (cons_exn t v) with Full -> None
let pop_exn t =
if (empty [@inlined]) t then raise Empty;
let r = Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t t.r) in
t.r <- t.r + 1;
r
let pop t = try Some (pop_exn t) with Empty -> None
let peek_exn t =
if (empty [@inlined]) t then raise Empty;
Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t t.r)
let peek t = try Some (peek_exn t) with Empty -> None
let compress t =
let len = length t in
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then (
if (available [@inlined]) t >= pre then (
blit t.v 0 t.v pre rst;
blit t.v msk t.v 0 pre)
else
let tmp = Bigarray.Array1.create t.k Bigarray.c_layout pre in
blit t.v msk tmp 0 pre;
blit t.v 0 t.v pre rst;
blit tmp 0 t.v 0 pre)
else blit t.v msk t.v 0 len;
t.r <- 0;
t.w <- len
module N = struct
type ('a, 'b) bigarray = ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t
type ('a, 'b) blit = 'a -> int -> 'b -> int -> int -> unit
type 'a length = 'a -> int
let push_exn t ~blit ~length ?(off = 0) ?len v =
let len = match len with None -> length v - off | Some len -> len in
if (available [@inlined]) t < len then raise Full;
let msk = (mask [@inlined]) t t.w in
let pre = t.c - msk in
let rst = len - pre in
let ret =
if rst > 0 then (
blit v off t.v msk pre;
blit v (off + pre) t.v 0 rst;
[
Bigarray.Array1.sub t.v ((mask [@inlined]) t t.w) pre;
Bigarray.Array1.sub t.v 0 rst;
])
else (
blit v off t.v msk len;
[ Bigarray.Array1.sub t.v ((mask [@inlined]) t t.w) len ])
in
t.w <- t.w + len;
ret
let push t ~blit ~length ?off ?len v =
try Some (push_exn t ~blit ~length ?off ?len v) with Full -> None
let keep_exn t ~blit ~length ?(off = 0) ?len v =
let len = match len with None -> length v - off | Some len -> len in
if (size [@inlined]) t < len then raise Empty;
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then (
blit t.v msk v off pre;
blit t.v 0 v (off + pre) rst)
else blit t.v msk v off len
let keep t ~blit ~length ?off ?len v =
try Some (keep_exn t ~blit ~length ?off ?len v) with Empty -> None
let peek t =
let len = (size [@inlined]) t in
if len == 0 then []
else
let msk = (mask [@inlined]) t t.r in
let pre = t.c - msk in
let rst = len - pre in
if rst > 0 then
[ Bigarray.Array1.sub t.v msk pre; Bigarray.Array1.sub t.v 0 rst ]
else [ Bigarray.Array1.sub t.v msk len ]
let unsafe_shift t len = t.r <- t.r + len
let shift_exn t len =
if (size [@inlined]) t < len then raise Empty;
unsafe_shift t len
let shift t len = try Some (shift_exn t len) with Empty -> None
end
let iter f t =
let idx = ref t.r in
let max = t.w in
while !idx <> max do
f (Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t !idx));
incr idx
done
let rev_iter f t =
if t.r == t.w then ()
else
let idx = ref (pred t.w) in
let min = t.r in
while
f (Bigarray.Array1.unsafe_get t.v ((mask [@inlined]) t !idx));
!idx <> min
do
decr idx
done
let fold f a t =
let a = ref a in
iter (fun x -> a := f !a x) t;
!a
let pp ?sep pp_elt = Fmt.iter ?sep iter pp_elt
let dump pp_elt = Fmt.Dump.iter iter (Fmt.any "rke:weighted") pp_elt
let clear q =
q.r <- 0;
q.w <- 0
let unsafe_bigarray { v; _ } = v
end