Source file substring_heavy_hitters.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
open! Core
open Memtrace_viewer_common
module type Char = sig
include Hashable.S_plain
include Comparable.S_plain with type t := t
include Sexpable.S with type t := t
val dummy : t
end
module Node_id = struct
include Identifier.Make ()
let dummy = first_special
end
module Make (X : Char) = struct
module Node : sig
module Id : Identifier.S
type t
val id : t -> Id.t
val label : t -> X.t array
module Root : sig
type node = t
type t
val create : unit -> t
val node : t -> node
val is_node : t -> node -> bool
end
val add_leaf : root:Root.t -> parent:t -> array:X.t array -> index:int -> key:X.t -> t
val split_edge : root:Root.t -> parent:t -> child:t -> len:int -> t
val set_suffix : root:Root.t -> t -> suffix:t -> unit
val add_to_count : root:Root.t -> depth:int -> count:int -> t -> unit
type find_result =
| Found of t
| Added of t
val find_or_add_leaf
: root:Root.t
-> parent:t
-> array:X.t array
-> index:int
-> find_result
val get_child : root:Root.t -> t -> X.t -> t
val get_child_opt : root:Root.t -> t -> X.t -> t option
val edge_array : t -> X.t array
val edge_start : t -> int
val edge_length : t -> int
val edge_key : t -> X.t
val edge_char : t -> int -> X.t
val has_suffix : t -> bool
val suffix : t -> t
val parent : t -> t
val compress : root:Root.t -> threshold:int -> unit
val has_summary : t -> bool
val reset_summary : t -> unit
val update_parent_totals : t -> root:Root.t -> unit
val update_delta_from_parents : t -> root:Root.t -> unit
val update_parent_heavy_totals : t -> root:Root.t -> heaviness_threshold:int -> unit
val finalize_summary : t -> heaviness_threshold:int -> unit
val iter_children : t -> root:Root.t -> f:(t -> unit) -> unit
val fold_children : t -> root:Root.t -> init:'a -> f:(t -> 'a -> 'a) -> 'a
val is_heavy : t -> heaviness_threshold:int -> bool
val contains_heavy : t -> heaviness_threshold:int -> bool
val count : t -> int
val total_count : t -> int
val light_count : t -> int
val representative : t -> t
module Debug : sig
type nonrec t = t [@@deriving sexp_of]
end
module Debug_full : sig
type nonrec t = t [@@deriving sexp_of]
end
end = struct
module Id = Node_id
type t =
{ id : Id.t
; mutable edge_array : X.t array
; mutable edge_start : int
; mutable edge_len : int
; mutable edge_key : X.t
; mutable parent : t
; mutable suffix_link : t
; mutable next_sibling : t
; mutable first_child : t
; mutable refcount : int
; mutable summary : summary
; mutable queue_item : queue_item
; mutable count : int
; mutable max_edge_squashed : int
; mutable max_child_squashed : int
; mutable delta : int
}
and queue_item =
{ node : t
; mutable next : queue_item
; mutable previous : queue_item
}
and summary =
| No_summary
| Summary of
{ mutable descendents_count : int
; mutable heavy_descendents_count : int
; mutable heavy_descendents : Id.Set.t
; mutable representative : t
}
let id t = t.id
let same t1 t2 = phys_equal t1 t2
let is_removable refcount = refcount = 0
let is_mergable refcount = refcount = 1
let is_mergable_or_removable refcount = refcount <= 1
let dummy_array = [||]
let rec dummy_queue_item =
{ node = dummy; next = dummy_queue_item; previous = dummy_queue_item }
and dummy =
let id = Id.dummy in
let edge_array = dummy_array in
let edge_start = 0 in
let edge_len = 0 in
let edge_key = X.dummy in
let refcount = 0 in
let count = 0 in
let delta = 0 in
let max_child_squashed = 0 in
let max_edge_squashed = 0 in
let summary = No_summary in
{ id
; edge_array
; edge_start
; edge_len
; edge_key
; parent = dummy
; suffix_link = dummy
; next_sibling = dummy
; first_child = dummy
; refcount
; summary
; queue_item = dummy_queue_item
; count
; delta
; max_edge_squashed
; max_child_squashed
}
;;
let is_dummy t = same t dummy
let is_real t = not (is_dummy t)
module Queue = struct
module Item = struct
type nonrec t = queue_item
let root = { node = dummy; next = dummy_queue_item; previous = dummy_queue_item }
let dummy = dummy_queue_item
let same t1 t2 = phys_equal t1 t2
let is_dummy t = same t dummy
let is_real t = not (is_dummy t)
let next item = item.next
let set_next item ~next = item.next <- next
let set_previous item ~previous = if is_real item then item.previous <- previous
let fresh ~node ~previous ~next = { node; previous; next }
end
type t =
{ mutable fronts : Item.t Array.t
; mutable max : int
}
let create () =
let fronts = [||] in
let max = -1 in
{ fronts; max }
;;
let enlarge t =
let fronts = t.fronts in
let old_length = Array.length fronts in
let new_length = (old_length * 2) + 1 in
let new_fronts = Array.create ~len:new_length dummy_queue_item in
Array.blit ~src:fronts ~src_pos:0 ~dst:new_fronts ~dst_pos:0 ~len:old_length;
t.fronts <- new_fronts
;;
let fresh_front_sentinel () =
let node = dummy in
let previous = dummy_queue_item in
let next = dummy_queue_item in
Item.fresh ~node ~previous ~next
;;
let add t ~depth ~node =
if depth > t.max
then (
while depth >= Array.length t.fronts do
enlarge t
done;
t.max <- depth);
let front = t.fronts.(depth) in
let item =
if Item.is_real front
then (
let previous = front in
let next = Item.next front in
let item = Item.fresh ~node ~next ~previous in
Item.set_previous next ~previous:item;
Item.set_next previous ~next:item;
item)
else (
let previous = fresh_front_sentinel () in
t.fronts.(depth) <- previous;
let next = Item.dummy in
let item = Item.fresh ~node ~previous ~next in
Item.set_next previous ~next:item;
item)
in
node.queue_item <- item
;;
let remove ~node ~item =
let next = item.next in
let previous = item.previous in
Item.set_previous next ~previous;
Item.set_next previous ~next;
node.queue_item <- Item.dummy
;;
let iter_front front depth f =
let current = ref (Item.next front) in
while Item.is_real !current do
let next_current = !current.next in
f ~depth !current;
current := next_current
done
;;
let iter t f =
let fronts = t.fronts in
for i = t.max downto 0 do
iter_front fronts.(i) i f
done
;;
end
let label t =
let rec loop acc t =
let edge = Array.sub t.edge_array ~pos:t.edge_start ~len:t.edge_len in
if not (same t t.parent)
then loop (edge :: acc) t.parent
else Array.concat (edge :: acc)
in
loop [] t
;;
module Root = struct
type node = t
type t =
{ node : node
; children : node X.Table.t
; queue : Queue.t
; id_gen : Id.Generator.t
}
let create () =
let id_gen = Id.Generator.create () in
let id = Id.Generator.generate id_gen in
let edge_array = dummy_array in
let edge_start = 0 in
let edge_len = 0 in
let edge_key = X.dummy in
let next_sibling = dummy in
let first_child = dummy in
let refcount = 0 in
let queue_item = Queue.Item.root in
let summary = No_summary in
let count = 0 in
let delta = 0 in
let max_child_squashed = 0 in
let max_edge_squashed = 0 in
let rec node =
{ id
; edge_array
; edge_start
; edge_len
; edge_key
; parent = node
; suffix_link = node
; next_sibling
; first_child
; refcount
; summary
; queue_item
; count
; delta
; max_edge_squashed
; max_child_squashed
}
in
let children = X.Table.create ~size:37 () in
let queue = Queue.create () in
{ node; children; queue; id_gen }
;;
let node t = t.node
let is_node t node = same t.node node
let children t = t.children
let queue t = t.queue
let gen_id t = Id.Generator.generate t.id_gen
end
let rec set_child_in_list previous current old_child new_child =
let next = current.next_sibling in
if same current old_child
then (
previous.next_sibling <- new_child;
new_child.next_sibling <- next)
else set_child_in_list current next old_child new_child
;;
let set_child ~root ~parent ~key ~old_child ~new_child =
if Root.is_node root parent
then Hashtbl.set (Root.children root) ~key ~data:new_child
else (
let first_child = parent.first_child in
let second_child = first_child.next_sibling in
if same first_child old_child
then (
parent.first_child <- new_child;
new_child.next_sibling <- second_child)
else set_child_in_list first_child second_child old_child new_child)
;;
let rec add_child_to_list ~key ~child previous current =
let current_key = current.edge_key in
if X.(key < current_key)
then (
child.next_sibling <- current;
previous.next_sibling <- child)
else add_child_to_list ~key ~child current current.next_sibling
;;
let add_child ~root ~parent ~key ~child =
if Root.is_node root parent
then Hashtbl.add_exn (Root.children root) ~key ~data:child
else (
parent.refcount <- parent.refcount + 1;
let first_child = parent.first_child in
let first_key = first_child.edge_key in
if X.(key < first_key)
then (
child.next_sibling <- first_child;
parent.first_child <- child)
else add_child_to_list ~key ~child first_child first_child.next_sibling)
;;
let rec remove_from_child_list previous current child =
let next = current.next_sibling in
if same current child
then previous.next_sibling <- next
else remove_from_child_list current next child
;;
let remove_child ~root ~parent ~child =
if Root.is_node root parent
then (
let key = child.edge_key in
Hashtbl.remove (Root.children root) key;
Int.max_value)
else (
let first_child = parent.first_child in
let second_child = first_child.next_sibling in
let refcount = parent.refcount - 1 in
parent.refcount <- refcount;
if same first_child child
then parent.first_child <- second_child
else remove_from_child_list first_child second_child child;
refcount)
;;
let set_suffix ~root t ~suffix =
t.suffix_link <- suffix;
if not (Root.is_node root suffix) then suffix.refcount <- suffix.refcount + 2
;;
let remove_incoming ~root t =
if Root.is_node root t
then Int.max_value
else (
let refcount = t.refcount - 2 in
t.refcount <- refcount;
refcount)
;;
let fresh_leaf ~root ~parent ~array ~index ~key =
let id = Root.gen_id root in
let edge_array = array in
let edge_start = index in
let edge_len = Array.length array - index in
let edge_key = key in
let suffix_link = dummy in
let next_sibling = dummy in
let first_child = dummy in
let queue_item = dummy_queue_item in
let summary = No_summary in
let count = 0 in
let refcount = 0 in
let delta = parent.max_child_squashed in
let max_edge_squashed = delta in
let max_child_squashed = delta in
{ id
; edge_array
; edge_start
; edge_len
; edge_key
; parent
; suffix_link
; next_sibling
; first_child
; refcount
; summary
; queue_item
; count
; delta
; max_edge_squashed
; max_child_squashed
}
;;
let add_leaf ~root ~parent ~array ~index ~key =
let node = fresh_leaf ~root ~parent ~array ~index ~key in
add_child ~root ~parent ~key ~child:node;
node
;;
let split_edge ~root ~parent ~child ~len =
if len = 0
then parent
else (
let edge_array = child.edge_array in
let edge_start = child.edge_start in
let edge_key = child.edge_key in
let child_key = edge_array.(edge_start + len) in
let new_node =
let id = Root.gen_id root in
let edge_len = len in
let suffix_link = dummy in
let next_sibling = dummy in
let refcount = 1 in
let first_child = child in
let queue_item = dummy_queue_item in
let summary = No_summary in
let count = 0 in
let delta = child.max_edge_squashed in
let max_edge_squashed = delta in
let max_child_squashed = delta in
{ id
; edge_array
; edge_start
; edge_len
; edge_key
; parent
; suffix_link
; next_sibling
; first_child
; refcount
; summary
; queue_item
; count
; delta
; max_edge_squashed
; max_child_squashed
}
in
set_child ~root ~parent ~key:edge_key ~old_child:child ~new_child:new_node;
child.edge_start <- edge_start + len;
child.edge_len <- child.edge_len - len;
child.edge_key <- child_key;
child.parent <- new_node;
child.next_sibling <- dummy;
new_node)
;;
let merge_child ~root ~parent t =
let child = t.first_child in
let key = t.edge_key in
let edge_len = t.edge_len in
let child_edge_start = child.edge_start in
child.edge_key <- key;
if child_edge_start >= edge_len
then child.edge_start <- child_edge_start - edge_len
else (
let edge_array = t.edge_array in
let edge_start = t.edge_start in
let common_prefix = edge_start + (edge_len - child_edge_start) in
let common =
if Array.length edge_array = common_prefix
then edge_array
else Array.sub edge_array ~pos:0 ~len:common_prefix
in
let array = Array.append common child.edge_array in
child.edge_array <- array;
child.edge_start <- t.edge_start);
child.edge_len <- edge_len + child.edge_len;
let max_edge_squashed = t.max_edge_squashed in
let child_max_edge_squashed = child.max_edge_squashed in
if max_edge_squashed > child_max_edge_squashed
then child.max_edge_squashed <- max_edge_squashed;
child.parent <- parent;
set_child ~root ~parent ~key ~old_child:t ~new_child:child
;;
type find_result =
| Found of t
| Added of t
let rec find_or_add_leaf_in_list ~root ~parent ~array ~index ~key previous current =
let current_key = current.edge_key in
if X.(key < current_key)
then (
let child = fresh_leaf ~root ~parent ~array ~index ~key in
child.next_sibling <- current;
previous.next_sibling <- child;
parent.refcount <- parent.refcount + 1;
Added child)
else if X.equal key current_key
then Found current
else
find_or_add_leaf_in_list
~root
~parent
~array
~index
~key
current
current.next_sibling
;;
let find_or_add_leaf ~root ~parent ~array ~index =
let key = array.(index) in
if Root.is_node root parent
then (
let children = Root.children root in
match Hashtbl.find children key with
| Some child -> Found child
| None ->
let leaf = fresh_leaf ~root ~parent ~array ~index ~key in
Hashtbl.add_exn children ~key ~data:leaf;
Added leaf)
else (
let first_child = parent.first_child in
let first_key = first_child.edge_key in
if X.(key < first_key)
then (
let leaf = fresh_leaf ~root ~parent ~array ~index ~key in
leaf.next_sibling <- first_child;
parent.first_child <- leaf;
parent.refcount <- parent.refcount + 1;
Added leaf)
else if X.equal key first_key
then Found first_child
else
find_or_add_leaf_in_list
~root
~parent
~array
~index
~key
first_child
first_child.next_sibling)
;;
exception No_such_child
let rec get_child_in_list current char =
if X.equal current.edge_key char
then current
else if is_dummy current
then raise No_such_child
else get_child_in_list current.next_sibling char
;;
let get_child0 ~root t char =
if Root.is_node root t
then (
match Hashtbl.find (Root.children root) char with
| Some child -> child
| None -> raise No_such_child)
else get_child_in_list t.first_child char
;;
let get_child ~root t char =
try get_child0 ~root t char with
| No_such_child -> failwith "get_child: No such child"
;;
let get_child_opt ~root t char =
match get_child0 ~root t char with
| child -> Some child
| exception No_such_child -> None
;;
let edge_array t = t.edge_array
let edge_start t = t.edge_start
let edge_length t = t.edge_len
let edge_key t = t.edge_key
let edge_char t i = if i = 0 then t.edge_key else t.edge_array.(t.edge_start + i)
let has_suffix t = is_real t.suffix_link
let suffix t = t.suffix_link
let parent t = t.parent
let count t = t.count
module Debug = struct
type nonrec t = t
let sexp_of_t ({ id; count; delta; _ } as node) =
[%message
(id : Id.t)
(count : int)
(delta : int)
~total_count:
((match node.summary with
| No_summary -> [%sexp "???"]
| Summary { descendents_count; _ } ->
[%sexp (node.count + descendents_count : int)])
: Sexp.t)
~label:(label node : X.t array)]
;;
end
module Summary = struct
type nonrec t = summary =
| No_summary
| Summary of
{ mutable descendents_count : int
; mutable heavy_descendents_count : int
; mutable heavy_descendents : Id.Set.t
; mutable representative : t
}
let empty representative =
let descendents_count = 0 in
let heavy_descendents_count = 0 in
let heavy_descendents = Id.Set.empty in
Summary
{ descendents_count
; heavy_descendents_count
; heavy_descendents
; representative
}
;;
let descendents_count t =
match t with
| No_summary -> failwith "No summary"
| Summary { descendents_count; _ } -> descendents_count
;;
let heavy_descendents_count t =
match t with
| No_summary -> failwith "No summary"
| Summary { heavy_descendents_count; _ } -> heavy_descendents_count
;;
let heavy_descendents t =
match t with
| No_summary -> failwith "No summary"
| Summary { heavy_descendents; _ } -> heavy_descendents
;;
let representative t =
match t with
| No_summary -> failwith "No summary"
| Summary { representative; _ } -> representative
;;
let add_grand_child t ~grand_child_total_count =
match t with
| No_summary -> failwith "No summary"
| Summary s ->
s.descendents_count <- s.descendents_count - grand_child_total_count
;;
let add_child t ~child_total_count =
match t with
| No_summary -> failwith "No summary"
| Summary s -> s.descendents_count <- s.descendents_count + child_total_count
;;
let add_child_heavy_count t ~child ~child_heavy_count =
match t with
| No_summary -> failwith "No summary"
| Summary s ->
(match child.summary with
| No_summary -> failwith "No summary"
| Summary { heavy_descendents; representative; _ } ->
s.heavy_descendents_count <- s.heavy_descendents_count + child_heavy_count;
s.heavy_descendents <- Set.union s.heavy_descendents heavy_descendents;
if Set.length heavy_descendents = Set.length s.heavy_descendents
then s.representative <- representative)
;;
let add_grand_child_heavy_count t ~grand_child_heavy_count =
match t with
| No_summary -> failwith "No summary"
| Summary s ->
s.heavy_descendents_count <- s.heavy_descendents_count - grand_child_heavy_count
;;
let add_heavy_descendent t ~node =
match t with
| No_summary -> failwith "No summary"
| Summary s -> s.heavy_descendents <- Set.add s.heavy_descendents node.id
;;
let finalize_representative t ~node =
match t with
| No_summary -> failwith "No summary"
| Summary s ->
let node_length = Set.length s.heavy_descendents in
let rep_descendents = heavy_descendents s.representative.summary in
let rep_length = Set.length rep_descendents in
if node_length > rep_length
then s.representative <- node
else (
assert (node_length = rep_length);
s.heavy_descendents <- rep_descendents)
;;
end
let has_summary t =
match t.summary with
| No_summary -> false
| Summary _ -> true
;;
let reset_summary t = t.summary <- Summary.empty t
let descendents_count t = Summary.descendents_count t.summary
let heavy_descendents_count t = Summary.heavy_descendents_count t.summary
let heavy_descendents t = Summary.heavy_descendents t.summary
let representative t = Summary.representative t.summary
let total_count t = count t + descendents_count t
let light_count t = total_count t - heavy_descendents_count t
let is_heavy t ~heaviness_threshold =
let light_count = light_count t in
let delta = t.delta in
light_count + delta > heaviness_threshold
;;
let heavy_count t ~heaviness_threshold =
if is_heavy t ~heaviness_threshold then total_count t else heavy_descendents_count t
;;
let contains_heavy t ~heaviness_threshold =
let total_count = total_count t in
let delta = t.delta in
total_count + delta > heaviness_threshold
;;
module Debug_full = struct
type nonrec t = t
let rec sexp_of_t
({ id
; count
; delta
; max_edge_squashed
; max_child_squashed
; edge_array
; edge_start
; edge_len
; parent
; suffix_link
; summary
; first_child
; _
} as node)
=
let child_ids =
let rec siblings node =
if is_real node then node :: siblings node.next_sibling else []
in
siblings first_child |> List.map ~f:(fun node -> node.id)
in
let summary_data =
match summary with
| No_summary -> [%sexp "No_summary"]
| Summary _ ->
[%message
""
~total_count:(total_count node : int)
~light_count:(light_count node : int)
~max_light_count:(light_count node + max_edge_squashed : int)
~heavy_descendents_count:(heavy_descendents_count node : int)
~heavy_descendents:(heavy_descendents node : Id.Set.t)]
in
let representative =
match summary with
| No_summary -> [%sexp "<no summary>"]
| Summary _ ->
if same node (representative node)
then [%sexp "<self>"]
else [%sexp (representative node : t)]
in
[%message
(id : Id.t)
~label:(label node : X.t array)
(count : int)
(delta : int)
(max_edge_squashed : int)
(max_child_squashed : int)
(summary_data : Sexp.t)
~edge:(Array.sub edge_array ~pos:edge_start ~len:edge_len : X.t array)
(parent.id : Id.t)
(suffix_link.id : Id.t)
(child_ids : Id.t list)
(representative : Sexp.t)]
;;
end
let update_parent_totals t ~root =
if not (Root.is_node root t)
then (
let total_count = total_count t in
let parent = t.parent in
let suffix = t.suffix_link in
let grand_parent = t.parent.suffix_link in
if is_real parent
then Summary.add_child parent.summary ~child_total_count:total_count;
if is_real suffix
then Summary.add_child suffix.summary ~child_total_count:total_count;
if is_real grand_parent
then
Summary.add_grand_child
grand_parent.summary
~grand_child_total_count:total_count)
;;
let update_delta_from_parent_or_suffix t ~parent_or_suffix ~root =
if not (Root.is_node root t)
then
if Root.is_node root parent_or_suffix
then
()
else (
let parent_total_count = total_count parent_or_suffix in
let parent_delta = parent_or_suffix.delta in
let total_count = total_count t in
let delta = t.delta in
if total_count + delta > parent_total_count + parent_delta
then (
let delta = parent_total_count + parent_delta - total_count in
assert (delta >= 0);
t.delta <- delta))
;;
let update_delta_from_parents t ~root =
update_delta_from_parent_or_suffix t ~parent_or_suffix:t.parent ~root;
update_delta_from_parent_or_suffix t ~parent_or_suffix:t.suffix_link ~root
;;
let update_parent_heavy_totals t ~root ~heaviness_threshold =
if not (Root.is_node root t)
then (
let heavy_count = heavy_count t ~heaviness_threshold in
let parent = t.parent in
let suffix = t.suffix_link in
let grand_parent = t.parent.suffix_link in
if is_real parent
then
Summary.add_child_heavy_count
parent.summary
~child:t
~child_heavy_count:heavy_count;
if is_real suffix
then
Summary.add_child_heavy_count
suffix.summary
~child:t
~child_heavy_count:heavy_count;
if is_real grand_parent
then
Summary.add_grand_child_heavy_count
grand_parent.summary
~grand_child_heavy_count:heavy_count)
;;
let finalize_summary t ~heaviness_threshold =
if is_heavy t ~heaviness_threshold
then Summary.add_heavy_descendent t.summary ~node:t;
Summary.finalize_representative t.summary ~node:t
;;
let iter_over_child_list f current =
let current = ref current in
while not (phys_equal !current dummy) do
let child = !current in
f child;
current := child.next_sibling
done
;;
let iter_children t ~root ~f =
if Root.is_node root t
then Hashtbl.iter ~f (Root.children root)
else iter_over_child_list f t.first_child
;;
let fold_over_child_list ~f current acc =
let acc = ref acc in
let current = ref current in
while not (phys_equal !current dummy) do
let child = !current in
acc := f child !acc;
current := child.next_sibling
done;
!acc
;;
let fold_children t ~root ~init ~f =
if Root.is_node root t
then Hashtbl.fold ~f:(fun ~key:_ ~data acc -> f data acc) (Root.children root) ~init
else fold_over_child_list ~f t.first_child init
;;
let add_to_count t ~count = t.count <- t.count + count
let register_for_compression ~queue ~depth t =
if Queue.Item.is_dummy t.queue_item then Queue.add queue ~depth ~node:t
;;
let add_squashed_child t ~upper_bound =
if upper_bound > t.max_child_squashed then t.max_child_squashed <- upper_bound
;;
let add_squashed_edge t ~upper_bound =
if upper_bound > t.max_edge_squashed then t.max_edge_squashed <- upper_bound
;;
let rec squash ~root ~queue ~threshold ~depth ~count ~upper_bound ~refcount t =
t.count <- 0;
let parent = t.parent in
let suffix = t.suffix_link in
let grand_parent = t.parent.suffix_link in
add_squashed_edge t ~upper_bound;
add_squashed_child parent ~upper_bound;
let parent_depth = depth - t.edge_len in
let suffix_depth = depth - 1 in
let grand_parent_count = 0 - count in
add_to_count grand_parent ~count:grand_parent_count;
add_to_count parent ~count;
add_to_count suffix ~count;
if is_removable refcount
then (
let parent_refcount = remove_child ~root ~parent ~child:t in
if is_mergable_or_removable parent_refcount
then register_for_compression ~queue ~depth:parent_depth parent)
else if is_mergable refcount
then merge_child ~root ~parent t;
if is_mergable_or_removable refcount
then (
let suffix_refcount = remove_incoming ~root suffix in
if is_removable suffix_refcount
then (
let count = suffix.count in
let delta = suffix.delta in
let upper_bound = count + delta in
if upper_bound < threshold
then
squash
~root
~queue
~threshold
~depth:suffix_depth
~count
~upper_bound
~refcount:suffix_refcount
suffix
else register_for_compression ~queue ~depth:suffix_depth suffix)
else if is_mergable suffix_refcount
then register_for_compression ~queue ~depth:suffix_depth suffix)
;;
let maybe_squash_item ~root ~queue ~threshold ~depth item =
let node = item.node in
let refcount = node.refcount in
if not (is_mergable_or_removable refcount)
then Queue.remove ~node ~item
else (
let count = node.count in
let delta = node.delta in
let upper_bound = count + delta in
if upper_bound < threshold
then (
Queue.remove ~node ~item;
squash ~root ~queue ~threshold ~depth ~count ~upper_bound ~refcount node))
;;
let compress ~root ~threshold =
let queue = Root.queue root in
Queue.iter queue (maybe_squash_item ~root ~queue ~threshold)
;;
let add_to_count ~root ~depth ~count t =
let queue = Root.queue root in
add_to_count ~count t;
register_for_compression ~queue ~depth t
;;
end
module Cursor : sig
type t
val create : at:Node.t -> t
val goto : t -> Node.t -> unit
val retract : t -> distance:int -> unit
type find_result =
| Found
| Added of
{ parent : Node.t
; leaf : Node.t
}
val find_or_add_leaf
: root:Node.Root.t
-> t
-> array:X.t array
-> index:int
-> find_result
val split_at : root:Node.Root.t -> t -> Node.t
val goto_suffix : root:Node.Root.t -> t -> Node.t -> unit
end = struct
type t =
{ mutable parent : Node.t
; mutable len : int
; mutable child : Node.t
}
let create ~at = { parent = at; len = 0; child = at }
let goto t node =
t.parent <- node;
t.len <- 0;
t.child <- node
;;
let rec retract t ~distance =
let len = t.len in
if len > distance
then t.len <- len - distance
else if len = distance
then (
t.len <- 0;
t.child <- t.parent)
else (
let distance = distance - len in
let parent = t.parent in
t.child <- parent;
t.parent <- Node.parent parent;
t.len <- Node.edge_length parent;
retract t ~distance)
;;
let extend t =
let len = t.len + 1 in
if Node.edge_length t.child <= len
then (
t.parent <- t.child;
t.len <- 0)
else t.len <- len
;;
let extend_n t n =
let len = t.len in
let target = len + n in
let edge_len = Node.edge_length t.child in
if edge_len <= target
then (
t.parent <- t.child;
t.len <- 0;
edge_len - len)
else (
t.len <- target;
n)
;;
type find_result =
| Found
| Added of
{ parent : Node.t
; leaf : Node.t
}
let find_or_add_leaf ~root t ~array ~index =
let len = t.len in
let parent = t.parent in
if len = 0
then (
match Node.find_or_add_leaf ~root ~parent ~array ~index with
| Found child ->
t.child <- child;
extend t;
Found
| Added leaf -> Added { parent; leaf })
else (
let char = array.(index) in
let next_char = Node.edge_char t.child len in
if X.equal char next_char
then (
extend t;
Found)
else (
let child = t.child in
let parent = Node.split_edge ~root ~parent ~child ~len in
let leaf = Node.add_leaf ~root ~parent ~array ~index ~key:char in
goto t parent;
Added { parent; leaf }))
;;
let split_at ~root t =
let len = t.len in
if len = 0
then t.parent
else (
let node = Node.split_edge ~root ~parent:t.parent ~child:t.child ~len in
goto t node;
node)
;;
let rec rescan ~root t ~array ~start ~len =
if len <> 0
then (
if t.len = 0
then (
let char = array.(start) in
let child = Node.get_child ~root t.parent char in
t.child <- child);
let diff = extend_n t len in
let start = start + diff in
let len = len - diff in
rescan ~root t ~array ~start ~len)
;;
let rescan1 ~root t ~key =
if t.len = 0
then (
let child = Node.get_child ~root t.parent key in
t.child <- child);
extend t
;;
let rec goto_suffix ~root t node =
if Node.Root.is_node root node
then goto t node
else if Node.has_suffix node
then goto t (Node.suffix node)
else (
let parent = Node.parent node in
let len = Node.edge_length node in
if len = 1
then
if Node.Root.is_node root parent
then goto t parent
else (
let key = Node.edge_key node in
goto_suffix ~root t parent;
rescan1 ~root t ~key)
else (
let array = Node.edge_array node in
let start = Node.edge_start node in
if Node.Root.is_node root parent
then (
goto t parent;
let start = start + 1 in
let len = len - 1 in
rescan ~root t ~array ~start ~len)
else (
goto_suffix ~root t parent;
rescan ~root t ~array ~start ~len)))
;;
end
module Elaborated : sig
module Plain_node = Node
module Node : sig
type t [@@deriving sexp_of]
val plain : t -> Plain_node.t
val parent : t -> t
val suffix : t -> t
val children : t -> (X.t array, t) List.Assoc.t
val prefixes : t -> (X.t array, t) List.Assoc.t
end
type t
val of_root : Plain_node.Root.t -> merge_prefixes:bool -> t
val find_node_exn : t -> Plain_node.t -> Node.t
end = struct
module Plain_node = Node
module Node = struct
type t =
{ plain : Plain_node.t
; mutable parent : t
; mutable suffix : t
; children : (X.t array, t) List.Assoc.t
; mutable prefixes : (X.t array, t) List.Assoc.t
}
let plain t = t.plain
let parent t = t.parent
let suffix t = t.suffix
let children t = t.children
let prefixes t = t.prefixes
let rec sexp_of_t t =
let total_count =
if Plain_node.has_summary t.plain
then [%sexp (Plain_node.total_count t.plain : int)]
else [%sexp "???"]
in
[%message
""
~count:(Plain_node.count t.plain : int)
(total_count : Sexp.t)
~id:(Plain_node.id t.plain : Plain_node.Id.t)
~prefixes:(t.prefixes : (X.t array, t) List.Assoc.t)
~children:(t.children : (X.t array, t) List.Assoc.t)]
;;
end
type t = Node.t Plain_node.Id.Table.t
let find_node_exn t node = Hashtbl.find_exn t (Plain_node.id node)
let of_root plain_root ~merge_prefixes : t =
let t = Plain_node.Id.Table.create () in
let plain_root_node = Plain_node.Root.node plain_root in
let rec dummy : Node.t =
{ plain = plain_root_node
; parent = dummy
; suffix = dummy
; children = []
; prefixes = []
}
in
let rec mk_node plain : Node.t =
let children =
Plain_node.fold_children
plain
~root:plain_root
~init:[]
~f:(fun plain_child children ->
let edge =
Array.sub
(Plain_node.edge_array plain_child)
~pos:(Plain_node.edge_start plain_child)
~len:(Plain_node.edge_length plain_child)
in
let child = mk_node plain_child in
(edge, child) :: children)
in
let parent = dummy in
let suffix = dummy in
let prefixes = [] in
let node : Node.t = { plain; parent; suffix; children; prefixes } in
Hashtbl.add_exn t ~key:(Plain_node.id plain) ~data:node;
node
in
let root = mk_node plain_root_node in
root.parent <- root;
root.suffix <- root;
let rec fix_back_pointers (node : Node.t) ~parent ~leading_edge =
node.parent <- parent;
let suffix = find_node_exn t (Plain_node.suffix node.plain) in
node.suffix <- suffix;
suffix.prefixes <- (leading_edge, node) :: suffix.prefixes;
List.iter node.children ~f:(fun (_, child) ->
fix_back_pointers child ~parent:node ~leading_edge)
in
List.iter root.children ~f:(fun (edge, child) ->
let leading_edge =
[| edge.(0) |]
in
fix_back_pointers child ~parent:root ~leading_edge);
let rec do_merge_prefixes (node : Node.t) =
node.prefixes
<- List.map node.prefixes ~f:(fun (leading_edge, child) ->
let rec chain (desc : Node.t) edges =
if Plain_node.count desc.plain <> 0
then edges, desc
else (
match desc.children, desc.prefixes with
| [], [ (edge, child) ] -> chain child (edge :: edges)
| _ -> edges, desc)
in
let edges, last_child = chain child [ leading_edge ] in
Array.concat edges, last_child);
List.iter node.children ~f:(fun (_, child) -> do_merge_prefixes child)
in
if merge_prefixes then do_merge_prefixes root;
t
;;
end
type state =
| Uncompressed
| Compressed of X.t array
type t =
{ root : Node.Root.t
; mutable max_length : int
; mutable count : int
; bucket_size : int
; mutable current_bucket : int
; mutable remaining_in_current_bucket : int
; active : Cursor.t
; mutable previous_length : int
; mutable state : state
; mutable heaviness_threshold : int
}
let create ~tolerance =
let root = Node.Root.create () in
let max_length = 0 in
let count = 0 in
let bucket_size = Float.to_int (Float.round_up (1.0 /. tolerance)) in
let current_bucket = 0 in
let remaining_in_current_bucket = bucket_size in
let active = Cursor.create ~at:(Node.Root.node root) in
let previous_length = 0 in
let state = Uncompressed in
let heaviness_threshold = 0 in
{ root
; max_length
; count
; bucket_size
; current_bucket
; remaining_in_current_bucket
; active
; previous_length
; state
; heaviness_threshold
}
;;
let update_summaries ~heaviness_threshold t =
let root = t.root in
let nodes : Node.t list array = Array.create ~len:(t.max_length + 1) [] in
let rec loop depth node =
Node.reset_summary node;
let depth = depth + Node.edge_length node in
nodes.(depth) <- node :: nodes.(depth);
Node.iter_children ~root ~f:(loop depth) node
in
loop 0 (Node.Root.node root);
for i = t.max_length downto 0 do
List.iter nodes.(i) ~f:(fun node -> Node.update_parent_totals ~root node)
done;
for i = 1 to t.max_length do
List.iter nodes.(i) ~f:(fun node -> Node.update_delta_from_parents ~root node)
done;
for i = t.max_length downto 0 do
List.iter nodes.(i) ~f:(fun node ->
Node.finalize_summary ~heaviness_threshold node;
Node.update_parent_heavy_totals ~root ~heaviness_threshold node)
done
;;
let rec ensure_suffix ~root cursor t =
if not (Node.has_suffix t)
then (
Cursor.goto_suffix ~root cursor t;
let suffix = Cursor.split_at ~root cursor in
ensure_suffix ~root cursor suffix;
Node.set_suffix ~root t ~suffix)
;;
let insert t ~common_prefix array ~count =
assert (count > 0);
let len = Array.length array in
let total_len = common_prefix + len in
if total_len > t.max_length then t.max_length <- total_len;
t.count <- t.count + count;
let root = t.root in
let active = t.active in
let array, len, base =
match t.state with
| Uncompressed ->
Cursor.retract active ~distance:(t.previous_length - common_prefix);
array, len, common_prefix
| Compressed previous_label ->
let common = Array.sub previous_label ~pos:0 ~len:common_prefix in
let array = Array.append common array in
array, total_len, 0
in
let rec loop array len base root active index j =
if index >= len
then (
let destination = Cursor.split_at ~root active in
ensure_suffix ~root active destination;
destination)
else loop_inner array len base root active index j
and loop_inner array len base root active index j =
if j > base + index
then loop array len base root active (index + 1) j
else (
match Cursor.find_or_add_leaf ~root active ~array ~index with
| Found -> loop array len base root active (index + 1) j
| Added { parent; leaf } ->
Cursor.goto_suffix ~root active parent;
let leaf_suffix =
if Node.has_suffix parent
then loop_inner array len base root active index (j + 1)
else (
let suffix = Cursor.split_at ~root active in
let leaf_suffix = loop_inner array len base root active index (j + 1) in
Node.set_suffix ~root parent ~suffix;
leaf_suffix)
in
Node.set_suffix ~root leaf ~suffix:leaf_suffix;
leaf)
in
let destination = loop array len base root active 0 0 in
Node.add_to_count ~root ~depth:total_len ~count destination;
let remaining = t.remaining_in_current_bucket - 1 in
if remaining <= 0
then (
t.current_bucket <- t.current_bucket + 1;
t.remaining_in_current_bucket <- t.bucket_size;
let destination_label = Node.label destination in
Cursor.goto active (Node.Root.node t.root);
let threshold = t.current_bucket in
Node.compress ~root ~threshold;
t.previous_length <- 0;
t.state <- Compressed destination_label)
else (
t.remaining_in_current_bucket <- remaining;
Cursor.goto active destination;
t.previous_length <- total_len;
t.state <- Uncompressed)
;;
let root t = t.root
let threshold_of_frequency t frequency =
Float.to_int (Float.round_down (frequency *. Float.of_int t.count))
;;
let total_count t = t.count
let maximum_depth t = t.max_length
let calculate_totals t ~heaviness_frequency =
let heaviness_threshold = heaviness_frequency |> threshold_of_frequency t in
update_summaries ~heaviness_threshold t;
t.heaviness_threshold <- heaviness_threshold
;;
let is_heavy t node = Node.is_heavy node ~heaviness_threshold:t.heaviness_threshold
let contains_heavy t node =
Node.contains_heavy node ~heaviness_threshold:t.heaviness_threshold
;;
let dump_subtree t node =
let elaborated_tree = Elaborated.of_root t.root ~merge_prefixes:true in
let elaborated_node = Elaborated.find_node_exn elaborated_tree node in
Elaborated.Node.sexp_of_t elaborated_node
;;
end