package incr_map

  1. Overview
  2. Docs

Source file incr_map.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
open! Core
include Incr_map_intf

(** This type lets us capture the kind of map function being performed, so we can with
    one implementation perform map and filter-map operations.

    Here, ['input_data] is the type of data in the input map, ['output_data] is the type
    of data in the output map, and ['f_output] is the return type of the [~f] function
    passed to the mapping function. *)
module Map_type = struct
  type ('input_data, 'output_data, 'f_output) t =
    | Map : ('input_data, 'output_data, 'output_data) t
    | Filter_map : ('input_data, 'output_data, 'output_data option) t

  (* The extra type variable 'a is to allow in future:
     | Filter : ('output_data, 'output_data, bool) t *)
end

module Generic = struct
  let with_old i ~f =
    let open Incremental.Let_syntax in
    let old = ref None in
    let%map a = i in
    let b = f ~old:!old a in
    old := Some (a, b);
    b
  ;;

  let unordered_fold
        ?(data_equal = phys_equal)
        ?update
        ?specialized_initial
        ?(revert_to_init_when_empty = false)
        map
        ~init
        ~add
        ~remove
    =
    let update =
      let default ~key ~old_data ~new_data acc =
        add ~key ~data:new_data (remove ~key ~data:old_data acc)
      in
      Option.value update ~default
    in
    with_old map ~f:(fun ~old new_in ->
      match old with
      | None ->
        (match specialized_initial with
         | None -> Map.fold ~init ~f:add new_in
         | Some initial -> initial ~init new_in)
      | Some (old_in, old_out) ->
        if revert_to_init_when_empty && Map.length new_in = 0
        then init
        else
          Map.fold_symmetric_diff
            ~init:old_out
            old_in
            new_in
            ~data_equal
            ~f:(fun acc (key, change) ->
              match change with
              | `Left old -> remove ~key ~data:old acc
              | `Right new_ -> add ~key ~data:new_ acc
              | `Unequal (old, new_) -> update ~key ~old_data:old ~new_data:new_ acc))
  ;;

  let unordered_fold_nested_maps
        ?(data_equal = phys_equal)
        ?revert_to_init_when_empty
        ?update
        incr_map
        ~init
        ~add
        ~remove
    =
    let update =
      match update with
      | Some update -> update
      | None ->
        fun ~outer_key ~inner_key ~old_data ~new_data acc ->
          add
            ~outer_key
            ~inner_key
            ~data:new_data
            (remove ~outer_key ~inner_key ~data:old_data acc)
    in
    unordered_fold
      incr_map
      ?revert_to_init_when_empty
      ~init
      ~update:(fun ~key:outer_key ~old_data:old_inner_map ~new_data:new_inner_map acc ->
        (Map.fold_symmetric_diff old_inner_map new_inner_map ~data_equal)
          ~init:acc
          ~f:(fun acc (inner_key, diff) ->
            match diff with
            | `Left data_removed -> remove ~outer_key ~inner_key ~data:data_removed acc
            | `Right data_added -> add ~outer_key ~inner_key ~data:data_added acc
            | `Unequal (old_data, new_data) ->
              update ~outer_key ~inner_key ~old_data ~new_data acc))
      ~add:(fun ~key:outer_key ~data:inner_map acc ->
        Map.fold inner_map ~init:acc ~f:(fun ~key:inner_key ~data acc ->
          add ~outer_key ~inner_key ~data acc))
      ~remove:(fun ~key:outer_key ~data:inner_map acc ->
        Map.fold inner_map ~init:acc ~f:(fun ~key:inner_key ~data acc ->
          remove ~outer_key ~inner_key ~data acc))
  ;;

  let with_comparator' get_comparator x f =
    Incremental.bind (Incremental.freeze (Incremental.map x ~f:get_comparator)) ~f
  ;;

  (** Captures the comparator (which can't change anyway, since the type determines the
      comparator) by freezing the corresponding map.  Note that by first using Incremental.map to
      get the comparator out of the map, we allow the initial map itself to be garbage
      collected *)
  let with_comparator map f = with_comparator' Map.comparator map f

  let of_set set =
    with_comparator' Set.comparator set (fun comparator ->
      let old_input = ref (Set.Using_comparator.empty ~comparator) in
      let old_output = ref (Map.Using_comparator.empty ~comparator) in
      Incremental.map set ~f:(fun new_input ->
        let new_output =
          Sequence.fold
            (Set.symmetric_diff !old_input new_input)
            ~init:!old_output
            ~f:(fun output -> function
              | First k -> Map.remove output k
              | Second k -> Map.add_exn output ~key:k ~data:())
        in
        old_input := new_input;
        old_output := new_output;
        new_output))
  ;;

  let generic_mapi
        (type input_data output_data f_output state_witness)
        (witness : (input_data, output_data, f_output) Map_type.t)
        ?(data_equal = phys_equal)
        (map : (('key, input_data, 'cmp) Map.t, state_witness) Incremental.t)
        ~(f : key:'key -> data:input_data -> f_output)
    =
    with_old map ~f:(fun ~old input ->
      match old, Map.length input with
      | _, 0 | None, _ ->
        (match witness with
         | Map_type.Map -> (Map.mapi input ~f : ('key, output_data, 'cmp) Map.t)
         | Map_type.Filter_map -> Map.filter_mapi input ~f)
      | Some (old_input, old_output), _ ->
        Map.fold_symmetric_diff
          old_input
          input
          ~data_equal
          ~init:old_output
          ~f:(fun output (key, change) ->
            match change with
            | `Left _ -> Map.remove output key
            | `Right new_data | `Unequal (_, new_data) ->
              let res = f ~key ~data:new_data in
              (match witness with
               | Map_type.Map -> Map.set output ~key ~data:res
               | Map_type.Filter_map ->
                 (match res with
                  | None -> Map.remove output key
                  | Some output_data -> Map.set output ~key ~data:output_data))))
  ;;

  let mapi ?data_equal map ~f = generic_mapi Map ?data_equal map ~f
  let filter_mapi ?data_equal map ~f = generic_mapi Filter_map ?data_equal map ~f
  let map ?data_equal map ~f = mapi ?data_equal map ~f:(fun ~key:_ ~data -> f data)

  let filter_map ?data_equal map ~f =
    filter_mapi ?data_equal map ~f:(fun ~key:_ ~data -> f data)
  ;;

  let with_old2 i1 i2 ~f =
    let old = ref None in
    Incremental.map2 i1 i2 ~f:(fun a1 a2 ->
      let b = f ~old:!old a1 a2 in
      old := Some (a1, a2, b);
      b)
  ;;

  let mapi_count
        (type a cmp)
        ?(data_equal = phys_equal)
        input
        ~(comparator :
            (module Comparator.S with type t = a and type comparator_witness = cmp))
        ~f
    =
    let module M = (val comparator) in
    let add new_key acc =
      Map.update acc new_key ~f:(function
        | None -> 1
        | Some n -> n + 1)
    in
    let remove new_key acc =
      Map.change acc new_key ~f:(function
        | None -> None
        | Some 1 -> None
        | Some n -> Some (n - 1))
    in
    unordered_fold
      ~data_equal
      input
      ~init:(Map.empty (module M))
      ~add:(fun ~key ~data acc -> add (f ~key ~data) acc)
      ~remove:(fun ~key ~data acc -> remove (f ~key ~data) acc)
      ~update:(fun ~key ~old_data ~new_data acc ->
        let prev_key = f ~key ~data:old_data in
        let new_key = f ~key ~data:new_data in
        if M.comparator.compare prev_key new_key = 0
        then acc
        else acc |> remove prev_key |> add new_key)
  ;;

  let map_count ?data_equal input ~comparator ~f =
    mapi_count ?data_equal input ~comparator ~f:(fun ~key:_ ~data -> f data)
  ;;

  let min_helper map =
    match Map.min_elt map with
    | None -> None
    | Some (min, _) -> Some min
  ;;

  let max_helper map =
    match Map.max_elt map with
    | None -> None
    | Some (max, _) -> Some max
  ;;

  let bounds_helper map =
    match Map.min_elt map, Map.max_elt map with
    | None, None -> None
    | Some (min, _), Some (max, _) -> Some (min, max)
    | _ -> assert false
  ;;

  let mapi_min ?data_equal input ~comparator ~f =
    Incremental.map ~f:min_helper (mapi_count ?data_equal input ~comparator ~f)
  ;;

  let mapi_max ?data_equal input ~comparator ~f =
    Incremental.map ~f:max_helper (mapi_count ?data_equal input ~comparator ~f)
  ;;

  let mapi_bounds ?data_equal input ~comparator ~f =
    Incremental.map ~f:bounds_helper (mapi_count ?data_equal input ~comparator ~f)
  ;;

  let mapi_mn ?data_equal input ~comparator ~f =
    mapi_min ?data_equal input ~comparator ~f:(fun ~key:_ ~data -> f data)
  ;;

  let map_max ?data_equal input ~comparator ~f =
    mapi_max ?data_equal input ~comparator ~f:(fun ~key:_ ~data -> f data)
  ;;

  let min_value ?data_equal input ~comparator =
    mapi_mn ?data_equal input ~comparator ~f:Fn.id
  ;;

  let max_value ?data_equal input ~comparator =
    map_max ?data_equal input ~comparator ~f:Fn.id
  ;;

  let map_bounds ?data_equal input ~comparator ~f =
    mapi_bounds ?data_equal input ~comparator ~f:(fun ~key:_ ~data -> f data)
  ;;

  let value_bounds ?data_equal input ~comparator =
    map_bounds ?data_equal input ~comparator ~f:Fn.id
  ;;

  let merge
        ?(data_equal_left = phys_equal)
        ?(data_equal_right = phys_equal)
        left_map
        right_map
        ~f
    =
    with_old2 left_map right_map ~f:(fun ~old new_left_map new_right_map ->
      let comparator = Map.comparator new_left_map in
      let old_left_map, old_right_map, old_output =
        match old with
        | None ->
          let empty = Map.Using_comparator.empty ~comparator in
          empty, empty, empty
        | Some x -> x
      in
      let left_diff =
        Map.symmetric_diff old_left_map new_left_map ~data_equal:data_equal_left
      in
      let right_diff =
        Map.symmetric_diff old_right_map new_right_map ~data_equal:data_equal_right
      in
      (* We merge the two sides of the diffs together so we can make sure to handle each
         key exactly once. This relies on symmetric diff giving sorted output. *)
      Sequence.merge_with_duplicates
        left_diff
        right_diff
        ~compare:(fun (left_key, _) (right_key, _) ->
          comparator.compare left_key right_key)
      |> Sequence.fold ~init:old_output ~f:(fun output diff_element ->
        let key =
          match diff_element with
          | Left (key, _) | Right (key, _) -> key
          | Both ((left_key, _), (right_key, _)) ->
            assert (comparator.compare left_key right_key = 0);
            left_key
        in
        (* These values represent whether there is data for the given key in the new
           input in the left and right map. *)
        let left_data_opt, right_data_opt =
          let new_data = function
            | `Left _ -> None
            | `Right x | `Unequal (_, x) -> Some x
          in
          match diff_element with
          | Both ((_, left_diff), (_, right_diff)) ->
            new_data left_diff, new_data right_diff
          | Left (_, left_diff) -> new_data left_diff, Map.find new_right_map key
          | Right (_, right_diff) -> Map.find new_left_map key, new_data right_diff
        in
        let output_data_opt =
          match left_data_opt, right_data_opt with
          | None, None -> None
          | Some x, None -> f ~key (`Left x)
          | None, Some y -> f ~key (`Right y)
          | Some x, Some y -> f ~key (`Both (x, y))
        in
        match output_data_opt with
        | None -> Map.remove output key
        | Some data -> Map.set output ~key ~data))
  ;;

  let generic_mapi_with_comparator'
        (type input_data output_data f_output state_witness)
        (witness : (input_data, output_data, f_output) Map_type.t)
        ?cutoff
        ?(data_equal = phys_equal)
        (lhs : (('key, input_data, 'cmp) Map.t, state_witness) Incremental.t)
        ~(comparator : ('key, 'cmp) Comparator.t)
        ~(f :
            key:'key
          -> data:(input_data, state_witness) Incremental.t
          -> (f_output, state_witness) Incremental.t)
    : (('key, output_data, 'cmp) Map.t, state_witness) Incremental.t
    =
    let module E = Incremental.Expert in
    let incremental_state = Incremental.state lhs in
    let empty_map = Map.Using_comparator.empty ~comparator in
    let prev_map = ref empty_map in
    let prev_nodes = ref empty_map in
    let acc : ('key, output_data, 'cmp) Map.t ref = ref empty_map in
    let result = E.Node.create incremental_state (fun () -> !acc) in
    let (on_inner_change : key:'key -> f_output -> unit) =
      match witness with
      | Map_type.Map -> fun ~key data -> acc := Map.set !acc ~key ~data
      | Map_type.Filter_map ->
        fun ~key opt ->
          let old = !acc in
          acc
          := (match opt with
            | None -> Map.remove old key
            | Some data -> Map.set old ~key ~data)
    in
    let rec lhs_change =
      lazy
        (Incremental.map lhs ~f:(fun map ->
           let new_nodes =
             Map.fold_symmetric_diff
               ~data_equal
               !prev_map
               map
               ~init:!prev_nodes
               ~f:(fun nodes (key, changed) ->
                 match changed with
                 | `Unequal _ ->
                   let node, _dep = Map.find_exn nodes key in
                   E.Node.make_stale node;
                   nodes
                 | `Left _ ->
                   let node, dep = Map.find_exn nodes key in
                   let nodes = Map.remove nodes key in
                   E.Node.remove_dependency result dep;
                   acc := Map.remove !acc key;
                   E.Node.invalidate node;
                   nodes
                 | `Right _ ->
                   let node =
                     E.Node.create incremental_state (fun () ->
                       Map.find_exn !prev_map key)
                   in
                   Option.iter cutoff ~f:(fun c ->
                     Incremental.set_cutoff (E.Node.watch node) c);
                   E.Node.add_dependency node (E.Dependency.create (force lhs_change));
                   let user_function_dep =
                     E.Dependency.create
                       (f ~key ~data:(E.Node.watch node))
                       ~on_change:(on_inner_change ~key)
                   in
                   E.Node.add_dependency result user_function_dep;
                   Map.set nodes ~key ~data:(node, user_function_dep))
           in
           prev_nodes := new_nodes;
           prev_map := map))
    in
    E.Node.add_dependency result (E.Dependency.create (force lhs_change));
    E.Node.watch result
  ;;

  let filter_mapi' ?cutoff ?data_equal map ~f =
    with_comparator map (fun comparator ->
      generic_mapi_with_comparator'
        Map_type.Filter_map
        ?cutoff
        ?data_equal
        map
        ~f
        ~comparator)
  ;;

  let mapi' ?cutoff ?data_equal map ~f =
    with_comparator map (fun comparator ->
      generic_mapi_with_comparator' Map_type.Map ?cutoff ?data_equal map ~f ~comparator)
  ;;

  let map' ?cutoff ?data_equal map ~f =
    mapi' ?cutoff ?data_equal map ~f:(fun ~key:_ ~data -> f data)
  ;;

  let filter_map' ?cutoff ?data_equal map ~f =
    filter_mapi' ?cutoff ?data_equal map ~f:(fun ~key:_ ~data -> f data)
  ;;

  let merge' ?cutoff ?data_equal_left ?data_equal_right map1 map2 ~f =
    merge ?data_equal_left ?data_equal_right map1 map2 ~f:(fun ~key:_ diff -> Some diff)
    |> filter_mapi' ?cutoff ~f:(fun ~key ~data:diff -> f ~key diff)
  ;;

  let unzip_mapi_with_comparator
        (type v v1 v2 state_witness)
        ?(data_equal : v -> v -> bool = phys_equal)
        ?(left_result_equal : v1 -> v1 -> bool = phys_equal)
        ?(right_result_equal : v2 -> v2 -> bool = phys_equal)
        (input : (('key, v, 'cmp) Map.t, state_witness) Incremental.t)
        ~(comparator : ('key, 'cmp) Comparator.t)
        ~(f : key:'key -> data:v -> v1 * v2)
    : (('key, v1, 'cmp) Map.t, state_witness) Incremental.t
      * (('key, v2, 'cmp) Map.t, state_witness) Incremental.t
    =
    let module E = Incremental.Expert in
    let incremental_state = Incremental.state input in
    let empty_map = Map.Using_comparator.empty ~comparator in
    let left_acc : ('key, v1, 'cmp) Map.t ref = ref empty_map in
    let left_result = E.Node.create incremental_state (fun () -> !left_acc) in
    let right_acc : ('key, v2, 'cmp) Map.t ref = ref empty_map in
    let right_result = E.Node.create incremental_state (fun () -> !right_acc) in
    let prev_map = ref empty_map in
    let input_change =
      Incremental.map input ~f:(fun map ->
        let left, right =
          match Map.is_empty !prev_map, Map.is_empty map with
          | true, true | false, true -> empty_map, empty_map
          | true, false ->
            (* Mapping on a map is way faster than symmetric diffing and then
               building the maps up piece by piece, so we do this whenever we
               transition from "empty" to "something", which will almost always
               happen on the first stabilization. *)
            let left =
              Map.mapi map ~f:(fun ~key ~data ->
                let l, _ = f ~key ~data in
                l)
            in
            let right =
              Map.mapi map ~f:(fun ~key ~data ->
                let _, r = f ~key ~data in
                r)
            in
            left, right
          | false, false ->
            Map.fold_symmetric_diff
              ~data_equal
              !prev_map
              map
              ~init:(!left_acc, !right_acc)
              ~f:(fun (left, right) (key, changed) ->
                match changed with
                | `Unequal (prev, new_) ->
                  let prev_a, prev_b = f ~key ~data:prev in
                  let new_a, new_b = f ~key ~data:new_ in
                  let left =
                    if left_result_equal prev_a new_a
                    then left
                    else Map.set left ~key ~data:new_a
                  in
                  let right =
                    if right_result_equal prev_b new_b
                    then right
                    else Map.set right ~key ~data:new_b
                  in
                  left, right
                | `Left _ -> Map.remove left key, Map.remove right key
                | `Right element ->
                  let a, b = f ~key ~data:element in
                  Map.set left ~key ~data:a, Map.set right ~key ~data:b)
        in
        if not (phys_equal !left_acc left) then E.Node.make_stale left_result;
        if not (phys_equal !right_acc right) then E.Node.make_stale right_result;
        left_acc := left;
        right_acc := right;
        prev_map := map)
    in
    E.Node.add_dependency left_result (E.Dependency.create input_change);
    E.Node.add_dependency right_result (E.Dependency.create input_change);
    E.Node.watch left_result, E.Node.watch right_result
  ;;

  let unzip_mapi ?data_equal ?left_result_equal ?right_result_equal input ~f =
    let pair =
      with_comparator input (fun comparator ->
        input
        |> unzip_mapi_with_comparator
             ?data_equal
             ?left_result_equal
             ?right_result_equal
             ~comparator
             ~f
        |> Tuple2.uncurry Incremental.both)
    in
    Incremental.map ~f:fst pair, Incremental.map ~f:snd pair
  ;;

  let unzip ?left_result_equal ?right_result_equal input =
    let data_equal =
      Option.map2 left_result_equal right_result_equal ~f:(fun l r ->
        Tuple2.equal ~eq1:l ~eq2:r)
    in
    unzip_mapi
      ?data_equal
      ?left_result_equal
      ?right_result_equal
      input
      ~f:(fun ~key:_ ~data -> data)
  ;;

  let unzip_mapi_with_comparator'
        (type v v1 v2 state_witness)
        ?cutoff
        ?(data_equal = phys_equal)
        (input : (('key, v, 'cmp) Map.t, state_witness) Incremental.t)
        ~(comparator : ('key, 'cmp) Comparator.t)
        ~(f :
            key:'key
          -> data:(v, state_witness) Incremental.t
          -> (v1, state_witness) Incremental.t * (v2, state_witness) Incremental.t)
    : (('key, v1, 'cmp) Map.t, state_witness) Incremental.t
      * (('key, v2, 'cmp) Map.t, state_witness) Incremental.t
    =
    let module E = Incremental.Expert in
    let incremental_state = Incremental.state input in
    let empty_map = Map.Using_comparator.empty ~comparator in
    let prev_map = ref empty_map in
    let prev_nodes = ref empty_map in
    let left_acc : ('key, v1, 'cmp) Map.t ref = ref empty_map in
    let left_result = E.Node.create incremental_state (fun () -> !left_acc) in
    let right_acc : ('key, v2, 'cmp) Map.t ref = ref empty_map in
    let right_result = E.Node.create incremental_state (fun () -> !right_acc) in
    let left_on_inner_change ~key data = left_acc := Map.set !left_acc ~key ~data in
    let right_on_inner_change ~key data = right_acc := Map.set !right_acc ~key ~data in
    let rec input_change =
      lazy
        (Incremental.map input ~f:(fun map ->
           let new_nodes =
             Map.fold_symmetric_diff
               ~data_equal
               !prev_map
               map
               ~init:!prev_nodes
               ~f:(fun nodes (key, changed) ->
                 match changed with
                 | `Unequal _ ->
                   let node, _left_dep, _right_dep = Map.find_exn nodes key in
                   E.Node.make_stale node;
                   nodes
                 | `Left _ ->
                   let node, left_dep, right_dep = Map.find_exn nodes key in
                   let nodes = Map.remove nodes key in
                   E.Node.remove_dependency left_result left_dep;
                   E.Node.remove_dependency right_result right_dep;
                   left_acc := Map.remove !left_acc key;
                   right_acc := Map.remove !right_acc key;
                   E.Node.invalidate node;
                   nodes
                 | `Right _ ->
                   let node =
                     E.Node.create incremental_state (fun () ->
                       Map.find_exn !prev_map key)
                   in
                   Option.iter cutoff ~f:(fun c ->
                     Incremental.set_cutoff (E.Node.watch node) c);
                   E.Node.add_dependency node (E.Dependency.create (force input_change));
                   let left_incr, right_incr = f ~key ~data:(E.Node.watch node) in
                   let left_user_function_dep =
                     E.Dependency.create
                       left_incr
                       ~on_change:(left_on_inner_change ~key)
                   in
                   let right_user_function_dep =
                     E.Dependency.create
                       right_incr
                       ~on_change:(right_on_inner_change ~key)
                   in
                   E.Node.add_dependency left_result left_user_function_dep;
                   E.Node.add_dependency right_result right_user_function_dep;
                   Map.set
                     nodes
                     ~key
                     ~data:(node, left_user_function_dep, right_user_function_dep))
           in
           prev_nodes := new_nodes;
           prev_map := map))
    in
    E.Node.add_dependency left_result (E.Dependency.create (force input_change));
    E.Node.add_dependency right_result (E.Dependency.create (force input_change));
    E.Node.watch left_result, E.Node.watch right_result
  ;;

  let unzip_mapi' ?cutoff ?data_equal map ~f =
    let pair =
      with_comparator map (fun comparator ->
        map
        |> unzip_mapi_with_comparator' ?cutoff ?data_equal ~comparator ~f
        |> Tuple2.uncurry Incremental.both)
    in
    Incremental.map ~f:fst pair, Incremental.map ~f:snd pair
  ;;

  let keys map =
    with_comparator map (fun comparator ->
      let add ~key ~data:_ acc = Set.add acc key in
      let remove ~key ~data:_ acc = Set.remove acc key in
      let data_equal _ _ = true in
      unordered_fold
        map
        ~init:(Set.Using_comparator.empty ~comparator)
        ~revert_to_init_when_empty:true
        ~data_equal
        ~add
        ~remove)
  ;;

  let partition_mapi ?data_equal map ~f =
    with_comparator map (fun comparator ->
      let empty = Map.Using_comparator.empty ~comparator in
      unordered_fold
        ?data_equal
        map
        ~init:(empty, empty)
        ~revert_to_init_when_empty:true
        ~update:(fun ~key ~old_data:_ ~new_data:data (first, second) ->
          match f ~key ~data with
          | First data -> Map.set first ~key ~data, Map.remove second key
          | Second data -> Map.remove first key, Map.set second ~key ~data)
        ~add:(fun ~key ~data (first, second) ->
          match f ~key ~data with
          | First data -> Map.add_exn first ~key ~data, second
          | Second data -> first, Map.add_exn second ~key ~data)
        ~remove:(fun ~key ~data:_ (first, second) ->
          Map.remove first key, Map.remove second key))
  ;;

  let flatten state map =
    let module E = Incremental.Expert in
    let result = ref (Map.Using_comparator.empty ~comparator:(Map.comparator map)) in
    let node = E.Node.create state (fun () -> !result) in
    Map.iteri map ~f:(fun ~key ~data:incr ->
      E.Node.add_dependency
        node
        (E.Dependency.create incr ~on_change:(fun a ->
           result := Map.set !result ~key ~data:a)));
    E.Node.watch node
  ;;

  let join_with_comparator map_incr ~comparator =
    let module E = Incremental.Expert in
    let incremental_state = Incremental.state map_incr in
    let empty_map = Map.Using_comparator.empty ~comparator in
    let result_map = ref empty_map in
    let old_map_of_incrs = ref empty_map in
    let current_dependencies = ref empty_map in
    let result = E.Node.create incremental_state (fun () -> !result_map) in
    let add_subnode current_dependencies ~key ~data_node =
      let new_dep =
        E.Dependency.create data_node ~on_change:(fun data ->
          result_map := Map.set !result_map ~key ~data)
      in
      E.Node.add_dependency result new_dep;
      Map.set current_dependencies ~key ~data:new_dep
    in
    let remove_subnode current_dependencies ~key =
      let dep = Map.find_exn current_dependencies key in
      E.Node.remove_dependency result dep;
      result_map := Map.remove !result_map key;
      Map.remove current_dependencies key
    in
    let lhs_change =
      Incremental.map map_incr ~f:(fun map_of_incrs ->
        let new_dependency_map =
          Map.fold_symmetric_diff
            ~data_equal:phys_equal
            !old_map_of_incrs
            map_of_incrs
            ~init:!current_dependencies
            ~f:(fun current_dependencies (key, diff) ->
              match diff with
              | `Left _ -> remove_subnode current_dependencies ~key
              | `Right data_node -> add_subnode current_dependencies ~key ~data_node
              | `Unequal (_, data_node) ->
                remove_subnode current_dependencies ~key |> add_subnode ~key ~data_node)
        in
        current_dependencies := new_dependency_map;
        old_map_of_incrs := map_of_incrs)
    in
    E.Node.add_dependency result (E.Dependency.create lhs_change);
    E.Node.watch result
  ;;

  let join map =
    with_comparator map (fun comparator -> join_with_comparator map ~comparator)
  ;;

  module Separate_state = struct
    type ('k, 'v, 'cmp, 'w) t =
      { mutable input_map : ('k, 'v, 'cmp) Map.t
      ; mutable expert_nodes : ('k, ('v, 'w) Incremental.Expert.Node.t, 'cmp) Map.t
      ; mutable output_map : ('k, ('v, 'w) Incremental.t, 'cmp) Map.t
      }

    let create comparator =
      let empty = Map.Using_comparator.empty ~comparator in
      { input_map = empty; expert_nodes = empty; output_map = empty }
    ;;

    let create_lookup_node state t key =
      Incremental.Expert.Node.create state (fun () -> Map.find_exn t.input_map key)
    ;;
  end

  let separate input_map ~data_equal =
    let incremental_state = Incremental.state input_map in
    with_comparator input_map (fun comparator ->
      let state = Separate_state.create comparator in
      let output_map_node =
        Incremental.Expert.Node.create incremental_state (fun () -> state.output_map)
      in
      let make_node_depend_on_input_map_changed node ~input_map_changed =
        let dependency =
          Incremental.Expert.Dependency.create (Lazy.force_val input_map_changed)
        in
        Incremental.Expert.Node.add_dependency node dependency
      in
      (* We want to make nodes depend on [input_map_changed] so that [input_map_changed]
         is allowed to make them stale, but we do not want them to be recomputed for any
         other reason. So we make [input_map_changed] a unit incremental (that therefore
         never changes) and this way [output_map_node] and the lookup nodes will only be
         recomputed when they are explicitly made stale.
      *)
      let rec input_map_changed =
        lazy
          (Incremental.map input_map ~f:(fun input_map ->
             let prev_input_map = state.input_map in
             let expert_nodes, output_map =
               Map.fold_symmetric_diff
                 prev_input_map
                 input_map
                 ~data_equal
                 ~init:(state.expert_nodes, state.output_map)
                 ~f:(fun (expert_nodes, output_map) (key, change) ->
                   match change with
                   | `Left _old_value ->
                     let old_node = Map.find_exn expert_nodes key in
                     Incremental.Expert.Node.invalidate old_node;
                     Incremental.Expert.Node.make_stale output_map_node;
                     Map.remove expert_nodes key, Map.remove output_map key
                   | `Right _new_value ->
                     let node =
                       Separate_state.create_lookup_node incremental_state state key
                     in
                     make_node_depend_on_input_map_changed node ~input_map_changed;
                     Incremental.Expert.Node.make_stale output_map_node;
                     ( Map.add_exn expert_nodes ~key ~data:node
                     , Map.add_exn
                         output_map
                         ~key
                         ~data:(Incremental.Expert.Node.watch node) )
                   | `Unequal (_old_value, _new_value) ->
                     Incremental.Expert.Node.make_stale
                       (Map.find_exn expert_nodes key);
                     expert_nodes, output_map)
             in
             state.input_map <- input_map;
             state.expert_nodes <- expert_nodes;
             state.output_map <- output_map))
      in
      make_node_depend_on_input_map_changed output_map_node ~input_map_changed;
      Incremental.Expert.Node.watch output_map_node)
  ;;

  (* Just for deriving structural equality. *)
  type 'a maybe_bound_structurally = 'a Maybe_bound.t =
    | Incl of 'a
    | Excl of 'a
    | Unbounded
  [@@deriving equal]

  let subrange
        (type k v cmp state_witness)
        ?(data_equal = phys_equal)
        (map_incr : ((k, v, cmp) Map.t, state_witness) Incremental.t)
        range
    =
    with_old2 map_incr range ~f:(fun ~old map range ->
      let compare = (Map.comparator map).compare in
      let equal l r = compare l r = 0 in
      let ( > ) a b = compare a b > 0
      and ( >= ) a b = compare a b >= 0 in
      let maybe_bound_equal a b : bool = equal_maybe_bound_structurally equal a b in
      let range_is_empty ~min ~max : bool =
        match min, max with
        | Unbounded, (Unbounded | Excl _ | Incl _) | (Excl _ | Incl _), Unbounded ->
          false
        | Incl min, Incl max -> min > max
        | Excl min, Excl max | Incl min, Excl max | Excl min, Incl max -> min >= max
      in
      let range_includes ~min ~max key : bool =
        Maybe_bound.is_lower_bound min ~of_:key ~compare
        && Maybe_bound.is_upper_bound max ~of_:key ~compare
      in
      match range with
      | None ->
        (* Empty new range means empty map *)
        Map.Using_comparator.empty ~comparator:(Map.comparator map)
      | Some ((min, max) as range) ->
        let from_scratch () = Map.subrange map ~lower_bound:min ~upper_bound:max in
        (match old with
         | None | Some (_, None, _) ->
           (* no old range *)
           from_scratch ()
         | Some (_, Some (old_min, old_max), _)
           when range_is_empty ~min:old_min ~max:old_max
             || range_is_empty ~min ~max:old_max
             || range_is_empty ~min:old_min ~max ->
           (* empty old range or old range disjoint with new *)
           from_scratch ()
         | Some (old_map, Some ((old_min, old_max) as old_range), old_res) ->
           with_return (fun { return } ->
             (* Returns true iff the key is in both new and old ranges *)
             let in_range_intersection key =
               range_includes ~min ~max key
               && range_includes ~min:old_min ~max:old_max key
             in
             (* Apply changes to keys which are in the intersection of both ranges.

                [outside] is the number of updates outside the range intersection that we
                tolerate before giving up and reconstructing based on the new range. This
                is an optimisation in the case that the map changes in a very big way, at
                which point computing based on the new range is cheaper.  *)
             let apply_diff_in_intersection (outside, map) (key, data) =
               if in_range_intersection key
               then (
                 match data with
                 | `Left _ -> outside, Map.remove map key
                 | `Right data | `Unequal (_, data) -> outside, Map.set map ~key ~data)
               else (
                 let outside = outside - 1 in
                 if Int.O.(outside < 0)
                 then return (from_scratch ())
                 else outside, Map.remove map key)
             in
             (* First update the keys in /both/ the old and the new range. *)
             let with_updated_values_in_intersection =
               (* Cutoff the big diff computation if we reach O(|submap|) number of
                  changes that are outside the range *)
               let outside_cutoff = Map.length old_res / 4 in
               Map.fold_symmetric_diff
                 ~data_equal
                 old_map
                 map
                 ~init:(outside_cutoff, old_res)
                 ~f:apply_diff_in_intersection
               |> snd
             in
             if Tuple2.equal
                  ~eq1:maybe_bound_equal
                  ~eq2:maybe_bound_equal
                  old_range
                  range
             then
               (* There are no keys to remove and everything in range is updated. *)
               with_updated_values_in_intersection
             else (
               (* Remove any keys which are not in the new range. *)
               let without_keys_out_of_range =
                 Map.subrange
                   with_updated_values_in_intersection
                   ~lower_bound:min
                   ~upper_bound:max
               in
               (* Add in any keys which are in the new range but not the old range. *)
               let with_new_keys_now_in_range =
                 let map_append_exn lower_part upper_part =
                   match Map.append ~lower_part ~upper_part with
                   | `Ok map -> map
                   | `Overlapping_key_ranges ->
                     failwith "impossible case: BUG in incr_map.ml subrange"
                 in
                 let lower_part =
                   match old_min with
                   | Unbounded ->
                     Map.Using_comparator.empty ~comparator:(Map.comparator map)
                   | Excl old_min ->
                     Map.subrange map ~lower_bound:min ~upper_bound:(Incl old_min)
                   | Incl old_min ->
                     Map.subrange map ~lower_bound:min ~upper_bound:(Excl old_min)
                 and upper_part =
                   match old_max with
                   | Unbounded ->
                     Map.Using_comparator.empty ~comparator:(Map.comparator map)
                   | Excl old_max ->
                     Map.subrange map ~lower_bound:(Incl old_max) ~upper_bound:max
                   | Incl old_max ->
                     Map.subrange map ~lower_bound:(Excl old_max) ~upper_bound:max
                 in
                 map_append_exn
                   lower_part
                   (map_append_exn without_keys_out_of_range upper_part)
               in
               with_new_keys_now_in_range))))
  ;;

  let rekey ?data_equal map_incr ~comparator:outer_comparator ~f =
    unordered_fold
      map_incr
      ?data_equal
      ~init:(Map.empty outer_comparator)
      ~revert_to_init_when_empty:true
      ~add:(fun ~key ~data output -> Map.add_exn output ~key:(f ~key ~data) ~data)
      ~remove:(fun ~key ~data output -> Map.remove output (f ~key ~data))
      ~update:(fun ~key ~old_data ~new_data output ->
        let prev_key = f ~key ~data:old_data in
        let new_key = f ~key ~data:new_data in
        if (Map.comparator output).compare prev_key new_key = 0
        then Map.set output ~key:new_key ~data:new_data
        else Map.remove output prev_key |> Map.add_exn ~key:new_key ~data:new_data)
  ;;

  let index_byi ?data_equal map_incr ~comparator:outer_comparator ~index =
    with_comparator map_incr (fun inner_comparator ->
      unordered_fold
        ?data_equal
        map_incr
        ~init:(Map.empty outer_comparator)
        ~revert_to_init_when_empty:true
        ~add:(fun ~key:inner_key ~data outer_map ->
          match index ~key:inner_key ~data with
          | None -> outer_map
          | Some outer_key ->
            Map.update outer_map outer_key ~f:(function
              | None ->
                Map.Using_comparator.singleton
                  inner_key
                  data
                  ~comparator:inner_comparator
              | Some inner_map -> Map.add_exn inner_map ~key:inner_key ~data))
        ~remove:(fun ~key:inner_key ~data outer_map ->
          match index ~key:inner_key ~data with
          | None -> outer_map
          | Some outer_key ->
            Map.change outer_map outer_key ~f:(function
              | None ->
                failwith "BUG: Hit supposedly impossible case in Incr_map.index_by"
              | Some inner_map ->
                let inner_map = Map.remove inner_map inner_key in
                if Map.is_empty inner_map then None else Some inner_map)))
  ;;

  let index_by ?data_equal map_incr ~comparator ~index =
    index_byi ?data_equal map_incr ~comparator ~index:(fun ~key:_ ~data -> index data)
  ;;


  (** Find two keys in map by index, O(n). We use just one fold (two Map.nth would use two)
      and optimize for keys close to either beginning or end by using either fold or
      fold_right.
  *)
  module Key_status = struct
    type 'k t =
      | Known of 'k
      | Known_none
      | Unknown

    let is_known = function
      | Unknown -> false
      | _ -> true
    ;;

    let to_option = function
      | Unknown | Known_none -> None
      | Known k -> Some k
    ;;
  end

  let find_key_range_linear (type k) ~from ~to_ (map : (k, _, _) Map.t)
    : (k * k option) option
    =
    let open Key_status in
    let len = Map.length map in
    let begin_key = if Int.( >= ) from len then Known_none else Unknown in
    let end_key = if Int.( >= ) to_ len then Known_none else Unknown in
    let find_keys fold ~start_pos ~advance_pos =
      with_return (fun { return } ->
        fold
          map
          ~init:(begin_key, end_key, start_pos)
          ~f:(fun ~key ~data:_ (begin_key, end_key, pos) ->
            let begin_key = if Int.( = ) pos from then Known key else begin_key in
            let end_key = if Int.( = ) pos to_ then Known key else end_key in
            if is_known begin_key && is_known end_key
            then return (begin_key, end_key, pos)
            else begin_key, end_key, advance_pos pos))
    in
    let begin_key, end_key, _ =
      (* Searching from left takes O(to_), from right - O(len - from), so select the
         smaller one. *)
      if to_ < len - from
      then find_keys Map.fold ~start_pos:0 ~advance_pos:(fun pos -> pos + 1)
      else find_keys Map.fold_right ~start_pos:(len - 1) ~advance_pos:(fun pos -> pos - 1)
    in
    Option.map (Key_status.to_option begin_key) ~f:(fun begin_key ->
      begin_key, Key_status.to_option end_key)
  ;;

  let nth_from_either_side (type k) n (map : (k, _, _) Map.t) : k option =
    Option.map ~f:fst (find_key_range_linear ~from:n ~to_:n map)
  ;;

  (** Find key [by] positions earlier/later in a map. Returns none if out of bounds. *)
  let rec offset (key : 'k) (map : ('k, _, _) Map.t) ~by : 'k option =
    if Int.( = ) by 0
    then Some key
    else (
      let closest_dir, add =
        if Int.( < ) by 0 then `Less_than, 1 else `Greater_than, -1
      in
      match Map.closest_key map closest_dir key with
      | None -> None
      | Some (key, _) -> offset key map ~by:(by + add))
  ;;

  (** Find how we need to move [key] if [changed_key] changed in the given
      way *)
  let find_offset ~compare ~key ~changed_key change =
    if Int.( < ) (compare changed_key key) 0
    then (
      match change with
      | `Left _ -> 1
      | `Right _ -> -1
      | _ -> 0)
    else 0
  ;;

  let rank
        (type k v cmp state_witness)
        (map : ((k, v, cmp) Map.t, state_witness) Incremental.t)
        (key : (k, state_witness) Incremental.t)
    =
    with_comparator map (fun comparator ->
      let compare_key = comparator.compare in
      let same_key a b = compare_key a b = 0 in
      let when_key_changed ~map ~old_key ~new_key ~old_rank =
        if compare_key new_key old_key < 0
        then (
          (* If the new key is smaller than the old key, find the size of the map subrange
             between them and subtract it from the previous rank *)
          let lower_bound, upper_bound = Excl new_key, Excl old_key in
          let subrange = Map.subrange map ~lower_bound ~upper_bound in
          old_rank - Map.length subrange - 1)
        else (
          (* Otherwise, the new key is larger than the old key, so find the size of the
             map subrange between them and add it to the previous rank *)
          let lower_bound, upper_bound = Excl old_key, Excl new_key in
          let subrange = Map.subrange map ~lower_bound ~upper_bound in
          old_rank + Map.length subrange + 1)
      in
      let when_map_changed ~old_map ~new_map ~key ~old_rank =
        Map.fold_symmetric_diff
          (* We don't care about the data, so optimize these checks *)
          ~data_equal:(fun _ _ -> true)
          old_map
          new_map
          ~init:old_rank
          ~f:(fun acc (diff_key, diff) ->
            match diff with
            | `Left _ when compare_key diff_key key < 0 -> acc - 1
            | `Right _ when compare_key diff_key key < 0 -> acc + 1
            | _ -> acc)
      in
      let rec process ~(old : ((k, v, _) Map.t * _ * _) option) new_map (new_key : k) =
        if not (Map.mem new_map new_key)
        then None
        else (
          match old with
          (* If the map and key are the same, just reuse the old rank *)
          | Some (old_map, old_key, old_rank)
            when phys_equal new_map old_map && same_key old_key new_key -> old_rank
          (* If the map is the same but the key changed *)
          | Some (old_map, old_key, Some old_rank) when phys_equal new_map old_map ->
            Some (when_key_changed ~map:new_map ~old_key ~new_key ~old_rank)
          (* If the key is the same but the map changed *)
          | Some (old_map, old_key, Some old_rank) when same_key new_key old_key ->
            Some (when_map_changed ~old_map ~new_map ~key:new_key ~old_rank)
          (* If both the map and the key changed, this can be simulated as the
             map changing followed by the key changing *)
          | Some (old_map, old_key, Some old_rank) ->
            (* We call [process] recursively instead of directly calling
               [when_map_changed] followed by [when_key_changed] since it might be the
               case that [old_key] is in [old_map] and [new_key] is in [new_map], but
               [old_key] is not in [new_map]. *)
            let old_rank =
              process ~old:(Some (old_map, old_key, Some old_rank)) new_map old_key
            in
            process ~old:(Some (new_map, old_key, old_rank)) new_map new_key
          (* If the previous key was not in the map or this is the first stabilization,
             compute the rank from scratch *)
          | Some (_, _, None) | None -> Map.rank new_map new_key)
      in
      with_old2 map key ~f:process)
  ;;

  (** Range map by indices *)
  let subrange_by_rank
        (type k state_witness)
        ?data_equal
        (map : ((k, _, _) Map.t, state_witness) Incremental.t)
        (range : (int Maybe_bound.t * int Maybe_bound.t, state_witness) Incremental.t)
    =
    let find_key_range (range : (int * int, state_witness) Incremental.t)
      : ((k * k option) option, state_witness) Incremental.t
      =
      with_old2 map range ~f:(fun ~old map (from, to_) ->
        (* This function returns no keys, only begin key, or begin and end keys.
           These are the keys at [from] and [to_] positions in the map, or None if the
           indices are too big. As always [0 <= from && from <= to_], there is no
           possibility of only [to_] being a valid position.
        *)
        if Int.( < ) to_ from || Int.( < ) from 0
        then raise_s [%message "Invalid indices" (from : int) (to_ : int)];
        match old with
        | Some (old_map, (old_from, old_to), Some (begin_key, end_key_opt)) ->
          let find_offset = find_offset ~compare:(Map.comparator map).compare in
          let range_offset_begin = from - old_from in
          let range_offset_end = to_ - old_to in
          let adjust_and_offset ~by key =
            let by = by + if by >= 0 && not (Map.mem map key) then 1 else 0 in
            offset key map ~by
          in
          (* We only care about the keys changing and not the data, so [data_equal] here
             can be always true *)
          let diff ~init ~f =
            Map.fold_symmetric_diff ~data_equal:(fun _ _ -> true) old_map map ~init ~f
          in
          let begin_key_opt, end_key_opt =
            match end_key_opt with
            | Some end_key ->
              let map_offset_begin, map_offset_end =
                diff ~init:(0, 0) ~f:(fun (offset_begin, offset_end) (key, change) ->
                  ( offset_begin + find_offset ~key:begin_key ~changed_key:key change
                  , offset_end + find_offset ~key:end_key ~changed_key:key change ))
              in
              ( adjust_and_offset begin_key ~by:(map_offset_begin + range_offset_begin)
              , adjust_and_offset end_key ~by:(map_offset_end + range_offset_end) )
            | None ->
              let map_offset_begin =
                diff ~init:0 ~f:(fun offset_begin (key, change) ->
                  offset_begin + find_offset ~key:begin_key ~changed_key:key change)
              in
              ( adjust_and_offset begin_key ~by:(map_offset_begin + range_offset_begin)
              , nth_from_either_side to_ map )
          in
          assert (Option.for_all ~f:(Map.mem map) begin_key_opt);
          assert (Option.for_all ~f:(Map.mem map) end_key_opt);
          Option.map begin_key_opt ~f:(fun begin_key -> begin_key, end_key_opt)
        | None | Some (_, _, None) ->
          (* On first run (when we have to) or when both the keys are none, run O(n)
             scan. This is fine for keys-are-none case as it happens when the positions
             are past end of the map, so they shouldn't be too far from end after the
             map changes, and [find_key_range_linear] is fast in such case. *)
          find_key_range_linear map ~from ~to_)
    in
    (* Handle different Maybe_bound cases and call find_key_range if necessary. It's
       nicer to do this here as opposed to making find_key_range even more complicated *)
    let open Incremental.Let_syntax in
    let ( >>> ) new_ bound = Maybe_bound.map ~f:(fun _ -> new_) bound in
    let return = Incremental.return (Incremental.state map) in
    let key_range =
      match%pattern_bind range with
      | Maybe_bound.Unbounded, Maybe_bound.Unbounded ->
        return (Some (Maybe_bound.Unbounded, Maybe_bound.Unbounded))
      | ( ((Maybe_bound.Incl l | Maybe_bound.Excl l) as lb)
        , ((Maybe_bound.Incl u | Maybe_bound.Excl u) as ub) ) ->
        let%map key_range = find_key_range (Incremental.both l u)
        and lb = lb
        and ub = ub in
        (match key_range with
         | Some (begin_key, Some end_key) -> Some (begin_key >>> lb, end_key >>> ub)
         | Some (begin_key, None) -> Some (begin_key >>> lb, Unbounded)
         | None -> None)
      | ((Maybe_bound.Incl l | Maybe_bound.Excl l) as lb), Maybe_bound.Unbounded ->
        let%map key_range = find_key_range (Incremental.both l l)
        and lb = lb in
        (match key_range with
         | Some (key, _) -> Some (key >>> lb, Unbounded)
         | None -> None)
      | Maybe_bound.Unbounded, ((Maybe_bound.Incl u | Maybe_bound.Excl u) as ub) ->
        let%map key_range = find_key_range (Incremental.both u u)
        and ub = ub in
        (match key_range with
         | Some (key, _) -> Some (Unbounded, key >>> ub)
         | None -> None)
    in
    subrange ?data_equal map key_range
  ;;

  let transpose
    : type k1 k2 v k1_cmp k2_cmp state_witness.
      ?data_equal:(v -> v -> bool)
      -> (k2, k2_cmp) Map.comparator
      -> ((k1, (k2, v, k2_cmp) Map.t, k1_cmp) Map.t, state_witness) Incremental.t
      -> ((k2, (k1, v, k1_cmp) Map.t, k2_cmp) Map.t, state_witness) Incremental.t
    =
    fun ?(data_equal = phys_equal) k2_comparator m ->
      with_comparator m (fun k1_comparator ->
        let update
          :  key:k1 -> old_data:(k2, v, k2_cmp) Map.t -> new_data:(k2, v, k2_cmp) Map.t
            -> (k2, (k1, v, k1_cmp) Map.t, k2_cmp) Map.t
            -> (k2, (k1, v, k1_cmp) Map.t, k2_cmp) Map.t
          =
          fun ~key:k1 ~old_data ~new_data acc ->
            Map.fold_symmetric_diff
              old_data
              new_data
              ~data_equal
              ~init:acc
              ~f:(fun acc (k2, diff) ->
                let value =
                  match diff with
                  | `Left _ -> None
                  | `Right x | `Unequal (_, x) -> Some x
                in
                Map.change acc k2 ~f:(fun acc_inner ->
                  let acc_inner =
                    Map.change
                      (Option.value
                         acc_inner
                         ~default:(Map.Using_comparator.empty ~comparator:k1_comparator))
                      k1
                      ~f:(fun _ -> value)
                  in
                  if Map.is_empty acc_inner then None else Some acc_inner))
        in
        let add ~key ~data =
          update ~key ~old_data:(Map.empty k2_comparator) ~new_data:data
        in
        let remove ~key ~data =
          update ~key ~old_data:data ~new_data:(Map.empty k2_comparator)
        in
        unordered_fold
          m
          ~init:(Map.empty k2_comparator)
          ~revert_to_init_when_empty:true
          ~update
          ~add
          ~remove)
  ;;

  let collapse_by
        (type outer_key outer_cmp inner_key inner_cmp combined_key combined_cmp)
        ?data_equal
        (map_incr :
           ((outer_key, (inner_key, _, inner_cmp) Map.t, outer_cmp) Map.t, _) Incremental.t)
        ~(merge_keys : outer_key -> inner_key -> combined_key)
        ~(comparator : (combined_key, combined_cmp) Map.comparator)
    =
    unordered_fold_nested_maps
      ?data_equal
      map_incr
      ~init:(Map.empty comparator)
      ~revert_to_init_when_empty:true
      ~update:(fun ~outer_key ~inner_key ~old_data:_ ~new_data acc ->
        Map.set acc ~key:(merge_keys outer_key inner_key) ~data:new_data)
      ~add:(fun ~outer_key ~inner_key ~data acc ->
        Map.add_exn acc ~key:(merge_keys outer_key inner_key) ~data)
      ~remove:(fun ~outer_key ~inner_key ~data:_ acc ->
        Map.remove acc (merge_keys outer_key inner_key))
  ;;

  let collapse
        (type outer_key outer_cmp inner_key inner_cmp)
        ?data_equal
        (map_incr :
           ((outer_key, (inner_key, _, inner_cmp) Map.t, outer_cmp) Map.t, _) Incremental.t)
        ~comparator:(inner_comparator : (inner_key, inner_cmp) Map.comparator)
    =
    with_comparator map_incr (fun outer_comparator ->
      let module Cmp = struct
        type t = outer_key * inner_key
        type comparator_witness = (outer_cmp, inner_cmp) Tuple2.comparator_witness

        let comparator =
          let inner_comparator =
            let module M = (val inner_comparator) in
            M.comparator
          in
          Tuple2.comparator outer_comparator inner_comparator
        ;;
      end
      in
      collapse_by
        ?data_equal
        map_incr
        ~merge_keys:Tuple2.create
        ~comparator:(module Cmp))
  ;;

  let expand ?data_equal map_incr ~outer_comparator ~inner_comparator =
    unordered_fold
      ?data_equal
      map_incr
      ~init:(Map.empty outer_comparator)
      ~revert_to_init_when_empty:true
      ~update:(fun ~key:(outer_key, inner_key) ~old_data:_ ~new_data acc ->
        Map.update acc outer_key ~f:(function
          | None -> Map.singleton inner_comparator inner_key new_data
          | Some map -> Map.set map ~key:inner_key ~data:new_data))
      ~add:(fun ~key:(outer_key, inner_key) ~data acc ->
        Map.update acc outer_key ~f:(function
          | None -> Map.singleton inner_comparator inner_key data
          | Some map -> Map.add_exn map ~key:inner_key ~data))
      ~remove:(fun ~key:(outer_key, inner_key) ~data:_ acc ->
        Map.change acc outer_key ~f:(function
          | None -> None
          | Some map ->
            let map = Map.remove map inner_key in
            Option.some_if (not (Map.is_empty map)) map))
  ;;

  let counti ?data_equal map_incr ~f =
    unordered_fold
      ?data_equal
      map_incr
      ~init:0
      ~revert_to_init_when_empty:true
      ~add:(fun ~key ~data count -> if f ~key ~data then count + 1 else count)
      ~remove:(fun ~key ~data count -> if f ~key ~data then count - 1 else count)
  ;;

  let count ?data_equal map_incr ~f =
    counti ?data_equal map_incr ~f:(fun ~key:_ ~data -> f data)
  ;;

  let existsi ?data_equal map_incr ~f =
    Incremental.map (counti ?data_equal map_incr ~f) ~f:(fun count -> count <> 0)
  ;;

  let exists ?data_equal map_incr ~f =
    existsi ?data_equal map_incr ~f:(fun ~key:_ ~data -> f data)
  ;;

  let sum
        (type u)
        ?data_equal
        (map_incr : ((_, _, _) Map.t, _) Incremental.t)
        (module Group : Abstract_algebra.Commutative_group.Without_sexp with type t = u)
        ~f
    =
    unordered_fold
      ?data_equal
      map_incr
      ~init:Group.zero
      ~revert_to_init_when_empty:true
      ~add:(fun ~key:_ ~data:v acc -> Group.( + ) acc (f v))
      ~remove:(fun ~key:_ ~data:v acc -> Group.( - ) acc (f v))
  ;;

  let for_alli ?data_equal map_incr ~f =
    Incremental.map
      (counti ?data_equal map_incr ~f:(fun ~key ~data -> not (f ~key ~data)))
      ~f:(fun count -> count = 0)
  ;;

  let for_all ?data_equal map_incr ~f =
    for_alli ?data_equal map_incr ~f:(fun ~key:_ ~data -> f data)
  ;;

  module For_testing = struct
    let find_key_range_linear = find_key_range_linear
  end

  module Lookup = struct
    type ('v, 'w) entry =
      { mutable saved_value : 'v option
      ; node : ('v option, 'w) Incremental.Expert.Node.t
      }

    type ('k, 'v, 'cmp, 'w) t =
      { mutable saved_map : ('k, 'v, 'cmp) Map.t
      (* We may have multiple entries per key if nodes become necessary again after being
         removed. *)
      ; mutable lookup_entries : ('k, ('v, 'w) entry list, 'cmp) Map.t
      ; updater_node : (unit, 'w) Incremental.t
      ; scope : 'w Incremental.Scope.t
      }

    module M (K : sig
        type t
        type comparator_witness
      end) =
    struct
      type nonrec ('v, 'w) t = (K.t, 'v, K.comparator_witness, 'w) t
    end

    let create ?(data_equal = phys_equal) input_map ~comparator =
      let rec self =
        lazy
          (let updater_node =
             Incremental.map input_map ~f:(fun input_map ->
               let (lazy self) = self in
               Map.fold_symmetric_diff
                 self.saved_map
                 input_map
                 ~data_equal
                 ~init:()
                 ~f:(fun () (key, changed_value) ->
                   let entries = Map.find_multi self.lookup_entries key in
                   List.iter entries ~f:(fun entry ->
                     entry.saved_value
                     <- (match changed_value with
                       | `Left _ -> None
                       | `Right new_value | `Unequal (_, new_value) ->
                         Some new_value);
                     Incremental.Expert.Node.make_stale entry.node));
               self.saved_map <- input_map)
           in
           let empty_map = Map.Using_comparator.empty ~comparator in
           { saved_map = empty_map
           ; lookup_entries = empty_map
           ; updater_node
           ; scope = Incremental.Scope.current (Incremental.state input_map) ()
           })
      in
      Lazy.force self
    ;;

    let[@cold] slow_path_link_entry t entry ~key ~is_now_observable =
      let (lazy entry) = entry in
      let current_entries = Map.find_multi t.lookup_entries key in
      let is_linked = List.exists current_entries ~f:(phys_equal entry) in
      if Bool.equal is_linked is_now_observable
      then ()
      else if is_now_observable
      then
        t.lookup_entries
        <- Map.update t.lookup_entries key ~f:(function
          | Some (other_entry :: _ as other_entries) ->
            (* Update this entry's value to be current. *)
            entry.saved_value <- other_entry.saved_value;
            entry :: other_entries
          | None | Some [] ->
            entry.saved_value <- Map.find t.saved_map key;
            [ entry ])
      else (
        let new_entries =
          List.filter current_entries ~f:(fun x -> not (phys_equal entry x))
        in
        t.lookup_entries
        <- (if List.is_empty new_entries
            then Map.remove t.lookup_entries key
            else Map.set t.lookup_entries ~key ~data:new_entries))
    ;;

    let[@cold] slow_path_create_node t key =
      let incremental_state = Incremental.state t.updater_node in
      Incremental.Scope.within incremental_state t.scope ~f:(fun () ->
        let rec entry =
          lazy
            { saved_value = Map.find t.saved_map key
            ; node =
                Incremental.Expert.Node.create
                  incremental_state
                  (fun () -> (force entry).saved_value)
                  ~on_observability_change:(slow_path_link_entry t entry ~key)
            }
        in
        let (lazy entry) = entry in
        Incremental.Expert.Node.add_dependency
          entry.node
          (Incremental.Expert.Dependency.create t.updater_node);
        Incremental.Expert.Node.watch entry.node)
    ;;

    let find t key =
      match Map.find_multi t.lookup_entries key with
      | entry :: _ -> Incremental.Expert.Node.watch entry.node
      | [] -> slow_path_create_node t key
    ;;

    module For_debug = struct
      let[@cold] sexp_of_entry sexp_of_value entry =
        let { saved_value; node } = entry in
        let node = Incremental.Expert.Node.watch node in
        [%sexp
          { saved_value : value option
          ; node_info = (Incremental.user_info node : (Info.t option[@sexp.option]))
          ; node_is_const =
              (Option.some_if (Incremental.is_const node) () : (unit option[@sexp.option]))
          ; node_is_invalid =
              (Option.some_if (not (Incremental.is_valid node)) () : (unit option
                                                                      [@sexp.option]))
          ; node_is_unnecessary =
              (Option.some_if (not (Incremental.is_necessary node)) () : (unit option
                                                                          [@sexp.option]))
          }]
      ;;

      let[@cold] sexp_of_t sexp_of_key sexp_of_value t =
        let info_per_key =
          Map.merge t.saved_map t.lookup_entries ~f:(fun ~key data ->
            let actual_value, entries =
              match data with
              | `Left x -> Some x, []
              | `Right y -> None, y
              | `Both (x, y) -> Some x, y
            in
            Some
              [%sexp
                { key : key
                ; actual_value : (value option[@sexp.option])
                ; entries : value entry list
                }])
        in
        Sexp.List (Map.data info_per_key)
      ;;
    end
  end
end

module type S = sig
  type state_witness

  include
    S_gen
    with type 'a Incr.t = ('a, state_witness) Incremental.t
     and type 'a Incr.Cutoff.t = 'a Incremental.Cutoff.t
     and type ('k, 'v, 'cmp) Lookup.t = ('k, 'v, 'cmp, state_witness) Generic.Lookup.t
end

module Make (Incr : Incremental.S) = struct
  include Generic

  let flatten x = flatten Incr.State.t x

  module Lookup = struct
    include Lookup

    type ('k, 'v, 'cmp) t = ('k, 'v, 'cmp, Incr.state_witness) Lookup.t

    module M (K : sig
        type t
        type comparator_witness
      end) : sig
      type nonrec 'v t = (K.t, 'v, K.comparator_witness) t
    end = struct
      type nonrec 'v t = (K.t, 'v, K.comparator_witness) t
    end
  end
end

include Generic
OCaml

Innovation. Community. Security.