Source file ShareableChunk.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# 1 "ShareableChunk.cppo.ml"
open PublicTypeAbbreviations
open PrivateSignatures
module[@inline] Make
(EChunk : ECHUNK)
= struct
module EChunk = EChunk
module View = EChunk.View
type 'a chunk = 'a EChunk.t
type view = View.view
type 'a t = {
owner : owner;
support : 'a chunk;
mutable view : view;
mutable weight : weight;
# 91 "ShareableChunk.cppo.ml"
}
let debug =
false
let[@inline] log format =
if debug then
Printf.fprintf stderr format
else
Printf.ifprintf stderr format
type 'a measure =
| MUnit : 'a measure
| MSWeight : 'a t measure
let[@inline] apply (type a) (m : a measure) (x : a) : weight =
match m with
| MUnit ->
1
| MSWeight ->
x.weight
let[@inline] iter_segments pov p yield =
View.iter_segments pov (p.support, p.view) yield
let iteri_segments_front schunk yield =
let head = ref 0 in
iter_segments Front schunk (fun ((_a, _i, k) as seg) ->
yield !head seg;
head := !head + k
)
let iter pov f p =
ArrayExtra.iter iter_segments pov f p
let fold_left f seed p =
Adapters.fold_left (iter Front) f seed p
let to_list p =
Adapters.to_list (iter Back) p
let[@inline] is_uniquely_owned p o =
Owner.is_uniquely_owned p.owner o
# 178 "ShareableChunk.cppo.ml"
let[@inline] is_aligned p =
View.is_aligned p.support p.view
let check_except_weight o p =
EChunk.check p.support;
View.check p.support p.view;
assert (if is_uniquely_owned p o then is_aligned p else true)
let check m o p =
check_except_weight o p;
assert (p.weight = fold_left (fun sum x -> sum + apply m x) 0 p)
let[@inline] check m o p =
assert (check m o p; true)
let[@inline] validate_except_weight o p =
assert (check_except_weight o p; true);
p
let[@inline] is_dummy p =
EChunk.is_dummy p.support
let dummy d =
let owner = Owner.none
and support = EChunk.dummy d
and view = View.dummy
and weight = 0 in
{ owner; support; view; weight;
# 239 "ShareableChunk.cppo.ml"
}
let print m print p =
let open PPrint in
let open PPrint.OCaml in
if is_dummy p then
!^ "<dummy>"
else
let model () =
try
flowing_list print (to_list p)
with
| Assert_failure (s, i, j) ->
utf8format "<cannot print: Assert_failure (%S, %d, %d)>" s i j
| Invalid_argument s ->
utf8format "<cannot print: Invalid_argument %S>" s
in
record "schunk" ([
# 269 "ShareableChunk.cppo.ml"
"weight", int p.weight;
"owner", !^ (Owner.show p.owner);
"view", View.print p.view;
"support", View.print (EChunk.view p.support);
] @ (if m = MUnit then [
"model", model();
] else []))
let[@inline] default p =
EChunk.default p.support
let[@inline] length p =
View.size p.view
let[@inline] weight p =
p.weight
let[@inline] data p =
EChunk.data p.support
let[@inline] support p =
p.support
let[@inline] capacity p =
EChunk.capacity p.support
let[@inline] is_empty p =
length p = 0
let[@inline] is_full p =
length p = capacity p
let[@inline] remaining_length pov i p =
match pov with
| Front ->
length p - i
| Back ->
i + 1
let[@inline] segment pov i k p =
assert (0 <= i && i < length p);
assert (0 <= k && k <= remaining_length pov i p);
View.segment pov i k p.support p.view
let[@inline] segment_max pov i p =
assert (0 <= i && i < length p);
let k = remaining_length pov i p in
segment pov i k p
# 336 "ShareableChunk.cppo.ml"
let create d n o =
let owner = o in
let support = EChunk.create d n in
let view = EChunk.view support in
assert (View.size view = 0);
let weight = 0 in
validate_except_weight o
{ owner; support; view; weight;
# 347 "ShareableChunk.cppo.ml"
}
let of_chunk_destructive c o =
let owner = o
and support = c
and view = EChunk.view c
and weight = EChunk.length c in
validate_except_weight o
{ owner; support; view; weight;
# 359 "ShareableChunk.cppo.ml"
}
let[@inline] of_array_segment d n a head size o =
let c = EChunk.of_array_segment d n a head size in
of_chunk_destructive c o
let[@inline] make d n k v o =
let c = EChunk.make d n k v in
of_chunk_destructive c o
let[@inline] init d n k i f o =
let c = EChunk.init d n k i f in
of_chunk_destructive c o
let share p o view weight =
assert (not (is_uniquely_owned p o));
let owner = Owner.none
and support = p.support in
validate_except_weight o
{ owner; support; view; weight;
# 390 "ShareableChunk.cppo.ml"
}
let sub c o view weight =
let support = View.sub c view
and owner = o in
let p =
{ owner; support; view; weight;
# 412 "ShareableChunk.cppo.ml"
}
in
assert (is_aligned p);
validate_except_weight o p
let[@inline] copy p o =
sub p.support o p.view p.weight
let to_chunk p o =
check MUnit o p;
if is_uniquely_owned p o then begin
assert (is_aligned p);
p.support
end
else
View.sub p.support p.view
let[@inline] push_unique pov p x m =
assert (is_aligned p);
assert (not (EChunk.is_full p.support));
EChunk.push pov p.support x;
p.view <- View.push pov p.support p.view;
p.weight <- p.weight + apply m x;
p
let[@inline] push_shared pov p x m o =
let support, owner, shared =
if View.is_flush pov p.support p.view &&
not (EChunk.is_full p.support)
then
p.support, p.owner, true
else
View.sub p.support p.view, o, false
in
assert (View.is_flush pov support p.view);
assert (not (EChunk.is_full support));
EChunk.push pov support x;
let view = View.push pov support p.view
and weight = p.weight + apply m x in
{ owner; support; view; weight;
# 497 "ShareableChunk.cppo.ml"
}
let[@inline] push pov p x m o =
assert (not (is_full p));
if is_uniquely_owned p o then
push_unique pov p x m
else
push_shared pov p x m o
let[@inline] pop_unique pov p m =
assert (is_aligned p);
let x = EChunk.pop pov p.support in
p.view <- View.pop pov p.support p.view;
p.weight <- p.weight - apply m x;
x, p
let[@inline] pop_shared pov p m o =
let x = View.peek pov p.support p.view in
let view = View.pop pov p.support p.view
and weight = p.weight - apply m x in
x, share p o view weight
let[@inline] pop pov p m o =
assert (not (is_empty p));
if is_uniquely_owned p o then
pop_unique pov p m
else
pop_shared pov p m o
let[@inline] peek pov p =
View.peek pov p.support p.view
let[@inline] get p i =
assert (0 <= i && i < length p);
View.get p.support p.view i
let[@inline] set_unique p i x delta =
p.weight <- p.weight + delta;
View.set p.support p.view i x;
p
let[@inline] set_shared p i x delta o =
if delta = 0 && x == get p i then
p
else
set_unique (copy p o) i x delta
let[@inline] set p i x delta o =
assert (0 <= i && i < length p);
if is_uniquely_owned p o then
set_unique p i x delta
else
set_shared p i x delta o
let[@inline] concat_unique pov p1 p2 =
assert (is_aligned p2);
let view = View.copy pov p1.support p1.view p2.support p2.view in
p2.view <- view;
p2.weight <- p1.weight + p2.weight;
p2
let optimize =
true
let concat_shared p1 p2 o =
if optimize &&
View.is_flush Back p1.support p1.view &&
EChunk.length p1.support + length p2 <= capacity p1
then begin
let view = View.copy Back p2.support p2.view p1.support p1.view in
share p1 o view (p1.weight + p2.weight)
end
else if optimize &&
View.is_flush Front p2.support p2.view &&
length p1 + EChunk.length p2.support <= capacity p2
then begin
let view = View.copy Front p1.support p1.view p2.support p2.view in
share p2 o view (p1.weight + p2.weight)
end
else begin
let support = EChunk.create (default p1) (capacity p1) in
let view = EChunk.view support in
assert (View.size view = 0);
let view = View.copy Back p1.support p1.view support view in
let view = View.copy Back p2.support p2.view support view in
assert (view = EChunk.view support);
let owner = o
and weight = p1.weight + p2.weight in
{ owner; support; view; weight;
# 661 "ShareableChunk.cppo.ml"
}
end
let[@inline] concat p1 p2 o =
assert (capacity p1 = capacity p2);
assert (length p1 + length p2 <= capacity p2);
if is_uniquely_owned p1 o then
concat_unique Back p2 p1
else if is_uniquely_owned p2 o then
concat_unique Front p1 p2
else
concat_shared p1 p2 o
let weight_of_prefix p m (i : index) =
assert (0 <= i && i <= length p);
let prefix = View.restrict p.support p.view 0 i in
View.fold_left (fun accu x -> accu + apply m x) 0 (p.support, prefix)
let[@inline] validate p m (w : weight) ((weight, index) as pair) =
assert (0 <= index && index < length p);
assert (weight = weight_of_prefix p m index);
assert (
let x = View.get p.support p.view index in
weight <= w && w < weight + apply m x
);
pair
exception Break
let[@specialise] reach_from (type a)
(m : a measure) (p : a t) (pindex : index) (pwindex : weight) (w : weight)
: weight * index =
assert (0 <= w && w < p.weight);
assert (0 <= pindex && pindex <= length p);
assert (pwindex = weight_of_prefix p m pindex);
match m with
| MUnit ->
w, w
| MSWeight ->
if pwindex <= w then begin
let index, weight = ref pindex, ref pwindex in
let range = View.restrict p.support p.view pindex (length p - pindex) in
begin try
View.iter Front (fun x ->
let next_weight = !weight + x.weight in
if w < next_weight then
raise Break;
weight := next_weight;
index := !index + 1
) (p.support, range);
assert false
with Break ->
assert (!index < length p)
end;
validate p m w (!weight, !index)
end
else begin
assert (w < pwindex);
let range = View.restrict p.support p.view 0 pindex in
let index, weight = ref pindex, ref pwindex in
begin try
View.iter Back (fun x ->
index := !index - 1;
weight := !weight - x.weight ;
if !weight <= w then
raise Break;
) (p.support, range);
assert false
with Break ->
assert (0 <= !index)
end;
validate p m w (!weight, !index)
end
let[@inline] reach (type a) (m : a measure) (p : a t) (w : weight) =
assert (0 <= w && w <= p.weight);
match m with
| MUnit ->
w, w
| MSWeight ->
let pindex, pwindex =
if w < p.weight - w then
0, 0
else
length p, p.weight
in
reach_from m p pindex pwindex w
let[@inline] adjust_weight_index p w m =
let weight, j = reach m p w in
let w = w - weight in
w, j
let[@inline] get_by_weight m p w =
let w, j = adjust_weight_index p w m in
let x = get p j in
w, x
let update_by_weight m o f p w =
let w, j = adjust_weight_index p w m in
let x = get p j in
let x' = f x w in
let delta = 0 in
set p j x' delta o
let[@inline] three_way_split_unique p o view1 weight1 x view2 weight2 =
let p1 = sub p.support o view1 weight1
and p2 = sub p.support o view2 weight2 in
p1, x, p2
let[@inline] three_way_split_shared p o view1 weight1 x view2 weight2 =
let p1 = share p o view1 weight1
and p2 = share p o view2 weight2 in
p1, x, p2
let three_way_split p i m o =
assert (0 <= i && i < weight p);
let weight1, j = reach m p i in
let view1, x, view2 = View.three_way_split p.support p.view j in
assert (View.size view1 = j);
assert (View.size view2 = length p - j - 1);
let weight2 = p.weight - weight1 - apply m x in
if is_uniquely_owned p o then
three_way_split_unique p o view1 weight1 x view2 weight2
else
three_way_split_shared p o view1 weight1 x view2 weight2
let[@inline] take_unique p o view1 weight1 j x =
let p1 = sub p.support o view1 weight1 in
assert (length p1 = j);
assert (is_aligned p1);
p1, x
let[@inline] take_shared p o view1 weight1 j x =
let p1 = share p o view1 weight1 in
assert (length p1 = j);
p1, x
let take p i m o =
assert (0 <= i && i < weight p);
let weight1, j = reach m p i in
let view1, x = View.take p.support p.view j in
if is_uniquely_owned p o then
take_unique p o view1 weight1 j x
else
take_shared p o view1 weight1 j x
let[@inline] drop_unique p o j x view2 weight2 =
let p2 = sub p.support o view2 weight2 in
assert (length p2 = length p - j - 1);
assert (is_aligned p2);
x, p2
let[@inline] drop_shared p o j x view2 weight2 =
let p2 = share p o view2 weight2 in
assert (length p2 = length p - j - 1);
x, p2
let drop p i m o =
assert (0 <= i && i < weight p);
let weight1, j = reach m p i in
let x, view2 = View.drop p.support p.view j in
let weight2 = p.weight - weight1 - apply m x in
if is_uniquely_owned p o then
drop_unique p o j x view2 weight2
else
drop_shared p o j x view2 weight2
end