qrcode-barcode-scanner 0.12.3

QR code decoding and scannability scoring for artistic, AI-generated, and photo-captured QR codes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
//! Collect, then reconcile (07-pipeline property 6 § Reconciliation).
//!
//! A walk collects reads: every engine hit of every attempt, upgraded by the
//! scanner's own parses (the re-read), the candidate parses of grids rqrr
//! detected but did not decode (no engine), and the S5 rescue. Identical
//! reads fold into one. Refusals are records of refused streams — one per
//! stream and names, wherever and however often it was refused; what they
//! withhold, and what protects a read from them, is evaluated over the
//! whole walk — so retroactively — whenever the walk asks what stands.
//! [`ReadStore::reconcile`] turns what stands into the distinct contents
//! the scanner can tell apart — a function of the read set, never of the
//! order the reads arrived in:
//!
//! 1. a candidate parse joins only when it contains an engine read of its
//!    bytes with an agreeing account;
//! 2. per (symbology, raw): located and no-ECI reads with an observed FNC1
//!    position form one detection per (evidence, position); a signalled
//!    read joins the one located detection of its position that contains
//!    it, or the group's signalled detection when there is no located one;
//!    reads with unobserved evidence, then reads with an unobserved
//!    position, join the one agreeing detection that contains them, else
//!    form one detection per published identity;
//! 3. a detection no engine read belongs to is not published; at most
//!    [`MAX_DETECTIONS`], QR family first, then by earliest read.
//!
//! What stands is first weighed by the acceptance (07-pipeline
//! § Acceptance, [`admitted`]): a read of a symbology the readers invent
//! from noise waits for a read of its bytes on a plane of independent
//! scale, and a QR content whose every measured correction margin is
//! negative is refused, its reads of unknown margin with it. A held read
//! stays in the walk — it protects agreeing reads and keeps re-derivation
//! running — but it neither settles a stage nor stops S5, and it is never
//! published.

#[cfg(all(test, feature = "engine-rxing"))]
use super::Grid;
use super::content::{ContentKey, DoubleByteSpan, EciEvidence, EngineReading, Fnc1, Segments};
use super::occurrence::{self, Observation};
use super::{MaskedStream, MatrixRegion, Refusal};
use crate::report::{EcLevel, EngineKind, Point, Symbology};
use crate::rescue::Margin;

/// Distinct reads a walk reconciles; later distinct reads are counted.
pub(crate) const MAX_DISTINCT_READS: usize = 64;

/// Detections a report publishes — a tile of thousands of micro-QRs is a
/// CPU/report amplifier, not a use case.
pub(crate) const MAX_DETECTIONS: usize = 16;

/// Refused streams a walk remembers for the whole scan: one record per
/// stream and pair of names (rqrr's decode names both; the re-read and a
/// re-derived grid name the port's reading only), wherever and however
/// often the stream was refused.
pub(crate) const MAX_REFUSALS: usize = 16;

/// Records past [`MAX_REFUSALS`]: each withholds what was collected up to
/// the latest attempt that refused its stream, then lapses; past them the
/// walk fails closed. The opposite-background walk receives them all.
const MAX_OVERFLOW: usize = MAX_REFUSALS;

/// Distinct places a folded read or a refusal record remembers — distinct
/// symbols: a place whose centre lies inside a recorded one is the same
/// symbol ([`recorded`]). A refusal withholds a read only when every place
/// it was read at lies inside the refused symbol; a record refused at more
/// places withholds wherever its names match.
const MAX_PLACES: usize = 8;

/// Where a read sits in the walk: its attempt, its source within the
/// attempt ([`source`]) and its index in that source's output.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Rank {
    /// Attempt index in walk order.
    pub(crate) attempt: u32,
    /// The source within the attempt.
    pub(crate) source: u8,
    /// Index within the source's output.
    pub(crate) index: u32,
}

/// The sources a [`Rank`] orders, within an attempt.
pub(crate) mod source {
    /// rxing's reads.
    pub(crate) const RXING: u8 = 0;
    /// rqrr's reads.
    pub(crate) const RQRR: u8 = 1;
    /// Candidate parses (grids rqrr detected, re-derived by the scanner).
    pub(crate) const CANDIDATE: u8 = 2;
    /// The S5 rescue.
    pub(crate) const RESCUE: u8 = 3;
}

/// What a read measured of its symbol (original image space).
#[derive(Debug, Clone, Default)]
pub(crate) struct Geometry {
    /// Symbol corners (rqrr, candidates, the rescue) and whether the
    /// attempt that measured them inverted the symbol photometrically.
    pub(crate) corners: Option<([Point; 4], bool)>,
    /// The verified stream with the version, EC level and mask read from
    /// it — the synthetic-UEC input, adopted as a whole.
    pub(crate) stream: Option<(MaskedStream, u8, EcLevel, u8)>,
    /// An EC level stated without a stream (rxing's port).
    pub(crate) ec: Option<EcLevel>,
}

/// One read of the walk.
#[derive(Debug, Clone)]
pub(crate) struct Read {
    /// The symbology it was read as.
    pub(crate) symbology: Symbology,
    /// Its bytes, ECI evidence and FNC1 position.
    pub(crate) key: ContentKey,
    /// The Kanji/Hanzi segments a parse of it observed.
    pub(crate) segments: Segments,
    /// The decoding engine's mode-aware reading (rule 1e).
    pub(crate) engine_reading: EngineReading,
    /// Engines that delivered it, each with its earliest rank; empty: a
    /// candidate parse.
    pub(crate) engines: Vec<(EngineKind, Rank)>,
    /// Its earliest rank.
    pub(crate) rank: Rank,
    /// The last attempt it was read in.
    pub(crate) last_attempt: u32,
    /// The corner points of the symbol it was first read at (internal).
    pub(crate) location: Option<[Point; 4]>,
    /// Every distinct symbol it was read at (at most [`MAX_PLACES`]).
    pub(crate) places: Vec<[Point; 4]>,
    /// Actual outer-corner observations, independent of published geometry,
    /// each with its symbol's grid where a reader measured it.
    /// Three-finder reconstructions never enter this bounded collection.
    pub(crate) matrix_regions: Vec<MatrixRegion>,
    /// The validated footprints its reads measured (original space): the
    /// earliest [`occurrence::MAX_OBSERVATIONS`] distinct ones by rank,
    /// grouped into the published occurrences. Separate from `places`,
    /// which only keep refusals and joins apart.
    pub(crate) observations: Vec<Observation>,
    /// A footprint was left out past [`occurrence::MAX_OBSERVATIONS`].
    pub(crate) observations_dropped: bool,
    /// What it measured.
    pub(crate) geometry: Geometry,
    /// The ranks of the reads its location and geometry parts come from.
    sources: Sources,
    /// The scanner's own parser read its stream.
    pub(crate) parsed: bool,
    /// The best correction margin a read folded into this one measured on
    /// its own received word (`Unknown`: none did).
    pub(crate) margin: Margin,
    /// Its symbology is one the readers invent from noise: it waits for a
    /// read of its bytes on a plane of independent scale (cleared when a
    /// read of it that needs none folds in).
    pub(crate) needs_confirmation: bool,
    /// The least and the greatest longest side of the planes it was read
    /// on (`None`: none recorded).
    pub(crate) scales: Option<(u32, u32)>,
}

/// Which read of a fold supplied each part: every part comes from the
/// earliest read that carries it.
#[derive(Debug, Clone, Copy)]
struct Sources {
    location: Rank,
    corners: Rank,
    stream: Rank,
    ec: Rank,
}

impl Read {
    /// A read by `engine` (`None`: a candidate parse) at `rank`.
    pub(crate) fn new(
        symbology: Symbology,
        key: ContentKey,
        engine: Option<EngineKind>,
        rank: Rank,
    ) -> Self {
        Self {
            symbology,
            key,
            segments: Segments::NotObserved,
            engine_reading: EngineReading::None,
            engines: engine.map(|engine| (engine, rank)).into_iter().collect(),
            rank,
            last_attempt: rank.attempt,
            location: None,
            places: Vec::new(),
            matrix_regions: Vec::new(),
            observations: Vec::new(),
            observations_dropped: false,
            geometry: Geometry::default(),
            sources: Sources {
                location: rank,
                corners: rank,
                stream: rank,
                ec: rank,
            },
            parsed: false,
            margin: Margin::Unknown,
            needs_confirmation: false,
            scales: None,
        }
    }

    /// The read, made at `location`.
    pub(crate) fn at(mut self, location: Option<[Point; 4]>) -> Self {
        self.location = location;
        self.places = location.into_iter().collect();
        self
    }

    /// Same fold identity: identical in symbology, raw, FNC1, evidence,
    /// segments and engine reading.
    fn identical(&self, other: &Self) -> bool {
        self.symbology == other.symbology
            && self.key == other.key
            && self.segments == other.segments
            && self.engine_reading == other.engine_reading
    }

    /// Fold an identical read in: engines united (earliest rank each), the
    /// earliest rank, every symbol it was read at; the location and each
    /// geometry part from the earliest read that carries it.
    fn fold(&mut self, other: Self) {
        for (engine, rank) in other.engines {
            match self.engines.iter_mut().find(|(own, _)| *own == engine) {
                Some((_, own)) => *own = (*own).min(rank),
                None => self.engines.push((engine, rank)),
            }
        }
        self.rank = self.rank.min(other.rank);
        self.last_attempt = self.last_attempt.max(other.last_attempt);
        // one symbol read at every attempt scale is one place: the slots
        // stay for other symbols (a twin elsewhere)
        for place in other.places {
            if self.places.len() < MAX_PLACES && !recorded(&self.places, place) {
                self.places.push(place);
            }
        }
        // the masks of the non-QR search: one per copy it may publish
        for region in other.matrix_regions {
            record_region(
                &mut self.matrix_regions,
                region,
                occurrence::MAX_OCCURRENCES,
            );
        }
        self.observations_dropped |= other.observations_dropped
            | occurrence::merge(&mut self.observations, other.observations);
        let take = |own: bool, own_rank: Rank, theirs: bool, their_rank: Rank| {
            theirs && (!own || their_rank < own_rank)
        };
        let (own, theirs) = (self.sources, other.sources);
        if take(
            self.location.is_some(),
            own.location,
            other.location.is_some(),
            theirs.location,
        ) {
            self.location = other.location;
            self.sources.location = theirs.location;
        }
        if take(
            self.geometry.corners.is_some(),
            own.corners,
            other.geometry.corners.is_some(),
            theirs.corners,
        ) {
            self.geometry.corners = other.geometry.corners;
            self.sources.corners = theirs.corners;
        }
        if take(
            self.geometry.stream.is_some(),
            own.stream,
            other.geometry.stream.is_some(),
            theirs.stream,
        ) {
            self.geometry.stream = other.geometry.stream;
            self.sources.stream = theirs.stream;
        }
        if take(
            self.geometry.ec.is_some(),
            own.ec,
            other.geometry.ec.is_some(),
            theirs.ec,
        ) {
            self.geometry.ec = other.geometry.ec;
            self.sources.ec = theirs.ec;
        }
        self.parsed |= other.parsed;
        self.margin = self.margin.best(other.margin);
        self.needs_confirmation &= other.needs_confirmation;
        self.scales = match (self.scales, other.scales) {
            (Some((lo, hi)), Some((their_lo, their_hi))) => {
                Some((lo.min(their_lo), hi.max(their_hi)))
            }
            (scales, None) | (None, scales) => scales,
        };
    }

    /// An engine delivered it (it is no candidate parse).
    fn by_engine(&self) -> bool {
        !self.engines.is_empty()
    }
}

/// What a refusal does to one read.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Cover {
    /// Nothing.
    Nothing,
    /// Withholds rqrr's part of it.
    Rqrr,
    /// Withholds all of it.
    Whole,
}

/// Whether `place` is a recorded symbol: its centre lies inside the box of
/// one of `places`.
fn recorded(places: &[[Point; 4]], place: [Point; 4]) -> bool {
    places.iter().any(|&own| inside(Some(place), Some(own)))
}

/// Record `region` in `regions` (at most `cap`) unless it is a recorded
/// symbol. The corners first recorded stay, the mask of the non-QR search;
/// where they have no grid and `region` measured the same symbol (each
/// centre inside the other's box, sides within a quarter of each other),
/// its grid joins them: rqrr measures the grid of a symbol rxing read first
/// (07-pipeline property 8).
fn record_region(regions: &mut Vec<MatrixRegion>, region: MatrixRegion, cap: usize) {
    let mut corners = regions.iter().map(|own| own.corners);
    let Some(at) = corners.position(|own| inside(Some(region.corners), Some(own))) else {
        if regions.len() < cap {
            regions.push(region);
        }
        return;
    };
    let own = &mut regions[at];
    if own.grid.is_none() && same_symbol(own.corners, region.corners) {
        own.grid = region.grid;
    }
}

/// Whether two measured outlines are one symbol's: each centre inside the
/// other's box, and the mean sides within a quarter of each other.
fn same_symbol(a: [Point; 4], b: [Point; 4]) -> bool {
    let side = |quad: [Point; 4]| {
        (0..4)
            .map(|i| {
                let (p, q) = (quad[i], quad[(i + 1) % 4]);
                (q.x - p.x).hypot(q.y - p.y)
            })
            .sum::<f32>()
            / 4.0
    };
    let (a_side, b_side) = (side(a), side(b));
    inside(Some(a), Some(b))
        && inside(Some(b), Some(a))
        && a_side <= 1.25 * b_side
        && b_side <= 1.25 * a_side
}

