Source file ShareableSequence.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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
open PublicTypeAbbreviations
open PublicSettings
open PrivateSignatures
module[@inline] Make
(SChunk : SCHUNK)
(C : CAPACITY)
= struct
open C
type 'a schunk =
'a SChunk.t
type 'a measure = 'a SChunk.measure =
| MUnit : 'a measure
| MSWeight : 'a schunk measure
let apply =
SChunk.apply
type 'a t =
| Zero of { default : 'a; }
| One of { default : 'a; x : 'a }
| Short of { default : 'a; a : 'a array }
| Level of {
weight : weight;
front : 'a schunk;
middle : 'a schunk t;
back : 'a schunk;
}
let[@inline] default s =
match s with
| Zero { default; _ }
| One { default; _ }
| Short { default; _ } ->
default
| Level { front; _ } ->
SChunk.default front
let[@inline] dummy s =
match s with
| Zero { default; _ }
| One { default; _ }
| Short { default; _ } ->
SChunk.dummy default
| Level { middle; _ } ->
default middle
let[@inline] weight_of s =
match s with
| Zero _ ->
0
| One _ ->
1
| Short { a; _ } ->
Array.length a
| Level { weight; _ } ->
weight
let[@inline] is_empty s =
match s with
| Zero _ ->
true
| One _
| Short _
| Level _ ->
false
let create default =
Zero { default }
let[@inline] create_middle default =
let default = SChunk.dummy default in
create default
let[@specialise] nonempty_level pov weight this middle that =
assert (weight > 0);
let front, back = exchange pov this that in
Level { weight; front; middle; back }
let[@inline] level pov weight this middle that =
if weight = 0 then
let default = SChunk.default this in
create default
else
nonempty_level pov weight this middle that
let promote p o =
let default = SChunk.default p in
let weight = SChunk.weight p in
let front = SChunk.create default (SChunk.capacity p) o in
let middle = create_middle default in
let back = p
in
level Front weight front middle back
let[@specialise] rec iter_segments
: 'a. pov -> 'a t -> 'a segments
= fun pov s yield ->
match s with
| Zero _ ->
()
| One { x; _ } ->
yield ([|x|], 0, 1)
| Short { a; _ } ->
yield (a, 0, Array.length a)
| Level { front; middle; back; _ } ->
let this, that = exchange pov front back in
SChunk.iter_segments pov this yield;
iter pov (fun p -> SChunk.iter_segments pov p yield) middle;
SChunk.iter_segments pov that yield
and[@specialise] iter
: 'a. pov -> ('a -> unit) -> 'a t -> unit
= fun pov f s ->
ArrayExtra.iter iter_segments pov f s
let iter_left g s =
iter Front g s
let iter_right g s =
iter Back g s
let fold_left f seed s =
Adapters.fold_left iter_left f seed s
let to_list s =
Adapters.to_list iter_right s
let to_array s =
match s with
| Zero _ ->
[||]
| One { x; _ } ->
[|x|]
| Short { a; _ } ->
Array.copy a
| Level _ ->
ArrayExtra.concat_segments Front (default s) (weight_of s)
(iter_segments Front s)
module Printing = struct
open PPrint
open PPrint.OCaml
let rec print : 'a. 'a measure -> ('a -> document) -> 'a t -> document =
fun m element s ->
match s with
| Zero _ ->
!^ "Zero"
| One { x; _ } ->
!^ "One" ^^ record "pwseq" [
"model", flowing_list element [x]
]
| Short { a; _ } ->
!^ "Short" ^^ record "pwseq" [
"model", flowing_list element (Array.to_list a)
]
| Level { front; middle; back; weight } ->
let schunk = SChunk.print m element in
!^ "Level " ^^ record "pwseq" ([
"weight", int weight;
"front", schunk front;
"middle", print MSWeight schunk middle;
"back", schunk back;
] @ (if m = MUnit then [
"model", flowing_list element (to_list s);
] else []))
end
include Printing
let rec check : 'a. 'a t -> 'a measure -> owner -> depth -> unit =
fun s m o depth ->
match s with
| Zero _ ->
()
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
assert (weight > 0);
if SChunk.is_empty front || SChunk.is_empty back then
assert (is_empty middle);
SChunk.check m o front;
SChunk.check m o back;
check_middle middle m o depth;
assert (
weight =
SChunk.weight front +
fold_left (fun sum p -> sum + SChunk.weight p) 0 middle +
SChunk.weight back
)
and check_middle : 'a. 'a schunk t -> 'a measure -> owner -> depth -> unit =
fun middle m o depth ->
check middle MSWeight o (depth + 1);
iter Front (SChunk.check m o) middle;
let n = capacity depth in
let previous_length = ref n in
iter Front (fun p ->
assert (!previous_length + SChunk.length p > n);
previous_length := SChunk.length p
) middle
let[@inline] check s m o depth =
assert (check s m o depth; true)
let[@specialise] peek pov s =
match s with
| Zero _ ->
raise Empty
| One { x; _ } ->
x
| Short { a; _ } ->
assert (2 <= Array.length a);
begin match pov with
| Front ->
Array.get a 0
| Back ->
let n = Array.length a in
Array.get a (n-1)
end
| Level { front; back; _ } ->
let this, that = exchange pov front back
and front, back = (), () in front; back;
if not (SChunk.is_empty this) then
SChunk.peek pov this
else
SChunk.peek pov that
let[@specialise] rec push
: 'a. pov -> 'a t -> 'a -> 'a measure -> owner -> depth -> 'a t
= fun pov s x m o depth ->
match s with
| Zero { default } ->
let p = SChunk.create default (capacity depth) o in
let p = SChunk.push pov p x m o in
promote p o
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
let this, that = exchange pov front back
and front, back = (), () in front; back;
let weight = weight + apply m x in
if not (SChunk.is_full this) then begin
let this = SChunk.push pov this x m o in
nonempty_level pov weight this middle that
end
else if SChunk.is_empty that then begin
assert (is_empty middle);
let this, that = SChunk.push pov that x m o, this in
nonempty_level pov weight this middle that
end
else begin
let p = SChunk.create (SChunk.default this) (capacity depth) o in
let this, middle =
SChunk.push pov p x m o,
push pov middle this MSWeight o (depth + 1)
in
nonempty_level pov weight this middle that
end
let[@specialise] rec pop : 'a. pov -> 'a t -> 'a measure -> owner -> 'a * 'a t =
fun pov s m o ->
match s with
| Zero _ ->
raise Empty
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
let this, that = exchange pov front back
and front, back = (), () in front; back;
if SChunk.is_empty this then begin
assert (is_empty middle);
assert (not (SChunk.is_empty that));
let x, that = SChunk.pop pov that m o in
let weight = weight - apply m x in
x, level pov weight this middle that
end
else begin
let x, this = SChunk.pop pov this m o in
let weight = weight - apply m x in
if weight = 0 then
let default = SChunk.default this in
x, create default
else
let this, middle =
if SChunk.is_empty this && not (is_empty middle)
then pop pov middle MSWeight o
else this, middle
in
x, nonempty_level pov weight this middle that
end
let[@inline] populate pov s o =
match s with
| Zero _ ->
s
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
let this, that = exchange pov front back
and front, back = (), () in front; back;
if SChunk.is_empty this && not (is_empty middle) then begin
let this, middle = pop pov middle MSWeight o in
assert (not (SChunk.is_empty this));
nonempty_level pov weight this middle that
end
else
s
let populate_both_nonempty_level weight front middle back o =
assert (weight > 0);
let s = Level { weight; front; middle; back } in
let s = populate Front s o in
let s = populate Back s o in
s
let[@specialise] populate_nonempty_level pov weight front middle back o =
assert (weight > 0);
let s = Level { weight; front; middle; back } in
let s = populate pov s o in
s
let[@inline] populate_level pov weight front middle back o =
if weight = 0 then
let default = SChunk.default front in
create default
else
populate_nonempty_level pov weight front middle back o
let create_by_segments default size o create_schunk =
let depth = 0 in
if size = 0 then
create default
else begin
let n = capacity depth in
let weight = size in
let[@inline] create_schunk (i, k) = create_schunk n i k in
let front, foreach_middle_segment, back = ArrayExtra.cut n n size in
let front = create_schunk front in
let middle = ref (create_middle default) in
foreach_middle_segment (fun i k ->
middle := push Back !middle (create_schunk (i, k)) MSWeight o (depth + 1)
);
let middle = !middle in
let back = create_schunk back in
Level { weight; front; middle; back }
end
let of_array_segment default a head size o =
assert (Segment.is_valid (a, head, size));
create_by_segments default size o (fun n i k ->
SChunk.of_array_segment default n a (head + i) k o
)
let make default size v o =
assert (0 <= size);
assert (o = Owner.none);
let depth = 0 in
let n = capacity depth in
let p = SChunk.make default n n v o in
create_by_segments default size o (fun n _i k ->
assert (n = SChunk.capacity p);
if k = n then
p
else
let p1, _x = SChunk.take p k MUnit o in
p1
)
let init default size f o =
assert (0 <= size);
create_by_segments default size o (fun n i k ->
SChunk.init default n k i f o
)
let[@inline] fuse_back middle p o depth =
if SChunk.is_empty p then
middle
else if is_empty middle then
push Back middle p MSWeight o (depth + 1)
else
let n = SChunk.capacity p in
if SChunk.length (peek Back middle) + SChunk.length p > n then
push Back middle p MSWeight o (depth + 1)
else begin
let last, middle = pop Back middle MSWeight o in
let last = SChunk.concat last p o in
push Back middle last MSWeight o (depth + 1)
end
let[@inline] eject this middle that =
if SChunk.is_empty this then begin
assert (is_empty middle);
that, this
end
else
this, that
let rec concat : 'a. 'a t -> 'a t -> owner -> depth -> 'a t =
fun s1 s2 o depth ->
match s1, s2 with
| Zero _, s
| s, Zero _ ->
s
| (One _ | Short _), _
| _, (One _ | Short _) ->
assert false
| Level { weight = weight1; front = front1; middle = middle1;
back = back1 },
Level { weight = weight2; front = front2; middle = middle2;
back = back2 } ->
let weight = weight1 + weight2 in
let front1, back1 = eject front1 middle1 back1 in
assert (not (SChunk.is_empty front1));
let back2, front2 = eject back2 middle2 front2 in
assert (not (SChunk.is_empty back2));
let middle1 = fuse_back middle1 back1 o depth in
let middle1 = fuse_back middle1 front2 o depth in
let middle = fuse middle1 middle2 o depth in
populate_both_nonempty_level weight front1 middle back2 o
and fuse : 'a. 'a schunk t -> 'a schunk t -> owner -> depth -> 'a schunk t =
fun s1 s2 o depth ->
if is_empty s1 then
s2
else if is_empty s2 then
s1
else
let last1 = peek Back s1
and first2 = peek Front s2 in
let n = SChunk.capacity last1 in
assert (n = SChunk.capacity first2);
if SChunk.length last1 + SChunk.length first2 > n then
concat s1 s2 o (depth + 1)
else
let first2, s2 = pop Front s2 MSWeight o in
let s1 = fuse_back s1 first2 o depth in
concat s1 s2 o (depth + 1)
let rec three_way_split :
'a. 'a t -> weight -> 'a measure -> owner -> 'a t * 'a * 'a t
=
fun s i m o ->
match s with
| Zero _ ->
assert false
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
assert (0 <= i && i < weight);
let weight_front = SChunk.weight front in
if i < weight_front then begin
let front1, x, front2 = SChunk.three_way_split front i m o in
let weight1 = SChunk.weight front1 in
let s1 = promote front1 o in
let weight2 = weight - weight1 - apply m x in
let s2 = populate_level Front weight2 front2 middle back o in
s1, x, s2
end
else
let i = i - weight_front in
let weight_middle = weight_of middle in
if weight_middle <= i then begin
let i = i - weight_middle in
let back1, x, back2 = SChunk.three_way_split back i m o in
let weight2 = SChunk.weight back2 in
let s2 = promote back2 o in
let weight1 = weight - weight2 - apply m x in
let s1 = populate_level Back weight1 front middle back1 o in
s1, x, s2
end
else begin
let middle1, p, middle2 = three_way_split middle i MSWeight o in
let weight_middle1 = weight_of middle1 in
let i = i - weight_middle1 in
let p1, x, p2 = SChunk.three_way_split p i m o in
let weight1 =
SChunk.weight front + weight_middle1 + SChunk.weight p1 in
let s1 = populate_nonempty_level Back weight1 front middle1 p1 o in
let weight2 = weight - weight1 - apply m x in
let s2 = populate_nonempty_level Front weight2 p2 middle2 back o in
s1, x, s2
end
let[@inline] three_way_split s i m o =
let result = three_way_split s i m o in
assert (
let s1, x, _s2 = result in
weight_of s1 <= i && i < weight_of s1 + apply m x
);
result
let rec take :
'a. 'a t -> weight -> 'a measure -> owner -> 'a t * 'a
=
fun s i m o ->
match s with
| Zero _ ->
assert false
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
assert (0 <= i && i < weight);
let weight_front = SChunk.weight front in
if i < weight_front then begin
let front1, x = SChunk.take front i m o in
let s1 = promote front1 o in
s1, x
end
else
let i = i - weight_front in
let weight_middle = weight_of middle in
if weight_middle <= i then begin
let i = i - weight_middle in
let back1, x = SChunk.take back i m o in
let weight1 =
SChunk.weight front + weight_of middle + SChunk.weight back1 in
let s1 = populate_level Back weight1 front middle back1 o in
s1, x
end
else begin
let middle1, p = take middle i MSWeight o in
let weight_middle1 = weight_of middle1 in
let i = i - weight_middle1 in
let p1, x = SChunk.take p i m o in
let weight1 =
SChunk.weight front + weight_middle1 + SChunk.weight p1 in
let s1 = populate_nonempty_level Back weight1 front middle1 p1 o in
s1, x
end
let rec drop :
'a. 'a t -> weight -> 'a measure -> owner -> 'a * 'a t
=
fun s i m o ->
match s with
| Zero _ ->
assert false
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
assert (0 <= i && i < weight);
let weight_front = SChunk.weight front in
if i < weight_front then begin
let x, front2 = SChunk.drop front i m o in
let weight2 =
SChunk.weight front2 + weight_of middle + SChunk.weight back in
let s2 = populate_level Front weight2 front2 middle back o in
x, s2
end
else
let i = i - weight_front in
let weight_middle = weight_of middle in
if weight_middle <= i then begin
let i = i - weight_middle in
let x, back2 = SChunk.drop back i m o in
let s2 = promote back2 o in
x, s2
end
else begin
let p, middle2 = drop middle i MSWeight o in
let weight_of_middle1 =
weight_of middle - SChunk.weight p - weight_of middle2 in
let i = i - weight_of_middle1 in
let x, p2 = SChunk.drop p i m o in
let weight2 =
SChunk.weight p2 + weight_of middle2 + SChunk.weight back in
let s2 = populate_nonempty_level Front weight2 p2 middle2 back o in
x, s2
end
let rec get : 'a. 'a t -> weight -> 'a measure -> weight * 'a =
fun s i m ->
match s with
| Zero _
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
assert (0 <= i && i < weight);
let weight_front = SChunk.weight front in
if i < weight_front then
SChunk.get_by_weight m front i
else
let i = i - weight_front in
let weight_middle = weight_of middle in
if weight_middle <= i then
let i = i - weight_middle in
SChunk.get_by_weight m back i
else
let i, p = get middle i MSWeight in
SChunk.get_by_weight m p i
let rec update : 'a. 'a measure -> owner -> 'a update -> 'a t update =
fun m o f s i ->
match s with
| Zero _ ->
assert false
| One _
| Short _ ->
assert false
| Level { weight; front; middle; back } ->
assert (0 <= i && i < weight);
let weight_front = SChunk.weight front in
if i < weight_front then
let front' = SChunk.update_by_weight m o f front i in
if front == front' then s else
let front = front' in
Level { weight; front; middle; back }
else
let i = i - weight_front in
let weight_middle = weight_of middle in
if weight_middle <= i then
let i = i - weight_middle in
let back' = SChunk.update_by_weight m o f back i in
if back == back' then s else
let back = back' in
Level { weight; front; middle; back }
else
let middle' =
update MSWeight o (
SChunk.update_by_weight m o f
) middle i
in
if middle == middle' then s else
let middle = middle' in
Level { weight; front; middle; back }
let[@inline] set s i m o x' =
let f x i =
assert (apply m x = apply m x');
assert (0 <= i && i < apply m x);
x'
in
update m o f s i
let weight =
weight_of
end