mopenzl 0.2.1

OpenZL compression and decompression in safe Rust; encoding is byte-identical to Meta's reference library
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
//! Compression graphs: standard graphs and nodes, parameterization, and
//! user-defined selectors, function graphs and segmenters.
//!
//! A frame records the codecs that produced it, not the graph; the graph is
//! only the recipe the encoder follows. [`StandardGraph`] and
//! [`StandardNode`] name the reference's registry with its numeric IDs. A
//! [`GraphBuilder`] derives new graphs from them (a node followed by
//! successors, parameterized copies, selectors and segmenters written in
//! Rust) and [`GraphBuilder::build`] freezes the result into a cheap-to-share
//! [`CompressorGraph`] for [`CompressorBuilder::with_graph`](crate::CompressorBuilder::with_graph).
//!
//! Standard nodes whose codecs are not ported yet exist in the registry but
//! fail when run: the engine reacts as the reference does to a failing codec
//! (a permissive compression falls back to `compress_generic`, a chunk that
//! cannot be built is an error).
//!
//! # Examples
//!
//! Store typed data, then Zstandard-compress serial data:
//!
//! ```
//! use mopenzl::graph::{GraphBuilder, StandardGraph};
//! use mopenzl::{Compressor, EncoderConfig};
//!
//! let graph = GraphBuilder::new().build(StandardGraph::Store)?;
//! let mut compressor = Compressor::builder(EncoderConfig::default())
//!     .with_graph(graph)
//!     .build()?;
//! let frame = compressor.compress(b"hello world, hello world")?;
//! # #[cfg(feature = "decompression")]
//! assert_eq!(mopenzl::Decompressor::default().decompress(&frame)?, b"hello world, hello world");
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use alloc::borrow::Cow;

use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::ops::BitOr;
use core::sync::atomic::{AtomicU32, Ordering};

use thiserror::Error;

use crate::DataType;
use crate::compressor::core as engine;
use crate::compressor::core::data::{Bytes, Stream, type_mask};
use crate::compressor::core::error::ExecError;
use crate::compressor::core::exec::{Edge as CoreEdge, GraphCx, SegmenterCx, SelectorCx};
use crate::compressor::core::params::{LocalParams as CoreParams, RefParam};
use crate::compressor::core::registry::dict::LoadError;
use crate::compressor::core::registry::{
    self as reg, Callback, GraphDef, GraphKind, NodeDef, Registry, RuntimeParams,
};
mod config;
pub mod params;

pub use config::{ClusteringConfig, ConfigError, GbtModel, GbtNode, MlSelectorConfig};

/// The integer metadata ID that carries an input's tag for the clustering
/// graph (`ZL_CLUSTERING_TAG_METADATA_ID`).
pub const CLUSTERING_TAG_METADATA_ID: i32 = params::clustering::TAG_METADATA_ID;

/// The local parameter ID under which the clustering graph reads its CBOR
/// configuration (`ZL_GENERIC_CLUSTERING_CONFIG_ID`).
const CLUSTERING_CONFIG_PARAM: i32 = engine::graphs::clustering::CONFIG_PARAM_ID;

use crate::dict::{DictBundle, DictError, DictId};

/// A numeric ID that names no graph or node here.
#[derive(Error, Debug, Clone, Copy, Eq, PartialEq)]
#[error("{0} is not the ID of a supported standard graph or node")]
pub struct UnknownId(pub u32);

/// A node of the reference registry, by its `ZL_StandardNodeID`. IDs from 57 on are private in the reference but stable here.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
#[repr(u32)]
pub enum StandardNode {
    /// `delta_int` (2).
    DeltaInt = 2,
    /// `transpose_split` (3).
    TransposeSplit = 3,
    /// `zigzag` (4).
    Zigzag = 4,
    /// `dispatchN_byTag` (5).
    DispatchNByTag = 5,
    /// `float32_deconstruct` (6).
    Float32Deconstruct = 6,
    /// `bfloat16_deconstruct` (7).
    Bfloat16Deconstruct = 7,
    /// `float16_deconstruct` (8).
    Float16Deconstruct = 8,
    /// `field_lz` (9).
    FieldLz = 9,
    /// `convert_struct_to_serial` (10).
    ConvertStructToSerial = 10,
    /// `convert_num_to_struct_le` (11).
    ConvertNumToStructLe = 11,
    /// `convert_num_to_serial_le` (12).
    ConvertNumToSerialLe = 12,
    /// `convert_serial_to_struct` (13).
    ConvertSerialToStruct = 13,
    /// `convert_serial_to_struct2` (14).
    ConvertSerialToStruct2 = 14,
    /// `convert_serial_to_struct4` (15).
    ConvertSerialToStruct4 = 15,
    /// `convert_serial_to_struct8` (16).
    ConvertSerialToStruct8 = 16,
    /// `convert_struct_to_num_le` (17).
    ConvertStructToNumLe = 17,
    /// `convert_struct_to_num_be` (18).
    ConvertStructToNumBe = 18,
    /// `convert_serial_to_num8` (19).
    ConvertSerialToNum8 = 19,
    /// `convert_serial_to_num_le16` (20).
    ConvertSerialToNumLe16 = 20,
    /// `convert_serial_to_num_le32` (21).
    ConvertSerialToNumLe32 = 21,
    /// `convert_serial_to_num_le64` (22).
    ConvertSerialToNumLe64 = 22,
    /// `convert_serial_to_num_be16` (23).
    ConvertSerialToNumBe16 = 23,
    /// `convert_serial_to_num_be32` (24).
    ConvertSerialToNumBe32 = 24,
    /// `convert_serial_to_num_be64` (25).
    ConvertSerialToNumBe64 = 25,
    /// `separate_string_components` (26).
    SeparateStringComponents = 26,
    /// `bitunpack` (27).
    Bitunpack = 27,
    /// `range_pack` (28).
    RangePack = 28,
    /// `merge_sorted` (29).
    MergeSorted = 29,
    /// `prefix` (30).
    Prefix = 30,
    /// `divide_by` (31).
    DivideBy = 31,
    /// `dispatch_string` (32).
    DispatchString = 32,
    /// `concat_serial` (33).
    ConcatSerial = 33,
    /// `concat_num` (34).
    ConcatNum = 34,
    /// `concat_struct` (35).
    ConcatStruct = 35,
    /// `concat_string` (36).
    ConcatString = 36,
    /// `dedup_num` (37).
    DedupNum = 37,
    /// `parse_int` (38).
    ParseInt = 38,
    /// `interleave_string` (39).
    InterleaveString = 39,
    /// `tokenize_struct` (40).
    TokenizeStruct = 40,
    /// `tokenize_numeric` (41).
    TokenizeNumeric = 41,
    /// `tokenize_string` (42).
    TokenizeString = 42,
    /// `quantize_offsets` (43).
    QuantizeOffsets = 43,
    /// `quantize_lengths` (44).
    QuantizeLengths = 44,
    /// `bitsplit_top8` (45).
    BitsplitTop8 = 45,
    /// `bitsplit_fp` (46).
    BitsplitFp = 46,
    /// `bitsplit_bf16` (47).
    BitsplitBf16 = 47,
    /// `partition` (48).
    Partition = 48,
    /// `split_byrange` (49).
    SplitByrange = 49,
    /// `sentinel_byte` (50).
    SentinelByte = 50,
    /// `sentinel_num` (51).
    SentinelNum = 51,
    /// `lz` (52).
    Lz = 52,
    /// `mux_lengths` (53).
    MuxLengths = 53,
    /// `sparse_num` (54).
    SparseNum = 54,
    /// `sparse_num_auto` (55).
    SparseNumAuto = 55,
    /// `set_string_lens` (57).
    SetStringLens = 57,
    /// `fse_v2` (58).
    FseV2 = 58,
    /// `huffman_v2` (59).
    HuffmanV2 = 59,
    /// `huffman_struct_v2` (60).
    HuffmanStructV2 = 60,
    /// `fse_ncount` (61).
    FseNcount = 61,
    /// `zstd` (62).
    Zstd = 62,
    /// `bitpack_serial` (63).
    BitpackSerial = 63,
    /// `bitpack_int` (64).
    BitpackInt = 64,
    /// `flatpack` (65).
    Flatpack = 65,
    /// `splitN` (66).
    SplitN = 66,
    /// `splitN_struct` (67).
    SplitNStruct = 67,
    /// `splitN_num` (68).
    SplitNNum = 68,
    /// `split_by_struct` (69).
    SplitByStruct = 69,
    /// `constant_serial` (70).
    ConstantSerial = 70,
    /// `constant_fixed` (71).
    ConstantFixed = 71,
    /// `tokenize_sorted` (72).
    TokenizeSorted = 72,
    /// `tokenize_string_sorted` (73).
    TokenizeStringSorted = 73,
    /// `dedup_num_trusted` (74).
    DedupNumTrusted = 74,
    /// `bitSplit` (75).
    BitSplit = 75,
    /// `lz4` (86).
    Lz4 = 86,
    /// `pivco_huffman` (87).
    PivcoHuffman = 87,
}

impl TryFrom<u32> for StandardNode {
    type Error = UnknownId;

    /// Looks the node up by its numeric ID in the reference.
    ///
    /// # Errors
    ///
    /// [`UnknownId`] for IDs that name no node supported here.
    fn try_from(id: u32) -> Result<Self, Self::Error> {
        Ok(match id {
            2 => Self::DeltaInt,
            3 => Self::TransposeSplit,
            4 => Self::Zigzag,
            5 => Self::DispatchNByTag,
            6 => Self::Float32Deconstruct,
            7 => Self::Bfloat16Deconstruct,
            8 => Self::Float16Deconstruct,
            9 => Self::FieldLz,
            10 => Self::ConvertStructToSerial,
            11 => Self::ConvertNumToStructLe,
            12 => Self::ConvertNumToSerialLe,
            13 => Self::ConvertSerialToStruct,
            14 => Self::ConvertSerialToStruct2,
            15 => Self::ConvertSerialToStruct4,
            16 => Self::ConvertSerialToStruct8,
            17 => Self::ConvertStructToNumLe,
            18 => Self::ConvertStructToNumBe,
            19 => Self::ConvertSerialToNum8,
            20 => Self::ConvertSerialToNumLe16,
            21 => Self::ConvertSerialToNumLe32,
            22 => Self::ConvertSerialToNumLe64,
            23 => Self::ConvertSerialToNumBe16,
            24 => Self::ConvertSerialToNumBe32,
            25 => Self::ConvertSerialToNumBe64,
            26 => Self::SeparateStringComponents,
            27 => Self::Bitunpack,
            28 => Self::RangePack,
            29 => Self::MergeSorted,
            30 => Self::Prefix,
            31 => Self::DivideBy,
            32 => Self::DispatchString,
            33 => Self::ConcatSerial,
            34 => Self::ConcatNum,
            35 => Self::ConcatStruct,
            36 => Self::ConcatString,
            37 => Self::DedupNum,
            38 => Self::ParseInt,
            39 => Self::InterleaveString,
            40 => Self::TokenizeStruct,
            41 => Self::TokenizeNumeric,
            42 => Self::TokenizeString,
            43 => Self::QuantizeOffsets,
            44 => Self::QuantizeLengths,
            45 => Self::BitsplitTop8,
            46 => Self::BitsplitFp,
            47 => Self::BitsplitBf16,
            48 => Self::Partition,
            49 => Self::SplitByrange,
            50 => Self::SentinelByte,
            51 => Self::SentinelNum,
            52 => Self::Lz,
            53 => Self::MuxLengths,
            54 => Self::SparseNum,
            55 => Self::SparseNumAuto,
            57 => Self::SetStringLens,
            58 => Self::FseV2,
            59 => Self::HuffmanV2,
            60 => Self::HuffmanStructV2,
            61 => Self::FseNcount,
            62 => Self::Zstd,
            63 => Self::BitpackSerial,
            64 => Self::BitpackInt,
            65 => Self::Flatpack,
            66 => Self::SplitN,
            67 => Self::SplitNStruct,
            68 => Self::SplitNNum,
            69 => Self::SplitByStruct,
            70 => Self::ConstantSerial,
            71 => Self::ConstantFixed,
            72 => Self::TokenizeSorted,
            73 => Self::TokenizeStringSorted,
            74 => Self::DedupNumTrusted,
            75 => Self::BitSplit,
            86 => Self::Lz4,
            87 => Self::PivcoHuffman,
            _ => return Err(UnknownId(id)),
        })
    }
}