/// A refused stream as the walk remembers it: its names, the symbols it was
/// refused at, and the latest attempt that refused it.
#[derive(Debug, Clone)]
struct Refused {
    /// What rxing's port reports for the stream.
    port: Option<ContentKey>,
    /// The bytes rqrr decoded from it.
    rqrr: Option<Vec<u8>>,
    /// The distinct symbols it was refused at (at most [`MAX_PLACES`]);
    /// `None`: anywhere — refused without a place, or at more symbols.
    places: Option<Vec<[Point; 4]>>,
    /// The latest attempt that refused it.
    attempt: u32,
}

impl Refused {
    fn new(refusal: Refusal, attempt: u32) -> Self {
        Self {
            port: refusal.port,
            rqrr: refusal.rqrr,
            places: refusal.place.map(|place| vec![place]),
            attempt,
        }
    }

    /// The same names: the same refused stream, wherever it was refused.
    fn names(&self, refusal: &Refusal) -> bool {
        self.port == refusal.port && self.rqrr == refusal.rqrr
    }

    /// The stream refused again, at `place` in `attempt`.
    fn again(&mut self, place: Option<[Point; 4]>, attempt: u32) {
        self.attempt = self.attempt.max(attempt);
        let Some(place) = place else {
            self.places = None;
            return;
        };
        if let Some(places) = &mut self.places {
            if recorded(places, place) {
                return;
            }
            if places.len() < MAX_PLACES {
                places.push(place);
                return;
            }
        }
        // refused at more symbols than it remembers: anywhere (fail closed)
        self.places = None;
    }

    /// Whether a read made at `place` lies inside the refused symbol.
    fn holds(&self, place: [Point; 4]) -> bool {
        self.places
            .as_ref()
            .is_none_or(|places| recorded(places, place))
    }

    /// Whether it refused the read's content: the port's account agrees, or
    /// rqrr decoded the same bytes.
    fn names_content(&self, read: &Read) -> bool {
        self.port
            .as_ref()
            .is_some_and(|port| port.agrees(&read.key))
            || self
                .rqrr
                .as_ref()
                .is_some_and(|bytes| *bytes == read.key.raw)
    }

    /// The refusals that record it again: one per place, or one without a
    /// place when it withholds anywhere.
    fn refusals(&self) -> Vec<Refusal> {
        let refusal = |place| Refusal {
            port: self.port.clone(),
            rqrr: self.rqrr.clone(),
            place,
            grid: None,
        };
        match &self.places {
            Some(places) => places.iter().map(|&place| refusal(Some(place))).collect(),
            None => vec![refusal(None)],
        }
    }
}

/// What a refusal record withholds of a read: an unparsed QR Code read
/// lying inside the refused symbol — every place it was read at inside one
/// of the record's — whose account the port name matches (all of it), or
/// of rqrr's bytes (rqrr's part). A read or a record without a place never
/// fails the place test.
fn cover(refused: &Refused, read: &Read) -> Cover {
    if read.symbology != Symbology::QrCode || read.parsed {
        return Cover::Nothing;
    }
    if !read.places.iter().all(|&place| refused.holds(place)) {
        return Cover::Nothing;
    }
    if refused
        .port
        .as_ref()
        .is_some_and(|port| port.agrees(&read.key))
    {
        Cover::Whole
    } else if refused
        .rqrr
        .as_ref()
        .is_some_and(|bytes| *bytes == read.key.raw)
        && read.engines.iter().any(|(e, _)| *e == EngineKind::Rqrr)
    {
        Cover::Rqrr
    } else {
        Cover::Nothing
    }
}

/// What a walk hands the opposite-background walk: its refusal records
/// (those past capacity included) as refusals, whether it failed closed,
/// the parsed contents that protect reads, and the re-reads the scan
/// already spent.
#[derive(Debug, Clone, Default)]
pub(crate) struct Seed {
    /// Every refusal record of the walk, one refusal per place (one
    /// without a place when the record withholds anywhere).
    pub(crate) refusals: Vec<Refusal>,
    /// The walk failed closed: refused streams past every record.
    pub(crate) closed: bool,
    /// Contents a parse of the walk read: no refusal withholds a read they
    /// agree with.
    pub(crate) protection: Vec<(Symbology, ContentKey)>,
    /// Re-reads already spent by the scan.
    pub(crate) rereads: usize,
}

/// The reads of one walk.
#[derive(Debug, Default)]
pub(crate) struct ReadStore {
    reads: Vec<Read>,
    /// Distinct reads past [`MAX_DISTINCT_READS`]: counted, not reconciled.
    pub(crate) dropped: u32,
    /// Refused streams within capacity: they withhold across the whole walk.
    refusals: Vec<Refused>,
    /// Refused streams past capacity: each withholds what was read up to the
    /// latest attempt that refused it.
    overflow: Vec<Refused>,
    /// Refused streams past every record: the walk fails closed — every
    /// unparsed QR Code read collected up to this attempt is withheld.
    closed: Option<u32>,
    /// Another walk's parsed contents.
    protection: Vec<(Symbology, ContentKey)>,
}

impl ReadStore {
    /// A store that starts from another walk's refusals — recorded by the
    /// rule of [`ReadStore::refuse`], each withholding for this whole walk —
    /// its failing closed (for this whole walk too) and its protection.
    pub(crate) fn seeded(seed: Seed) -> Self {
        let mut store = Self {
            protection: seed.protection,
            closed: seed.closed.then_some(u32::MAX),
            ..Self::default()
        };
        for refusal in seed.refusals {
            store.refuse(refusal, u32::MAX);
        }
        store
    }

    /// Collect one read: folded into an identical read, or kept while
    /// capacity lasts (later distinct reads are counted).
    pub(crate) fn collect(&mut self, read: Read) {
        if let Some(own) = self.reads.iter_mut().find(|own| own.identical(&read)) {
            own.fold(read);
        } else if self.reads.len() < MAX_DISTINCT_READS {
            self.reads.push(read);
        } else {
            self.dropped = self.dropped.saturating_add(1);
        }
    }

    /// Record a refused stream found in `attempt`: the record of its names
    /// takes the place and the attempt; new names take a record while
    /// capacity lasts; past every record the walk fails closed up to
    /// `attempt`.
    pub(crate) fn refuse(&mut self, refusal: Refusal, attempt: u32) {
        if let Some(own) = self
            .refusals
            .iter_mut()
            .chain(self.overflow.iter_mut())
            .find(|own| own.names(&refusal))
        {
            own.again(refusal.place, attempt);
        } else if self.refusals.len() < MAX_REFUSALS {
            self.refusals.push(Refused::new(refusal, attempt));
        } else if self.overflow.len() < MAX_OVERFLOW {
            self.overflow.push(Refused::new(refusal, attempt));
        } else {
            self.closed = Some(self.closed.map_or(attempt, |closed| closed.max(attempt)));
        }
    }

    /// Whether a parsed read, a candidate parse or a seeded content of the
    /// same symbology agrees with the read: then no refusal withholds it.
    fn protected(&self, read: &Read) -> bool {
        self.reads
            .iter()
            .any(|p| p.parsed && p.symbology == read.symbology && p.key.agrees(&read.key))
            || self
                .protection
                .iter()
                .any(|(symbology, key)| *symbology == read.symbology && key.agrees(&read.key))
    }

    /// The read as it stands: `None` when withheld; rqrr's part removed when
    /// only that is withheld.
    fn standing(&self, read: &Read) -> Option<Read> {
        // a parse of its content protects the read from being withheld, not
        // its refused copies (below)
        let protected = self.protected(read);
        if !protected
            && self
                .closed
                .is_some_and(|closed| read.last_attempt <= closed)
            && read.symbology == Symbology::QrCode
            && !read.parsed
        {
            return None;
        }
        let refusals = || {
            self.refusals.iter().chain(
                self.overflow
                    .iter()
                    .filter(|refused| read.last_attempt <= refused.attempt),
            )
        };
        let mut rqrr_withheld = false;
        if !protected {
            for refusal in refusals() {
                match cover(refusal, read) {
                    Cover::Whole => return None,
                    Cover::Rqrr => rqrr_withheld = true,
                    Cover::Nothing => {}
                }
            }
        }
        let mut read = read.clone();
        if rqrr_withheld {
            read.engines
                .retain(|(engine, _)| *engine != EngineKind::Rqrr);
            read.rank = read.engines.iter().map(|(_, rank)| *rank).min()?;
        }
        // an unparsed QR Code read standing by its other places, or on a
        // parse made elsewhere, publishes no copy where a placed refusal of
        // its content holds: that copy's stream was proved malformed
        if read.symbology == Symbology::QrCode && !read.parsed {
            let holding: Vec<&Refused> = refusals()
                .filter(|refused| refused.places.is_some() && refused.names_content(&read))
                .collect();
            read.observations.retain(|observation| {
                !observation.footprint.centre().is_some_and(|(x, y)| {
                    #[expect(
                        clippy::cast_possible_truncation,
                        reason = "image coordinates bounded by Limits::max_dimension"
                    )]
                    let at = [Point {
                        x: x as f32,
                        y: y as f32,
                    }; 4];
                    holding.iter().any(|refused| refused.holds(at))
                })
            });
        }
        Some(read)
    }

    /// Whether the recorded refusals withhold this read whole (what the
    /// re-read skips: it would be withheld anyway).
    pub(crate) fn withheld(&self, read: &Read) -> bool {
        self.standing(read).is_none()
    }

    /// Whether a standing read of the read's content (its symbology, an
    /// agreeing account) measured a margin of zero or more on its own word:
    /// a margin re-read of it would measure nothing the acceptance needs.
    pub(crate) fn has_margin(&self, read: &Read) -> bool {
        self.live().iter().any(|own| {
            own.symbology == read.symbology
                && own.key.agrees(&read.key)
                && own.margin.within_reserve()
        })
    }

    /// The reads that stand.
    fn live(&self) -> Vec<Read> {
        self.reads
            .iter()
            .filter_map(|read| self.standing(read))
            .collect()
    }

    /// The reads that stand and that the acceptance admits ([`admitted`]).
    fn accepted(&self) -> Vec<Read> {
        let live = self.live();
        live.iter()
            .filter(|read| admitted(&live, read))
            .cloned()
            .collect()
    }

    /// Whether standing reads of `symbology` and `raw` confirm each other
    /// ([`confirmed`]): what a confirmation decode waits for.
    pub(crate) fn confirmed(&self, symbology: Symbology, raw: &[u8]) -> bool {
        confirmed(&self.live(), symbology, raw)
    }

    /// Whether the walk publishes something — what the S5 trigger asks.
    pub(crate) fn found(&self) -> bool {
        !self.reconcile().is_empty()
    }

    /// Measured regions of admitted QR-family engine reads, in original
    /// image space, with their grids where a reader measured them.
    /// Candidate-only grids and refused/held reads never mask another
    /// family's search; duplicate payloads retain distinct places.
    /// (Whether anything outside the QR Codes is left to read at all,
    /// 07-pipeline property 8, also counts the grids rqrr measured in the
    /// attempt it asks for.)
    #[cfg(feature = "engine-rxing")]
    pub(crate) fn qr_regions(&self) -> Vec<MatrixRegion> {
        let mut regions = Vec::new();
        for read in self.accepted() {
            if !read.by_engine() || !read.symbology.is_qr_family() {
                continue;
            }
            for region in read.matrix_regions {
                record_region(
                    &mut regions,
                    region,
                    MAX_DETECTIONS * occurrence::MAX_OCCURRENCES,
                );
            }
        }
        regions
    }

    /// Whether refused streams past every record closed the walk: its
    /// unparsed QR Code reads up to then are withheld for want of records,
    /// not refused (saturation of the reads).
    pub(crate) fn failed_closed(&self) -> bool {
        self.closed.is_some()
    }

    /// Whether a QR Code read the scanner has not parsed stands — what
    /// re-deriving candidates waits for.
    pub(crate) fn unparsed_qr(&self) -> bool {
        self.live()
            .iter()
            .any(|read| read.by_engine() && read.symbology == Symbology::QrCode && !read.parsed)
    }

    /// Whether the walk may stop after a stage (07-pipeline property 2): an
    /// engine read stands that the acceptance admits, and not every such QR
    /// Code read only signals an ECI it could not locate. A signalled read
    /// a candidate parse contains with an agreeing account counts as
    /// located; other symbologies neither stop nor prolong the walk.
    pub(crate) fn settled(&self) -> bool {
        let live = self.live();
        let engine_reads: Vec<&Read> = live
            .iter()
            .filter(|read| read.by_engine() && admitted(&live, read))
            .collect();
        if engine_reads.is_empty() {
            return false;
        }
        let qr: Vec<&&Read> = engine_reads
            .iter()
            .filter(|read| read.symbology == Symbology::QrCode)
            .collect();
        qr.is_empty() || !qr.iter().all(|read| awaits_location(&live, read))
    }

    /// What the opposite-background walk starts with.
    pub(crate) fn seed(&self, rereads: usize) -> Seed {
        Seed {
            refusals: self
                .refusals
                .iter()
                .chain(&self.overflow)
                .flat_map(Refused::refusals)
                .collect(),
            closed: self.closed.is_some(),
            protection: self
                .reads
                .iter()
                .filter(|read| read.parsed)
                .map(|read| (read.symbology, read.key.clone()))
                .chain(self.protection.iter().cloned())
                .collect(),
            rereads,
        }
    }

    /// The distinct contents of the walk (module doc): of the reads the
    /// acceptance admits.
    pub(crate) fn reconcile(&self) -> Vec<Reconciled> {
        reconcile(self.accepted())
    }

    /// [`ReadStore::reconcile`], and whether contents were left out past
    /// [`MAX_DETECTIONS`].
    pub(crate) fn reconcile_counted(&self) -> (Vec<Reconciled>, bool) {
        reconcile_counted(self.accepted())
    }

    /// The refusal records kept, within capacity and past it.
    #[cfg(test)]
    pub(crate) fn records(&self) -> usize {
        self.refusals.len() + self.overflow.len()
    }
}

