Source file set.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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
open! Import
include Set_intf
let with_return = With_return.with_return
module Tree0 = struct
type 'a t =
| Empty
| Leaf of 'a
| Node of 'a t * 'a * 'a t * int * int
type 'a tree = 'a t
let height = function
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, h, _) -> h
;;
let length = function
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, _, s) -> s
;;
let invariants =
let in_range lower upper compare_elt v =
(match lower with
| None -> true
| Some lower -> compare_elt lower v < 0)
&&
match upper with
| None -> true
| Some upper -> compare_elt v upper < 0
in
let rec loop lower upper compare_elt t =
match t with
| Empty -> true
| Leaf v -> in_range lower upper compare_elt v
| Node (l, v, r, h, n) ->
let hl = height l
and hr = height r in
abs (hl - hr) <= 2
&& h = max hl hr + 1
&& n = length l + length r + 1
&& in_range lower upper compare_elt v
&& loop lower (Some v) compare_elt l
&& loop (Some v) upper compare_elt r
in
fun t ~compare_elt -> loop None None compare_elt t
;;
let is_empty = function
| Empty -> true
| Leaf _ | Node _ -> false
;;
let create l v r =
let hl =
match l with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, h, _) -> h
in
let hr =
match r with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, h, _) -> h
in
let h = if hl >= hr then hl + 1 else hr + 1 in
if h = 1
then Leaf v
else (
let sl =
match l with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, _, s) -> s
in
let sr =
match r with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, _, s) -> s
in
Node (l, v, r, h, sl + sr + 1))
;;
let of_increasing_iterator_unchecked ~len ~f =
let rec loop n ~f i =
match n with
| 0 -> Empty
| 1 ->
let k = f i in
Leaf k
| 2 ->
let kl = f i in
let k = f (i + 1) in
create (Leaf kl) k Empty
| 3 ->
let kl = f i in
let k = f (i + 1) in
let kr = f (i + 2) in
create (Leaf kl) k (Leaf kr)
| n ->
let left_length = n lsr 1 in
let right_length = n - left_length - 1 in
let left = loop left_length ~f i in
let k = f (i + left_length) in
let right = loop right_length ~f (i + left_length + 1) in
create left k right
in
loop len ~f 0
;;
let of_sorted_array_unchecked array ~compare_elt =
let array_length = Array.length array in
let next =
if array_length < 2 || compare_elt array.(0) array.(1) < 0
then fun i -> array.(i)
else fun i -> array.(array_length - 1 - i)
in
of_increasing_iterator_unchecked ~len:array_length ~f:next
;;
let of_sorted_array array ~compare_elt =
match array with
| [||] | [| _ |] -> Result.Ok (of_sorted_array_unchecked array ~compare_elt)
| _ ->
with_return (fun r ->
let increasing =
match compare_elt array.(0) array.(1) with
| 0 -> r.return (Or_error.error_string "of_sorted_array: duplicated elements")
| i -> i < 0
in
for i = 1 to Array.length array - 2 do
match compare_elt array.(i) array.(i + 1) with
| 0 -> r.return (Or_error.error_string "of_sorted_array: duplicated elements")
| i ->
if Poly.( <> ) (i < 0) increasing
then
r.return (Or_error.error_string "of_sorted_array: elements are not ordered")
done;
Result.Ok (of_sorted_array_unchecked array ~compare_elt))
;;
let bal l v r =
let hl =
match l with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, h, _) -> h
in
let hr =
match r with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, h, _) -> h
in
if hl > hr + 2
then (
match l with
| Empty -> assert false
| Leaf _ -> assert false
| Node (ll, lv, lr, _, _) ->
if height ll >= height lr
then create ll lv (create lr v r)
else (
match lr with
| Empty -> assert false
| Leaf lrv ->
assert (is_empty ll);
create (create ll lv Empty) lrv (create Empty v r)
| Node (lrl, lrv, lrr, _, _) -> create (create ll lv lrl) lrv (create lrr v r)))
else if hr > hl + 2
then (
match r with
| Empty -> assert false
| Leaf _ -> assert false
| Node (rl, rv, rr, _, _) ->
if height rr >= height rl
then create (create l v rl) rv rr
else (
match rl with
| Empty -> assert false
| Leaf rlv ->
assert (is_empty rr);
create (create l v Empty) rlv (create Empty rv rr)
| Node (rll, rlv, rlr, _, _) -> create (create l v rll) rlv (create rlr rv rr)))
else (
let h = if hl >= hr then hl + 1 else hr + 1 in
let sl =
match l with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, _, s) -> s
in
let sr =
match r with
| Empty -> 0
| Leaf _ -> 1
| Node (_, _, _, _, s) -> s
in
if h = 1 then Leaf v else Node (l, v, r, h, sl + sr + 1))
;;
exception Same
let add t x ~compare_elt =
let rec aux = function
| Empty -> Leaf x
| Leaf v ->
let c = compare_elt x v in
if c = 0
then Exn.raise_without_backtrace Same
else if c < 0
then create (Leaf x) v Empty
else create Empty v (Leaf x)
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0
then Exn.raise_without_backtrace Same
else if c < 0
then bal (aux l) v r
else bal l v (aux r)
in
try aux t with
| Same -> t
;;
let rec add_min x t =
match t with
| Empty -> Leaf x
| Leaf _ -> Node (Empty, x, t, 2, 2)
| Node (l, v, r, _, _) -> bal (add_min x l) v r
;;
let rec add_max t x =
match t with
| Empty -> Leaf x
| Leaf _ -> Node (t, x, Empty, 2, 2)
| Node (l, v, r, _, _) -> bal l v (add_max r x)
;;
let rec join l v r =
match l, r with
| Empty, _ -> add_min v r
| _, Empty -> add_max l v
| Leaf lv, _ -> add_min lv (add_min v r)
| _, Leaf rv -> add_max (add_max l v) rv
| Node (ll, lv, lr, lh, _), Node (rl, rv, rr, rh, _) ->
if lh > rh + 2
then bal ll lv (join lr v r)
else if rh > lh + 2
then bal (join l v rl) rv rr
else create l v r
;;
let rec min_elt = function
| Empty -> None
| Leaf v | Node (Empty, v, _, _, _) -> Some v
| Node (l, _, _, _, _) -> min_elt l
;;
exception Set_min_elt_exn_of_empty_set [@@deriving_inline sexp]
let () =
Sexplib0.Sexp_conv.Exn_converter.add
[%extension_constructor Set_min_elt_exn_of_empty_set]
(function
| Set_min_elt_exn_of_empty_set ->
Sexplib0.Sexp.Atom "set.ml.Tree0.Set_min_elt_exn_of_empty_set"
| _ -> assert false)
;;
[@@@end]
exception Set_max_elt_exn_of_empty_set [@@deriving_inline sexp]
let () =
Sexplib0.Sexp_conv.Exn_converter.add
[%extension_constructor Set_max_elt_exn_of_empty_set]
(function
| Set_max_elt_exn_of_empty_set ->
Sexplib0.Sexp.Atom "set.ml.Tree0.Set_max_elt_exn_of_empty_set"
| _ -> assert false)
;;
[@@@end]
let min_elt_exn t =
match min_elt t with
| None -> raise Set_min_elt_exn_of_empty_set
| Some v -> v
;;
let fold_until t ~init ~f ~finish =
let rec fold_until_helper ~f t acc =
match t with
| Empty -> Container.Continue_or_stop.Continue acc
| Leaf value -> f acc value [@nontail]
| Node (left, value, right, _, _) ->
(match fold_until_helper ~f left acc with
| Stop _a as x -> x
| Continue acc ->
(match f acc value with
| Stop _a as x -> x
| Continue a -> fold_until_helper ~f right a))
in
match fold_until_helper ~f t init with
| Continue x -> finish x [@nontail]
| Stop x -> x
;;
let rec max_elt = function
| Empty -> None
| Leaf v | Node (_, v, Empty, _, _) -> Some v
| Node (_, _, r, _, _) -> max_elt r
;;
let max_elt_exn t =
match max_elt t with
| None -> raise Set_max_elt_exn_of_empty_set
| Some v -> v
;;
let rec remove_min_elt = function
| Empty -> invalid_arg "Set.remove_min_elt"
| Leaf _ -> Empty
| Node (Empty, _, r, _, _) -> r
| Node (l, v, r, _, _) -> bal (remove_min_elt l) v r
;;
let merge t1 t2 =
match t1, t2 with
| Empty, t -> t
| t, Empty -> t
| _, _ -> bal t1 (min_elt_exn t2) (remove_min_elt t2)
;;
let concat t1 t2 =
match t1, t2 with
| Empty, t | t, Empty -> t
| _, _ -> join t1 (min_elt_exn t2) (remove_min_elt t2)
;;
let split t x ~compare_elt =
let rec split t =
match t with
| Empty -> Empty, None, Empty
| Leaf v ->
let c = compare_elt x v in
if c = 0
then Empty, Some v, Empty
else if c < 0
then Empty, None, Leaf v
else Leaf v, None, Empty
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0
then l, Some v, r
else if c < 0
then (
let ll, maybe_elt, rl = split l in
ll, maybe_elt, join rl v r)
else (
let lr, maybe_elt, rr = split r in
join l v lr, maybe_elt, rr)
in
split t
;;
let rec split_le_gt t x ~compare_elt =
match t with
| Empty -> Empty, Empty
| Leaf v -> if compare_elt x v >= 0 then Leaf v, Empty else Empty, Leaf v
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0
then add_max l v, r
else if c < 0
then (
let ll, rl = split_le_gt l x ~compare_elt in
ll, join rl v r)
else (
let lr, rr = split_le_gt r x ~compare_elt in
join l v lr, rr)
;;
let rec split_lt_ge t x ~compare_elt =
match t with
| Empty -> Empty, Empty
| Leaf v -> if compare_elt x v > 0 then Leaf v, Empty else Empty, Leaf v
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0
then l, add_min v r
else if c < 0
then (
let ll, rl = split_lt_ge l x ~compare_elt in
ll, join rl v r)
else (
let lr, rr = split_lt_ge r x ~compare_elt in
join l v lr, rr)
;;
let empty = Empty
let rec mem t x ~compare_elt =
match t with
| Empty -> false
| Leaf v ->
let c = compare_elt x v in
c = 0
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
c = 0 || mem (if c < 0 then l else r) x ~compare_elt
;;
let singleton x = Leaf x
let remove t x ~compare_elt =
let rec aux t =
match t with
| Empty -> Exn.raise_without_backtrace Same
| Leaf v -> if compare_elt x v = 0 then Empty else Exn.raise_without_backtrace Same
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0 then merge l r else if c < 0 then bal (aux l) v r else bal l v (aux r)
in
try aux t with
| Same -> t
;;
let remove_index t i ~compare_elt:_ =
let rec aux t i =
match t with
| Empty -> Exn.raise_without_backtrace Same
| Leaf _ -> if i = 0 then Empty else Exn.raise_without_backtrace Same
| Node (l, v, r, _, _) ->
let l_size = length l in
let c = Poly.compare i l_size in
if c = 0
then merge l r
else if c < 0
then bal (aux l i) v r
else bal l v (aux r (i - l_size - 1))
in
try aux t i with
| Same -> t
;;
let union s1 s2 ~compare_elt =
let rec union s1 s2 =
if phys_equal s1 s2
then s1
else (
match s1, s2 with
| Empty, t | t, Empty -> t
| Leaf v1, _ -> union (Node (Empty, v1, Empty, 1, 1)) s2
| _, Leaf v2 -> union s1 (Node (Empty, v2, Empty, 1, 1))
| Node (l1, v1, r1, h1, _), Node (l2, v2, r2, h2, _) ->
if h1 >= h2
then
if h2 = 1
then add s1 v2 ~compare_elt
else (
let l2, _, r2 = split s2 v1 ~compare_elt in
join (union l1 l2) v1 (union r1 r2))
else if h1 = 1
then add s2 v1 ~compare_elt
else (
let l1, _, r1 = split s1 v2 ~compare_elt in
join (union l1 l2) v2 (union r1 r2)))
in
union s1 s2
;;
let union_list ~comparator ~to_tree xs =
let compare_elt = comparator.Comparator.compare in
List.fold xs ~init:empty ~f:(fun ac x -> union ac (to_tree x) ~compare_elt)
;;
let inter s1 s2 ~compare_elt =
let rec inter s1 s2 =
if phys_equal s1 s2
then s1
else (
match s1, s2 with
| Empty, _ | _, Empty -> Empty
| (Leaf elt as singleton), other_set | other_set, (Leaf elt as singleton) ->
if mem other_set elt ~compare_elt then singleton else Empty
| Node (l1, v1, r1, _, _), t2 ->
(match split t2 v1 ~compare_elt with
| l2, None, r2 -> concat (inter l1 l2) (inter r1 r2)
| l2, Some v1, r2 -> join (inter l1 l2) v1 (inter r1 r2)))
in
inter s1 s2
;;
let diff s1 s2 ~compare_elt =
let rec diff s1 s2 =
if phys_equal s1 s2
then Empty
else (
match s1, s2 with
| Empty, _ -> Empty
| t1, Empty -> t1
| Leaf v1, t2 -> diff (Node (Empty, v1, Empty, 1, 1)) t2
| Node (l1, v1, r1, _, _), t2 ->
(match split t2 v1 ~compare_elt with
| l2, None, r2 -> join (diff l1 l2) v1 (diff r1 r2)
| l2, Some _, r2 -> concat (diff l1 l2) (diff r1 r2)))
in
diff s1 s2
;;
module Enum = struct
type increasing
type decreasing
type ('a, 'direction) t =
| End
| More of 'a * 'a tree * ('a, 'direction) t
let rec cons s (e : (_, increasing) t) : (_, increasing) t =
match s with
| Empty -> e
| Leaf v -> More (v, Empty, e)
| Node (l, v, r, _, _) -> cons l (More (v, r, e))
;;
let rec cons_right s (e : (_, decreasing) t) : (_, decreasing) t =
match s with
| Empty -> e
| Leaf v -> More (v, Empty, e)
| Node (l, v, r, _, _) -> cons_right r (More (v, l, e))
;;
let of_set s : (_, increasing) t = cons s End
let of_set_right s : (_, decreasing) t = cons_right s End
let starting_at_increasing t key compare : (_, increasing) t =
let rec loop t e =
match t with
| Empty -> e
| Leaf v -> loop (Node (Empty, v, Empty, 1, 1)) e
| Node (_, v, r, _, _) when compare v key < 0 -> loop r e
| Node (l, v, r, _, _) -> loop l (More (v, r, e))
in
loop t End
;;
let starting_at_decreasing t key compare : (_, decreasing) t =
let rec loop t e =
match t with
| Empty -> e
| Leaf v -> loop (Node (Empty, v, Empty, 1, 1)) e
| Node (l, v, _, _, _) when compare v key > 0 -> loop l e
| Node (l, v, r, _, _) -> loop r (More (v, l, e))
in
loop t End
;;
let compare compare_elt e1 e2 =
let rec loop e1 e2 =
match e1, e2 with
| End, End -> 0
| End, _ -> -1
| _, End -> 1
| More (v1, r1, e1), More (v2, r2, e2) ->
let c = compare_elt v1 v2 in
if c <> 0
then c
else if phys_equal r1 r2
then loop e1 e2
else loop (cons r1 e1) (cons r2 e2)
in
loop e1 e2
;;
let rec iter ~f = function
| End -> ()
| More (a, tree, enum) ->
f a;
iter (cons tree enum) ~f
;;
let iter2 compare_elt t1 t2 ~f =
let rec loop t1 t2 =
match t1, t2 with
| End, End -> ()
| End, _ -> iter t2 ~f:(fun a -> f (`Right a)) [@nontail]
| _, End -> iter t1 ~f:(fun a -> f (`Left a)) [@nontail]
| More (a1, tree1, enum1), More (a2, tree2, enum2) ->
let compare_result = compare_elt a1 a2 in
if compare_result = 0
then (
f (`Both (a1, a2));
loop (cons tree1 enum1) (cons tree2 enum2))
else if compare_result < 0
then (
f (`Left a1);
loop (cons tree1 enum1) t2)
else (
f (`Right a2);
loop t1 (cons tree2 enum2))
in
loop t1 t2 [@nontail]
;;
let symmetric_diff t1 t2 ~compare_elt =
let step state : ((_, _) Either.t, _) Sequence.Step.t =
match state with
| End, End -> Done
| End, More (elt, tree, enum) ->
Yield { value = Second elt; state = End, cons tree enum }
| More (elt, tree, enum), End ->
Yield { value = First elt; state = cons tree enum, End }
| (More (a1, tree1, enum1) as left), (More (a2, tree2, enum2) as right) ->
let compare_result = compare_elt a1 a2 in
if compare_result = 0
then (
let next_state =
if phys_equal tree1 tree2
then enum1, enum2
else cons tree1 enum1, cons tree2 enum2
in
Skip { state = next_state })
else if compare_result < 0
then Yield { value = First a1; state = cons tree1 enum1, right }
else Yield { value = Second a2; state = left, cons tree2 enum2 }
in
Sequence.unfold_step ~init:(of_set t1, of_set t2) ~f:step
;;
end
let to_sequence_increasing comparator ~from_elt t =
let next enum =
match enum with
| Enum.End -> Sequence.Step.Done
| Enum.More (k, t, e) -> Sequence.Step.Yield { value = k; state = Enum.cons t e }
in
let init =
match from_elt with
| None -> Enum.of_set t
| Some key -> Enum.starting_at_increasing t key comparator.Comparator.compare
in
Sequence.unfold_step ~init ~f:next
;;
let to_sequence_decreasing comparator ~from_elt t =
let next enum =
match enum with
| Enum.End -> Sequence.Step.Done
| Enum.More (k, t, e) ->
Sequence.Step.Yield { value = k; state = Enum.cons_right t e }
in
let init =
match from_elt with
| None -> Enum.of_set_right t
| Some key -> Enum.starting_at_decreasing t key comparator.Comparator.compare
in
Sequence.unfold_step ~init ~f:next
;;
let to_sequence
comparator
?(order = `Increasing)
?greater_or_equal_to
?less_or_equal_to
t
=
let inclusive_bound side t bound =
let compare_elt = comparator.Comparator.compare in
let l, maybe, r = split t bound ~compare_elt in
let t = side (l, r) in
match maybe with
| None -> t
| Some elt -> add t elt ~compare_elt
in
match order with
| `Increasing ->
let t = Option.fold less_or_equal_to ~init:t ~f:(inclusive_bound fst) in
to_sequence_increasing comparator ~from_elt:greater_or_equal_to t
| `Decreasing ->
let t = Option.fold greater_or_equal_to ~init:t ~f:(inclusive_bound snd) in
to_sequence_decreasing comparator ~from_elt:less_or_equal_to t
;;
let rec find_first_satisfying t ~f =
match t with
| Empty -> None
| Leaf v -> if f v then Some v else None
| Node (l, v, r, _, _) ->
if f v
then (
match find_first_satisfying l ~f with
| None -> Some v
| Some _ as x -> x)
else find_first_satisfying r ~f
;;
let rec find_last_satisfying t ~f =
match t with
| Empty -> None
| Leaf v -> if f v then Some v else None
| Node (l, v, r, _, _) ->
if f v
then (
match find_last_satisfying r ~f with
| None -> Some v
| Some _ as x -> x)
else find_last_satisfying l ~f
;;
let binary_search t ~compare how v =
match how with
| `Last_strictly_less_than ->
find_last_satisfying t ~f:(fun x -> compare x v < 0) [@nontail]
| `Last_less_than_or_equal_to ->
find_last_satisfying t ~f:(fun x -> compare x v <= 0) [@nontail]
| `First_equal_to ->
(match find_first_satisfying t ~f:(fun x -> compare x v >= 0) with
| Some x as elt when compare x v = 0 -> elt
| None | Some _ -> None)
| `Last_equal_to ->
(match find_last_satisfying t ~f:(fun x -> compare x v <= 0) with
| Some x as elt when compare x v = 0 -> elt
| None | Some _ -> None)
| `First_greater_than_or_equal_to ->
find_first_satisfying t ~f:(fun x -> compare x v >= 0) [@nontail]
| `First_strictly_greater_than ->
find_first_satisfying t ~f:(fun x -> compare x v > 0) [@nontail]
;;
let binary_search_segmented t ~segment_of how =
let is_left x =
match segment_of x with
| `Left -> true
| `Right -> false
in
let is_right x = not (is_left x) in
match how with
| `Last_on_left -> find_last_satisfying t ~f:is_left [@nontail]
| `First_on_right -> find_first_satisfying t ~f:is_right [@nontail]
;;
let merge_to_sequence
comparator
?(order = `Increasing)
?greater_or_equal_to
?less_or_equal_to
t
t'
=
Sequence.merge_with_duplicates
(to_sequence comparator ~order ?greater_or_equal_to ?less_or_equal_to t)
(to_sequence comparator ~order ?greater_or_equal_to ?less_or_equal_to t')
~compare:
(match order with
| `Increasing -> comparator.compare
| `Decreasing -> Fn.flip comparator.compare)
;;
let compare compare_elt s1 s2 =
Enum.compare compare_elt (Enum.of_set s1) (Enum.of_set s2)
;;
let iter2 s1 s2 ~compare_elt ~f =
Enum.iter2 compare_elt (Enum.of_set s1) (Enum.of_set s2) ~f
;;
let equal s1 s2 ~compare_elt = compare compare_elt s1 s2 = 0
let is_subset s1 ~of_:s2 ~compare_elt =
let rec is_subset s1 ~of_:s2 =
match s1, s2 with
| Empty, _ -> true
| _, Empty -> false
| Leaf v1, t2 -> mem t2 v1 ~compare_elt
| Node (l1, v1, r1, _, _), Leaf v2 ->
(match l1, r1 with
| Empty, Empty ->
compare_elt v1 v2 = 0
| _, _ -> false)
| Node (l1, v1, r1, _, _), (Node (l2, v2, r2, _, _) as t2) ->
let c = compare_elt v1 v2 in
if c = 0
then
phys_equal s1 s2 || (is_subset l1 ~of_:l2 && is_subset r1 ~of_:r2)
else if c < 0
then is_subset (Node (l1, v1, Empty, 0, 0)) ~of_:l2 && is_subset r1 ~of_:t2
else is_subset (Node (Empty, v1, r1, 0, 0)) ~of_:r2 && is_subset l1 ~of_:t2
in
is_subset s1 ~of_:s2
;;
let rec are_disjoint s1 s2 ~compare_elt =
match s1, s2 with
| Empty, _ | _, Empty -> true
| Leaf elt, other_set | other_set, Leaf elt -> not (mem other_set elt ~compare_elt)
| Node (l1, v1, r1, _, _), t2 ->
if phys_equal s1 s2
then false
else (
match split t2 v1 ~compare_elt with
| l2, None, r2 ->
are_disjoint l1 l2 ~compare_elt && are_disjoint r1 r2 ~compare_elt
| _, Some _, _ -> false)
;;
let iter t ~f =
let rec iter = function
| Empty -> ()
| Leaf v -> f v
| Node (l, v, r, _, _) ->
iter l;
f v;
iter r
in
iter t [@nontail]
;;
let symmetric_diff = Enum.symmetric_diff
let rec fold s ~init:accu ~f =
match s with
| Empty -> accu
| Leaf v -> f accu v
| Node (l, v, r, _, _) -> fold ~f r ~init:(f (fold ~f l ~init:accu) v)
;;
let hash_fold_t_ignoring_structure hash_fold_elem state t =
fold t ~init:(hash_fold_int state (length t)) ~f:hash_fold_elem
;;
let count t ~f = Container.count ~fold t ~f
let sum m t ~f = Container.sum ~fold m t ~f
let rec fold_right s ~init:accu ~f =
match s with
| Empty -> accu
| Leaf v -> f v accu
| Node (l, v, r, _, _) -> fold_right ~f l ~init:(f v (fold_right ~f r ~init:accu))
;;
let rec for_all t ~f:p =
match t with
| Empty -> true
| Leaf v -> p v
| Node (l, v, r, _, _) -> p v && for_all ~f:p l && for_all ~f:p r
;;
let rec exists t ~f:p =
match t with
| Empty -> false
| Leaf v -> p v
| Node (l, v, r, _, _) -> p v || exists ~f:p l || exists ~f:p r
;;
let filter s ~f:p =
let rec filt = function
| Empty -> Empty
| Leaf v as t -> if p v then t else Empty
| Node (l, v, r, _, _) as t ->
let l' = filt l in
let keep_v = p v in
let r' = filt r in
if keep_v && phys_equal l l' && phys_equal r r'
then t
else if keep_v
then join l' v r'
else concat l' r'
in
filt s [@nontail]
;;
let filter_map s ~f:p ~compare_elt =
let rec filt accu = function
| Empty -> accu
| Leaf v ->
(match p v with
| None -> accu
| Some v -> add accu v ~compare_elt)
| Node (l, v, r, _, _) ->
filt
(filt
(match p v with
| None -> accu
| Some v -> add accu v ~compare_elt)
l)
r
in
filt Empty s [@nontail]
;;
let partition_tf s ~f:p =
let rec loop = function
| Empty -> Empty, Empty
| Leaf v as t -> if p v then t, Empty else Empty, t
| Node (l, v, r, _, _) as t ->
let l't, l'f = loop l in
let keep_v_t = p v in
let r't, r'f = loop r in
let mk keep_v l' r' =
if keep_v && phys_equal l l' && phys_equal r r'
then t
else if keep_v
then join l' v r'
else concat l' r'
in
mk keep_v_t l't r't, mk (not keep_v_t) l'f r'f
in
loop s [@nontail]
;;
let rec elements_aux accu = function
| Empty -> accu
| Leaf v -> v :: accu
| Node (l, v, r, _, _) -> elements_aux (v :: elements_aux accu r) l
;;
let elements s = elements_aux [] s
let choose t =
match t with
| Empty -> None
| Leaf v -> Some v
| Node (_, v, _, _, _) -> Some v
;;
let choose_exn =
let not_found = Not_found_s (Atom "Set.choose_exn: empty set") in
let choose_exn t =
match choose t with
| None -> raise not_found
| Some v -> v
in
choose_exn
;;
let of_list lst ~compare_elt =
List.fold lst ~init:empty ~f:(fun t x -> add t x ~compare_elt)
;;
let of_sequence sequence ~compare_elt =
Sequence.fold sequence ~init:empty ~f:(fun t x -> add t x ~compare_elt)
;;
let to_list s = elements s
let of_array a ~compare_elt =
Array.fold a ~init:empty ~f:(fun t x -> add t x ~compare_elt)
;;
let to_array = function
| Empty -> [||]
| Leaf v -> [| v |]
| Node (l, v, r, _, s) ->
let res = Array.create ~len:s v in
let pos_ref = ref 0 in
let rec loop = function
| Empty -> ()
| Leaf v ->
res.(!pos_ref) <- v;
incr pos_ref
| Node (l, v, r, _, _) ->
loop l;
res.(!pos_ref) <- v;
incr pos_ref;
loop r
in
loop l;
incr pos_ref;
loop r;
res
;;
let map t ~f ~compare_elt =
fold t ~init:empty ~f:(fun t x -> add t (f x) ~compare_elt) [@nontail]
;;
let group_by set ~equiv =
let rec loop set equiv_classes =
if is_empty set
then equiv_classes
else (
let x = choose_exn set in
let equiv_x, not_equiv_x =
partition_tf set ~f:(fun elt -> phys_equal x elt || equiv x elt)
in
loop not_equiv_x (equiv_x :: equiv_classes))
in
loop set [] [@nontail]
;;
let rec find t ~f =
match t with
| Empty -> None
| Leaf v -> if f v then Some v else None
| Node (l, v, r, _, _) ->
if f v
then Some v
else (
match find l ~f with
| None -> find r ~f
| Some _ as r -> r)
;;
let rec find_map t ~f =
match t with
| Empty -> None
| Leaf v -> f v
| Node (l, v, r, _, _) ->
(match f v with
| Some _ as r -> r
| None ->
(match find_map l ~f with
| None -> find_map r ~f
| Some _ as r -> r))
;;
let find_exn t ~f =
match find t ~f with
| None -> failwith "Set.find_exn failed to find a matching element"
| Some e -> e
;;
let rec nth t i =
match t with
| Empty -> None
| Leaf v -> if i = 0 then Some v else None
| Node (l, v, r, _, s) ->
if i >= s
then None
else (
let l_size = length l in
let c = Poly.compare i l_size in
if c < 0 then nth l i else if c = 0 then Some v else nth r (i - l_size - 1))
;;
let stable_dedup_list xs ~compare_elt =
let rec loop xs leftovers already_seen =
match xs with
| [] -> List.rev leftovers
| hd :: tl ->
if mem already_seen hd ~compare_elt
then loop tl leftovers already_seen
else loop tl (hd :: leftovers) (add already_seen hd ~compare_elt)
in
loop xs [] empty
;;
let t_of_sexp_direct a_of_sexp sexp ~compare_elt =
match sexp with
| Sexp.List lst ->
let elt_lst = List.map lst ~f:a_of_sexp in
let set = of_list elt_lst ~compare_elt in
if length set = List.length lst
then set
else (
let set = ref empty in
List.iter2_exn lst elt_lst ~f:(fun el_sexp el ->
if mem !set el ~compare_elt
then of_sexp_error "Set.t_of_sexp: duplicate element in set" el_sexp
else set := add !set el ~compare_elt);
assert false)
| sexp -> of_sexp_error "Set.t_of_sexp: list needed" sexp
;;
let sexp_of_t sexp_of_a t =
Sexp.List (fold_right t ~init:[] ~f:(fun el acc -> sexp_of_a el :: acc))
;;
module Named = struct
let is_subset
(subset : _ Named.t)
~of_:(superset : _ Named.t)
~sexp_of_elt
~compare_elt
=
let invalid_elements = diff subset.set superset.set ~compare_elt in
if is_empty invalid_elements
then Ok ()
else (
let invalid_elements_sexp = sexp_of_t sexp_of_elt invalid_elements in
Or_error.error_s
(Sexp.message
(subset.name ^ " is not a subset of " ^ superset.name)
[ "invalid_elements", invalid_elements_sexp ]))
;;
let equal s1 s2 ~sexp_of_elt ~compare_elt =
Or_error.combine_errors_unit
[ is_subset s1 ~of_:s2 ~sexp_of_elt ~compare_elt
; is_subset s2 ~of_:s1 ~sexp_of_elt ~compare_elt
]
;;
end
end
type ('a, 'comparator) t =
{
comparator : ('a, 'comparator) Comparator.t
; tree : 'a Tree0.t
}
type ('a, 'comparator) tree = 'a Tree0.t
let like { tree = _; comparator } tree = { tree; comparator }
let like_maybe_no_op ({ tree = old_tree; comparator } as old_t) tree =
if phys_equal old_tree tree then old_t else { tree; comparator }
;;
let compare_elt t = t.comparator.Comparator.compare
module Accessors = struct
let comparator t = t.comparator
let comparator_s (type k cmp) t : (k, cmp) Comparator.Module.t =
(module struct
type t = k
type comparator_witness = cmp
let comparator = t.comparator
end)
;;
let invariants t = Tree0.invariants t.tree ~compare_elt:(compare_elt t)
let length t = Tree0.length t.tree
let is_empty t = Tree0.is_empty t.tree
let elements t = Tree0.elements t.tree
let min_elt t = Tree0.min_elt t.tree
let min_elt_exn t = Tree0.min_elt_exn t.tree
let max_elt t = Tree0.max_elt t.tree
let max_elt_exn t = Tree0.max_elt_exn t.tree
let choose t = Tree0.choose t.tree
let choose_exn t = Tree0.choose_exn t.tree
let to_list t = Tree0.to_list t.tree
let to_array t = Tree0.to_array t.tree
let fold t ~init ~f = Tree0.fold t.tree ~init ~f
let fold_until t ~init ~f ~finish = Tree0.fold_until t.tree ~init ~f ~finish
let fold_right t ~init ~f = Tree0.fold_right t.tree ~init ~f
let fold_result t ~init ~f = Container.fold_result ~fold ~init ~f t
let iter t ~f = Tree0.iter t.tree ~f
let iter2 a b ~f = Tree0.iter2 a.tree b.tree ~f ~compare_elt:(compare_elt a)
let exists t ~f = Tree0.exists t.tree ~f
let for_all t ~f = Tree0.for_all t.tree ~f
let count t ~f = Tree0.count t.tree ~f
let sum m t ~f = Tree0.sum m t.tree ~f
let find t ~f = Tree0.find t.tree ~f
let find_exn t ~f = Tree0.find_exn t.tree ~f
let find_map t ~f = Tree0.find_map t.tree ~f
let mem t a = Tree0.mem t.tree a ~compare_elt:(compare_elt t)
let filter t ~f = like_maybe_no_op t (Tree0.filter t.tree ~f)
let add t a = like t (Tree0.add t.tree a ~compare_elt:(compare_elt t))
let remove t a = like t (Tree0.remove t.tree a ~compare_elt:(compare_elt t))
let union t1 t2 = like t1 (Tree0.union t1.tree t2.tree ~compare_elt:(compare_elt t1))
let inter t1 t2 = like t1 (Tree0.inter t1.tree t2.tree ~compare_elt:(compare_elt t1))
let diff t1 t2 = like t1 (Tree0.diff t1.tree t2.tree ~compare_elt:(compare_elt t1))
let symmetric_diff t1 t2 =
Tree0.symmetric_diff t1.tree t2.tree ~compare_elt:(compare_elt t1)
;;
let compare_direct t1 t2 = Tree0.compare (compare_elt t1) t1.tree t2.tree
let equal t1 t2 = Tree0.equal t1.tree t2.tree ~compare_elt:(compare_elt t1)
let is_subset t ~of_ = Tree0.is_subset t.tree ~of_:of_.tree ~compare_elt:(compare_elt t)
let are_disjoint t1 t2 =
Tree0.are_disjoint t1.tree t2.tree ~compare_elt:(compare_elt t1)
;;
module Named = struct
let to_named_tree (named : (_, _) t Named.t) = { named with set = named.set.tree }
let is_subset subset ~of_:superset =
Tree0.Named.is_subset
(to_named_tree subset)
~of_:(to_named_tree superset)
~compare_elt:(compare_elt subset.set)
~sexp_of_elt:subset.set.comparator.sexp_of_t
;;
let equal t1 t2 =
Or_error.combine_errors_unit [ is_subset t1 ~of_:t2; is_subset t2 ~of_:t1 ]
;;
include Named
end
let partition_tf t ~f =
let tree_t, tree_f = Tree0.partition_tf t.tree ~f in
like_maybe_no_op t tree_t, like_maybe_no_op t tree_f
;;
let split t a =
let tree1, b, tree2 = Tree0.split t.tree a ~compare_elt:(compare_elt t) in
like t tree1, b, like t tree2
;;
let split_le_gt t a =
let tree1, tree2 = Tree0.split_le_gt t.tree a ~compare_elt:(compare_elt t) in
like t tree1, like t tree2
;;
let split_lt_ge t a =
let tree1, tree2 = Tree0.split_lt_ge t.tree a ~compare_elt:(compare_elt t) in
like t tree1, like t tree2
;;
let group_by t ~equiv = List.map (Tree0.group_by t.tree ~equiv) ~f:(like t)
let nth t i = Tree0.nth t.tree i
let remove_index t i = like t (Tree0.remove_index t.tree i ~compare_elt:(compare_elt t))
let sexp_of_t sexp_of_a _ t = Tree0.sexp_of_t sexp_of_a t.tree
let to_sequence ?order ?greater_or_equal_to ?less_or_equal_to t =
Tree0.to_sequence t.comparator ?order ?greater_or_equal_to ?less_or_equal_to t.tree
;;
let binary_search t ~compare how v = Tree0.binary_search t.tree ~compare how v
let binary_search_segmented t ~segment_of how =
Tree0.binary_search_segmented t.tree ~segment_of how
;;
let merge_to_sequence ?order ?greater_or_equal_to ?less_or_equal_to t t' =
Tree0.merge_to_sequence
t.comparator
?order
?greater_or_equal_to
?less_or_equal_to
t.tree
t'.tree
;;
let hash_fold_direct hash_fold_key state t =
Tree0.hash_fold_t_ignoring_structure hash_fold_key state t.tree
;;
end
include Accessors
let compare _ _ t1 t2 = compare_direct t1 t2
module Tree = struct
type ('a, 'comparator) t = ('a, 'comparator) tree
let ce comparator = comparator.Comparator.compare
let t_of_sexp_direct ~comparator a_of_sexp sexp =
Tree0.t_of_sexp_direct ~compare_elt:(ce comparator) a_of_sexp sexp
;;
let empty_without_value_restriction = Tree0.empty
let empty ~comparator:_ = empty_without_value_restriction
let singleton ~comparator:_ e = Tree0.singleton e
let length t = Tree0.length t
let invariants ~comparator t = Tree0.invariants t ~compare_elt:(ce comparator)
let is_empty t = Tree0.is_empty t
let elements t = Tree0.elements t
let min_elt t = Tree0.min_elt t
let min_elt_exn t = Tree0.min_elt_exn t
let max_elt t = Tree0.max_elt t
let max_elt_exn t = Tree0.max_elt_exn t
let choose t = Tree0.choose t
let choose_exn t = Tree0.choose_exn t
let to_list t = Tree0.to_list t
let to_array t = Tree0.to_array t
let iter t ~f = Tree0.iter t ~f
let exists t ~f = Tree0.exists t ~f
let for_all t ~f = Tree0.for_all t ~f
let count t ~f = Tree0.count t ~f
let sum m t ~f = Tree0.sum m t ~f
let find t ~f = Tree0.find t ~f
let find_exn t ~f = Tree0.find_exn t ~f
let find_map t ~f = Tree0.find_map t ~f
let fold t ~init ~f = Tree0.fold t ~init ~f
let fold_until t ~init ~f ~finish = Tree0.fold_until t ~init ~f ~finish
let fold_right t ~init ~f = Tree0.fold_right t ~init ~f
let map ~comparator t ~f = Tree0.map t ~f ~compare_elt:(ce comparator)
let filter t ~f = Tree0.filter t ~f
let filter_map ~comparator t ~f = Tree0.filter_map t ~f ~compare_elt:(ce comparator)
let partition_tf t ~f = Tree0.partition_tf t ~f
let iter2 ~comparator a b ~f = Tree0.iter2 a b ~f ~compare_elt:(ce comparator)
let mem ~comparator t a = Tree0.mem t a ~compare_elt:(ce comparator)
let add ~comparator t a = Tree0.add t a ~compare_elt:(ce comparator)
let remove ~comparator t a = Tree0.remove t a ~compare_elt:(ce comparator)
let union ~comparator t1 t2 = Tree0.union t1 t2 ~compare_elt:(ce comparator)
let inter ~comparator t1 t2 = Tree0.inter t1 t2 ~compare_elt:(ce comparator)
let diff ~comparator t1 t2 = Tree0.diff t1 t2 ~compare_elt:(ce comparator)
let symmetric_diff ~comparator t1 t2 =
Tree0.symmetric_diff t1 t2 ~compare_elt:(ce comparator)
;;
let compare_direct ~comparator t1 t2 = Tree0.compare (ce comparator) t1 t2
let equal ~comparator t1 t2 = Tree0.equal t1 t2 ~compare_elt:(ce comparator)
let is_subset ~comparator t ~of_ = Tree0.is_subset t ~of_ ~compare_elt:(ce comparator)
let are_disjoint ~comparator t1 t2 =
Tree0.are_disjoint t1 t2 ~compare_elt:(ce comparator)
;;
let of_list ~comparator l = Tree0.of_list l ~compare_elt:(ce comparator)
let of_sequence ~comparator s = Tree0.of_sequence s ~compare_elt:(ce comparator)
let of_array ~comparator a = Tree0.of_array a ~compare_elt:(ce comparator)
let of_sorted_array_unchecked ~comparator a =
Tree0.of_sorted_array_unchecked a ~compare_elt:(ce comparator)
;;
let of_increasing_iterator_unchecked ~comparator:_ ~len ~f =
Tree0.of_increasing_iterator_unchecked ~len ~f
;;
let of_sorted_array ~comparator a = Tree0.of_sorted_array a ~compare_elt:(ce comparator)
let union_list ~comparator l = Tree0.union_list l ~to_tree:Fn.id ~comparator
let stable_dedup_list ~comparator xs =
Tree0.stable_dedup_list xs ~compare_elt:(ce comparator)
;;
let group_by t ~equiv = Tree0.group_by t ~equiv
let split ~comparator t a = Tree0.split t a ~compare_elt:(ce comparator)
let split_le_gt ~comparator t a = Tree0.split_le_gt t a ~compare_elt:(ce comparator)
let split_lt_ge ~comparator t a = Tree0.split_lt_ge t a ~compare_elt:(ce comparator)
let nth t i = Tree0.nth t i
let remove_index ~comparator t i = Tree0.remove_index t i ~compare_elt:(ce comparator)
let sexp_of_t sexp_of_a _ t = Tree0.sexp_of_t sexp_of_a t
let to_tree t = t
let of_tree ~comparator:_ t = t
let to_sequence ~comparator ?order ?greater_or_equal_to ?less_or_equal_to t =
Tree0.to_sequence comparator ?order ?greater_or_equal_to ?less_or_equal_to t
;;
let binary_search ~comparator:_ t ~compare how v = Tree0.binary_search t ~compare how v
let binary_search_segmented ~comparator:_ t ~segment_of how =
Tree0.binary_search_segmented t ~segment_of how
;;
let merge_to_sequence ~comparator ?order ?greater_or_equal_to ?less_or_equal_to t t' =
Tree0.merge_to_sequence comparator ?order ?greater_or_equal_to ?less_or_equal_to t t'
;;
let fold_result t ~init ~f = Container.fold_result ~fold ~init ~f t
module Named = struct
include Tree0.Named
let is_subset ~comparator t1 ~of_:t2 =
Tree0.Named.is_subset
t1
~of_:t2
~compare_elt:(ce comparator)
~sexp_of_elt:comparator.Comparator.sexp_of_t
;;
let equal ~comparator t1 t2 =
Tree0.Named.equal
t1
t2
~compare_elt:(ce comparator)
~sexp_of_elt:comparator.Comparator.sexp_of_t
;;
end
end
module Using_comparator = struct
type nonrec ('elt, 'cmp) t = ('elt, 'cmp) t
include Accessors
let to_tree t = t.tree
let of_tree ~comparator tree = { comparator; tree }
let t_of_sexp_direct ~comparator a_of_sexp sexp =
of_tree
~comparator
(Tree0.t_of_sexp_direct ~compare_elt:comparator.compare a_of_sexp sexp)
;;
let empty ~comparator = { comparator; tree = Tree0.empty }
module Empty_without_value_restriction (Elt : Comparator.S1) = struct
let empty = { comparator = Elt.comparator; tree = Tree0.empty }
end
let singleton ~comparator e = { comparator; tree = Tree0.singleton e }
let union_list ~comparator l =
of_tree ~comparator (Tree0.union_list ~comparator ~to_tree l)
;;
let of_sorted_array_unchecked ~comparator array =
let tree =
Tree0.of_sorted_array_unchecked array ~compare_elt:comparator.Comparator.compare
in
{ comparator; tree }
;;
let of_increasing_iterator_unchecked ~comparator ~len ~f =
of_tree ~comparator (Tree0.of_increasing_iterator_unchecked ~len ~f)
;;
let of_sorted_array ~comparator array =
Or_error.Monad_infix.(
Tree0.of_sorted_array array ~compare_elt:comparator.Comparator.compare
>>| fun tree -> { comparator; tree })
;;
let of_list ~comparator l =
{ comparator; tree = Tree0.of_list l ~compare_elt:comparator.Comparator.compare }
;;
let of_sequence ~comparator s =
{ comparator; tree = Tree0.of_sequence s ~compare_elt:comparator.Comparator.compare }
;;
let of_array ~comparator a =
{ comparator; tree = Tree0.of_array a ~compare_elt:comparator.Comparator.compare }
;;
let stable_dedup_list ~comparator xs =
Tree0.stable_dedup_list xs ~compare_elt:comparator.Comparator.compare
;;
let map ~comparator t ~f =
{ comparator; tree = Tree0.map t.tree ~f ~compare_elt:comparator.Comparator.compare }
;;
let filter_map ~comparator t ~f =
{ comparator
; tree = Tree0.filter_map t.tree ~f ~compare_elt:comparator.Comparator.compare
}
;;
module Tree = Tree
end
type ('elt, 'cmp) comparator =
(module Comparator.S with type t = 'elt and type comparator_witness = 'cmp)
let to_comparator (type elt cmp) ((module M) : (elt, cmp) comparator) = M.comparator
let empty m = Using_comparator.empty ~comparator:(to_comparator m)
let singleton m a = Using_comparator.singleton ~comparator:(to_comparator m) a
let union_list m a = Using_comparator.union_list ~comparator:(to_comparator m) a
let of_sorted_array_unchecked m a =
Using_comparator.of_sorted_array_unchecked ~comparator:(to_comparator m) a
;;
let of_increasing_iterator_unchecked m ~len ~f =
Using_comparator.of_increasing_iterator_unchecked ~comparator:(to_comparator m) ~len ~f
;;
let of_sorted_array m a = Using_comparator.of_sorted_array ~comparator:(to_comparator m) a
let of_list m a = Using_comparator.of_list ~comparator:(to_comparator m) a
let of_sequence m a = Using_comparator.of_sequence ~comparator:(to_comparator m) a
let of_array m a = Using_comparator.of_array ~comparator:(to_comparator m) a
let stable_dedup_list m a =
Using_comparator.stable_dedup_list ~comparator:(to_comparator m) a
;;
let map m a ~f = Using_comparator.map ~comparator:(to_comparator m) a ~f
let filter_map m a ~f = Using_comparator.filter_map ~comparator:(to_comparator m) a ~f
let to_tree = Using_comparator.to_tree
let of_tree m t = Using_comparator.of_tree ~comparator:(to_comparator m) t
module M (Elt : sig
type t
type comparator_witness
end) =
struct
type nonrec t = (Elt.t, Elt.comparator_witness) t
end
module type Sexp_of_m = sig
type t [@@deriving_inline sexp_of]
val sexp_of_t : t -> Sexplib0.Sexp.t
[@@@end]
end
module type M_of_sexp = sig
type t [@@deriving_inline of_sexp]
val t_of_sexp : Sexplib0.Sexp.t -> t
[@@@end]
include Comparator.S with type t := t
end
module type M_sexp_grammar = sig
type t [@@deriving_inline sexp_grammar]
val t_sexp_grammar : t Sexplib0.Sexp_grammar.t
[@@@end]
end
module type Compare_m = sig end
module type Equal_m = sig end
module type Hash_fold_m = Hasher.S
let sexp_of_m__t (type elt) (module Elt : Sexp_of_m with type t = elt) t =
sexp_of_t Elt.sexp_of_t (fun _ -> Sexp.Atom "_") t
;;
let m__t_of_sexp
(type elt cmp)
(module Elt : M_of_sexp with type t = elt and type comparator_witness = cmp)
sexp
=
Using_comparator.t_of_sexp_direct ~comparator:Elt.comparator Elt.t_of_sexp sexp
;;
let m__t_sexp_grammar (type elt) (module Elt : M_sexp_grammar with type t = elt)
: (elt, _) t Sexplib0.Sexp_grammar.t
=
Sexplib0.Sexp_grammar.coerce (list_sexp_grammar Elt.t_sexp_grammar)
;;
let compare_m__t (module _ : Compare_m) t1 t2 = compare_direct t1 t2
let equal_m__t (module _ : Equal_m) t1 t2 = equal t1 t2
let hash_fold_m__t (type elt) (module Elt : Hash_fold_m with type t = elt) state =
hash_fold_direct Elt.hash_fold_t state
;;
let hash_m__t folder t =
let state = hash_fold_m__t folder (Hash.create ()) t in
Hash.get_hash_value state
;;
module Poly = struct
type comparator_witness = Comparator.Poly.comparator_witness
type nonrec ('elt, 'cmp) set = ('elt, comparator_witness) t
type nonrec 'elt t = ('elt, comparator_witness) t
type nonrec 'elt tree = ('elt, comparator_witness) tree
include Accessors
let comparator = Comparator.Poly.comparator
include Using_comparator.Empty_without_value_restriction (Comparator.Poly)
let singleton a = Using_comparator.singleton ~comparator a
let union_list a = Using_comparator.union_list ~comparator a
let of_sorted_array_unchecked a =
Using_comparator.of_sorted_array_unchecked ~comparator a
;;
let of_increasing_iterator_unchecked ~len ~f =
Using_comparator.of_increasing_iterator_unchecked ~comparator ~len ~f
;;
let of_sorted_array a = Using_comparator.of_sorted_array ~comparator a
let of_list a = Using_comparator.of_list ~comparator a
let of_sequence a = Using_comparator.of_sequence ~comparator a
let of_array a = Using_comparator.of_array ~comparator a
let stable_dedup_list a = Using_comparator.stable_dedup_list ~comparator a
let map a ~f = Using_comparator.map ~comparator a ~f
let filter_map a ~f = Using_comparator.filter_map ~comparator a ~f
let of_tree tree = { comparator; tree }
let to_tree t = t.tree
end