impl From<StandardNode> for u32 {
    fn from(value: StandardNode) -> Self {
        value as Self
    }
}

/// A graph of the reference registry, by its `ZL_StandardGraphID`. IDs from 32 on are private in the reference but stable here.
///
/// # Successors
///
/// Most graphs are self-contained. A few take successor graphs (and, for the
/// splits and clustering, nodes) as the *custom graphs* and *custom nodes* of
/// a [`GraphParams`]; their variants say so below, and a graph that is not
/// mentioned needs none. Run without what it needs, such a graph fails when it
/// runs (a permissive compression then falls back to `compress_generic`). The
/// [`GraphBuilder`] helpers named in each variant check what they can.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
#[repr(u32)]
pub enum StandardGraph {
    /// `store` (2).
    Store = 2,
    /// `fse` (3).
    Fse = 3,
    /// `huffman` (4).
    Huffman = 4,
    /// `huffman_huf0` (5).
    HuffmanHuf0 = 5,
    /// `huffman_pivco` (6).
    HuffmanPivco = 6,
    /// `entropy` (7).
    Entropy = 7,
    /// `constant` (8).
    Constant = 8,
    /// `zstd` (9).
    Zstd = 9,
    /// `bitpack` (10).
    Bitpack = 10,
    /// `flatpack` (11).
    Flatpack = 11,
    /// `field_lz` (12).
    ///
    /// Needs no successors. Custom graphs are optional overrides: the integer
    /// parameters [`params::field_lz`]`::*_GRAPH_OVERRIDE_INDEX` pick one of the custom graphs for
    /// the literals, tokens, offsets and the two extra-length streams.
    FieldLz = 12,
    /// `compress_generic` (13).
    CompressGeneric = 13,
    /// `select_generic_lz_backend` (14).
    SelectGenericLzBackend = 14,
    /// `segment_numeric` (15).
    ///
    /// Needs no successors: it cuts numeric input every 16 MiB and compresses
    /// each chunk with the default numeric graph.
    SegmentNumeric = 15,
    /// `select_numeric` (16).
    SelectNumeric = 16,
    /// `ml_selector` (17).
    ///
    /// Needs one successor per class of its model and the model itself as a
    /// materialized parameter; use [`GraphBuilder::ml_selector`] or
    /// [`GraphBuilder::untrained_ml_selector`]. Numeric input only.
    MlSelector = 17,
    /// `clustering` (18).
    ///
    /// Needs the CBOR configuration ([`params::clustering::CONFIG`]), the
    /// clustering codec nodes, and the successors the configuration indexes;
    /// without a successor for a tag it uses `compress_generic`. Use
    /// [`GraphBuilder::clustering`] or [`GraphBuilder::clustering_with_codecs`].
    Clustering = 18,
    /// `try_parse_int` (19).
    ///
    /// Needs exactly three successors, in this order: the integers (numeric,
    /// the parsed strings), the exceptions (strings that are not integers,
    /// or the whole input when none parses) and the dispatch indices
    /// (which strings were parsed). No builder helper; pass them to
    /// [`GraphBuilder::parameterize_graph`] with [`GraphParams::with_graphs`].
    TryParseInt = 19,
    /// `simple_data_description_language` (20).
    ///
    /// Needs exactly one successor, which receives every output of the
    /// dispatch node, and the compiled description
    /// ([`params::sddl::DESCRIPTION`]); use [`GraphBuilder::sddl`].
    SimpleDataDescriptionLanguage = 20,
    /// `simple_data_description_language_v2` (21).
    ///
    /// Takes no successor or one (default `compress_generic`) and needs the
    /// bytecode ([`params::sddl2::BYTECODE`]); use [`GraphBuilder::sddl2`].
    SimpleDataDescriptionLanguageV2 = 21,
    /// `lz4` (22).
    Lz4 = 22,
    /// `partition_bitpack` (23).
    PartitionBitpack = 23,
    /// `segment_num8_from_serial` (24).
    ///
    /// A segmenter: takes one successor, which receives each chunk as
    /// *serial* data (default `interpret_num8_compress`, which reinterprets
    /// it). Use [`GraphBuilder::num_from_serial_segmenter`].
    SegmentNum8FromSerial = 24,
    /// `segment_num16_from_serial` (25).
    ///
    /// As [`StandardGraph::SegmentNum8FromSerial`] with 2-byte elements
    /// (default `interpret_num16_compress`).
    SegmentNum16FromSerial = 25,
    /// `segment_num32_from_serial` (26).
    ///
    /// As [`StandardGraph::SegmentNum8FromSerial`] with 4-byte elements
    /// (default `interpret_num32_compress`).
    SegmentNum32FromSerial = 26,
    /// `segment_num64_from_serial` (27).
    ///
    /// As [`StandardGraph::SegmentNum8FromSerial`] with 8-byte elements
    /// (default `interpret_num64_compress`).
    SegmentNum64FromSerial = 27,
    /// `lz` (28).
    ///
    /// Needs no successors. Custom graphs are optional overrides picked by
    /// the integer parameters [`params::lz`]`::*_GRAPH_IDX`.
    Lz = 28,
    /// `segment_serial` (29).
    ///
    /// A segmenter: takes one successor for every chunk (default
    /// `compress_generic`). Use [`GraphBuilder::serial_segmenter`] to set the
    /// chunk size ([`params::segmenters::SERIAL_CHUNK_BYTE_SIZE`]).
    SegmentSerial = 29,
    /// `transformer_numeric` (30).
    TransformerNumeric = 30,
    /// `brute_force` (31).
    ///
    /// Needs one or more successors; it tries each that accepts the input
    /// and keeps the smallest, or stores the input when none wins (or none
    /// is given). Use [`GraphBuilder::brute_force`].
    BruteForce = 31,
    /// `serial_store` (1).
    SerialStore = 1,
    /// `store1` (32).
    Store1 = 32,
    /// `string_store` (33).
    StringStore = 33,
    /// `compress1` (34).
    Compress1 = 34,
    /// `serial_compress` (35).
    SerialCompress = 35,
    /// `struct_compress` (36).
    StructCompress = 36,
    /// `numeric_compress` (37).
    NumericCompress = 37,
    /// `string_compress` (38).
    StringCompress = 38,
    /// `string_separate_compress` (39).
    StringSeparateCompress = 39,
    /// `bitpack_serial` (40).
    BitpackSerial = 40,
    /// `bitpack_int` (41).
    BitpackInt = 41,
    /// `constant_serial` (42).
    ConstantSerial = 42,
    /// `constant_fixed` (43).
    ConstantFixed = 43,
    /// `fse_ncount` (44).
    FseNcount = 44,
    /// `field_lz_literals` (45).
    FieldLzLiterals = 45,
    /// `field_lz_literals_channel` (46).
    FieldLzLiteralsChannel = 46,
    /// `delta_huffman` (47).
    DeltaHuffman = 47,
    /// `delta_flatpack` (48).
    DeltaFlatpack = 48,
    /// `delta_zstd` (49).
    DeltaZstd = 49,
    /// `delta_huffman_internal` (50).
    DeltaHuffmanInternal = 50,
    /// `delta_flatpack_internal` (51).
    DeltaFlatpackInternal = 51,
    /// `delta_zstd_internal` (52).
    DeltaZstdInternal = 52,
    /// `delta_field_lz` (53).
    DeltaFieldLz = 53,
    /// `range_pack` (54).
    RangePack = 54,
    /// `range_pack_zstd` (55).
    RangePackZstd = 55,
    /// `tokenize_delta_field_lz` (56).
    TokenizeDeltaFieldLz = 56,
    /// `split_serial` (57).
    ///
    /// Needs exactly one custom node (the splitting node) and one custom
    /// graph per output of that node, in output order. No builder helper;
    /// use [`GraphBuilder::parameterize_graph`] with [`GraphParams::with_nodes`]
    /// and [`GraphParams::with_graphs`]. The same holds for
    /// [`StandardGraph::SplitStruct`], [`StandardGraph::SplitNumeric`] and
    /// [`StandardGraph::SplitString`], which differ in the input type.
    SplitSerial = 57,
    /// `split_struct` (58).
    SplitStruct = 58,
    /// `split_numeric` (59).
    SplitNumeric = 59,
    /// `split_string` (60).
    SplitString = 60,
    /// `sddl2_chunk` (61).
    ///
    /// Internal replay graph of SDDL2 (it needs the replay parameters that
    /// the SDDL2 segmenter sets); not meant to be used directly.
    Sddl2Chunk = 61,
    /// `n_to_n` (62).
    ///
    /// Needs exactly one successor per input: input `i` goes to successor
    /// `i`.
    NToN = 62,
    /// `merge_sorted` (63).
    ///
    /// Needs exactly two successors: the merge graph, used for sorted
    /// 4-byte input within the run limit, then the fallback for anything
    /// else.
    MergeSorted = 63,
    /// `transpose_split` (64).
    ///
    /// Needs exactly five successors: the deprecated width 1, 2, 4 and 8
    /// implementations, then the `transpose_split` node's graph (always used
    /// from format version 11).
    TransposeSplit = 64,
    /// `interpret_num8_compress` (65).
    InterpretNum8Compress = 65,
    /// `interpret_num16_compress` (66).
    InterpretNum16Compress = 66,
    /// `interpret_num32_compress` (67).
    InterpretNum32Compress = 67,
    /// `interpret_num64_compress` (68).
    InterpretNum64Compress = 68,
    /// `compress_small_lengths` (69).
    CompressSmallLengths = 69,
    /// `transformer_delta_int` (70).
    TransformerDeltaInt = 70,
    /// `transformer_range_pack` (71).
    TransformerRangePack = 71,
    /// `transformer_divide_by_gcd` (72).
    TransformerDivideByGcd = 72,
    /// `transformer_tokenize_numeric` (73).
    TransformerTokenizeNumeric = 73,
    /// `transformer_tokenize_numeric_sorted` (74).
    TransformerTokenizeNumericSorted = 74,
    /// `transformer_sparse_num` (75).
    TransformerSparseNum = 75,
    /// `transformer_static_core` (76).
    TransformerStaticCore = 76,
    /// `transformer_static_delta` (77).
    TransformerStaticDelta = 77,
    /// `transformer_static_range_pack` (78).
    TransformerStaticRangePack = 78,
    /// `transformer_static_range_pack_delta` (79).
    TransformerStaticRangePackDelta = 79,
    /// `transformer_static_zigzag` (80).
    TransformerStaticZigzag = 80,
    /// `transformer_static_gcd` (81).
    TransformerStaticGcd = 81,
    /// `transformer_static_index` (82).
    TransformerStaticIndex = 82,
    /// `transformer_static_tok_sorted` (83).
    TransformerStaticTokSorted = 83,
    /// `transformer_static_delta_tok` (84).
    TransformerStaticDeltaTok = 84,
    /// `transformer_static_tok_mono` (85).
    TransformerStaticTokMono = 85,
    /// `transformer_static_delta_tok_mono` (86).
    TransformerStaticDeltaTokMono = 86,
    /// `transformer_static_tok_mono_lz` (87).
    TransformerStaticTokMonoLz = 87,
    /// `transformer_static_delta_tok_mono_lz` (88).
    TransformerStaticDeltaTokMonoLz = 88,
    /// `transformer_static_fallback` (89).
    TransformerStaticFallback = 89,
    /// `transformer_numeric1` (90).
    TransformerNumeric1 = 90,
}