/// Whether the acceptance admits a standing read (07-pipeline
/// § Acceptance): it is no read of a symbology the readers invent from
/// noise still waiting for confirmation ([`confirmed`]), and its content —
/// the reads of its symbology whose account agrees with its own — is not
/// refused for its margins: a refused content has a read with a known
/// margin and every known margin negative, errors alone having taken each
/// measured word past the Annex B reserve. Reads of it with no margin
/// share the refusal (both engines sample the same damaged modules); one
/// read keeping a margin of zero or more admits them all.
fn admitted(live: &[Read], read: &Read) -> bool {
    if read.needs_confirmation && !confirmed(live, read.symbology, &read.key.raw) {
        return false;
    }
    let mut known = live
        .iter()
        .filter(|other| other.symbology == read.symbology && other.key.agrees(&read.key))
        .filter_map(|other| match other.margin {
            Margin::Known(margin) => Some(margin),
            Margin::Unknown => None,
        })
        .peekable();
    known.peek().is_none() || known.any(|margin| margin >= 0)
}

/// Whether reads of `symbology` and `raw` stand confirmed: one of them needs
/// no confirmation, or two were read on planes whose longest sides differ
/// by a factor of 1.25 or more (the transforms and colour channels of one
/// plane, and an inverted attempt, share its size: never independent).
fn confirmed(live: &[Read], symbology: Symbology, raw: &[u8]) -> bool {
    let mut span: Option<(u32, u32)> = None;
    for read in live
        .iter()
        .filter(|read| read.symbology == symbology && read.key.raw == raw)
    {
        if !read.needs_confirmation {
            return true;
        }
        if let Some((lo, hi)) = read.scales {
            span = Some(span.map_or((lo, hi), |(own_lo, own_hi)| {
                (own_lo.min(lo), own_hi.max(hi))
            }));
        }
    }
    span.is_some_and(|(lo, hi)| u64::from(hi) * 4 >= u64::from(lo) * 5)
}

/// A signalled QR Code read no agreeing candidate parse contains.
fn awaits_location(live: &[Read], read: &Read) -> bool {
    read.key.eci == EciEvidence::Signalled
        && !live.iter().any(|candidate| {
            !candidate.by_engine()
                && candidate.symbology == read.symbology
                && candidate.key.agrees(&read.key)
                && inside(read.location, candidate.location)
        })
}

/// One published content and what its reads measured.
#[derive(Debug, Clone)]
pub(crate) struct Reconciled {
    /// The symbology.
    pub(crate) symbology: Symbology,
    /// Its identity: bytes, the detection's evidence, FNC1 position.
    pub(crate) key: ContentKey,
    /// The one distinct non-empty segment observation of its parsed reads.
    pub(crate) segments: Segments,
    /// Its reads' one engine reading.
    pub(crate) engine_reading: EngineReading,
    /// Engines, in the order of their earliest reads.
    pub(crate) engines: Vec<EngineKind>,
    /// Geometry from its earliest reads that carry it.
    pub(crate) geometry: Geometry,
    /// Its earliest read.
    pub(crate) rank: Rank,
    /// The footprints its reads measured ([`occurrence::union`]), grouped
    /// into occurrences at publication.
    pub(crate) observations: Vec<Observation>,
    /// A footprint of its reads was left out past a capacity.
    pub(crate) observations_dropped: bool,
}

/// Whether the centre of a read's corner points lies inside the box of a
/// detection's; reads or detections without corners never fail it.
pub(crate) fn inside(read: Option<[Point; 4]>, detection: Option<[Point; 4]>) -> bool {
    let (Some(read), Some(own)) = (read, detection) else {
        return true;
    };
    let (x, y) = read
        .iter()
        .fold((0.0, 0.0), |(x, y), p| (x + p.x / 4.0, y + p.y / 4.0));
    let (mut min_x, mut max_x, mut min_y, mut max_y) = (
        f32::INFINITY,
        f32::NEG_INFINITY,
        f32::INFINITY,
        f32::NEG_INFINITY,
    );
    for p in own {
        min_x = min_x.min(p.x);
        max_x = max_x.max(p.x);
        min_y = min_y.min(p.y);
        max_y = max_y.max(p.y);
    }
    (min_x..=max_x).contains(&x) && (min_y..=max_y).contains(&y)
}

/// A detection being formed: its account and the reads that joined it.
struct Forming {
    eci: EciEvidence,
    fnc1: Fnc1,
    /// The corner points of the earliest read that formed it.
    place: Option<[Point; 4]>,
    members: Vec<Read>,
}

impl Forming {
    fn new(read: Read) -> Self {
        Self {
            eci: read.key.eci.clone(),
            fnc1: read.key.fnc1,
            place: read.location,
            members: vec![read],
        }
    }

    fn identity(&self) -> Identity {
        identity(&self.eci, self.fnc1)
    }

    fn contains(&self, read: &Read) -> bool {
        inside(read.location, self.place)
    }

    fn agrees(&self, read: &Read) -> bool {
        self.eci.agrees_with(&read.key.eci) && self.fnc1.agrees_with(read.key.fnc1)
    }
}

/// What a detection publishes of its account: the `eci` key (absent, `[]`
/// or the list) and the `fnc1` key.
type Identity = (Option<EciEvidence>, Option<Fnc1>);

fn identity(eci: &EciEvidence, fnc1: Fnc1) -> Identity {
    let eci = matches!(eci, EciEvidence::Located(_) | EciEvidence::Signalled).then(|| eci.clone());
    let fnc1 = matches!(fnc1, Fnc1::First | Fnc1::Second).then_some(fnc1);
    (eci, fnc1)
}

/// Reconcile a read set (the module doc; spec/07 property 6).
pub(crate) fn reconcile(reads: Vec<Read>) -> Vec<Reconciled> {
    reconcile_counted(reads).0
}

/// [`reconcile`], and whether contents were left out past
/// [`MAX_DETECTIONS`].
pub(crate) fn reconcile_counted(mut reads: Vec<Read>) -> (Vec<Reconciled>, bool) {
    // a total order: ranks are distinct within a walk; constructed sets
    // that share one fall back to the content
    reads.sort_by(|a, b| {
        a.rank
            .cmp(&b.rank)
            .then_with(|| format!("{:?}", a.key).cmp(&format!("{:?}", b.key)))
    });
    let admitted: Vec<Read> = reads
        .iter()
        .filter(|read| {
            read.by_engine()
                || reads.iter().any(|engine| {
                    engine.by_engine()
                        && engine.symbology == read.symbology
                        && engine.key.agrees(&read.key)
                        && inside(engine.location, read.location)
                })
        })
        .cloned()
        .collect();
    let mut groups: Vec<Vec<Read>> = Vec::new();
    for read in admitted {
        let group = groups.iter_mut().find(|group| {
            group.first().is_some_and(|first| {
                first.symbology == read.symbology && first.key.raw == read.key.raw
            })
        });
        match group {
            Some(group) => group.push(read),
            None => groups.push(vec![read]),
        }
    }
    let mut detections: Vec<Reconciled> = groups
        .into_iter()
        .flat_map(reconcile_group)
        .filter(|detection| !detection.engines.is_empty())
        .collect();
    detections.sort_by_key(|detection| (!detection.symbology.is_qr_family(), detection.rank));
    let saturated = detections.len() > MAX_DETECTIONS;
    detections.truncate(MAX_DETECTIONS);
    (detections, saturated)
}

/// Steps 1-3 within one (symbology, raw) group, its reads in order.
fn reconcile_group(reads: Vec<Read>) -> Vec<Reconciled> {
    let observed = |fnc1: Fnc1| fnc1 != Fnc1::Unobserved;
    let mut formed: Vec<Forming> = Vec::new();
    // step 1: located and no-ECI reads with an observed FNC1 position
    let (first, rest): (Vec<Read>, Vec<Read>) = reads.into_iter().partition(|read| {
        matches!(read.key.eci, EciEvidence::Located(_) | EciEvidence::NoEci)
            && observed(read.key.fnc1)
    });
    for read in first {
        match formed
            .iter_mut()
            .find(|f| f.eci == read.key.eci && f.fnc1 == read.key.fnc1)
        {
            Some(f) => f.members.push(read),
            None => formed.push(Forming::new(read)),
        }
    }
    // step 2: signalled reads with an observed FNC1 position
    let (signalled, rest): (Vec<Read>, Vec<Read>) = rest
        .into_iter()
        .partition(|read| read.key.eci == EciEvidence::Signalled && observed(read.key.fnc1));
    let step_one = formed.len();
    for read in signalled {
        join_signalled(&mut formed, step_one, read);
    }
    // step 3: unobserved evidence with an observed position, then an
    // unobserved position
    let (unobserved, unknown_position): (Vec<Read>, Vec<Read>) =
        rest.into_iter().partition(|read| observed(read.key.fnc1));
    join_or_form(&mut formed, unobserved);
    join_or_form(&mut formed, unknown_position);
    formed.into_iter().map(published).collect()
}

/// Step 2: a signalled read joins the one located step-1 detection of its
/// FNC1 position that contains it; several or none: nothing. Without any
/// such detection, the group's signalled detection for its position, which
/// the first such read opens.
fn join_signalled(formed: &mut Vec<Forming>, step_one: usize, read: Read) {
    let located: Vec<usize> = (0..step_one)
        .filter(|&i| {
            matches!(formed[i].eci, EciEvidence::Located(_)) && formed[i].fnc1 == read.key.fnc1
        })
        .collect();
    if located.is_empty() {
        match formed[step_one..]
            .iter_mut()
            .find(|f| f.fnc1 == read.key.fnc1)
        {
            Some(f) => f.members.push(read),
            None => formed.push(Forming::new(read)),
        }
        return;
    }
    let containing: Vec<usize> = located
        .into_iter()
        .filter(|&i| formed[i].contains(&read))
        .collect();
    if let [only] = containing[..] {
        formed[only].members.push(read);
    }
}

/// Step 3, one kind of read against the detections formed before: a read
/// joins the one agreeing detection that contains it; with several it
/// joins nothing; with none, the reads of that kind form one detection per
/// published identity, unless a detection with that identity already
/// exists — then they join nothing.
fn join_or_form(formed: &mut Vec<Forming>, reads: Vec<Read>) {
    let before = formed.len();
    let mut left: Vec<Read> = Vec::new();
    for read in reads {
        let containing: Vec<usize> = (0..before)
            .filter(|&i| formed[i].agrees(&read) && formed[i].contains(&read))
            .collect();
        match containing[..] {
            [only] => formed[only].members.push(read),
            [] => left.push(read),
            _ => {}
        }
    }
    for read in left {
        let identity = identity(&read.key.eci, read.key.fnc1);
        if formed[..before].iter().any(|f| f.identity() == identity) {
            continue;
        }
        match formed[before..]
            .iter_mut()
            .find(|f| f.identity() == identity)
        {
            Some(f) => f.members.push(read),
            None => formed.push(Forming::new(read)),
        }
    }
}

/// A formed detection as published: its account, the joined segments and
/// engine reading, engines by earliest read, the earliest geometry.
fn published(forming: Forming) -> Reconciled {
    let Forming {
        eci, fnc1, members, ..
    } = forming;
    let mut engines: Vec<(EngineKind, Rank)> = Vec::new();
    for &(engine, rank) in members.iter().flat_map(|m| m.engines.iter()) {
        match engines.iter_mut().find(|(own, _)| *own == engine) {
            Some((_, own)) => *own = (*own).min(rank),
            None => engines.push((engine, rank)),
        }
    }
    engines.sort_by_key(|&(_, rank)| rank);
    // members join in step order, not rank order: each part comes from the
    // member whose read that carries it is the earliest (ranks are distinct
    // within a walk)
    let geometry = Geometry {
        corners: members
            .iter()
            .filter(|m| m.geometry.corners.is_some())
            .min_by_key(|m| m.sources.corners)
            .and_then(|m| m.geometry.corners),
        stream: members
            .iter()
            .filter(|m| m.geometry.stream.is_some())
            .min_by_key(|m| m.sources.stream)
            .and_then(|m| m.geometry.stream.clone()),
        ec: members
            .iter()
            .filter(|m| m.geometry.ec.is_some())
            .min_by_key(|m| m.sources.ec)
            .and_then(|m| m.geometry.ec),
    };
    let rank = members.iter().map(|m| m.rank).min().unwrap_or_default();
    let (symbology, raw) = members
        .first()
        .map_or((Symbology::QrCode, Vec::new()), |m| {
            (m.symbology, m.key.raw.clone())
        });
    let (observations, observations_dropped) = occurrence::union(
        members
            .iter()
            .map(|m| (m.observations.as_slice(), m.observations_dropped)),
    );
    Reconciled {
        symbology,
        key: ContentKey { raw, eci, fnc1 },
        segments: joined_segments(&members),
        engine_reading: joined_engine_reading(&members),
        engines: engines.into_iter().map(|(engine, _)| engine).collect(),
        geometry,
        rank,
        observations,
        observations_dropped,
    }
}