impl TryFrom<u32> for StandardGraph {
    type Error = UnknownId;

    /// Looks the graph up by its numeric ID in the reference.
    ///
    /// # Errors
    ///
    /// [`UnknownId`] for IDs that name no graph supported here.
    fn try_from(id: u32) -> Result<Self, Self::Error> {
        Ok(match id {
            2 => Self::Store,
            3 => Self::Fse,
            4 => Self::Huffman,
            5 => Self::HuffmanHuf0,
            6 => Self::HuffmanPivco,
            7 => Self::Entropy,
            8 => Self::Constant,
            9 => Self::Zstd,
            10 => Self::Bitpack,
            11 => Self::Flatpack,
            12 => Self::FieldLz,
            13 => Self::CompressGeneric,
            14 => Self::SelectGenericLzBackend,
            15 => Self::SegmentNumeric,
            16 => Self::SelectNumeric,
            17 => Self::MlSelector,
            18 => Self::Clustering,
            19 => Self::TryParseInt,
            20 => Self::SimpleDataDescriptionLanguage,
            21 => Self::SimpleDataDescriptionLanguageV2,
            22 => Self::Lz4,
            23 => Self::PartitionBitpack,
            24 => Self::SegmentNum8FromSerial,
            25 => Self::SegmentNum16FromSerial,
            26 => Self::SegmentNum32FromSerial,
            27 => Self::SegmentNum64FromSerial,
            28 => Self::Lz,
            29 => Self::SegmentSerial,
            30 => Self::TransformerNumeric,
            31 => Self::BruteForce,
            1 => Self::SerialStore,
            32 => Self::Store1,
            33 => Self::StringStore,
            34 => Self::Compress1,
            35 => Self::SerialCompress,
            36 => Self::StructCompress,
            37 => Self::NumericCompress,
            38 => Self::StringCompress,
            39 => Self::StringSeparateCompress,
            40 => Self::BitpackSerial,
            41 => Self::BitpackInt,
            42 => Self::ConstantSerial,
            43 => Self::ConstantFixed,
            44 => Self::FseNcount,
            45 => Self::FieldLzLiterals,
            46 => Self::FieldLzLiteralsChannel,
            47 => Self::DeltaHuffman,
            48 => Self::DeltaFlatpack,
            49 => Self::DeltaZstd,
            50 => Self::DeltaHuffmanInternal,
            51 => Self::DeltaFlatpackInternal,
            52 => Self::DeltaZstdInternal,
            53 => Self::DeltaFieldLz,
            54 => Self::RangePack,
            55 => Self::RangePackZstd,
            56 => Self::TokenizeDeltaFieldLz,
            57 => Self::SplitSerial,
            58 => Self::SplitStruct,
            59 => Self::SplitNumeric,
            60 => Self::SplitString,
            61 => Self::Sddl2Chunk,
            62 => Self::NToN,
            63 => Self::MergeSorted,
            64 => Self::TransposeSplit,
            65 => Self::InterpretNum8Compress,
            66 => Self::InterpretNum16Compress,
            67 => Self::InterpretNum32Compress,
            68 => Self::InterpretNum64Compress,
            69 => Self::CompressSmallLengths,
            70 => Self::TransformerDeltaInt,
            71 => Self::TransformerRangePack,
            72 => Self::TransformerDivideByGcd,
            73 => Self::TransformerTokenizeNumeric,
            74 => Self::TransformerTokenizeNumericSorted,
            75 => Self::TransformerSparseNum,
            76 => Self::TransformerStaticCore,
            77 => Self::TransformerStaticDelta,
            78 => Self::TransformerStaticRangePack,
            79 => Self::TransformerStaticRangePackDelta,
            80 => Self::TransformerStaticZigzag,
            81 => Self::TransformerStaticGcd,
            82 => Self::TransformerStaticIndex,
            83 => Self::TransformerStaticTokSorted,
            84 => Self::TransformerStaticDeltaTok,
            85 => Self::TransformerStaticTokMono,
            86 => Self::TransformerStaticDeltaTokMono,
            87 => Self::TransformerStaticTokMonoLz,
            88 => Self::TransformerStaticDeltaTokMonoLz,
            89 => Self::TransformerStaticFallback,
            90 => Self::TransformerNumeric1,
            _ => return Err(UnknownId(id)),
        })
    }
}

impl From<StandardGraph> for u32 {
    fn from(value: StandardGraph) -> Self {
        value as Self
    }
}

macro_rules! id_conversions {
    ($public:ident, $standard:ident, $core:ident) => {
        impl From<$standard> for $public {
            fn from(value: $standard) -> Self {
                Self(reg::$core::std(u32::from(value)))
            }
        }
    };
}

/// A graph of a compressor: a standard one or one a [`GraphBuilder`]
/// created. IDs only mean something to the builder that created them.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GraphId(reg::GraphId);

/// A node of a compressor: a standard one or one a [`GraphBuilder`]
/// created.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct NodeId(reg::NodeId);

id_conversions!(GraphId, StandardGraph, GraphId);
id_conversions!(NodeId, StandardNode, NodeId);

/// A set of stream types a graph input accepts (`ZL_Type` masks).
///
/// # Examples
///
/// ```
/// use mopenzl::graph::TypeSet;
/// use mopenzl::DataType;
///
/// let set = TypeSet::from(DataType::Serial) | DataType::Numeric.into();
/// assert!(set.contains(DataType::Numeric) && !set.contains(DataType::String));
/// assert!(TypeSet::ANY.contains(DataType::String));
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct TypeSet(u8);

impl TypeSet {
    /// Every type.
    pub const ANY: Self = Self(15);

    /// Whether `ty` is in the set.
    #[must_use]
    pub const fn contains(self, ty: DataType) -> bool {
        self.0 & type_mask(ty) != 0
    }
}

impl From<DataType> for TypeSet {
    fn from(ty: DataType) -> Self {
        Self(type_mask(ty))
    }
}

impl BitOr for TypeSet {
    type Output = Self;

    fn bitor(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

/// Integer, byte and object parameters of a node or graph. Giving a node or
/// graph parameters replaces the ones it had.
///
/// # Examples
///
/// ```
/// use mopenzl::graph::LocalParams;
///
/// let params = LocalParams::default().with_int(100, 19).with_bytes(7, b"blob".to_vec());
/// assert_eq!(params.int(100), Some(19));
/// assert_eq!(params.int(1), None);
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct LocalParams(CoreParams);

impl LocalParams {
    /// Adds an integer parameter (`ZL_IntParam`).
    #[must_use]
    pub fn with_int(mut self, id: i32, value: i32) -> Self {
        self.0.ints.to_mut().push((id, value));
        self
    }

    /// Adds a copy parameter (`ZL_CopyParam`).
    #[must_use]
    pub fn with_bytes(mut self, id: i32, bytes: impl Into<Arc<[u8]>>) -> Self {
        self.0.copies.push((id, bytes.into()));
        self
    }

    /// The `setStringLens` node's lengths (`ZL_SETSTRINGLENS_ARRAY_PID`):
    /// the string lengths that split a serial input into strings.
    #[must_use]
    pub fn with_string_lengths(mut self, lengths: Vec<u32>) -> Self {
        let object: Arc<dyn core::any::Any + Send + Sync> = Arc::new(lengths);
        self.0.refs.push((
            engine::codecs::conversion::SET_STRING_LENS_ARRAY,
            RefParam(object),
        ));
        self
    }

    /// The first value of integer parameter `id`.
    #[must_use]
    pub fn int(&self, id: i32) -> Option<i32> {
        self.0.int(id)
    }
}

/// Overrides for a node (`ZL_NodeParameters`): what is set replaces the
/// node's own, what is not set is kept.
///
/// # Examples
///
/// ```
/// use mopenzl::dict::DictId;
/// use mopenzl::graph::{LocalParams, NodeParams};
///
/// let params = NodeParams::default()
///     .with_params(LocalParams::default().with_int(100, 19))
///     .with_dictionary(DictId::from([7; 32]));
/// # let _ = params;
/// ```
#[derive(Clone, Debug, Default)]
pub struct NodeParams {
    params: Option<LocalParams>,
    dictionary: Option<DictId>,
}

impl NodeParams {
    /// Replaces the node's local parameters.
    #[must_use]
    pub fn with_params(mut self, params: LocalParams) -> Self {
        self.params = Some(params);
        self
    }

    /// References the dictionary `id` of the bundle given to
    /// [`GraphBuilder::load_dictionaries`]. Only the Zstandard node uses a
    /// dictionary, and only at format version 25 or newer.
    #[must_use]
    pub fn with_dictionary(mut self, id: DictId) -> Self {
        self.dictionary = Some(id);
        self
    }
}

/// Overrides for a graph (`ZL_ParameterizedGraphDesc`): each part that is
/// set replaces the graph's own.
#[derive(Clone, Debug, Default)]
pub struct GraphParams {
    params: Option<LocalParams>,
    custom_graphs: Option<Vec<GraphId>>,
    custom_nodes: Option<Vec<NodeId>>,
    name: Option<String>,
}

impl GraphParams {
    /// Replaces the graph's local parameters.
    #[must_use]
    pub fn with_params(mut self, params: LocalParams) -> Self {
        self.params = Some(params);
        self
    }

    /// Replaces the graph's successor graphs.
    #[must_use]
    pub fn with_graphs(mut self, graphs: impl Into<Vec<GraphId>>) -> Self {
        self.custom_graphs = Some(graphs.into());
        self
    }

    /// Replaces the graph's nodes.
    #[must_use]
    pub fn with_nodes(mut self, nodes: impl Into<Vec<NodeId>>) -> Self {
        self.custom_nodes = Some(nodes.into());
        self
    }

    /// Names the new graph.
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }
}

/// Why a graph could not be built or run.
#[derive(Error, Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum GraphError {
    /// A registered custom encoder failed.
    #[cfg(feature = "custom-codec")]
    #[error("custom codec failed: {0}")]
    CustomCodec(#[from] crate::custom_codec::CodecError),
    /// An ID that is unknown or was created by another builder.
    #[error("unknown or foreign {0} ID")]
    UnknownId(&'static str),
    /// A graph description the registry rejects.
    #[error("{0}")]
    Invalid(&'static str),
    /// The engine failed while running graphs; the message names the node or
    /// graph and the cause.
    #[error("graph execution failed: {0}")]
    Execution(String),
    /// A user graph, selector or segmenter reported a failure.
    #[error("{0}")]
    User(&'static str),
    /// A dictionary bundle or one of its dictionaries was rejected.
    #[error("dictionary bundle: {0}")]
    Dictionary(#[from] DictError),
    /// [`GraphBuilder::load_dictionaries`] was called twice: a compressor
    /// holds one bundle.
    #[error("a dictionary bundle is already loaded")]
    BundleAlreadyLoaded,
    /// A node references a dictionary the loaded bundle does not hold, or no
    /// bundle is loaded.
    #[error("dictionary {0} is not in the loaded bundle")]
    DictionaryNotFound(DictId),
}

impl From<ExecError> for GraphError {
    fn from(error: ExecError) -> Self {
        match error {
            ExecError::User(message) => Self::User(message),
            #[cfg(feature = "custom-codec")]
            ExecError::Node {
                source: engine::error::NodeError::Custom(source),
                ..
            } => Self::CustomCodec(source),
            other => Self::Execution(other.to_string()),
        }
    }
}

/// A header comment stored in the frame (`ZL_CCtx_addHeaderComment`):
/// 1 to 10 000 bytes, format version 22 or newer.
///
/// # Examples
///
/// ```
/// use mopenzl::graph::Comment;
///
/// let comment = Comment::try_from(&b"produced by me"[..])?;
/// assert_eq!(comment.as_ref(), b"produced by me");
/// assert!(Comment::try_from(&b""[..]).is_err());
/// # Ok::<(), mopenzl::graph::InvalidComment>(())
/// ```
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Comment(Arc<[u8]>);

/// A comment of 0 or more than 10 000 bytes.
#[derive(Error, Debug, Clone, Copy, Eq, PartialEq)]
#[error("a header comment holds 1 to 10000 bytes, not {0}")]
pub struct InvalidComment(pub usize);

impl TryFrom<&[u8]> for Comment {
    type Error = InvalidComment;

    /// Validates the comment's length.
    ///
    /// # Errors
    ///
    /// [`InvalidComment`] for an empty comment or one above 10 000 bytes.
    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        if bytes.is_empty() || bytes.len() > engine::layout::COMMENT_MAX {
            return Err(InvalidComment(bytes.len()));
        }
        Ok(Self(Arc::from(bytes)))
    }
}

impl AsRef<[u8]> for Comment {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl Comment {
    pub(crate) fn shared(&self) -> Arc<[u8]> {
        Arc::clone(&self.0)
    }
}

/// A read-only view of a stream, as selectors and graphs see it.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct InputView<'v> {
    data_type: DataType,
    width: usize,
    bytes: &'v [u8],
    lens: &'v [u32],
    metadata: &'v [(i32, i32)],
}

impl<'v> InputView<'v> {
    fn new(stream: &'v Stream<'_>) -> Self {
        Self {
            data_type: stream.ty(),
            width: stream.width(),
            bytes: stream.bytes(),
            lens: stream.lens(),
            metadata: stream.metadata(),
        }
    }

    fn to_stream(self) -> Stream<'v> {
        let mut stream = match self.data_type {
            DataType::String => Stream::strings(self.bytes, self.lens),
            ty => Stream::new(ty, self.width, Bytes::Borrowed(self.bytes)),
        };
        for &(id, value) in self.metadata {
            stream.set_metadata(id, value);
        }
        stream
    }

    /// An integer metadata value set on the stream by an upstream graph
    /// (`ZL_Data_getIntMetadata`).
    #[must_use]
    pub fn int_metadata(&self, id: i32) -> Option<i32> {
        self.metadata
            .iter()
            .find(|&&(key, _)| key == id)
            .map(|&(_, value)| value)
    }

    /// The stream type.
    #[must_use]
    pub const fn data_type(&self) -> DataType {
        self.data_type
    }

    /// Bytes per element (1 for serial data and strings).
    #[must_use]
    pub const fn width(&self) -> usize {
        self.width
    }

    /// The content.
    #[must_use]
    pub const fn bytes(&self) -> &'v [u8] {
        self.bytes
    }

    /// The string lengths; empty for other types.
    #[must_use]
    pub const fn string_lengths(&self) -> &'v [u32] {
        self.lens
    }

    /// Elements in the stream.
    #[must_use]
    pub const fn len(&self) -> usize {
        match self.data_type {
            DataType::String => self.lens.len(),
            _ => self.bytes.len() / self.width,
        }
    }

    /// Whether the stream has no elements.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// A handle to a stream inside one invocation of a function graph.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Edge(CoreEdge);

/// A selector (`ZL_SelectorFn`): picks which successor compresses a stream.
pub trait Selector: Send + Sync + 'static {
    /// Chooses among `successors` (the graph's custom graphs) or any other
    /// graph of the compressor.
    fn select(
        &self,
        cx: &mut SelectorContext<'_>,
        input: InputView<'_>,
        successors: &[GraphId],
    ) -> GraphId;
}

/// A function graph (`ZL_FunctionGraphFn`): runs nodes on its inputs and
/// gives every resulting edge a destination.
pub trait FunctionGraph: Send + Sync + 'static {
    /// Runs the graph.
    ///
    /// # Errors
    ///
    /// Any failure, which fails the graph (or triggers the permissive
    /// fallback).
    fn compress(&self, cx: &mut GraphContext<'_>, inputs: &[Edge]) -> Result<(), GraphError>;
}

/// A segmenter (`ZL_SegmenterFn`): cuts the frame's inputs into chunks.
pub trait Segmenter: Send + Sync + 'static {
    /// Cuts the inputs, consuming all of them.
    ///
    /// # Errors
    ///
    /// Any failure of the segmenter or of a chunk.
    fn segment(&self, cx: &mut SegmenterContext<'_>) -> Result<(), GraphError>;
}

trait SelectorApi {
    fn level(&self) -> i32;
    fn format_version(&self) -> u32;
    fn depth(&self) -> u32;
    fn node_supported(&self, node: reg::NodeId) -> bool;
    fn local_int(&self, id: i32) -> Option<i32>;
    fn set_params(&mut self, params: CoreParams);
    fn try_graph(&mut self, input: &InputView<'_>, graph: reg::GraphId)
    -> Result<usize, ExecError>;
}

impl SelectorApi for SelectorCx<'_, '_, '_> {
    fn level(&self) -> i32 {
        SelectorCx::level(self)
    }
    fn format_version(&self) -> u32 {
        SelectorCx::format_version(self)
    }
    fn depth(&self) -> u32 {
        SelectorCx::depth(self)
    }
    fn node_supported(&self, node: reg::NodeId) -> bool {
        self.is_node_supported(node)
    }
    fn local_int(&self, id: i32) -> Option<i32> {
        self.params.int(id)
    }
    fn set_params(&mut self, params: CoreParams) {
        self.set_successor_params(params);
    }
    fn try_graph(
        &mut self,
        input: &InputView<'_>,
        graph: reg::GraphId,
    ) -> Result<usize, ExecError> {
        SelectorCx::try_graph(self, &input.to_stream(), graph)
    }
}

/// What a [`Selector`] can ask of the engine.
pub struct SelectorContext<'c> {
    api: &'c mut dyn SelectorApi,
}

impl fmt::Debug for SelectorContext<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("SelectorContext")
    }
}

impl SelectorContext<'_> {
    /// The compression level.
    #[must_use]
    pub fn level(&self) -> i32 {
        self.api.level()
    }

    /// The frame's format version.
    #[must_use]
    pub fn format_version(&self) -> u32 {
        self.api.format_version()
    }

    /// How deep in the graph the selector runs (1 at the top).
    #[must_use]
    pub fn depth(&self) -> u32 {
        self.api.depth()
    }

    /// Whether `node` exists at the frame's format version.
    #[must_use]
    pub fn is_node_supported(&self, node: impl Into<NodeId>) -> bool {
        self.api.node_supported(node.into().0)
    }

    /// An integer parameter of the selector graph.
    #[must_use]
    pub fn local_int(&self, id: i32) -> Option<i32> {
        self.api.local_int(id)
    }

    /// Makes the chosen successor run with these local parameters.
    pub fn set_successor_params(&mut self, params: LocalParams) {
        self.api.set_params(params.0);
    }

    /// The size of the complete frame `graph` would produce for `input`
    /// (`ZL_Selector_tryGraph`).
    ///
    /// # Errors
    ///
    /// Whatever makes the trial compression fail.
    pub fn try_graph(
        &mut self,
        input: &InputView<'_>,
        graph: impl Into<GraphId>,
    ) -> Result<usize, GraphError> {
        Ok(self.api.try_graph(input, graph.into().0)?)
    }
}

trait GraphApi {
    fn view(&self, edge: CoreEdge) -> Result<InputView<'_>, ExecError>;
    fn run_node(
        &mut self,
        node: reg::NodeId,
        inputs: &[CoreEdge],
        params: Option<&CoreParams>,
    ) -> Result<Vec<CoreEdge>, ExecError>;
    fn set_destination(
        &mut self,
        edges: &[CoreEdge],
        graph: reg::GraphId,
        params: Option<RuntimeParams>,
    ) -> Result<(), ExecError>;
    fn set_metadata(&mut self, edge: CoreEdge, id: i32, value: i32) -> Result<(), ExecError>;
    fn custom_graphs(&self) -> Vec<reg::GraphId>;
    fn custom_nodes(&self) -> Vec<reg::NodeId>;
    fn local_int(&self, id: i32) -> Option<i32>;
    fn level(&self) -> i32;
    fn format_version(&self) -> u32;
    fn depth(&self) -> u32;
    fn node_supported(&self, node: reg::NodeId) -> bool;
    fn try_graph(&mut self, input: CoreEdge, graph: reg::GraphId) -> Result<usize, ExecError>;
    fn run_split(&mut self, edge: CoreEdge, sizes: &[usize]) -> Result<Vec<CoreEdge>, ExecError>;
    fn run_dispatch(
        &mut self,
        edge: CoreEdge,
        instructions: &DispatchInstructions,
    ) -> Result<Vec<CoreEdge>, ExecError>;
    fn run_dispatch_string(
        &mut self,
        edge: CoreEdge,
        nb_outputs: i32,
        indices: &[u16],
    ) -> Result<Vec<CoreEdge>, ExecError>;
}