/// The one distinct non-empty segment observation of the parsed reads;
/// several conflict; parsed reads with none: no segment; no parsed read:
/// not observed.
fn joined_segments(members: &[Read]) -> Segments {
    let mut parsed = false;
    let mut seen: Vec<&Vec<DoubleByteSpan>> = Vec::new();
    for member in members.iter().filter(|m| m.parsed) {
        parsed = true;
        if let Segments::Observed(spans) = &member.segments
            && !spans.is_empty()
            && !seen.contains(&spans)
        {
            seen.push(spans);
        }
    }
    match seen[..] {
        [] if parsed => Segments::Observed(Vec::new()),
        [] => Segments::NotObserved,
        [only] => Segments::Observed(only.clone()),
        _ => Segments::Conflicting,
    }
}

/// The one distinct engine reading of the reads; several distinct ones:
/// none (rule 1e does not apply).
fn joined_engine_reading(members: &[Read]) -> EngineReading {
    let mut seen = EngineReading::None;
    for member in members {
        match (seen, member.engine_reading) {
            (_, EngineReading::None) => {}
            (EngineReading::None, reading) => seen = reading,
            (own, reading) if own == reading => {}
            _ => return EngineReading::None,
        }
    }
    seen
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use proptest::prelude::*;

    use super::*;
    use crate::engine::content::{self, DoubleByteMode, EciMark};
    use crate::report::Charset;

    use DoubleByteMode::{Hanzi, Kanji};
    use EciEvidence::{NoEci, Signalled, Unobserved};
    use EngineKind::{Rqrr, Rxing};

    const X: Option<EngineKind> = Some(Rxing);
    const Q: Option<EngineKind> = Some(Rqrr);
    /// A candidate parse: no engine.
    const C: Option<EngineKind> = None;
    /// Two places side by side, and none.
    const L: Option<f32> = Some(10.0);
    const R: Option<f32> = Some(210.0);
    const NOWHERE: Option<f32> = None;

    const A1: &[u8] = &[0xA1];
    const E_ACUTE: &[u8] = &[0xC3, 0xA9];
    const YAMADA: &[u8] = &[0x8E, 0x52, 0x93, 0x63];
    const CHUWEN: &[u8] = &[0xD6, 0xD0, 0xCE, 0xC4];

    /// A 100 px square at `x`.
    fn square_at(x: f32) -> [Point; 4] {
        [
            Point { x, y: 10.0 },
            Point {
                x: x + 100.0,
                y: 10.0,
            },
            Point {
                x: x + 100.0,
                y: 110.0,
            },
            Point { x, y: 110.0 },
        ]
    }

    fn located(marks: &[(u32, usize)]) -> EciEvidence {
        EciEvidence::Located(
            marks
                .iter()
                .map(|&(value, start)| EciMark { value, start })
                .collect(),
        )
    }

    fn eci4() -> EciEvidence {
        located(&[(4, 0)])
    }

    fn key(raw: &[u8], eci: EciEvidence, fnc1: Fnc1) -> ContentKey {
        ContentKey {
            raw: raw.to_vec(),
            eci,
            fnc1,
        }
    }

    /// The `n`-th read of a set, FNC1 none, made at `at` (a square there);
    /// rqrr reads and candidate parses measure its corners. A candidate
    /// parse is a parse.
    fn read(
        n: u32,
        raw: &[u8],
        eci: EciEvidence,
        engine: Option<EngineKind>,
        at: Option<f32>,
    ) -> Read {
        let rank = Rank {
            attempt: n,
            source: 0,
            index: 0,
        };
        let mut read = Read::new(Symbology::QrCode, key(raw, eci, Fnc1::None), engine, rank)
            .at(at.map(square_at));
        if engine != X {
            read.geometry.corners = at.map(|x| (square_at(x), false));
        }
        if engine.is_none() {
            read.parsed = true;
            read.segments = Segments::Observed(Vec::new());
        }
        read
    }

    #[test]
    #[cfg(feature = "engine-rxing")]
    fn qr_exclusion_regions_require_admitted_measured_engine_reads() {
        let mut store = ReadStore::default();
        for (n, raw, engine, x) in [
            (0, b"same".as_slice(), X, 10.0),
            (1, b"same".as_slice(), X, 210.0),
            (2, b"candidate".as_slice(), C, 410.0),
            (3, b"refused".as_slice(), X, 610.0),
        ] {
            let mut hit = read(n, raw, NoEci, engine, Some(x));
            hit.matrix_regions.push(region_at(x, None));
            if raw == b"refused" {
                hit.margin = Margin::Known(-1);
            }
            store.collect(hit);
        }
        // A place reconstructed from finder centres has no measured outer
        // region and must not become an exclusion polygon.
        store.collect(read(4, b"inferred", NoEci, X, Some(810.0)));
        assert_eq!(
            store.qr_regions(),
            [region_at(10.0, None), region_at(210.0, None)]
        );
    }

    /// The square at `x` as a measured region, its grid of `modules` there.
    #[cfg(feature = "engine-rxing")]
    fn region_at(x: f32, modules: Option<u16>) -> MatrixRegion {
        MatrixRegion {
            corners: square_at(x),
            grid: modules.map(|modules| Grid {
                outer: square_at(x),
                modules,
            }),
        }
    }

    #[test]
    #[cfg(feature = "engine-rxing")]
    fn a_region_without_a_grid_takes_the_grid_of_its_own_symbol_measured_later() {
        // rxing's outline first, then rqrr's grid of the same symbol a pixel
        // off: rxing's corners stay, the grid joins them
        let shifted = |x: f32, dx: f32, grow: f32| {
            let [a, b, c, d] = square_at(x);
            let at = |p: Point, sx: f32, sy: f32| Point {
                x: p.x + dx + sx * grow,
                y: p.y + sy * grow,
            };
            [
                at(a, -1.0, -1.0),
                at(b, 1.0, -1.0),
                at(c, 1.0, 1.0),
                at(d, -1.0, 1.0),
            ]
        };
        let collected = |second: [Point; 4], modules: Option<u16>| {
            let mut store = ReadStore::default();
            let mut hit = read(0, b"same", NoEci, X, Some(10.0));
            hit.matrix_regions.push(region_at(10.0, None));
            store.collect(hit);
            let mut hit = read(1, b"same", NoEci, X, Some(10.0));
            hit.matrix_regions.push(MatrixRegion {
                corners: second,
                grid: modules.map(|modules| Grid {
                    outer: square_at(10.0),
                    modules,
                }),
            });
            store.collect(hit);
            store.qr_regions()
        };
        assert_eq!(
            collected(shifted(10.0, 1.0, 0.0), Some(25)),
            [region_at(10.0, Some(25))]
        );
        // a region already measured keeps its grid
        let mut store = ReadStore::default();
        for (n, modules) in [(0, Some(21)), (1, Some(25))] {
            let mut hit = read(n, b"same", NoEci, X, Some(10.0));
            hit.matrix_regions.push(region_at(10.0, modules));
            store.collect(hit);
        }
        assert_eq!(store.qr_regions(), [region_at(10.0, Some(21))]);
        // a symbol a quarter larger or more around the same centre is
        // another symbol: its modules say nothing of the first one's
        let side = square_at(10.0)[1].x - square_at(10.0)[0].x;
        let larger = shifted(10.0, 0.0, side * 0.13);
        assert_eq!(collected(larger, Some(25)), [region_at(10.0, None)]);
        let close = shifted(10.0, 0.0, side * 0.12);
        assert_eq!(collected(close, Some(25)), [region_at(10.0, Some(25))]);
    }

    #[test]
    fn one_symbol_shares_both_centres_and_has_sides_a_quarter_apart_at_most() {
        let square = |at: f32, side: f32| {
            [
                Point { x: at, y: at },
                Point {
                    x: at + side,
                    y: at,
                },
                Point {
                    x: at + side,
                    y: at + side,
                },
                Point {
                    x: at,
                    y: at + side,
                },
            ]
        };
        let own = square(0.0, 100.0);
        // around the same centre (50, 50): a side exactly a quarter longer
        // is the same symbol, either way round; a little more is another
        let quarter = square(-12.5, 125.0);
        assert!(same_symbol(own, quarter));
        assert!(same_symbol(quarter, own));
        let past = square(-13.0, 126.0);
        assert!(!same_symbol(own, past));
        assert!(!same_symbol(past, own));
        // the first centre inside the second box, the second centre
        // (105, 105) outside the first: another symbol, either way round
        let beside = square(45.0, 120.0);
        assert!(inside(Some(own), Some(beside)));
        assert!(!inside(Some(beside), Some(own)));
        assert!(!same_symbol(own, beside));
        assert!(!same_symbol(beside, own));
    }

    /// The measure is the mean of the four sides, each corner to the next:
    /// a 10 × 10 square and a 16 × 4 rectangle around the same centre have
    /// the same perimeter and measure alike, where a side and two diagonals
    /// of each would put them 28 % apart.
    #[test]
    fn outlines_of_one_perimeter_measure_alike_whatever_their_shape() {
        let around = |width: f32, height: f32| {
            let (x, y) = (50.0 - width / 2.0, 50.0 - height / 2.0);
            [
                Point { x, y },
                Point { x: x + width, y },
                Point {
                    x: x + width,
                    y: y + height,
                },
                Point { x, y: y + height },
            ]
        };
        let (square, flat) = (around(10.0, 10.0), around(16.0, 4.0));
        assert!(same_symbol(square, flat));
        assert!(same_symbol(flat, square));
    }

    /// A read of `raw` at the `n`-th of a row of disjoint squares, its
    /// measured region recorded.
    #[cfg(feature = "engine-rxing")]
    fn region_read(n: u16, raw: &[u8]) -> Read {
        let x = 200.0 * f32::from(n);
        let mut hit = read(u32::from(n), raw, NoEci, X, Some(x));
        hit.matrix_regions.push(region_at(x, None));
        hit
    }

    #[test]
    #[cfg(feature = "engine-rxing")]
    fn one_content_masks_its_first_sixteen_regions_and_no_seventeenth() {
        let mut store = ReadStore::default();
        for n in 0..17 {
            store.collect(region_read(n, b"copies"));
        }
        let first: Vec<MatrixRegion> = (0..16_u16)
            .map(|n| region_at(200.0 * f32::from(n), None))
            .collect();
        assert_eq!(store.qr_regions(), first);
    }

    #[test]
    #[cfg(feature = "engine-rxing")]
    fn the_masked_regions_stop_at_two_hundred_fifty_six_in_all() {
        // seventeen contents of sixteen regions each, every region its own
        let mut store = ReadStore::default();
        for content in 0..17_u16 {
            let raw = format!("content {content}");
            for copy in 0..16 {
                store.collect(region_read(content * 16 + copy, raw.as_bytes()));
            }
        }
        let regions = store.qr_regions();
        assert_eq!(regions.len(), MAX_DETECTIONS * occurrence::MAX_OCCURRENCES);
        assert_eq!(regions.len(), 256);
        assert!(
            regions.iter().enumerate().all(|(i, own)| {
                let before: Vec<_> = regions[..i].iter().map(|r| r.corners).collect();
                !recorded(&before, own.corners)
            }),
            "every masked region is distinct"
        );
    }

    /// The read, parsed by the scanner.
    fn parsed(mut read: Read) -> Read {
        read.parsed = true;
        if read.segments == Segments::NotObserved {
            read.segments = Segments::Observed(Vec::new());
        }
        read
    }

    fn fnc1(mut read: Read, fnc1: Fnc1) -> Read {
        read.key.fnc1 = fnc1;
        read
    }

    fn spans(mut read: Read, spans: &[(DoubleByteMode, usize, usize)]) -> Read {
        read.segments = Segments::Observed(
            spans
                .iter()
                .map(|&(mode, start, end)| DoubleByteSpan { mode, start, end })
                .collect(),
        );
        read
    }

    fn engine_reading(mut read: Read, reading: EngineReading) -> Read {
        read.engine_reading = reading;
        read
    }

    /// A detection's reading, as the ladder interprets it.
    fn reading(detection: &Reconciled) -> (String, Charset) {
        let reading = content::read(
            &detection.key.raw,
            &detection.key.eci,
            &detection.segments,
            detection.engine_reading,
            detection.symbology.is_qr_family(),
        );
        (reading.text, reading.charset)
    }

    /// What a publication is, compared across orders.
    fn summary(detections: &[Reconciled]) -> Vec<String> {
        detections
            .iter()
            .map(|d| {
                format!(
                    "{:?} {:?} {:?} {:?} {:?} {:?}",
                    d.key,
                    d.engines,
                    d.rank,
                    d.geometry.corners.map(|(corners, _)| corners[0].x),
                    d.segments,
                    reading(d),
                )
            })
            .collect()
    }

    fn store(reads: impl IntoIterator<Item = Read>, refusals: &[Refusal]) -> ReadStore {
        let mut store = ReadStore::default();
        for refusal in refusals {
            store.refuse(refusal.clone(), 0);
        }
        for read in reads {
            store.collect(read);
        }
        store
    }

    /// Every permutation of `0..n` (Heap's algorithm).
    fn permutations(n: usize) -> Vec<Vec<usize>> {
        fn heap(k: usize, items: &mut Vec<usize>, out: &mut Vec<Vec<usize>>) {
            if k <= 1 {
                out.push(items.clone());
                return;
            }
            heap(k - 1, items, out);
            for i in 0..k - 1 {
                if k.is_multiple_of(2) {
                    items.swap(i, k - 1);
                } else {
                    items.swap(0, k - 1);
                }
                heap(k - 1, items, out);
            }
        }
        let mut out = Vec::new();
        heap(n, &mut (0..n).collect(), &mut out);
        out
    }

    /// Reconcile `reads` collected in every order; the publication must be
    /// one.
    fn every_order(reads: &[Read], refusals: &[Refusal]) -> Vec<Reconciled> {
        let found = store(reads.iter().cloned(), refusals).reconcile();
        let expected = summary(&found);
        for order in permutations(reads.len()) {
            let shuffled = order.iter().map(|&i| reads[i].clone());
            assert_eq!(
                summary(&store(shuffled, refusals).reconcile()),
                expected,
                "order {order:?}"
            );
        }
        found
    }

    fn engines(found: &[Reconciled]) -> Vec<Vec<EngineKind>> {
        found.iter().map(|d| d.engines.clone()).collect()
    }

    #[test]
    fn reconcile_is_order_independent() {
        // rxing and rqrr both located
        let found = every_order(
            &[
                parsed(read(0, A1, eci4(), X, NOWHERE)),
                parsed(read(1, A1, eci4(), Q, L)),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        assert_eq!(reading(&found[0]), ("\u{104}".into(), Charset::Eci));
        // a signalled port read joins the located one
        let found = every_order(
            &[
                read(0, A1, Signalled, X, NOWHERE),
                parsed(read(1, A1, eci4(), Q, L)),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        assert_eq!(found[0].key.eci, eci4());
        // twins ECI 26 / ECI 4 over the same bytes; a signalled read without
        // a place joins neither, one at the right joins ECI 4
        let twins = [
            parsed(read(0, E_ACUTE, located(&[(26, 0)]), Q, L)),
            parsed(read(1, E_ACUTE, eci4(), Q, R)),
        ];
        for (at, expected) in [
            (NOWHERE, [vec![Rqrr], vec![Rqrr]]),
            (R, [vec![Rqrr], vec![Rqrr, Rxing]]),
        ] {
            let mut reads = twins.to_vec();
            reads.push(read(2, E_ACUTE, Signalled, X, at));
            let found = every_order(&reads, &[]);
            assert_eq!(engines(&found), expected, "{at:?}");
            assert_eq!(reading(&found[0]), ("é".into(), Charset::Utf8));
            assert_eq!(reading(&found[1]), ("ĂŠ".into(), Charset::Eci));
        }
        // FNC1 first and second are two contents
        let found = every_order(
            &[
                fnc1(read(0, b"37abc", NoEci, X, NOWHERE), Fnc1::First),
                fnc1(read(1, b"37abc", NoEci, X, NOWHERE), Fnc1::Second),
            ],
            &[],
        );
        assert_eq!(found.len(), 2);
        // an unobserved read joins the one it lies in, none without a place
        let pair = [
            parsed(read(0, A1, NoEci, Q, L)),
            parsed(read(1, A1, eci4(), Q, R)),
        ];
        for (at, expected) in [
            (R, [vec![Rqrr], vec![Rqrr, Rxing]]),
            (NOWHERE, [vec![Rqrr], vec![Rqrr]]),
        ] {
            let mut reads = pair.to_vec();
            reads.push(read(2, A1, Unobserved, X, at));
            assert_eq!(engines(&every_order(&reads, &[])), expected, "{at:?}");
        }
        // FNC1 first + ECI 26, the port read located by the re-read
        let gs1: &[u8] = b"0109506000134352";
        let eci26 = located(&[(26, 0)]);
        let found = every_order(
            &[
                fnc1(parsed(read(0, gs1, eci26.clone(), X, L)), Fnc1::First),
                fnc1(read(1, gs1, eci26, C, L), Fnc1::First),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing]]);
        assert_eq!(found[0].key.fnc1, Fnc1::First);
        assert!(found[0].geometry.corners.is_some(), "the candidate's grid");
        // signalled beside no-ECI over the same bytes
        let found = every_order(
            &[
                read(0, E_ACUTE, Signalled, X, NOWHERE),
                read(1, E_ACUTE, NoEci, X, NOWHERE),
            ],
            &[],
        );
        assert_eq!(
            found.iter().map(reading).collect::<Vec<_>>(),
            [
                ("\u{FFFD}\u{FFFD}".to_owned(), Charset::Unknown),
                ("é".to_owned(), Charset::Utf8)
            ]
        );
    }

    #[test]
    fn double_byte_contents_reconcile_in_every_order() {
        // Kanji 山田, rxing (its own reading) + rqrr (segments)
        let found = every_order(
            &[
                engine_reading(read(0, YAMADA, NoEci, X, NOWHERE), EngineReading::ShiftJis),
                spans(parsed(read(1, YAMADA, NoEci, Q, L)), &[(Kanji, 0, 4)]),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        assert_eq!(reading(&found[0]), ("山田".into(), Charset::ShiftJis));
        // Hanzi 中文, rxing + a candidate parse (no engine)
        let found = every_order(
            &[
                read(0, CHUWEN, NoEci, X, L),
                spans(read(1, CHUWEN, NoEci, C, L), &[(Hanzi, 0, 4)]),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing]]);
        assert_eq!(reading(&found[0]), ("中文".into(), Charset::Gb18030));
        // Hanzi twice, one unobserved
        let found = every_order(
            &[
                engine_reading(read(0, CHUWEN, NoEci, X, NOWHERE), EngineReading::Gb18030),
                read(1, CHUWEN, Unobserved, Q, NOWHERE),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        assert_eq!(reading(&found[0]), ("中文".into(), Charset::Gb18030));
        // Kanji-mode and byte-mode twins
        let found = every_order(
            &[
                spans(parsed(read(0, YAMADA, NoEci, Q, L)), &[(Kanji, 0, 4)]),
                parsed(read(1, YAMADA, NoEci, Q, R)),
            ],
            &[],
        );
        assert_eq!(found.len(), 1);
        assert_eq!(reading(&found[0]), ("山田".into(), Charset::ShiftJis));
        // Kanji against Hanzi over one stream: conflicting segments
        let both: &[u8] = &[0xB0, 0xA1, 0xB0, 0xA1];
        let found = every_order(
            &[
                spans(parsed(read(0, both, NoEci, Q, L)), &[(Kanji, 0, 4)]),
                spans(parsed(read(1, both, NoEci, X, R)), &[(Hanzi, 0, 4)]),
            ],
            &[],
        );
        assert_eq!(found[0].segments, Segments::Conflicting);
        assert_eq!(reading(&found[0]), ("\u{FFFD}".repeat(4), Charset::Unknown));
    }

    #[test]
    fn reads_beside_candidates_and_refusals_reconcile_in_every_order() {
        // an unobserved read without a place between two candidates
        let found = every_order(
            &[
                read(0, A1, Unobserved, X, NOWHERE),
                read(1, A1, NoEci, C, L),
                read(2, A1, eci4(), C, R),
            ],
            &[],
        );
        assert!(found.is_empty(), "{:?}", summary(&found));
        // rxing-only signalled twins are one content
        let found = every_order(
            &[
                read(0, E_ACUTE, Signalled, X, L),
                read(1, E_ACUTE, Signalled, X, R),
            ],
            &[],
        );
        assert_eq!(found.len(), 1);
        assert_eq!(
            reading(&found[0]),
            ("\u{FFFD}\u{FFFD}".into(), Charset::Unknown)
        );
        // with a refusal (the place rules have their own tests below)
        let refusal = Refusal {
            port: Some(key(b"abc", Signalled, Fnc1::None)),
            rqrr: None,
            place: None,
            grid: None,
        };
        let found = every_order(
            &[
                parsed(read(0, b"abc", located(&[(3, 0)]), Q, L)),
                read(1, b"abc", Signalled, X, L),
                read(2, b"abc", Signalled, X, R),
            ],
            &[refusal],
        );
        assert_eq!(engines(&found), [vec![Rqrr, Rxing]], "protected, joined");
    }

    #[test]
    fn a_read_outside_its_only_candidate_never_lends_geometry() {
        // a located anchor on the left and an unobserved read of another
        // symbol on the right: two contents, each its own geometry
        let found = every_order(
            &[
                parsed(read(0, A1, eci4(), Q, L)),
                read(1, A1, Unobserved, Q, R),
            ],
            &[],
        );
        assert_eq!(found.len(), 2);
        let corners: Vec<Option<f32>> = found
            .iter()
            .map(|d| d.geometry.corners.map(|(c, _)| c[0].x))
            .collect();
        assert_eq!(corners, [L, R]);
        assert_eq!(reading(&found[1]), ("\u{FFFD}".into(), Charset::Unknown));
        // a signalled read beside a single located anchor joins none
        let found = every_order(
            &[
                parsed(read(0, A1, eci4(), Q, L)),
                read(1, A1, Signalled, X, R),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rqrr]]);
    }

    #[test]
    fn reads_without_place_join_none_and_order_never_matters() {
        // U (no place), N and S of [A1]: two detections; U agrees with both
        // and lies in both — it joins neither (their earliest reads stay N's
        // and S's)
        let found = every_order(
            &[
                read(0, A1, Unobserved, Q, NOWHERE),
                read(1, A1, NoEci, Q, L),
                read(2, A1, Signalled, X, R),
            ],
            &[],
        );
        let ranks: Vec<u32> = found.iter().map(|d| d.rank.attempt).collect();
        assert_eq!(ranks, [1, 2]);
        assert_eq!(found[0].key.eci, NoEci);
        assert_eq!(found[1].key.eci, Signalled);
    }

    #[test]
    fn a_candidate_joins_only_where_it_contains_an_engine_read() {
        // the only matching port read lies elsewhere: the candidate is not
        // admitted, the port read stands alone without geometry
        let found = every_order(
            &[
                fnc1(read(0, b"10ABC", NoEci, C, L), Fnc1::First),
                fnc1(read(1, b"10ABC", NoEci, X, R), Fnc1::First),
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing]]);
        assert!(found[0].geometry.corners.is_none());
    }

    #[test]
    fn a_read_two_candidates_contain_joins_neither() {
        // both candidates admitted, the read joins neither, neither is
        // published
        let found = every_order(
            &[
                read(0, A1, eci4(), C, L),
                read(1, A1, located(&[(7, 0)]), C, L),
                read(2, A1, Signalled, X, L),
            ],
            &[],
        );
        assert!(found.is_empty(), "{:?}", summary(&found));
    }

    #[test]
    fn reconciliation_follows_the_evidence_lattice() {
        // (first account, second account, detections, account kept first)
        let cases = [
            (Unobserved, NoEci, 1, NoEci),
            (NoEci, Unobserved, 1, NoEci),
            (Unobserved, eci4(), 1, eci4()),
            (Signalled, Signalled, 1, Signalled),
            (Signalled, eci4(), 1, eci4()),
            (eci4(), Signalled, 1, eci4()),
            (eci4(), eci4(), 1, eci4()),
            (NoEci, Signalled, 2, NoEci),
            (NoEci, eci4(), 2, NoEci),
            (eci4(), located(&[(4, 1)]), 2, eci4()),
            (eci4(), located(&[(3, 0)]), 2, eci4()),
        ];
        for (first, second, count, kept) in cases {
            let found = every_order(
                &[
                    read(0, A1, first.clone(), X, NOWHERE),
                    read(1, A1, second.clone(), Q, NOWHERE),
                ],
                &[],
            );
            assert_eq!(found.len(), count, "{first:?} then {second:?}");
            assert_eq!(found[0].key.eci, kept, "{first:?} then {second:?}");
        }
    }

    #[test]
    fn fnc1_first_and_second_never_reconcile() {
        // the same bytes under GS1 (first) and under an AIM application
        // (second) are two contents, a plain read a third; an unknown
        // position agrees with all three and joins none
        let found = every_order(
            &[
                fnc1(read(0, b"37abc", NoEci, Q, NOWHERE), Fnc1::Unobserved),
                fnc1(read(1, b"37abc", NoEci, X, NOWHERE), Fnc1::First),
                fnc1(
                    read(2, b"37abc", NoEci, Some(EngineKind::Rescue), NOWHERE),
                    Fnc1::Second,
                ),
                read(3, b"37abc", NoEci, Q, NOWHERE),
            ],
            &[],
        );
        let positions: Vec<Fnc1> = found.iter().map(|d| d.key.fnc1).collect();
        assert_eq!(positions, [Fnc1::First, Fnc1::Second, Fnc1::None]);
        assert!(found.iter().all(|d| d.engines.len() == 1));
    }

    #[test]
    fn a_read_with_corners_joins_the_admitting_detection_it_overlaps() {
        // two symbols of [A1] side by side — unmarked on the left, ECI 4 on
        // the right — and a read both admit: where it lies decides; a read
        // WITHOUT a place both admit joins none
        let base = [
            parsed(read(0, A1, NoEci, Q, L)),
            parsed(read(1, A1, eci4(), Q, R)),
            read(2, A1, Unobserved, Some(EngineKind::Rescue), Some(212.0)),
        ];
        let found = every_order(&base, &[]);
        assert_eq!(
            engines(&found),
            [vec![Rqrr], vec![Rqrr, EngineKind::Rescue]]
        );
        // (its FNC1 position unobserved too, so it never folds into the
        // rescue read)
        let mut reads = base.to_vec();
        reads.push(fnc1(read(3, A1, Unobserved, X, NOWHERE), Fnc1::Unobserved));
        assert_eq!(
            engines(&every_order(&reads, &[])),
            [vec![Rqrr], vec![Rqrr, EngineKind::Rescue]]
        );
    }

    #[test]
    fn identical_reads_fold_with_the_first_geometry() {
        // engines united; the first corners keep their photometry, whatever
        // the order the reads arrive in
        let mut later = read(1, b"same", NoEci, Q, R);
        later.geometry.corners = Some((square_at(210.0), true));
        let found = every_order(
            &[
                read(0, b"same", NoEci, X, NOWHERE),
                read(2, b"same", NoEci, Q, L),
                later,
            ],
            &[],
        );
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        assert_eq!(
            found[0]
                .geometry
                .corners
                .map(|(c, inverted)| (c[0].x, inverted)),
            Some((210.0, true)),
            "the earliest read that carries corners"
        );
    }

    #[test]
    fn reconcile_bounds() {
        // 65 distinct reads — 64 reconciled (the 65th counted), at most 16
        // published, the QR family first
        let mut store = ReadStore::default();
        for i in 0..65u32 {
            let mut read = read(i, format!("symbol-{i}").as_bytes(), NoEci, X, NOWHERE);
            if i % 2 == 0 {
                read.symbology = Symbology::DataMatrix;
                read.key.eci = Unobserved;
                read.key.fnc1 = Fnc1::Unobserved;
            }
            store.collect(read);
        }
        assert_eq!((store.reads.len(), store.dropped), (MAX_DISTINCT_READS, 1));
        let found = store.reconcile();
        assert_eq!(found.len(), MAX_DETECTIONS);
        assert!(found.iter().all(|d| d.symbology == Symbology::QrCode));
        assert_eq!(found[0].key.raw, b"symbol-1");
        // a known content still folds in past the bound
        store.collect(read(70, b"symbol-1", NoEci, Q, NOWHERE));
        assert_eq!(store.reconcile()[0].engines, [Rxing, Rqrr]);
    }

    fn port(raw: &[u8], place: Option<f32>) -> Refusal {
        Refusal {
            port: Some(key(raw, Signalled, Fnc1::None)),
            rqrr: None,
            place: place.map(square_at),
            grid: None,
        }
    }

    fn published(store: &ReadStore) -> Vec<(Vec<u8>, Vec<EngineKind>)> {
        store
            .reconcile()
            .into_iter()
            .map(|d| (d.key.raw, d.engines))
            .collect()
    }

    #[test]
    fn a_refusal_withholds_the_reads_its_names_cover_and_nothing_else() {
        // the port key (abc, signalled, none) withholds unparsed QR Code
        // reads (abc, signalled) and (abc, unobserved) — retroactively
        let mut store = store(
            [
                read(0, b"abc", Signalled, X, NOWHERE),
                read(1, b"abc", Unobserved, Q, NOWHERE),
            ],
            &[],
        );
        assert_eq!(published(&store).len(), 1);
        store.refuse(port(b"abc", None), 2);
        assert!(
            published(&store).is_empty(),
            "a later proof marks earlier reads"
        );
        // never a no-ECI read, a GS1 one, a parsed read or another symbology
        let mut micro = read(4, b"abc", Signalled, X, NOWHERE);
        micro.symbology = Symbology::MicroQrCode;
        for other in [
            read(3, b"abc", NoEci, X, NOWHERE),
            fnc1(read(3, b"abc", Signalled, X, NOWHERE), Fnc1::First),
            parsed(read(3, b"abc", eci4(), X, NOWHERE)),
            micro,
        ] {
            let store = store_with(other.clone(), port(b"abc", None));
            assert_eq!(published(&store).len(), 1, "{other:?}");
        }
        // rqrr's bytes withhold rqrr's part of an unparsed read only
        let refusal = Refusal {
            port: None,
            rqrr: Some(b"xyz".to_vec()),
            place: None,
            grid: None,
        };
        let store = store_with(read(0, b"xyz", Unobserved, Q, NOWHERE), refusal.clone());
        assert_eq!(published(&store).len(), 0);
        let mut both = read(0, b"xyz", Unobserved, Q, NOWHERE);
        both.engines.push((
            Rxing,
            Rank {
                attempt: 1,
                ..Rank::default()
            },
        ));
        assert_eq!(
            published(&store_with(both, refusal)),
            [(b"xyz".to_vec(), vec![Rxing])]
        );
    }

    fn store_with(read: Read, refusal: Refusal) -> ReadStore {
        let mut store = ReadStore::default();
        store.collect(read);
        store.refuse(refusal, 0);
        store
    }

    #[test]
    fn a_refusal_withholds_only_where_the_refused_symbol_is() {
        // a well-formed symbol beside a refused one, their port reads alike —
        // the refusal withholds the read that lies inside the refused grid,
        // never the other
        let store = store_with(read(0, b"abc", Signalled, X, L), port(b"abc", R));
        assert_eq!(published(&store).len(), 1, "elsewhere: it stands");
        let store = store_with(read(0, b"abc", Signalled, X, R), port(b"abc", R));
        assert!(published(&store).is_empty(), "inside: withheld");
        // a read made at both places is evidence of a symbol elsewhere
        let mut twice = read(0, b"abc", Signalled, X, R);
        twice.fold(read(1, b"abc", Signalled, X, L));
        assert_eq!(published(&store_with(twice, port(b"abc", R))).len(), 1);
        // a read without a place never fails the place test
        let store = store_with(read(0, b"abc", Signalled, X, NOWHERE), port(b"abc", R));
        assert_eq!(published(&store).len(), 0);
    }

    /// The quad footprint `detector` measured of the 100 px square at `x`,
    /// in `attempt`, on a 1000 px plane.
    fn seen_at(x: f32, attempt: u32, detector: EngineKind) -> occurrence::Observation {
        occurrence::Observation {
            footprint: occurrence::Footprint::candidate(
                occurrence::Kind::Quad,
                square_at(x).to_vec(),
                detector,
            )
            .validated((1000, 1000)),
            rank: Rank {
                attempt,
                source: 0,
                index: 0,
            },
            side: 1000,
        }
    }

    #[test]
    fn a_standing_read_publishes_no_copy_where_its_refusal_holds() {
        // an unparsed read made at both places stands by the copy elsewhere;
        // the refused copy is not one of its occurrences
        let at = |n: u32, x: Option<f32>| {
            let mut hit = read(n, b"abc", Signalled, X, x);
            hit.observations.push(seen_at(x.unwrap(), n, Rxing));
            hit
        };
        let mut twice = at(0, R);
        twice.fold(at(1, L));
        let store = store_with(twice.clone(), port(b"abc", R));
        let [detection] = &store.reconcile()[..] else {
            panic!("{:?}", store.reconcile());
        };
        assert_eq!(detection.observations, vec![seen_at(L.unwrap(), 1, Rxing)]);
        // a refusal of another content holds nothing of it
        let store = store_with(twice.clone(), port(b"xyz", R));
        assert_eq!(store.reconcile()[0].observations.len(), 2);
        // nor does one without a place (it would withhold the read whole)
        let store = store_with(parsed(twice), port(b"abc", R));
        assert_eq!(
            store.reconcile()[0].observations.len(),
            2,
            "parsed: no refusal applies"
        );
    }

    #[test]
    fn a_parsed_read_of_the_same_attempt_protects_an_earlier_read() {
        // a proof and a parsed read admitting an earlier observation in one
        // attempt — the observation is kept, with its engines and first-seen
        // order
        let mut store = ReadStore::default();
        store.collect(read(0, b"abc", Signalled, X, NOWHERE));
        store.collect(read(1, b"zzz", NoEci, X, NOWHERE));
        store.refuse(port(b"abc", None), 2);
        store.collect(parsed(read(2, b"abc", eci4(), Q, L)));
        assert_eq!(
            published(&store),
            [
                (b"abc".to_vec(), vec![Rxing, Rqrr]),
                (b"zzz".to_vec(), vec![Rxing])
            ]
        );
        // so does the other walk's parsed content (the alpha seed)
        let mut seeded = ReadStore::seeded(Seed {
            refusals: vec![port(b"abc", None)],
            protection: vec![(Symbology::QrCode, key(b"abc", eci4(), Fnc1::None))],
            ..Seed::default()
        });
        seeded.collect(read(0, b"abc", Signalled, X, NOWHERE));
        assert_eq!(published(&seeded).len(), 1);
    }

    #[test]
    fn a_refusal_past_capacity_withholds_up_to_its_attempt() {
        // 16 recorded refusals; a 17th, found in attempt 3, withholds what
        // was read up to attempt 3 — a later attempt's read stands
        let mut store = ReadStore::default();
        for i in 0..MAX_REFUSALS {
            store.refuse(port(&[u8::try_from(i).unwrap()], None), 0);
        }
        store.collect(read(3, b"zzz", Signalled, X, NOWHERE));
        store.refuse(port(b"zzz", None), 3);
        assert_eq!(store.refusals.len(), MAX_REFUSALS, "the capacity holds");
        assert_eq!(published(&store).len(), 0);
        store.collect(read(3, b"zzz", Signalled, X, NOWHERE));
        assert!(published(&store).is_empty(), "the same attempt's read too");
        store.collect(read(4, b"zzz", Signalled, X, NOWHERE));
        assert_eq!(published(&store).len(), 1, "then it lapses");
        // the opposite-background walk receives it
        assert_eq!(store.seed(0).refusals.len(), MAX_REFUSALS + 1);
        let mut walk2 = ReadStore::seeded(store.seed(0));
        walk2.collect(read(9, b"zzz", Signalled, X, NOWHERE));
        assert_eq!(published(&walk2).len(), 0);
    }

    /// A port refusal of `raw` at a 100 px square at `x`.
    fn port_at(raw: &[u8], x: f32) -> Refusal {
        port(raw, Some(x))
    }

    /// The publication as a set: what stands, never in which order.
    fn published_set(store: &ReadStore) -> Vec<(Vec<u8>, Vec<EngineKind>)> {
        let mut found = published(store);
        found.sort_by(|a, b| a.0.cmp(&b.0));
        found
    }

    #[test]
    fn a_refused_stream_is_one_record_wherever_it_is_refused() {
        // a record is keyed by the stream's names, never by where it was
        // refused. One name refused at 17 jittered places (one symbol
        // rescaled at every attempt) is one record, so a second name refused
        // after it is a full-strength record: its read stays withheld after
        // the attempt that refused it
        let mut store = ReadStore::default();
        for i in 0..=16u16 {
            store.refuse(port_at(b"aaa", 10.0 + f32::from(i) * 0.25), u32::from(i));
        }
        store.refuse(port(b"bbb", R), 17);
        store.collect(read(17, b"bbb", Signalled, X, R));
        store.collect(read(18, b"bbb", Signalled, X, R));
        assert!(published(&store).is_empty(), "{:?}", published(&store));
        assert_eq!(store.refusals.len(), 2, "one record per name");
    }

    #[test]
    fn a_refusal_found_again_past_capacity_withholds_up_to_its_latest_attempt() {
        // 16 refused streams fill the capacity; a 17th found again at the
        // same place in a later attempt withholds up to that attempt — then
        // lapses where it is not found again
        let mut store = ReadStore::default();
        for i in 0..MAX_REFUSALS {
            store.refuse(port(&[u8::try_from(i).unwrap()], None), 0);
        }
        store.refuse(port(b"zzz", R), 17);
        store.refuse(port(b"zzz", R), 18);
        store.collect(read(18, b"zzz", Signalled, X, R));
        assert!(published(&store).is_empty(), "found again: withheld");
        store.collect(read(19, b"zzz", Signalled, X, R));
        assert_eq!(published(&store).len(), 1, "then it lapses");
    }

    #[test]
    fn refusals_and_reads_withhold_alike_in_every_arrival_order() {
        // within capacity, what a walk withholds depends on its refusals and
        // reads, never on their order. One symbol refused at 15 jittered
        // places first; then a small set in every order, each event at the
        // attempt it arrives in: a 16th place of that symbol, a second
        // stream, its read, a read of the first content elsewhere (another
        // symbol: it stands) and an unrelated read
        type Event = fn(&mut ReadStore, u32);
        let events: [Event; 5] = [
            |store, attempt| store.refuse(port_at(b"aaa", 14.0), attempt),
            |store, attempt| store.refuse(port(b"bbb", R), attempt),
            |store, attempt| store.collect(read(attempt, b"bbb", Signalled, X, R)),
            |store, attempt| store.collect(read(attempt, b"aaa", Signalled, X, Some(410.0))),
            |store, attempt| store.collect(read(attempt, b"zzz", NoEci, X, NOWHERE)),
        ];
        for order in permutations(events.len()) {
            let mut store = ReadStore::default();
            for i in 0..15u16 {
                store.refuse(port_at(b"aaa", 10.0 + f32::from(i) * 0.25), u32::from(i));
            }
            for (k, &event) in order.iter().enumerate() {
                events[event](&mut store, 15 + u32::try_from(k).unwrap());
            }
            assert_eq!(
                published_set(&store),
                [
                    (b"aaa".to_vec(), vec![Rxing]),
                    (b"zzz".to_vec(), vec![Rxing])
                ],
                "order {order:?}"
            );
        }
    }

    #[test]
    fn the_refusal_bookkeeping_is_bounded_and_fails_closed() {
        // past capacity at most as many records again are kept, never one
        // per refused grid of every attempt; past that the walk fails
        // closed — every unparsed QR Code read collected up to that attempt
        // is withheld, a parsed one never
        let mut store = ReadStore::default();
        store.collect(read(500, b"plain", NoEci, X, NOWHERE));
        store.collect(parsed(read(500, b"parsed", eci4(), Q, L)));
        for i in 0..1000u32 {
            store.refuse(port(format!("s{i}").as_bytes(), None), i);
        }
        assert!(
            store.overflow.len() <= MAX_REFUSALS,
            "{}",
            store.overflow.len()
        );
        assert_eq!(published_set(&store), [(b"parsed".to_vec(), vec![Rqrr])]);
        store.collect(read(1000, b"later", NoEci, X, NOWHERE));
        assert_eq!(published(&store).len(), 2, "a later read stands");
        // the opposite-background walk starts from a bounded seed and fails
        // closed throughout
        let seed = store.seed(0);
        assert!(
            seed.refusals.len() <= 2 * MAX_REFUSALS,
            "{}",
            seed.refusals.len()
        );
        let mut walk2 = ReadStore::seeded(seed);
        walk2.collect(read(3, b"other", NoEci, X, NOWHERE));
        assert_eq!(published(&walk2).len(), 0);
    }

    #[test]
    fn a_read_made_at_nine_scales_of_one_symbol_keeps_a_place_for_its_twin() {
        // places are told apart by containment, never by exact equality:
        // one symbol read at nine attempt scales is one place, so a twin
        // read elsewhere is recorded too and a refusal of the first symbol
        // never withholds the folded read — whichever arrives last
        let scales: Vec<Read> = (1..=9u16)
            .map(|i| {
                read(
                    u32::from(i),
                    b"abc",
                    Signalled,
                    X,
                    Some(10.0 + f32::from(i) * 0.1),
                )
            })
            .collect();
        let twin = read(10, b"abc", Signalled, X, R);
        for twin_first in [true, false] {
            let mut reads = scales.clone();
            if twin_first {
                reads.insert(0, twin.clone());
            } else {
                reads.push(twin.clone());
            }
            let store = store(reads, &[port(b"abc", L)]);
            assert_eq!(published(&store).len(), 1, "twin first: {twin_first}");
            assert_eq!(store.reads[0].places.len(), 2, "one place per symbol");
        }
    }

    #[test]
    fn geometry_comes_from_the_detections_earliest_read_that_carries_it() {
        // spec/07: geometry comes from the detection's earliest read that
        // carries it. An unobserved rqrr read (attempt 0, corners) joins the
        // detection a later parsed read forms (attempt 1) in step 3: the
        // corners and their photometry are the earlier read's, in any order
        let mut early = read(0, A1, Unobserved, Q, L);
        early.geometry.corners = Some((square_at(10.0), true));
        let mut later = parsed(read(1, A1, NoEci, Q, Some(12.0)));
        later.geometry.corners = Some((square_at(12.0), false));
        let found = every_order(&[early, later], &[]);
        assert_eq!(found.len(), 1);
        assert_eq!(
            found[0]
                .geometry
                .corners
                .map(|(corners, inverted)| (corners[0].x, inverted)),
            Some((10.0, true))
        );
    }

    #[test]
    fn the_walk_goes_on_while_only_signalled_qr_reads_were_found() {
        // continuation over the reads that stand
        let settled = |reads: Vec<Read>, refusals: &[Refusal]| store(reads, refusals).settled();
        assert!(!settled(Vec::new(), &[]), "nothing found");
        assert!(!settled(vec![read(0, b"abc", Signalled, X, NOWHERE)], &[]));
        let mut ean = read(1, b"4006381333931", Unobserved, X, NOWHERE);
        ean.symbology = Symbology::Ean13;
        assert!(
            !settled(
                vec![read(0, b"abc", Signalled, X, NOWHERE), ean.clone()],
                &[]
            ),
            "other symbologies neither stop nor prolong it"
        );
        assert!(settled(vec![ean], &[]));
        for account in [NoEci, Unobserved, located(&[(26, 0)])] {
            assert!(
                settled(
                    vec![
                        read(0, b"abc", Signalled, X, NOWHERE),
                        read(1, b"xyz", account.clone(), X, NOWHERE),
                    ],
                    &[]
                ),
                "{account:?}"
            );
        }
        for symbology in [Symbology::MicroQrCode, Symbology::RectangularMicroQrCode] {
            let mut other = read(0, b"abc", Signalled, X, NOWHERE);
            other.symbology = symbology;
            assert!(settled(vec![other], &[]), "{symbology:?}");
        }
        // withheld reads do not count
        assert!(!settled(
            vec![
                read(0, b"abc", Signalled, X, NOWHERE),
                read(1, b"zzz", Signalled, X, NOWHERE),
                read(2, b"zzz", Unobserved, Q, NOWHERE),
            ],
            &[port(b"zzz", None)]
        ));
        // a signalled read a candidate parse contains with an agreeing
        // account counts as located
        assert!(settled(
            vec![
                read(0, b"abc", Signalled, X, L),
                read(1, b"abc", located(&[(26, 0)]), C, L),
            ],
            &[]
        ));
        assert!(!settled(
            vec![
                read(0, b"abc", Signalled, X, L),
                read(1, b"abc", located(&[(26, 0)]), C, R),
            ],
            &[]
        ));
    }

    fn arbitrary_read() -> impl Strategy<Value = (u8, u8, u8, u8, u8)> {
        (0u8..2, 0u8..5, 0u8..3, 0u8..3, 0u8..3)
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(64))]
        /// Order independence over arbitrary sets of up to 64 reads.
        #[test]
        fn reconcile_is_order_independent_over_arbitrary_sets(
            shapes in proptest::collection::vec(arbitrary_read(), 1..64)
                .prop_flat_map(|shapes| {
                    let n = shapes.len();
                    (Just(shapes), Just((0..n).collect::<Vec<usize>>()).prop_shuffle())
                }),
        ) {
            let (shapes, order) = shapes;
            let reads: Vec<Read> = shapes
                .iter()
                .enumerate()
                .map(|(n, &(raw, eci, fnc1_code, engine, at))| {
                    let raw: &[u8] = if raw == 0 { A1 } else { b"abc" };
                    let eci = match eci {
                        0 => Unobserved,
                        1 => NoEci,
                        2 => Signalled,
                        3 => eci4(),
                        _ => located(&[(7, 0)]),
                    };
                    let engine = [X, Q, C][usize::from(engine)];
                    let at = [NOWHERE, L, R][usize::from(at)];
                    let position = [Fnc1::None, Fnc1::First, Fnc1::Unobserved][usize::from(fnc1_code)];
                    let mut read = fnc1(read(u32::try_from(n).unwrap(), raw, eci, engine, at), position);
                    if engine == Q && matches!(read.key.eci, EciEvidence::Located(_) | NoEci) {
                        read = parsed(read);
                    }
                    read
                })
                .collect();
            let expected = summary(&store(reads.iter().cloned(), &[]).reconcile());
            let shuffled = order.iter().map(|&i| reads[i].clone());
            prop_assert_eq!(summary(&store(shuffled, &[]).reconcile()), expected);
        }
    }

    /// The read, with a known correction margin of its own word.
    fn margin(mut read: Read, m: i32) -> Read {
        read.margin = Margin::Known(m);
        read
    }

    #[test]
    fn a_content_whose_every_known_margin_is_negative_is_refused() {
        // 07-pipeline § Acceptance: errors alone took every measured word of
        // the content past the Annex B reserve
        let rqrr = |m| margin(parsed(read(1, b"abc", NoEci, Q, L)), m);
        let rxing = read(0, b"abc", NoEci, X, L);
        // alone: nothing published, nothing settled, S5 not stopped
        let alone = store([rqrr(-1)], &[]);
        assert!(alone.reconcile().is_empty() && !alone.settled() && !alone.found());
        // a read of unknown margin inherits the refusal
        assert!(store([rqrr(-1), rxing.clone()], &[]).reconcile().is_empty());
        // one read keeping a margin confirms the content: one detection, the
        // refused word's engine among its engines
        let found = every_order(&[rqrr(-1), margin(rxing.clone(), 0)], &[]);
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        // two negative margins never confirm each other
        assert!(every_order(&[rqrr(-1), margin(rxing.clone(), -2)], &[]).is_empty());
        // no known margin: published as before
        assert_eq!(
            engines(&store([rxing.clone()], &[]).reconcile()),
            [vec![Rxing]]
        );
        // another content is not touched: other bytes, or an account the
        // refused one does not agree with
        let other = read(2, b"xyz", NoEci, X, R);
        let declared = read(3, b"abc", located(&[(26, 0)]), X, R);
        let found = every_order(&[rqrr(-1), other, declared], &[]);
        assert_eq!(
            found.iter().map(|d| d.key.raw.clone()).collect::<Vec<_>>(),
            [b"xyz".to_vec(), b"abc".to_vec()]
        );
        // folded reads keep their best margin
        let mut folded = store([rqrr(-1)], &[]);
        folded.collect(margin(parsed(read(4, b"abc", NoEci, Q, L)), 2));
        assert_eq!(engines(&folded.reconcile()), [vec![Rqrr]]);
    }

    /// A UPC-E read of `raw` made on a plane whose longest side is `side`.
    fn upce(n: u32, raw: &[u8], side: u32) -> Read {
        let mut read = fnc1(read(n, raw, Unobserved, X, NOWHERE), Fnc1::Unobserved);
        read.symbology = Symbology::UpcE;
        read.needs_confirmation = true;
        read.scales = Some((side, side));
        read
    }

    #[test]
    fn a_read_that_needs_confirmation_stands_on_two_independent_scales() {
        let raw = b"04917052";
        // one plane: provisional — nothing published or settled, S5 runs
        let one = store([upce(0, raw, 512)], &[]);
        assert!(one.reconcile().is_empty() && !one.settled() && !one.found());
        assert!(!one.confirmed(Symbology::UpcE, raw));
        // planes of equal dimensions never confirm each other
        assert!(
            store([upce(0, raw, 552), upce(1, raw, 552)], &[])
                .reconcile()
                .is_empty()
        );
        // longest sides 1.25 apart do; just under does not
        let two = store([upce(0, raw, 512), upce(1, raw, 640)], &[]);
        assert_eq!(two.reconcile().len(), 1);
        assert!(two.settled() && two.found() && two.confirmed(Symbology::UpcE, raw));
        assert!(
            store([upce(0, raw, 512), upce(1, raw, 639)], &[])
                .reconcile()
                .is_empty()
        );
        // other bytes of the symbology confirm nothing
        assert!(
            store([upce(0, raw, 512), upce(1, b"15658982", 1024)], &[])
                .reconcile()
                .is_empty()
        );
        // an EAN-13 read alone stands and settles
        let mut ean = read(0, b"9506000134352", Unobserved, X, NOWHERE);
        ean.symbology = Symbology::Ean13;
        ean.scales = Some((512, 512));
        assert!(store([ean], &[]).settled());
        // the decision is one of the read set, in every order
        let reads = [upce(0, raw, 512), upce(1, raw, 552), upce(2, raw, 1024)];
        assert_eq!(every_order(&reads, &[]).len(), 1);
    }

    #[test]
    fn a_fold_unites_the_parse_keeps_eight_places_and_waits_only_if_both_wait() {
        // one read parsed and one not, identical otherwise: the stream was
        // parsed, whichever folds in
        let unparsed = read(0, A1, NoEci, X, L);
        let mut parsed_too = read(1, A1, NoEci, X, L);
        parsed_too.parsed = true;
        let mut folded = unparsed.clone();
        folded.fold(parsed_too.clone());
        assert!(folded.parsed);
        let mut folded = parsed_too;
        folded.fold(unparsed);
        assert!(folded.parsed);
        // a read that needs no confirmation clears the wait; two that wait,
        // wait
        let mut waiting = upce(0, b"04917052", 512);
        let mut settled = upce(1, b"04917052", 512);
        settled.needs_confirmation = false;
        waiting.fold(settled);
        assert!(!waiting.needs_confirmation);
        let mut waiting = upce(0, b"04917052", 512);
        waiting.fold(upce(1, b"04917052", 640));
        assert!(waiting.needs_confirmation);
        // nine symbols, 200 px apart: the first eight places are kept
        let mut many = read(0, A1, NoEci, X, Some(0.0));
        for (n, x) in (1u32..).zip([200.0, 400.0, 600.0, 800.0, 1000.0, 1200.0, 1400.0, 1600.0]) {
            many.fold(read(n, A1, NoEci, X, Some(x)));
        }
        assert_eq!(many.places.len(), MAX_PLACES);
        assert_eq!(many.places.last().map(|place| place[0].x), Some(1400.0));
    }

    #[test]
    fn a_refused_stream_remembers_eight_symbols_then_withholds_anywhere() {
        let mut refused = Refused::new(port(b"abc", Some(0.0)), 0);
        // a second symbol: both remembered, a read elsewhere is not held
        refused.again(Some(square_at(200.0)), 1);
        assert_eq!(refused.places.as_ref().map(Vec::len), Some(2));
        assert!(refused.holds(square_at(200.0)) && !refused.holds(square_at(400.0)));
        for (attempt, x) in (2u32..).zip([400.0, 600.0, 800.0, 1000.0, 1200.0, 1400.0]) {
            refused.again(Some(square_at(x)), attempt);
        }
        assert_eq!(refused.places.as_ref().map(Vec::len), Some(MAX_PLACES));
        assert!(!refused.holds(square_at(1600.0)));
        // a ninth symbol: refused at more than it remembers, anywhere
        refused.again(Some(square_at(1600.0)), 8);
        assert!(refused.places.is_none());
        assert!(refused.holds(square_at(5000.0)));
        assert_eq!(refused.attempt, 8);
    }

    #[test]
    fn rqrr_s_part_is_withheld_only_by_rqrr_s_own_bytes() {
        // a refusal naming another content where an rqrr read lies: no part
        // of that read is withheld, whichever name the refusal carries
        for refusal in [
            port(b"xyz", L),
            Refusal {
                port: None,
                rqrr: Some(b"xyz".to_vec()),
                place: L.map(square_at),
                grid: None,
            },
        ] {
            let store = store_with(read(0, A1, Unobserved, Q, L), refusal.clone());
            assert_eq!(
                published(&store),
                [(A1.to_vec(), vec![Rqrr])],
                "{refusal:?}"
            );
        }
    }

    #[test]
    fn a_margin_counts_for_a_standing_read_of_the_same_content_within_the_reserve() {
        let asked = read(9, A1, NoEci, X, L);
        let measured = |read: Read| store([read], &[]).has_margin(&asked);
        assert!(measured(margin(read(0, A1, NoEci, Q, L), 0)));
        // another content within the reserve, the same one past it or not
        // measured at all: no margin to rely on
        assert!(!measured(margin(read(0, E_ACUTE, NoEci, Q, L), 0)));
        assert!(!measured(margin(read(0, A1, eci4(), Q, L), 0)));
        assert!(!measured(margin(read(0, A1, NoEci, Q, L), -1)));
        assert!(!measured(read(0, A1, NoEci, Q, L)));
        // a read the refusals withhold does not stand
        let withheld = store_with(margin(read(0, A1, Signalled, Q, L), 0), port(A1, L));
        assert!(!withheld.has_margin(&read(9, A1, Signalled, X, L)));
    }

    #[test]
    fn an_unparsed_qr_read_is_a_standing_qr_code_engine_read_no_parse_read() {
        let unparsed = |read: Read| store([read], &[]).unparsed_qr();
        assert!(unparsed(read(0, A1, NoEci, X, L)));
        assert!(unparsed(read(0, A1, NoEci, Q, L)));
        assert!(!unparsed(parsed(read(0, A1, NoEci, X, L))), "parsed");
        let mut bare = read(0, A1, NoEci, C, L);
        bare.parsed = false;
        assert!(!unparsed(bare), "no engine");
        let mut micro = read(0, A1, NoEci, X, L);
        micro.symbology = Symbology::MicroQrCode;
        assert!(!unparsed(micro), "another symbology");
        assert!(!ReadStore::default().unparsed_qr());
        let withheld = store_with(read(0, A1, Signalled, X, L), port(A1, L));
        assert!(!withheld.unparsed_qr(), "withheld");
    }

    #[test]
    fn a_read_of_unknown_evidence_joins_only_a_detection_of_its_fnc1_position() {
        // a located GS1 detection at L and an rqrr read of its bytes there:
        // unobserved evidence agrees, FNC1 none does not — two detections
        let gs1 = fnc1(parsed(read(0, A1, eci4(), X, L)), Fnc1::First);
        let found = every_order(&[gs1.clone(), read(1, A1, Unobserved, Q, L)], &[]);
        assert_eq!(engines(&found), [vec![Rxing], vec![Rqrr]]);
        // a position unknown, a located account it does not agree with: the
        // same
        let declared = fnc1(read(1, A1, NoEci, Q, L), Fnc1::Unobserved);
        let found = every_order(&[gs1, declared], &[]);
        assert_eq!(engines(&found), [vec![Rxing], vec![Rqrr]]);
    }

    #[test]
    fn reads_forming_a_detection_of_one_identity_form_one() {
        // reconciled as given: identical reads would fold in a store first
        // signalled reads of one position, no located detection: one
        let found = reconcile(vec![
            read(0, A1, Signalled, X, L),
            read(1, A1, Signalled, Q, L),
        ]);
        assert_eq!(engines(&found), [vec![Rxing, Rqrr]]);
        // of two positions: two
        let found = reconcile(vec![
            read(0, A1, Signalled, X, L),
            fnc1(read(1, A1, Signalled, Q, L), Fnc1::First),
        ]);
        assert_eq!(engines(&found), [vec![Rxing], vec![Rqrr]]);
        // unobserved reads with nothing formed before them: one per identity
        let found = reconcile(vec![
            read(0, A1, Unobserved, Q, L),
            read(1, A1, Unobserved, X, L),
        ]);
        assert_eq!(engines(&found), [vec![Rqrr, Rxing]]);
    }

    #[test]
    fn a_detection_s_engine_reading_is_its_reads_one_reading() {
        // reconciled as given: identical reads would fold in a store first
        let with = |n, engine, reading| engine_reading(read(n, YAMADA, NoEci, engine, L), reading);
        let found = reconcile(vec![
            with(0, X, EngineReading::ShiftJis),
            with(1, Q, EngineReading::ShiftJis),
        ]);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].engine_reading, EngineReading::ShiftJis);
        let found = reconcile(vec![
            with(0, X, EngineReading::ShiftJis),
            with(1, Q, EngineReading::Gb18030),
        ]);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].engine_reading, EngineReading::None);
        // a read without one says nothing either way
        let found = reconcile(vec![
            with(0, X, EngineReading::None),
            with(1, Q, EngineReading::Gb18030),
        ]);
        assert_eq!(found[0].engine_reading, EngineReading::Gb18030);
    }

    #[test]
    fn a_standing_read_publishes_no_copy_where_rqrr_refused_its_bytes() {
        // rqrr decoded the read's bytes from the stream refused at R: that
        // copy's stream was proved malformed. An rqrr read made at R and L
        // stands by L — rqrr's bytes withhold a read only where every copy
        // of it lies inside the refused symbol — and publishes L alone
        let at = |n: u32, x: Option<f32>| {
            let mut hit = read(n, b"abc", NoEci, Q, x);
            hit.observations.push(seen_at(x.unwrap(), n, Rqrr));
            hit
        };
        let rqrr = |raw: &[u8]| Refusal {
            port: None,
            rqrr: Some(raw.to_vec()),
            place: R.map(square_at),
            grid: None,
        };
        let mut twice = at(0, R);
        twice.fold(at(1, L));
        let store = store_with(twice.clone(), rqrr(b"abc"));
        let [detection] = &store.reconcile()[..] else {
            panic!("{:?}", store.reconcile());
        };
        assert_eq!(detection.engines, [Rqrr]);
        assert_eq!(detection.observations, [seen_at(L.unwrap(), 1, Rqrr)]);
        // other bytes refused there hold no copy of it
        let store = store_with(twice, rqrr(b"xyz"));
        let [detection] = &store.reconcile()[..] else {
            panic!("{:?}", store.reconcile());
        };
        assert_eq!(
            detection.observations,
            [seen_at(R.unwrap(), 0, Rqrr), seen_at(L.unwrap(), 1, Rqrr)]
        );
    }

    #[test]
    fn a_read_standing_on_a_parse_elsewhere_publishes_no_refused_copy() {
        // the scanner parsed the content at L and refused the stream at R
        // under the port's account of it: rxing's read at R stands on that
        // parse, yet its copy at R is not one of its occurrences
        let refused = Refusal {
            port: Some(key(b"abc", NoEci, Fnc1::None)),
            rqrr: None,
            place: R.map(square_at),
            grid: None,
        };
        let mut at_r = read(0, b"abc", NoEci, X, R);
        at_r.observations.push(seen_at(R.unwrap(), 0, Rxing));
        let mut at_l = parsed(read(1, b"abc", NoEci, Q, L));
        at_l.observations.push(seen_at(L.unwrap(), 1, Rqrr));
        let store = store([at_r.clone(), at_l], std::slice::from_ref(&refused));
        let [detection] = &store.reconcile()[..] else {
            panic!("{:?}", store.reconcile());
        };
        assert_eq!(detection.engines, [Rxing, Rqrr]);
        assert_eq!(detection.observations, [seen_at(L.unwrap(), 1, Rqrr)]);
        // the other walk's parse (the alpha seed) protects it the same way:
        // a read made at R and L stands, without its copy at R
        let mut seeded = ReadStore::seeded(Seed {
            refusals: vec![refused],
            protection: vec![(Symbology::QrCode, key(b"abc", NoEci, Fnc1::None))],
            ..Seed::default()
        });
        let mut at_l = read(2, b"abc", NoEci, X, L);
        at_l.observations.push(seen_at(L.unwrap(), 2, Rxing));
        let mut twice = at_r;
        twice.fold(at_l);
        seeded.collect(twice);
        let [detection] = &seeded.reconcile()[..] else {
            panic!("{:?}", seeded.reconcile());
        };
        assert_eq!(detection.engines, [Rxing]);
        assert_eq!(detection.observations, [seen_at(L.unwrap(), 2, Rxing)]);
    }

    #[test]
    fn a_fold_flags_a_footprint_left_out_by_either_read_or_by_itself() {
        // a read's occurrences are incomplete once a footprint was left out
        // past MAX_OBSERVATIONS: the flag survives every fold, whichever
        // read carried it, and a fold that crowds a footprint out raises it
        let with = |n: u32, copies: std::ops::Range<u16>, dropped: bool| {
            let mut hit = read(n, A1, NoEci, X, L);
            hit.observations = copies.map(|x| seen_at(f32::from(x), n, Rxing)).collect();
            hit.observations_dropped = dropped;
            hit
        };
        let folded = |mut own: Read, other: Read| {
            own.fold(other);
            (own.observations.len(), own.observations_dropped)
        };
        assert_eq!(occurrence::MAX_OBSERVATIONS, 128);
        // room for both: nothing left out
        assert_eq!(
            folded(with(0, 0..64, false), with(1, 64..128, false)),
            (128, false)
        );
        // one footprint too many: the fold leaves one out and says so
        assert_eq!(
            folded(with(0, 0..64, false), with(1, 64..129, false)),
            (128, true)
        );
        // a folded-in read had left one out: flagged, though the fold had room
        assert_eq!(folded(with(0, 0..1, false), with(1, 1..2, true)), (2, true));
        // both left one out: still flagged
        assert_eq!(
            folded(with(0, 0..64, false), with(1, 64..129, true)),
            (128, true)
        );
        // the read's own flag survives a fold that leaves nothing out
        assert_eq!(folded(with(0, 0..1, true), with(1, 1..2, false)), (2, true));
        // and the walk's publication says so: the store folds the two reads
        let store = store([with(0, 0..64, false), with(1, 64..129, false)], &[]);
        let [detection] = &store.reconcile()[..] else {
            panic!("{:?}", store.reconcile());
        };
        assert_eq!(
            (detection.observations.len(), detection.observations_dropped),
            (128, true)
        );
    }

    #[test]
    fn the_published_contents_saturate_only_past_their_capacity() {
        // as many distinct contents as are published: every one of them,
        // nothing left out; one more: the latest is left out, and said so
        let contents = |count: u32| -> Vec<Read> {
            (0..count)
                .map(|n| read(n, format!("symbol-{n}").as_bytes(), NoEci, X, NOWHERE))
                .collect()
        };
        let published = |found: &[Reconciled]| -> Vec<String> {
            found
                .iter()
                .map(|detection| String::from_utf8(detection.key.raw.clone()).unwrap())
                .collect()
        };
        let capacity = u32::try_from(MAX_DETECTIONS).unwrap();
        let first: Vec<String> = (0..capacity).map(|n| format!("symbol-{n}")).collect();
        let (found, saturated) = reconcile_counted(contents(capacity));
        assert_eq!((published(&found), saturated), (first.clone(), false));
        let (found, saturated) = reconcile_counted(contents(capacity + 1));
        assert_eq!((published(&found), saturated), (first, true));
    }
}