impl GraphApi for GraphCx<'_, '_, '_> {
    fn view(&self, edge: CoreEdge) -> Result<InputView<'_>, ExecError> {
        Ok(InputView::new(self.data(edge)?))
    }
    fn run_node(
        &mut self,
        node: reg::NodeId,
        inputs: &[CoreEdge],
        params: Option<&CoreParams>,
    ) -> Result<Vec<CoreEdge>, ExecError> {
        GraphCx::run_node(self, node, inputs, params)
    }
    fn set_destination(
        &mut self,
        edges: &[CoreEdge],
        graph: reg::GraphId,
        params: Option<RuntimeParams>,
    ) -> Result<(), ExecError> {
        GraphCx::set_destination(self, edges, graph, params)
    }
    fn set_metadata(&mut self, edge: CoreEdge, id: i32, value: i32) -> Result<(), ExecError> {
        GraphCx::set_metadata(self, edge, id, value)
    }
    fn custom_graphs(&self) -> Vec<reg::GraphId> {
        self.eff.custom_graphs.to_vec()
    }
    fn custom_nodes(&self) -> Vec<reg::NodeId> {
        self.eff.custom_nodes.to_vec()
    }
    fn local_int(&self, id: i32) -> Option<i32> {
        self.eff.params.int(id)
    }
    fn level(&self) -> i32 {
        self.ex.p.level
    }
    fn format_version(&self) -> u32 {
        self.ex.p.version
    }
    fn depth(&self) -> u32 {
        self.depth
    }
    fn node_supported(&self, node: reg::NodeId) -> bool {
        self.is_node_supported(node)
    }
    fn try_graph(&mut self, input: CoreEdge, graph: reg::GraphId) -> Result<usize, ExecError> {
        let stream = self.data(input)?.clone();
        self.ex
            .try_graph(core::slice::from_ref(&stream), graph, None)
    }
    fn run_split(&mut self, edge: CoreEdge, sizes: &[usize]) -> Result<Vec<CoreEdge>, ExecError> {
        engine::graphs::run_split_node(self, edge, sizes)
    }
    fn run_dispatch(
        &mut self,
        edge: CoreEdge,
        instructions: &DispatchInstructions,
    ) -> Result<Vec<CoreEdge>, ExecError> {
        engine::graphs::run_dispatch_node(self, edge, &instructions.clone().into())
    }
    fn run_dispatch_string(
        &mut self,
        edge: CoreEdge,
        nb_outputs: i32,
        indices: &[u16],
    ) -> Result<Vec<CoreEdge>, ExecError> {
        engine::graphs::run_dispatch_string_node(self, edge, nb_outputs, indices)
    }
}

/// What a [`FunctionGraph`] can ask of the engine.
pub struct GraphContext<'c> {
    api: &'c mut dyn GraphApi,
}

impl fmt::Debug for GraphContext<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("GraphContext")
    }
}

fn core_edges(edges: &[Edge]) -> Vec<CoreEdge> {
    edges.iter().map(|edge| edge.0).collect()
}

impl GraphContext<'_> {
    /// The stream behind `edge`.
    ///
    /// # Errors
    ///
    /// An edge of another invocation.
    pub fn data(&self, edge: Edge) -> Result<InputView<'_>, GraphError> {
        Ok(self.api.view(edge.0)?)
    }

    /// Sets integer metadata on the stream behind `edge`; downstream graphs
    /// read it (`ZL_Edge_setIntMetadata`). It is never written to the frame.
    ///
    /// # Errors
    ///
    /// An edge of another invocation.
    pub fn set_int_metadata(&mut self, edge: Edge, id: i32, value: i32) -> Result<(), GraphError> {
        Ok(self.api.set_metadata(edge.0, id, value)?)
    }

    /// Runs `node` on `inputs` now (`ZL_Edge_runMultiInputNode`) and returns
    /// the edges of its outputs, in output order. Nodes do no implicit type
    /// conversion.
    ///
    /// # Errors
    ///
    /// A failing node, or an input edge that already has a destination.
    pub fn run_node(
        &mut self,
        node: impl Into<NodeId>,
        inputs: &[Edge],
    ) -> Result<Vec<Edge>, GraphError> {
        let edges = self
            .api
            .run_node(node.into().0, &core_edges(inputs), None)?;
        Ok(edges.into_iter().map(Edge).collect())
    }

    /// [`Self::run_node`] with local parameters replacing the node's own.
    ///
    /// # Errors
    ///
    /// As [`Self::run_node`].
    pub fn run_node_with(
        &mut self,
        node: impl Into<NodeId>,
        inputs: &[Edge],
        params: &LocalParams,
    ) -> Result<Vec<Edge>, GraphError> {
        let edges = self
            .api
            .run_node(node.into().0, &core_edges(inputs), Some(&params.0))?;
        Ok(edges.into_iter().map(Edge).collect())
    }

    /// Sends `edges` to `graph`. Successors run after this function returns,
    /// in the order of their first edge.
    ///
    /// # Errors
    ///
    /// An unknown graph, a wrong number of edges, or an edge that already
    /// has a destination.
    pub fn set_destination(
        &mut self,
        edges: &[Edge],
        graph: impl Into<GraphId>,
    ) -> Result<(), GraphError> {
        Ok(self
            .api
            .set_destination(&core_edges(edges), graph.into().0, None)?)
    }

    /// [`Self::set_destination`] with runtime overrides for the successor.
    ///
    /// # Errors
    ///
    /// As [`Self::set_destination`].
    pub fn set_destination_with(
        &mut self,
        edges: &[Edge],
        graph: impl Into<GraphId>,
        overrides: &GraphParams,
    ) -> Result<(), GraphError> {
        let runtime = RuntimeParams {
            params: overrides.params.as_ref().map(|params| params.0.clone()),
            custom_graphs: overrides
                .custom_graphs
                .as_ref()
                .map(|graphs| graphs.iter().map(|graph| graph.0).collect()),
            custom_nodes: overrides
                .custom_nodes
                .as_ref()
                .map(|nodes| nodes.iter().map(|node| node.0).collect()),
        };
        Ok(self
            .api
            .set_destination(&core_edges(edges), graph.into().0, Some(runtime))?)
    }

    /// The graph's successor graphs.
    #[must_use]
    pub fn custom_graphs(&self) -> Vec<GraphId> {
        self.api.custom_graphs().into_iter().map(GraphId).collect()
    }

    /// The graph's nodes.
    #[must_use]
    pub fn custom_nodes(&self) -> Vec<NodeId> {
        self.api.custom_nodes().into_iter().map(NodeId).collect()
    }

    /// An integer parameter of the graph.
    #[must_use]
    pub fn local_int(&self, id: i32) -> Option<i32> {
        self.api.local_int(id)
    }

    /// The compression level.
    #[must_use]
    pub fn level(&self) -> i32 {
        self.api.level()
    }

    /// The frame's format version.
    #[must_use]
    pub fn format_version(&self) -> u32 {
        self.api.format_version()
    }

    /// How deep in the graph this invocation runs (1 at the top).
    #[must_use]
    pub fn depth(&self) -> u32 {
        self.api.depth()
    }

    /// Whether `node` exists at the frame's format version.
    #[must_use]
    pub fn is_node_supported(&self, node: impl Into<NodeId>) -> bool {
        self.api.node_supported(node.into().0)
    }

    /// The size of the complete frame `graph` would produce for the stream
    /// behind `input`.
    ///
    /// # Errors
    ///
    /// Whatever makes the trial compression fail.
    pub fn try_graph(
        &mut self,
        input: Edge,
        graph: impl Into<GraphId>,
    ) -> Result<usize, GraphError> {
        Ok(self.api.try_graph(input.0, graph.into().0)?)
    }
}

/// How `dispatchN_byTag` cuts its serial input and routes the pieces
/// (`ZL_DispatchInstructions`).
///
/// The input is cut into consecutive segments of `segment_sizes` bytes;
/// segment `i` goes to output stream `tags[i]`, out of `tag_count` streams.
/// The sizes must add up to the input size and every tag must be below
/// `tag_count`.
///
/// # Examples
///
/// ```
/// use mopenzl::graph::DispatchInstructions;
///
/// // Alternate 3-byte and 1-byte segments between two streams.
/// let instructions = DispatchInstructions {
///     segment_sizes: vec![3, 1, 3, 1],
///     tags: vec![0, 1, 0, 1],
///     tag_count: 2,
/// };
/// assert_eq!(instructions.tag_count, 2);
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DispatchInstructions {
    /// The size in bytes of every segment, in input order.
    pub segment_sizes: Vec<usize>,
    /// The output stream of every segment.
    pub tags: Vec<u32>,
    /// The number of output streams.
    pub tag_count: usize,
}

impl From<DispatchInstructions> for engine::codecs::structure::DispatchInstructions {
    fn from(value: DispatchInstructions) -> Self {
        Self {
            segment_sizes: value.segment_sizes,
            tags: value.tags,
            nb_tags: value.tag_count,
        }
    }
}

impl GraphContext<'_> {
    /// Cuts the stream behind `edge` into consecutive segments of
    /// `segment_sizes` elements (`ZL_Edge_runSplitNode`) and returns the
    /// edge of every segment. A last size of 0 takes whatever is left; the
    /// sizes must otherwise add up to the length of the stream. Serial,
    /// struct and numeric streams can be split.
    ///
    /// # Errors
    ///
    /// A string stream, sizes that do not cover the stream, or a failing
    /// node (for example when the `split` feature is off).
    pub fn run_split_node(
        &mut self,
        edge: Edge,
        segment_sizes: &[usize],
    ) -> Result<Vec<Edge>, GraphError> {
        let edges = self.api.run_split(edge.0, segment_sizes)?;
        Ok(edges.into_iter().map(Edge).collect())
    }

    /// Runs `dispatchN_byTag` on the serial stream behind `edge`
    /// (`ZL_Edge_runDispatchNode`). The edges returned are the tag stream,
    /// the segment-size stream, and one serial stream per tag.
    ///
    /// # Errors
    ///
    /// Instructions that do not describe the stream, or a failing node.
    pub fn run_dispatch_node(
        &mut self,
        edge: Edge,
        instructions: &DispatchInstructions,
    ) -> Result<Vec<Edge>, GraphError> {
        let edges = self.api.run_dispatch(edge.0, instructions)?;
        Ok(edges.into_iter().map(Edge).collect())
    }

    /// Runs `dispatch_string` on the string stream behind `edge`
    /// (`ZL_Edge_runDispatchStringNode`): string `i` goes to output
    /// `indices[i]` of `output_count`. The edges returned are the index
    /// stream, then one string stream per output.
    ///
    /// # Errors
    ///
    /// More than 2048 outputs, an index out of range, fewer indices than
    /// strings, or a failing node.
    pub fn run_dispatch_string_node(
        &mut self,
        edge: Edge,
        output_count: usize,
        indices: &[u16],
    ) -> Result<Vec<Edge>, GraphError> {
        let count = i32::try_from(output_count).unwrap_or(i32::MAX);
        let edges = self.api.run_dispatch_string(edge.0, count, indices)?;
        Ok(edges.into_iter().map(Edge).collect())
    }
}

trait SegmenterApi {
    fn n_inputs(&self) -> usize;
    fn remaining(&self) -> Vec<usize>;
    fn custom_graphs(&self) -> Vec<reg::GraphId>;
    fn local_int(&self, id: i32) -> Option<i32>;
    fn format_version(&self) -> u32;
    fn process_chunk(&mut self, n_elts: &[usize], graph: reg::GraphId) -> Result<(), ExecError>;
}

impl SegmenterApi for SegmenterCx<'_, '_, '_> {
    fn n_inputs(&self) -> usize {
        SegmenterCx::n_inputs(self)
    }
    fn remaining(&self) -> Vec<usize> {
        SegmenterCx::remaining(self)
    }
    fn custom_graphs(&self) -> Vec<reg::GraphId> {
        self.custom_graphs.to_vec()
    }
    fn local_int(&self, id: i32) -> Option<i32> {
        self.params.int(id)
    }
    fn format_version(&self) -> u32 {
        SegmenterCx::format_version(self)
    }
    fn process_chunk(&mut self, n_elts: &[usize], graph: reg::GraphId) -> Result<(), ExecError> {
        SegmenterCx::process_chunk(self, n_elts, graph, None)
    }
}

/// What a [`Segmenter`] can ask of the engine.
pub struct SegmenterContext<'c> {
    api: &'c mut dyn SegmenterApi,
}

impl fmt::Debug for SegmenterContext<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("SegmenterContext")
    }
}

impl SegmenterContext<'_> {
    /// Number of inputs of the frame.
    #[must_use]
    pub fn n_inputs(&self) -> usize {
        self.api.n_inputs()
    }

    /// Elements left in every input.
    #[must_use]
    pub fn remaining(&self) -> Vec<usize> {
        self.api.remaining()
    }

    /// The segmenter's successor graphs.
    #[must_use]
    pub fn custom_graphs(&self) -> Vec<GraphId> {
        self.api.custom_graphs().into_iter().map(GraphId).collect()
    }

    /// An integer parameter of the segmenter.
    #[must_use]
    pub fn local_int(&self, id: i32) -> Option<i32> {
        self.api.local_int(id)
    }

    /// The frame's format version.
    #[must_use]
    pub fn format_version(&self) -> u32 {
        self.api.format_version()
    }

    /// Compresses the next `n_elts[i]` elements of input `i` as one chunk
    /// through `graph`.
    ///
    /// # Errors
    ///
    /// More elements than are left, or any failure of the chunk.
    pub fn process_chunk(
        &mut self,
        n_elts: &[usize],
        graph: impl Into<GraphId>,
    ) -> Result<(), GraphError> {
        Ok(self.api.process_chunk(n_elts, graph.into().0)?)
    }
}

struct UserSelector<S>(S);

impl<S: Selector> reg::SelectFn for UserSelector<S> {
    fn select(
        &self,
        cx: &mut SelectorCx<'_, '_, '_>,
        input: &Stream<'_>,
        successors: &[reg::GraphId],
    ) -> reg::GraphId {
        let view = InputView::new(input);
        let successors: Vec<GraphId> = successors.iter().map(|&graph| GraphId(graph)).collect();
        let mut context = SelectorContext { api: cx };
        self.0.select(&mut context, view, &successors).0
    }
}

struct UserGraph<F>(F);

impl<F: FunctionGraph> reg::GraphFn for UserGraph<F> {
    fn run(&self, cx: &mut GraphCx<'_, '_, '_>, inputs: &[CoreEdge]) -> Result<(), ExecError> {
        let edges: Vec<Edge> = inputs.iter().map(|&edge| Edge(edge)).collect();
        let mut context = GraphContext { api: cx };
        self.0
            .compress(&mut context, &edges)
            .map_err(|error| match error {
                GraphError::User(message) => ExecError::User(message),
                GraphError::Invalid(message) => ExecError::Parameter(message),
                _ => ExecError::User("a function graph failed"),
            })
    }
}

struct UserSegmenter<S>(S);

impl<S: Segmenter> reg::SegmentFn for UserSegmenter<S> {
    fn segment(&self, cx: &mut SegmenterCx<'_, '_, '_>) -> Result<(), ExecError> {
        let mut context = SegmenterContext { api: cx };
        self.0.segment(&mut context).map_err(|error| match error {
            GraphError::User(message) => ExecError::User(message),
            _ => ExecError::User("a segmenter failed"),
        })
    }
}

static NEXT_OWNER: AtomicU32 = AtomicU32::new(1);

/// Builds the graphs of a compressor.
///
/// Every `GraphId` and `NodeId` a builder returns belongs to it; the
/// standard ones belong to every builder.
///
/// # Examples
///
/// ```
/// use mopenzl::graph::{GraphBuilder, LocalParams, StandardGraph, StandardNode};
///
/// let mut builder = GraphBuilder::new();
/// // Interpret serial bytes as 32-bit numbers, then store them.
/// let graph = builder.static_graph(StandardNode::ConvertSerialToNumLe32, &[StandardGraph::Store.into()])?;
/// let compressor_graph = builder.build(graph)?;
/// # let _ = compressor_graph;
/// # Ok::<(), mopenzl::graph::GraphError>(())
/// ```
#[derive(Debug)]
pub struct GraphBuilder {
    registry: Registry,
}

impl Default for GraphBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl GraphBuilder {
    /// Registers an application codec in the custom wire-ID namespace.
    ///
    /// Use the returned node with [`Self::static_graph`] or a function graph.
    /// Several encoder configurations may share an ID when their wire format
    /// and signature agree. Register the matching decoder under that ID.
    ///
    /// # Errors
    ///
    /// Invalid port counts, repeated variable-output types, or a wire ID
    /// already registered with a different signature.
    #[cfg(feature = "custom-codec")]
    pub fn custom_codec(
        &mut self,
        id: u32,
        codec: impl crate::custom_codec::CustomCodec,
    ) -> Result<NodeId, crate::custom_codec::CodecError> {
        let signature = codec.signature();
        crate::custom_codec::validate(signature)?;
        if self
            .registry
            .nodes
            .iter()
            .filter_map(|node| node.custom.as_ref())
            .any(|registered| registered.id == id && registered.signature != signature)
        {
            return Err(crate::custom_codec::CodecError::Invalid(
                "custom codec ID has a different signature",
            ));
        }
        let spec = reg::NodeSpec {
            name: "custom",
            codec: id,
            min_version: 21,
            max_version: 27,
            inputs: signature.inputs,
            variable_input: signature.variable_input,
            singletons: signature.outputs,
            variable: signature.variable_outputs,
            defaults: &[],
            encoder: &engine::codecs::DISABLED,
        };
        Ok(NodeId(self.registry.add_node(NodeDef {
            spec: Callback::Shared(Arc::new(spec)),
            custom: Some(crate::custom_codec::Registered {
                id,
                signature,
                codec: Arc::new(codec),
            }),
            params: None,
            dict_id: None,
            dict_index: None,
        })))
    }

    /// An empty builder: only the standard graphs and nodes.
    #[must_use]
    pub fn new() -> Self {
        let owner = NEXT_OWNER.fetch_add(1, Ordering::Relaxed).max(1);
        Self {
            registry: Registry::new(owner, reg::gid::COMPRESS_GENERIC),
        }
    }

    fn graph_id(&self, id: GraphId) -> Result<reg::GraphId, GraphError> {
        self.registry
            .graph(id.0)
            .map(|_| id.0)
            .map_err(|_| GraphError::UnknownId("graph"))
    }

    fn node_id(&self, id: NodeId) -> Result<reg::NodeId, GraphError> {
        self.registry
            .node(id.0)
            .map(|_| id.0)
            .map_err(|_| GraphError::UnknownId("node"))
    }

    fn masks(inputs: &[TypeSet]) -> Result<Cow<'static, [u8]>, GraphError> {
        if inputs.is_empty() {
            return Err(GraphError::Invalid("a graph needs at least one input"));
        }
        Ok(Cow::Owned(inputs.iter().map(|set| set.0).collect()))
    }

    fn add(&mut self, def: GraphDef) -> GraphId {
        GraphId(self.registry.add_graph(def))
    }

    /// A graph that runs `head` and sends its outputs to `successors`, one
    /// graph per output (`ZL_Compressor_registerStaticGraph_fromNode`).
    ///
    /// # Errors
    ///
    /// Unknown IDs, or a number of successors that does not match the node's
    /// outputs.
    pub fn static_graph(
        &mut self,
        head: impl Into<NodeId>,
        successors: &[GraphId],
    ) -> Result<GraphId, GraphError> {
        let head = self.node_id(head.into())?;
        let spec = self
            .registry
            .node(head)
            .map_err(|_| GraphError::UnknownId("node"))?
            .spec;
        let outcomes = spec.singletons.len() + spec.variable.len();
        let expected_ok = if spec.variable.is_empty() {
            successors.len() == outcomes
        } else {
            successors.len() >= spec.singletons.len() && successors.len() == outcomes
        };
        if !expected_ok {
            return Err(GraphError::Invalid(
                "the successors do not match the node's outputs",
            ));
        }
        let successors = successors
            .iter()
            .map(|&graph| self.graph_id(graph))
            .collect::<Result<Vec<_>, _>>()?;
        let inputs: Vec<u8> = spec.inputs.iter().map(|&ty| type_mask(ty)).collect();
        Ok(self.add(GraphDef {
            name: Cow::Owned(alloc::format!("static({})", spec.name)),
            kind: GraphKind::Static,
            inputs: Cow::Owned(inputs),
            last_variable: spec.variable_input,
            custom_graphs: Cow::Owned(successors),
            custom_nodes: Cow::Owned(alloc::vec![head]),
            params: CoreParams::default(),
        }))
    }

    /// Nodes run in order, each feeding the next, the last feeding `last`
    /// (`ZL_Compressor_registerStaticGraph_fromPipelineNodes1o`).
    ///
    /// # Errors
    ///
    /// As [`Self::static_graph`], or an empty pipeline.
    pub fn pipeline(
        &mut self,
        nodes: &[NodeId],
        last: impl Into<GraphId>,
    ) -> Result<GraphId, GraphError> {
        if nodes.is_empty() {
            return Err(GraphError::Invalid("a pipeline needs at least one node"));
        }
        let mut next = last.into();
        for &node in nodes.iter().rev() {
            next = self.static_graph(node, &[next])?;
        }
        Ok(next)
    }

    /// A copy of `node` with replacement parameters
    /// (`ZL_Compressor_parameterizeNode`).
    ///
    /// # Errors
    ///
    /// An unknown node.
    pub fn parameterize_node(
        &mut self,
        node: impl Into<NodeId>,
        params: LocalParams,
    ) -> Result<NodeId, GraphError> {
        self.parameterize_node_with(node, NodeParams::default().with_params(params))
    }

    /// A copy of `node` with the overrides `params` sets, local parameters
    /// and a dictionary reference (`ZL_Compressor_parameterizeNode`).
    ///
    /// The dictionary is looked up when [`Self::build`] freezes the graphs.
    ///
    /// # Errors
    ///
    /// An unknown node.
    pub fn parameterize_node_with(
        &mut self,
        node: impl Into<NodeId>,
        params: NodeParams,
    ) -> Result<NodeId, GraphError> {
        let node = self.node_id(node.into())?;
        let view = self
            .registry
            .node(node)
            .map_err(|_| GraphError::UnknownId("node"))?;
        let (own, dict_id) = (view.params.cloned(), view.dict_id);
        #[cfg(feature = "custom-codec")]
        let custom = view.custom.cloned();
        let spec = if node.owner == 0 {
            Callback::Static(reg::nodes::standard(node.index).ok_or(GraphError::UnknownId("node"))?)
        } else {
            self.registry.nodes[(node.index - reg::FIRST_CUSTOM_NODE) as usize]
                .spec
                .clone()
        };
        Ok(NodeId(self.registry.add_node(NodeDef {
            #[cfg(feature = "custom-codec")]
            custom,
            spec,
            params: params.params.map(|params| params.0).or(own),
            dict_id: params.dictionary.or(dict_id),
            dict_index: None,
        })))
    }

    /// Attaches the dictionary bundle frames are compressed with
    /// (`ZL_Compressor_loadDictBundle`). A compressor holds one bundle.
    ///
    /// Every dictionary must already be referenced by a Zstandard node
    /// parameterized with its ID ([`NodeParams::with_dictionary`]); a
    /// dictionary nothing uses cannot be materialized. From format version 25
    /// frames then record the bundle's ID and every chunk a dictionary lane.
    ///
    /// # Errors
    ///
    /// [`GraphError::BundleAlreadyLoaded`] on a second call, and
    /// [`GraphError::Dictionary`] for a dictionary no node references, one
    /// the codec rejects, or two contents sharing an ID.
    pub fn load_dictionaries(&mut self, bundle: &DictBundle) -> Result<(), GraphError> {
        self.registry
            .load_bundle(bundle)
            .map_err(|error| match error {
                LoadError::AlreadyLoaded => GraphError::BundleAlreadyLoaded,
                LoadError::Dict(error) => GraphError::Dictionary(error),
                LoadError::NotFound(id) => GraphError::DictionaryNotFound(id),
            })
    }

    /// A copy of `graph` with overrides (`ZL_Compressor_parameterizeGraph`).
    ///
    /// # Errors
    ///
    /// Unknown IDs.
    pub fn parameterize_graph(
        &mut self,
        graph: impl Into<GraphId>,
        overrides: GraphParams,
    ) -> Result<GraphId, GraphError> {
        self.parameterize_graph_with(graph.into(), overrides, None)
    }

    /// [`parameterize_graph`](Self::parameterize_graph) that also sets the
    /// graph's materialized MParam object. A graph keeps the MParam of the
    /// graph it was derived from unless a new one is given, even when its
    /// local parameters are replaced.
    fn parameterize_graph_with(
        &mut self,
        graph: GraphId,
        overrides: GraphParams,
        mparam: Option<RefParam>,
    ) -> Result<GraphId, GraphError> {
        let base = self.graph_id(graph)?;
        let (name, kind, inputs, last_variable, custom_graphs, custom_nodes, params) = {
            let def = self
                .registry
                .graph(base)
                .map_err(|_| GraphError::UnknownId("graph"))?;
            let kind = match &def.kind {
                GraphKind::Selector(Callback::Static(f)) => {
                    GraphKind::Selector(Callback::Static(*f))
                }
                GraphKind::Selector(Callback::Shared(f)) => {
                    GraphKind::Selector(Callback::Shared(Arc::clone(f)))
                }
                GraphKind::Function(Callback::Static(f)) => {
                    GraphKind::Function(Callback::Static(*f))
                }
                GraphKind::Function(Callback::Shared(f)) => {
                    GraphKind::Function(Callback::Shared(Arc::clone(f)))
                }
                GraphKind::Segmenter(Callback::Static(f)) => {
                    GraphKind::Segmenter(Callback::Static(*f))
                }
                GraphKind::Segmenter(Callback::Shared(f)) => {
                    GraphKind::Segmenter(Callback::Shared(Arc::clone(f)))
                }
                GraphKind::Static => GraphKind::Static,
                GraphKind::Store => GraphKind::Store,
                GraphKind::Illegal => GraphKind::Illegal,
            };
            (
                def.name.clone(),
                kind,
                def.inputs.clone(),
                def.last_variable,
                def.custom_graphs.clone(),
                def.custom_nodes.clone(),
                def.params.clone(),
            )
        };
        let custom_graphs = match overrides.custom_graphs {
            Some(graphs) => Cow::Owned(
                graphs
                    .iter()
                    .map(|&graph| self.graph_id(graph))
                    .collect::<Result<Vec<_>, _>>()?,
            ),
            None => custom_graphs,
        };
        let custom_nodes = match overrides.custom_nodes {
            Some(nodes) => Cow::Owned(
                nodes
                    .iter()
                    .map(|&node| self.node_id(node))
                    .collect::<Result<Vec<_>, _>>()?,
            ),
            None => custom_nodes,
        };
        let inherited = params
            .refs
            .iter()
            .find(|(id, _)| *id == engine::exec::MPARAM_REF_ID)
            .map(|(_, object)| object.clone());
        let mut params = overrides.params.map_or(params, |params| params.0);
        if let Some(mparam) = mparam.or(inherited) {
            params
                .refs
                .retain(|(id, _)| *id != engine::exec::MPARAM_REF_ID);
            params.refs.push((engine::exec::MPARAM_REF_ID, mparam));
        }
        Ok(self.add(GraphDef {
            name: overrides.name.map_or(name, Cow::Owned),
            kind,
            inputs,
            last_variable,
            custom_graphs,
            custom_nodes,
            params,
        }))
    }

    /// A graph whose single input a selector routes
    /// (`ZL_Compressor_registerSelectorGraph`).
    ///
    /// # Errors
    ///
    /// Unknown successors.
    pub fn selector<S: Selector>(
        &mut self,
        name: &str,
        selector: S,
        input: TypeSet,
        successors: &[GraphId],
    ) -> Result<GraphId, GraphError> {
        let successors = successors
            .iter()
            .map(|&graph| self.graph_id(graph))
            .collect::<Result<Vec<_>, _>>()?;
        let callback: Arc<dyn reg::SelectFn> = Arc::new(UserSelector(selector));
        Ok(self.add(GraphDef {
            name: Cow::Owned(name.to_string()),
            kind: GraphKind::Selector(Callback::Shared(callback)),
            inputs: Cow::Owned(alloc::vec![input.0]),
            last_variable: false,
            custom_graphs: Cow::Owned(successors),
            custom_nodes: Cow::Borrowed(&[]),
            params: CoreParams::default(),
        }))
    }

    /// A graph written as a function (`ZL_Compressor_registerFunctionGraph`).
    ///
    /// `inputs` are the accepted types of each input port; with
    /// `last_input_variable` the last port takes any number of inputs.
    ///
    /// # Errors
    ///
    /// Unknown IDs, or no input port.
    pub fn function_graph<F: FunctionGraph>(
        &mut self,
        name: &str,
        function: F,
        inputs: &[TypeSet],
        last_input_variable: bool,
        successors: &[GraphId],
        nodes: &[NodeId],
    ) -> Result<GraphId, GraphError> {
        let masks = Self::masks(inputs)?;
        let successors = successors
            .iter()
            .map(|&graph| self.graph_id(graph))
            .collect::<Result<Vec<_>, _>>()?;
        let nodes = nodes
            .iter()
            .map(|&node| self.node_id(node))
            .collect::<Result<Vec<_>, _>>()?;
        let callback: Arc<dyn reg::GraphFn> = Arc::new(UserGraph(function));
        Ok(self.add(GraphDef {
            name: Cow::Owned(name.to_string()),
            kind: GraphKind::Function(Callback::Shared(callback)),
            inputs: masks,
            last_variable: last_input_variable,
            custom_graphs: Cow::Owned(successors),
            custom_nodes: Cow::Owned(nodes),
            params: CoreParams::default(),
        }))
    }

    /// A segmenter graph (`ZL_Compressor_registerSegmenter`).
    ///
    /// # Errors
    ///
    /// Unknown IDs, or no input port.
    pub fn segmenter<S: Segmenter>(
        &mut self,
        name: &str,
        segmenter: S,
        inputs: &[TypeSet],
        successors: &[GraphId],
    ) -> Result<GraphId, GraphError> {
        let masks = Self::masks(inputs)?;
        let successors = successors
            .iter()
            .map(|&graph| self.graph_id(graph))
            .collect::<Result<Vec<_>, _>>()?;
        let callback: Arc<dyn reg::SegmentFn> = Arc::new(UserSegmenter(segmenter));
        Ok(self.add(GraphDef {
            name: Cow::Owned(name.to_string()),
            kind: GraphKind::Segmenter(Callback::Shared(callback)),
            inputs: masks,
            last_variable: false,
            custom_graphs: Cow::Owned(successors),
            custom_nodes: Cow::Borrowed(&[]),
            params: CoreParams::default(),
        }))
    }

    /// The serial segmenter: cuts serial input every `chunk` bytes and
    /// compresses each piece with `successor`
    /// (`ZL_Compressor_buildSerialSegmenter`).
    ///
    /// # Errors
    ///
    /// A chunk size outside 32 KiB to `i32::MAX`, or an unknown successor.
    pub fn serial_segmenter(
        &mut self,
        chunk: crate::ChunkSize,
        successor: impl Into<GraphId>,
    ) -> Result<GraphId, GraphError> {
        let params = GraphParams::default()
            .with_graphs([successor.into()])
            .with_params(LocalParams::default().with_int(
                engine::graphs::SERIAL_CHUNK_BYTE_SIZE,
                i32::try_from(usize::from(chunk)).unwrap_or(i32::MAX),
            ));
        self.parameterize_graph(StandardGraph::SegmentSerial, params)
    }

    /// Cuts serial input into chunks aligned to `width`-byte integers and
    /// compresses each chunk with `successor`
    /// (`ZL_Compressor_buildNumFromSerialSegmenter`); `None` takes the
    /// default successor of the width.
    ///
    /// Each chunk reaches the successor as *serial* data, as in the
    /// reference: the segmenter only aligns the cuts to the element width, it
    /// does not convert the chunk. The default successor
    /// (`interpret_num8_compress` to `interpret_num64_compress`) converts the
    /// chunk to numbers; a custom successor must accept serial input and do
    /// that itself, for example a graph that starts with a
    /// `convert_serial_to_num` node (see the example). One that accepts only
    /// numeric input fails when it runs.
    ///
    /// # Errors
    ///
    /// A width other than 1, 2, 4 or 8, a bad chunk size, or an unknown
    /// successor.
    ///
    /// # Examples
    ///
    /// Little-endian 32-bit integers, converted to numbers by the successor:
    ///
    /// ```
    /// use mopenzl::graph::{GraphBuilder, StandardGraph, StandardNode};
    /// use mopenzl::{ChunkSize, Compressor, EncoderConfig};
    ///
    /// # #[cfg(feature = "conversion")]
    /// # {
    /// let mut builder = GraphBuilder::new();
    /// // serial chunk -> numbers -> the default numeric graph
    /// let numbers = builder.static_graph(
    ///     StandardNode::ConvertSerialToNumLe32,
    ///     &[StandardGraph::NumericCompress.into()],
    /// )?;
    /// let segmenter = builder.num_from_serial_segmenter(4, ChunkSize::MIN, Some(numbers))?;
    /// let graph = builder.build(segmenter)?;
    ///
    /// let input: Vec<u8> = (0..20_000u32).flat_map(|i| i.to_le_bytes()).collect();
    /// let mut compressor = Compressor::builder(EncoderConfig::default())
    ///     .with_graph(graph)
    ///     .build()?;
    /// let frame = compressor.compress(&input)?;
    /// # #[cfg(feature = "decompression")]
    /// assert_eq!(mopenzl::Decompressor::default().decompress(&frame)?, input);
    /// # }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn num_from_serial_segmenter(
        &mut self,
        width: usize,
        chunk: crate::ChunkSize,
        successor: Option<GraphId>,
    ) -> Result<GraphId, GraphError> {
        let (base, default) = match width {
            1 => (
                StandardGraph::SegmentNum8FromSerial,
                StandardGraph::InterpretNum8Compress,
            ),
            2 => (
                StandardGraph::SegmentNum16FromSerial,
                StandardGraph::InterpretNum16Compress,
            ),
            4 => (
                StandardGraph::SegmentNum32FromSerial,
                StandardGraph::InterpretNum32Compress,
            ),
            8 => (
                StandardGraph::SegmentNum64FromSerial,
                StandardGraph::InterpretNum64Compress,
            ),
            _ => {
                return Err(GraphError::Invalid(
                    "the element width must be 1, 2, 4 or 8",
                ));
            }
        };
        let params = GraphParams::default()
            .with_graphs([successor.unwrap_or_else(|| default.into())])
            .with_params(
                LocalParams::default()
                    .with_int(engine::graphs::NUM_FROM_SERIAL_ELT_WIDTH, width as i32)
                    .with_int(
                        engine::graphs::NUM_FROM_SERIAL_CHUNK_BYTE_SIZE,
                        i32::try_from(usize::from(chunk)).unwrap_or(i32::MAX),
                    ),
            );
        self.parameterize_graph(base, params)
    }

    /// The SDDL graph: parses serial data with a compiled description and
    /// sends the fields it cuts out to `successor`
    /// (`ZL_Compressor_buildSDDLGraph`).
    ///
    /// The description is the CBOR program the SDDL compiler writes. The
    /// successor receives every output of the dispatch node (tags, segment
    /// sizes, then one stream per destination of the description) as one
    /// multi-input destination.
    ///
    /// # Errors
    ///
    /// A description that is not a loadable SDDL program, or an unknown
    /// successor.
    ///
    /// # Examples
    ///
    /// ```
    /// use mopenzl::graph::{GraphBuilder, StandardGraph};
    ///
    /// // `{"exprs": []}`: a program that consumes nothing.
    /// let description = [0xA1, 0x65, b'e', b'x', b'p', b'r', b's', 0x80];
    /// let mut builder = GraphBuilder::new();
    /// let graph = builder.sddl(&description, StandardGraph::Store)?;
    /// let compressor_graph = builder.build(graph)?;
    /// # let _ = compressor_graph;
    /// # Ok::<(), mopenzl::graph::GraphError>(())
    /// ```
    pub fn sddl(
        &mut self,
        description: &[u8],
        successor: impl Into<GraphId>,
    ) -> Result<GraphId, GraphError> {
        if !engine::graphs::validate_sddl(description) {
            return Err(GraphError::Invalid(
                "the description is not a loadable SDDL program",
            ));
        }
        let params = GraphParams::default()
            .with_graphs([successor.into()])
            .with_params(
                LocalParams::default()
                    .with_bytes(engine::graphs::SDDL_DESCRIPTION_PID, description.to_vec()),
            );
        self.parameterize_graph(StandardGraph::SimpleDataDescriptionLanguage, params)
    }

    /// The SDDL2 segmenter: runs `bytecode` over serial data once, cuts the
    /// data into chunks of about `chunk_size` bytes along the segments the
    /// program defines, and compresses each chunk's typed fields with
    /// `successor` (`ZL_Compressor_registerSDDL2Graph_advanced`).
    ///
    /// A `chunk_size` of 0 takes the default of 16 MiB; `successor` defaults
    /// to `compress_generic`. The bytecode is validated when the data is
    /// compressed, not here.
    ///
    /// # Errors
    ///
    /// A bytecode whose length is not a multiple of 4, a chunk size above
    /// `i32::MAX`, or an unknown successor.
    ///
    /// # Examples
    ///
    /// ```
    /// use mopenzl::graph::GraphBuilder;
    ///
    /// // push.i32 4, segment.create_unspecified, halt
    /// let words: [u32; 4] = [0x0001_0003, 4, 0x000C_0001, 0x0005_0001];
    /// let bytecode: Vec<u8> = words.iter().flat_map(|word| word.to_le_bytes()).collect();
    /// let mut builder = GraphBuilder::new();
    /// let graph = builder.sddl2(&bytecode, 0, None)?;
    /// let compressor_graph = builder.build(graph)?;
    /// # let _ = compressor_graph;
    /// # Ok::<(), mopenzl::graph::GraphError>(())
    /// ```
    pub fn sddl2(
        &mut self,
        bytecode: &[u8],
        chunk_size: usize,
        successor: Option<GraphId>,
    ) -> Result<GraphId, GraphError> {
        if !bytecode.len().is_multiple_of(4) {
            return Err(GraphError::Invalid(
                "SDDL2 bytecode must be a whole number of 32-bit words",
            ));
        }
        let chunk_size = i32::try_from(chunk_size)
            .map_err(|_| GraphError::Invalid("the SDDL2 chunk size exceeds i32::MAX"))?;
        let mut local = LocalParams::default()
            .with_bytes(engine::graphs::SDDL2_BYTECODE_PARAM, bytecode.to_vec());
        if chunk_size != 0 {
            local = local.with_int(engine::graphs::SDDL2_CHUNK_BYTE_SIZE_PARAM, chunk_size);
        }
        let mut params = GraphParams::default().with_params(local);
        if let Some(successor) = successor {
            params = params.with_graphs([successor]);
        }
        self.parameterize_graph(StandardGraph::SimpleDataDescriptionLanguageV2, params)
    }

    /// `brute_force`: tries every successor on the stream and keeps the one
    /// with the smallest frame, or `store` (`ZL_Compressor_buildBruteForceSelectorGraph`).
    ///
    /// # Errors
    ///
    /// No successors, or unknown ones.
    pub fn brute_force(&mut self, successors: &[GraphId]) -> Result<GraphId, GraphError> {
        if successors.is_empty() {
            return Err(GraphError::Invalid(
                "brute force needs at least one successor",
            ));
        }
        self.parameterize_graph(
            StandardGraph::BruteForce,
            GraphParams::default().with_graphs(successors),
        )
    }

    /// The ML selector graph: sends its numeric input to the successor a
    /// gradient-boosted-tree model picks from statistics of the input
    /// (`ZL_MLSelector_registerGraph`).
    ///
    /// The number of `successors` is not checked here: as in the reference, a
    /// model whose successor count differs makes the graph fail when it runs,
    /// and a permissive compression then falls back to `compress_generic`.
    ///
    /// # Errors
    ///
    /// Unknown successors.
    ///
    /// # Examples
    ///
    /// ```
    /// use mopenzl::graph::{GraphBuilder, MlSelectorConfig, StandardGraph};
    ///
    /// let mut builder = GraphBuilder::new();
    /// let config = MlSelectorConfig::untrained(2)?;
    /// let store = StandardGraph::Store.into();
    /// let zstd = StandardGraph::Zstd.into();
    /// let selector = builder.ml_selector(&config, &[store, zstd])?;
    /// let graph = builder.build(selector)?;
    /// # let _ = graph;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn ml_selector(
        &mut self,
        config: &MlSelectorConfig,
        successors: &[GraphId],
    ) -> Result<GraphId, GraphError> {
        self.parameterize_graph_with(
            StandardGraph::MlSelector.into(),
            GraphParams::default().with_graphs(successors),
            Some(RefParam(config.materialized())),
        )
    }

    /// The ML selector of `ZL_Compressor_buildUntrainedMLSelector`: a model
    /// that always picks the first of `successors`.
    ///
    /// # Errors
    ///
    /// Fewer than 2 successors, or unknown ones.
    pub fn untrained_ml_selector(&mut self, successors: &[GraphId]) -> Result<GraphId, GraphError> {
        let config = MlSelectorConfig::untrained(successors.len())
            .map_err(|_| GraphError::Invalid("an ML selector needs at least two successors"))?;
        self.ml_selector(&config, successors)
    }

    /// The clustering graph with the four standard concat nodes as
    /// clustering codecs (`ZL_Clustering_registerGraph`).
    ///
    /// Every input of the graph must carry the integer metadata
    /// [`CLUSTERING_TAG_METADATA_ID`] (see
    /// [`Input::with_metadata`](crate::input::Input::with_metadata)).
    ///
    /// # Errors
    ///
    /// Unknown successors, or a configuration that names a clustering codec
    /// outside the four.
    ///
    /// # Examples
    ///
    /// ```
    /// use mopenzl::DataType;
    /// use mopenzl::graph::{ClusteringConfig, GraphBuilder, StandardGraph};
    ///
    /// let mut builder = GraphBuilder::new();
    /// let config = ClusteringConfig::default().with_cluster(DataType::Numeric, 4, 0, 0, [1, 2]);
    /// let clustering = builder.clustering(&config, &[StandardGraph::Store.into()])?;
    /// let graph = builder.build(clustering)?;
    /// # let _ = graph;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn clustering(
        &mut self,
        config: &ClusteringConfig,
        successors: &[GraphId],
    ) -> Result<GraphId, GraphError> {
        let codecs: Vec<NodeId> = [
            StandardNode::ConcatSerial,
            StandardNode::ConcatStruct,
            StandardNode::ConcatNum,
            StandardNode::ConcatString,
        ]
        .into_iter()
        .map(NodeId::from)
        .collect();
        self.clustering_with_codecs(config, successors, &codecs)
    }

    /// The clustering graph with custom clustering codecs: nodes with one
    /// variable input and an optional numeric output followed by a typed
    /// output (`ZL_Clustering_registerGraphWithCustomClusteringCodecs`).
    ///
    /// # Errors
    ///
    /// Unknown successors or codecs, or a configuration that names a
    /// clustering codec index past `codecs`.
    pub fn clustering_with_codecs(
        &mut self,
        config: &ClusteringConfig,
        successors: &[GraphId],
        codecs: &[NodeId],
    ) -> Result<GraphId, GraphError> {
        if config.needs().1 > codecs.len() {
            return Err(GraphError::Invalid(
                "a cluster names a clustering codec that does not exist",
            ));
        }
        let blob: Vec<u8> = config.into();
        self.parameterize_graph(
            StandardGraph::Clustering,
            GraphParams::default()
                .with_graphs(successors)
                .with_nodes(codecs)
                .with_params(LocalParams::default().with_bytes(CLUSTERING_CONFIG_PARAM, blob)),
        )
    }

    /// Freezes the graphs, compressing with `start`.
    ///
    /// # Errors
    ///
    /// An unknown start graph, or [`GraphError::DictionaryNotFound`] for a
    /// node that references a dictionary the loaded bundle (if any) lacks
    /// (`CGraph_resolveDictIndices`).
    pub fn build(mut self, start: impl Into<GraphId>) -> Result<CompressorGraph, GraphError> {
        self.registry.start = self.graph_id(start.into())?;
        self.registry
            .resolve_dict_indices()
            .map_err(|error| match error {
                LoadError::NotFound(id) => GraphError::DictionaryNotFound(id),
                LoadError::Dict(error) => GraphError::Dictionary(error),
                LoadError::AlreadyLoaded => GraphError::BundleAlreadyLoaded,
            })?;
        Ok(CompressorGraph(Arc::new(self.registry)))
    }
}

/// The frozen graphs of a compressor, cheap to clone and share between
/// threads.
#[derive(Clone, Debug)]
pub struct CompressorGraph(pub(crate) Arc<Registry>);

impl Default for CompressorGraph {
    /// Standard `compress_generic`.
    fn default() -> Self {
        Self(Arc::new(Registry::new(0, reg::gid::COMPRESS_GENERIC)))
    }
}

impl CompressorGraph {
    /// A compressor graph that starts at a standard graph.
    #[must_use]
    pub fn standard(start: StandardGraph) -> Self {
        Self(Arc::new(Registry::new(
            0,
            reg::GraphId::std(u32::from(start)),
        )))
    }
}

impl From<StandardGraph> for CompressorGraph {
    fn from(start: StandardGraph) -> Self {
        Self::standard(start)
    }
}