gprx 0.2.0

Gaussian process regression in Rust: exact GPR, sparse GPR (VFE), and SVGP.
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
//! Describes the pairwise squared-Euclidean distances, filled by the process-wide thread pool column partitions.

use super::KernelScalar;
use super::simd::dist::{try_fill_ard_column, try_fill_cross_chunk, try_fill_lower_col};
use crate::error::GprError;
use faer::reborrow::ReborrowMut;
use faer::{ColMut, Mat, MatMut, MatRef};
use rayon::prelude::*;
use std::borrow::Cow;
use std::convert::Infallible;
use std::fmt;
use std::marker::PhantomData;
use wide::f64x4;

/// Returns the Rayon pool size, at least 1.
pub(crate) fn worker_count() -> usize {
    rayon::current_num_threads().max(1)
}

/// Column range `(start, len)` for chunk `idx` of `n_chunks` covering `n` columns.
///
/// Matches faer's `par_col_partition` split so each view owns a disjoint column block.
pub(crate) fn col_chunk(n: usize, idx: usize, n_chunks: usize) -> (usize, usize) {
    let chunk_size = n / n_chunks;
    let rem = n % n_chunks;
    let start = |i: usize| {
        if i < rem {
            i * (chunk_size + 1)
        } else {
            rem + i * chunk_size
        }
    };
    let begin = start(idx);
    (begin, start(idx + 1) - begin)
}

/// Entries of the lower triangle (diagonal included) in columns `[0, c)` of
/// an `n × n` matrix: `c n − c (c − 1) / 2`.
fn lower_area(n: usize, c: usize) -> u128 {
    let (n, c) = (n as u128, c as u128);
    c * n - c * c.saturating_sub(1) / 2
}

/// First column of block `idx` of `n_blocks` whose lower triangles hold
/// about equal entries: the `c` whose `lower_area(c) · n_blocks` is nearest
/// `idx · lower_area(n)`, so two blocks differ by at most one column's
/// entries. Block `n_blocks` starts at `n`.
pub(crate) fn lower_block_start(n: usize, idx: usize, n_blocks: usize) -> usize {
    let n_blocks = n_blocks.max(1);
    if idx >= n_blocks {
        return n;
    }
    let target = idx as u128 * lower_area(n, n);
    let (mut lo, mut hi) = (0, n);
    while lo < hi {
        let mid = lo + (hi - lo) / 2;
        if lower_area(n, mid) * n_blocks as u128 >= target {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    // `lo` is the first column at or past the target; the one before may be nearer.
    let k = n_blocks as u128;
    if lo > 0 && target - lower_area(n, lo - 1) * k < lower_area(n, lo) * k - target {
        lo - 1
    } else {
        lo
    }
}

/// Runs `f(first_col, block)` on the Rayon pool over `n_blocks` column
/// blocks of the square `out` whose lower triangles hold about equal
/// entries ([`lower_block_start`]). Splits by halving, so nothing is
/// allocated. Returns the first error of the left-most failing block.
pub(crate) fn par_lower_blocks<T, E, F>(out: MatMut<'_, T>, n_blocks: usize, f: &F) -> Result<(), E>
where
    T: Send,
    E: Send,
    F: Fn(usize, MatMut<'_, T>) -> Result<(), E> + Sync,
{
    let n = out.ncols();
    let n_blocks = n_blocks.max(1);
    split_lower_blocks(out, n, (0, n_blocks), n_blocks, f)
}

fn split_lower_blocks<T, E, F>(
    block: MatMut<'_, T>,
    n: usize,
    (lo, hi): (usize, usize),
    n_blocks: usize,
    f: &F,
) -> Result<(), E>
where
    T: Send,
    E: Send,
    F: Fn(usize, MatMut<'_, T>) -> Result<(), E> + Sync,
{
    let start = lower_block_start(n, lo, n_blocks);
    if hi - lo <= 1 {
        return f(start, block);
    }
    let mid = lo + (hi - lo) / 2;
    let (left, right) = block.split_at_col_mut(lower_block_start(n, mid, n_blocks) - start);
    let (a, b) = rayon::join(
        || split_lower_blocks(left, n, (lo, mid), n_blocks, f),
        || split_lower_blocks(right, n, (mid, hi), n_blocks, f),
    );
    a.and(b)
}

/// Runs `f(col, rows)` for every column `col` of the lower triangle of the
/// square `out`, on the Rayon pool in [`par_lower_blocks`] of
/// [`worker_count`]: `rows` is rows `col..n` of that column. Returns the
/// first error of the left-most failing block.
pub(crate) fn par_lower_cols<T, E, F>(out: MatMut<'_, T>, f: &F) -> Result<(), E>
where
    T: Send,
    E: Send,
    F: Fn(usize, ColMut<'_, T>) -> Result<(), E> + Sync,
{
    par_lower_cols_in(out, worker_count(), f)
}

/// [`par_lower_cols`] over `n_blocks` blocks.
pub(crate) fn par_lower_cols_in<T, E, F>(
    out: MatMut<'_, T>,
    n_blocks: usize,
    f: &F,
) -> Result<(), E>
where
    T: Send,
    E: Send,
    F: Fn(usize, ColMut<'_, T>) -> Result<(), E> + Sync,
{
    let n = out.nrows();
    par_lower_blocks(out, n_blocks, &|start, mut part: MatMut<'_, T>| {
        for local in 0..part.ncols() {
            let col = start + local;
            f(col, part.rb_mut().col_mut(local).subrows_mut(col, n - col))?;
        }
        Ok(())
    })
}

/// [`par_lower_cols`] for an `f` that cannot fail.
pub(crate) fn for_each_lower_col<T, F>(out: MatMut<'_, T>, f: &F)
where
    T: Send,
    F: Fn(usize, ColMut<'_, T>) + Sync,
{
    let done: Result<(), Infallible> = par_lower_cols(out, &|col, rows| {
        f(col, rows);
        Ok(())
    });
    match done {
        Ok(()) => {}
        Err(never) => match never {},
    }
}

/// [`par_lower_fold`] for an `f` that cannot fail.
pub(crate) fn lower_fold_infallible<R, F, J>(n: usize, f: &F, join: &J) -> R
where
    R: Send,
    F: Fn(usize, usize) -> R + Sync,
    J: Fn(R, R) -> R + Sync,
{
    let folded: Result<R, Infallible> = par_lower_fold(n, &|start, end| Ok(f(start, end)), join);
    match folded {
        Ok(value) => value,
        Err(never) => match never {},
    }
}

/// Rows `col..` of column `col` of `m` as a slice, when `m` is column-major.
#[inline(always)]
pub(crate) fn lower_col<T>(m: MatRef<'_, T>, col: usize) -> Option<&[T]> {
    m.col(col).try_as_col_major().map(|c| &c.as_slice()[col..])
}

/// Columns evaluated together before their results are folded in order.
/// The fold itself is one column at a time, so this width does not change
/// the sum.
const COL_CHUNK: usize = 16;

/// Folds `f(col, col + 1)` from column `0` to column `n`, with `join`, in
/// that column order.
///
/// A group of [`COL_CHUNK`] columns is evaluated on the Rayon pool, then
/// folded on this thread. The association is the serial scan
/// `acc = join(acc, f(col, col + 1))`, so the result depends neither on
/// scheduling nor on the pool size. Joining area blocks is a different
/// association: near an optimum the gradient is mostly cancellation, and
/// that rounding steers L-BFGS. `n = 0` calls `f(0, 0)` once. Returns the
/// first error of the left-most failing column.
pub(crate) fn par_lower_fold<R, E, F, J>(n: usize, f: &F, join: &J) -> Result<R, E>
where
    R: Send,
    E: Send,
    F: Fn(usize, usize) -> Result<R, E> + Sync,
    J: Fn(R, R) -> R + Sync,
{
    if n == 0 {
        return f(0, 0);
    }
    let mut acc: Option<R> = None;
    let mut start = 0;
    while start < n {
        let len = COL_CHUNK.min(n - start);
        let mut slots: [Option<R>; COL_CHUNK] = std::array::from_fn(|_| None);
        fill_col_sums(&mut slots[..len], start, f)?;
        for (i, slot) in slots[..len].iter_mut().enumerate() {
            let part = match slot.take() {
                Some(part) => part,
                None => {
                    debug_assert!(false, "par_lower_fold wrote every column");
                    f(start + i, start + i + 1)?
                }
            };
            acc = Some(match acc {
                None => part,
                Some(prev) => join(prev, part),
            });
        }
        start += len;
    }
    // `n > 0` and every column was written, so `acc` holds the fold.
    match acc {
        Some(acc) => Ok(acc),
        None => f(0, 0),
    }
}

/// Writes `f(col, col + 1)` into `slots[0..]`, column `col0` first.
fn fill_col_sums<R, E, F>(slots: &mut [Option<R>], col0: usize, f: &F) -> Result<(), E>
where
    R: Send,
    E: Send,
    F: Fn(usize, usize) -> Result<R, E> + Sync,
{
    let n = slots.len();
    if n == 0 {
        return Ok(());
    }
    if n == 1 {
        slots[0] = Some(f(col0, col0 + 1)?);
        return Ok(());
    }
    let mid = n / 2;
    let (left, right) = slots.split_at_mut(mid);
    // One worker: the same columns, in order, without `rayon::join`. A join
    // from outside the pool queues a job, and the queue allocates a block
    // every few dozen jobs, which would break the zero-allocation hot path.
    let (a, b) = if rayon::current_num_threads() > 1 {
        rayon::join(
            || fill_col_sums(left, col0, f),
            || fill_col_sums(right, col0 + mid, f),
        )
    } else {
        let a = fill_col_sums(left, col0, f);
        let b = fill_col_sums(right, col0 + mid, f);
        (a, b)
    };
    a.and(b)
}

fn partition_count(thread_scratch: &[Mat<f64>]) -> usize {
    if thread_scratch.is_empty() {
        worker_count()
    } else {
        thread_scratch.len()
    }
    .max(1)
}

/// Writes squared Euclidean distances for every pair of rows of `x`.
///
/// Lower triangle is filled in parallel. The upper triangle is copied afterwards
/// so [`crate::kernel::Triangle::Full`] readers stay valid. `thread_scratch` is
/// the detached per-worker slice from [`crate::workspace::WorkspaceCore`]; an empty
/// slice still parallelizes with [`worker_count`].
pub(crate) fn fill_squared_euclidean(
    x: MatRef<'_, f64>,
    mut dist: MatMut<'_, f64>,
    thread_scratch: &mut [Mat<f64>],
) {
    let n = x.nrows();
    if n == 0 {
        return;
    }
    let n_parts = partition_count(thread_scratch);
    let done: Result<(), Infallible> = par_lower_cols_in(dist.rb_mut(), n_parts, &|col, rows| {
        fill_lower_col(x, col, rows);
        Ok(())
    });
    match done {
        Ok(()) => {}
        Err(never) => match never {},
    }
    copy_lower_to_upper(dist);
}

/// Number of entries in the lower triangle (diagonal included) of an
/// `n × n` matrix.
pub(crate) fn packed_len(n: usize) -> Result<usize, GprError> {
    n.checked_add(1)
        .and_then(|n1| n.checked_mul(n1))
        .map(|cells| cells / 2)
        .ok_or(GprError::SizeOverflow)
}

/// The lower triangles of the dense column-major `n × n` `tables`, packed
/// column run by column run and mapped by `f`, into one buffer of their
/// exact length.
fn pack_dense<T: Copy, U>(tables: &[Vec<T>], n: usize, f: impl Fn(T) -> U) -> Vec<U> {
    let len = packed_len(n)
        .ok()
        .and_then(|tri| tri.checked_mul(tables.len()))
        .unwrap_or(0);
    let mut out = Vec::with_capacity(len);
    for table in tables {
        for col in 0..n {
            out.extend(table[col * n + col..(col + 1) * n].iter().map(|&v| f(v)));
        }
    }
    out
}

/// Offset of column `col` in a column-packed lower triangle of order `n`.
#[inline]
fn packed_col_offset(n: usize, col: usize) -> usize {
    // Columns 0..col hold n, n-1, …, n-col+1 entries.
    col * (2 * n - col + 1) / 2
}

/// Raw `(Δx_d)²` for every pair of rows of `x`, owned.
///
/// Each dimension keeps one triangle (diagonal included) in one of three
/// layouts ([`ArdStore`]): the lower triangle packed as column runs
/// (column `col` holds rows `col..n`), the dense `n × n` tables a caller
/// handed over (kept as they are so a fit copies nothing), or, once an
/// online model grows or shrinks the cache, row runs (row `i` holds
/// columns `0..=i`), to which a new point is one contiguous run. Read it
/// through [`Self::view`].
#[derive(Clone, Debug)]
pub(crate) struct ArdSqDiffBuf<T> {
    data: ArdStore<T>,
    n: usize,
    d: usize,
}

/// The layout of an [`ArdSqDiffBuf`].
#[derive(Debug)]
enum ArdStore<T> {
    /// The lower triangles, dimension after dimension, column by column:
    /// column `col` holds rows `col..n`.
    Packed(Vec<T>),
    /// One dense column-major `n × n` table per dimension; only the lower
    /// triangle is read.
    Dense(Vec<Vec<T>>),
    /// One buffer per dimension, row by row: row `i` holds columns
    /// `0..=i` and starts at `i(i+1)/2`. A buffer's spare capacity is the
    /// room for later points.
    Rows(Vec<Vec<T>>),
}

/// The range of column `col`'s rows `col..n` of dimension `dim` in packed
/// lower triangles of order `n`.
pub(crate) fn packed_run(n: usize, dim: usize, col: usize) -> std::ops::Range<usize> {
    let start = dim * (n * (n + 1) / 2) + packed_col_offset(n, col);
    start..start + (n - col)
}

/// A clone keeps the room the row runs reserved, so a cloned online model
/// grows as the original does.
impl<T: Clone> Clone for ArdStore<T> {
    fn clone(&self) -> Self {
        match self {
            Self::Packed(data) => Self::Packed(data.clone()),
            Self::Dense(tables) => Self::Dense(tables.clone()),
            Self::Rows(rows) => Self::Rows(
                rows.iter()
                    .map(|dim| {
                        let mut copy = Vec::with_capacity(dim.capacity());
                        copy.extend_from_slice(dim);
                        copy
                    })
                    .collect(),
            ),
        }
    }
}

/// Offset of row `i` in a row-packed lower triangle.
#[inline]
fn row_offset(i: usize) -> usize {
    i * (i + 1) / 2
}

/// Columns of a lower triangle walked together into row runs.
const ROW_RUN_TILE: usize = 16;

/// Walks the lower triangle of order `n` (diagonal included) into row
/// runs, a tile of columns at a time, so a column-major source is read
/// down its columns: `f(i, row_offset(i) + j, j)` for each pair `j ≤ i`.
/// Every place that lays pairs out as row runs takes this one walk.
pub(crate) fn walk_row_runs(n: usize, mut f: impl FnMut(usize, usize, usize)) {
    let mut j0 = 0;
    while j0 < n {
        let j1 = (j0 + ROW_RUN_TILE).min(n);
        for i in j0..n {
            let base = row_offset(i);
            for j in j0..j1.min(i + 1) {
                f(i, base + j, j);
            }
        }
        j0 = j1;
    }
}

impl<T: KernelScalar> ArdSqDiffBuf<T> {
    /// Fills the cache for the rows of `x`.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::SizeOverflow`] when `d · n(n+1)/2` overflows.
    pub(crate) fn new(x: MatRef<'_, T>) -> Result<Self, GprError> {
        let n = x.nrows();
        let d = x.ncols();
        let len = packed_len(n)?
            .checked_mul(d)
            .ok_or(GprError::SizeOverflow)?;
        let mut data = vec![T::from_f64(0.0); len];
        T::write_ard(x, &mut data);
        Ok(Self::from_packed(data, n, d))
    }

    /// Packs the lower triangles of `d` dense `n × n` blocks; `pair(k, i, j)`
    /// is `(Δ_k)²` of the pair `(i, j)`, `i ≥ j`.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::SizeOverflow`] when `d · n(n+1)/2` overflows.
    #[cfg(test)]
    pub(crate) fn from_pairs(
        n: usize,
        d: usize,
        pair: impl Fn(usize, usize, usize) -> T,
    ) -> Result<Self, GprError> {
        let len = packed_len(n)?
            .checked_mul(d)
            .ok_or(GprError::SizeOverflow)?;
        let mut data = Vec::with_capacity(len);
        for k in 0..d {
            for col in 0..n {
                for row in col..n {
                    data.push(pair(k, row, col));
                }
            }
        }
        Ok(Self::from_packed(data, n, d))
    }

    /// Packs the lower triangles of `d` dense, column-major `n × n` blocks
    /// (`block(k)` is dimension `k`), one contiguous run per column.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::SizeOverflow`] when `d · n(n+1)/2` overflows.
    pub(crate) fn from_dense<'b>(
        n: usize,
        d: usize,
        block: impl Fn(usize) -> &'b [f64],
    ) -> Result<Self, GprError> {
        let len = packed_len(n)?
            .checked_mul(d)
            .ok_or(GprError::SizeOverflow)?;
        let mut data = Vec::with_capacity(len);
        for k in 0..d {
            let block = block(k);
            for col in 0..n {
                data.extend(
                    block[col * n + col..(col + 1) * n]
                        .iter()
                        .map(|&v| T::from_f64(v)),
                );
            }
        }
        Ok(Self::from_packed(data, n, d))
    }

    /// A cache of `n` points and `d` dimensions from its packed values:
    /// dimension after dimension, each the lower triangle column by column.
    pub(crate) fn from_packed(data: Vec<T>, n: usize, d: usize) -> Self {
        debug_assert_eq!(
            Some(data.len()),
            packed_len(n).ok().and_then(|l| l.checked_mul(d))
        );
        Self {
            data: ArdStore::Packed(data),
            n,
            d,
        }
    }

    /// The lower triangles in their persist order, whatever the layout:
    /// dimension after dimension, each column by column (column `col`
    /// holds rows `col..n`). A packed cache is read in place.
    pub(crate) fn packed(&self) -> Cow<'_, [T]>
    where
        T: Copy,
    {
        let n = self.n;
        match &self.data {
            ArdStore::Packed(data) => Cow::Borrowed(data),
            ArdStore::Dense(tables) => Cow::Owned(pack_dense(tables, n, |v| v)),
            ArdStore::Rows(rows) => {
                let mut out = Vec::with_capacity(self.stored_capacity_hint());
                for dim in rows {
                    for col in 0..n {
                        out.extend((col..n).map(|row| dim[row * (row + 1) / 2 + col]));
                    }
                }
                Cow::Owned(out)
            }
        }
    }

    /// `d · n(n+1)/2`, the packed length of every dimension: a capacity
    /// hint, `0` when it does not fit (a `Vec` then grows as it fills).
    fn stored_capacity_hint(&self) -> usize {
        packed_len(self.n)
            .ok()
            .and_then(|tri| tri.checked_mul(self.d))
            .unwrap_or(0)
    }

    /// A cache of `n` points from `d` dense column-major `n × n` tables,
    /// kept as they are (one per dimension).
    pub(crate) fn from_tables(tables: Vec<Vec<T>>, n: usize) -> Self {
        debug_assert!(tables.iter().all(|t| Some(t.len()) == n.checked_mul(n)));
        Self {
            d: tables.len(),
            data: ArdStore::Dense(tables),
            n,
        }
    }

    /// The same cache with every value mapped by `f` (a cast), in one pass
    /// over the stored triangles: row runs stay row runs, and the other
    /// layouts are packed as column runs.
    pub(crate) fn map<U>(&self, f: impl Fn(T) -> U) -> ArdSqDiffBuf<U>
    where
        T: Copy,
    {
        let data = match &self.data {
            ArdStore::Packed(data) => ArdStore::Packed(data.iter().map(|&v| f(v)).collect()),
            ArdStore::Rows(rows) => ArdStore::Rows(
                rows.iter()
                    .map(|dim| dim.iter().map(|&v| f(v)).collect())
                    .collect(),
            ),
            ArdStore::Dense(tables) => ArdStore::Packed(pack_dense(tables, self.n, f)),
        };
        ArdSqDiffBuf {
            data,
            n: self.n,
            d: self.d,
        }
    }

    /// Makes room for one more point: the cache becomes row runs (once,
    /// for a cache of column runs or dense tables), and each dimension's
    /// buffer reserves the new row, so [`Self::push_point`] does not
    /// allocate. A buffer grows as a `Vec` does, by doubling.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::SizeOverflow`] when the grown cache does not fit.
    pub(crate) fn reserve_point(&mut self) -> Result<(), GprError> {
        self.lay_out_rows()?;
        let n = self.n;
        if let ArdStore::Rows(rows) = &mut self.data {
            let more = n.checked_add(1).ok_or(GprError::SizeOverflow)?;
            for dim in rows {
                dim.try_reserve(more).map_err(|_| GprError::SizeOverflow)?;
            }
        }
        Ok(())
    }

    /// Lays the cache out as row runs, unless it is already, so a later
    /// [`Self::remove_point`] does not fail.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::SizeOverflow`] when the row runs do not fit.
    pub(crate) fn ready_to_change(&mut self) -> Result<(), GprError> {
        self.lay_out_rows()
    }

    /// Lays the cache out as row runs, unless it is already.
    fn lay_out_rows(&mut self) -> Result<(), GprError> {
        if matches!(self.data, ArdStore::Rows(_)) {
            return Ok(());
        }
        let n = self.n;
        let len = packed_len(n)?;
        let view = self.view();
        let Runs::Lower(lower) = view.runs() else {
            return Ok(());
        };
        let mut rows = Vec::with_capacity(self.d);
        for dim in 0..self.d {
            let mut out = Vec::new();
            out.try_reserve_exact(packed_len(n + 1)?)
                .map_err(|_| GprError::SizeOverflow)?;
            out.resize(len, T::from_f64(0.0));
            walk_row_runs(n, |i, at, j| out[at] = lower.column(dim, j)[i - j]);
            rows.push(out);
        }
        self.data = ArdStore::Rows(rows);
        Ok(())
    }

    /// Whether [`Self::push_point`] has its row runs and their room: after
    /// [`Self::reserve_point`].
    pub(crate) fn can_push(&self) -> bool {
        let n = self.n;
        matches!(&self.data, ArdStore::Rows(rows)
            if rows.iter().all(|dim| dim.capacity() - dim.len() > n))
    }

    /// Whether [`Self::remove_point`] has its row runs: after
    /// [`Self::ready_to_change`].
    pub(crate) fn can_remove(&self) -> bool {
        matches!(self.data, ArdStore::Rows(_))
    }

    /// Appends point `n`: `column(dim)` holds its `(Δ_dim)²` to the points
    /// `0..n` (at least `n` values); its own diagonal is zero. Each
    /// dimension's buffer takes one contiguous run, in the room
    /// [`Self::reserve_point`] made ([`Self::can_push`]).
    pub(crate) fn push_point<'c>(&mut self, column: impl Fn(usize) -> &'c [T])
    where
        T: 'c,
    {
        debug_assert!(self.can_push(), "push_point before reserve_point");
        let n = self.n;
        let ArdStore::Rows(rows) = &mut self.data else {
            return;
        };
        for (dim, buffer) in rows.iter_mut().enumerate() {
            buffer.extend_from_slice(&column(dim)[..n]);
            buffer.push(T::from_f64(0.0));
        }
        self.n = n + 1;
    }

    /// Removes point `index` (`< n`) in place: row `index` goes, each later
    /// row loses its column `index`, and the later points move up one. One
    /// forward pass over the rows past `index`; rows before it stay put.
    /// The cache holds row runs ([`Self::can_remove`]).
    pub(crate) fn remove_point(&mut self, index: usize) {
        let n = self.n;
        debug_assert!(index < n, "remove_point past the points");
        debug_assert!(self.can_remove(), "remove_point before ready_to_change");
        let ArdStore::Rows(rows) = &mut self.data else {
            return;
        };
        let compact = |buffer: &mut Vec<T>| {
            let mut write = row_offset(index);
            for i in index + 1..n {
                let start = row_offset(i);
                // Columns `0..index`, then `index + 1..=i`.
                buffer.copy_within(start..start + index, write);
                write += index;
                buffer.copy_within(start + index + 1..start + i + 1, write);
                write += i - index;
            }
            buffer.truncate(write);
        };
        // From inside the pool (a delete beside the factor's update), the
        // dimensions' buffers move side by side.
        if rows.len() > 1 && rayon::current_thread_index().is_some() {
            rows.par_iter_mut().for_each(compact);
        } else {
            rows.iter_mut().for_each(compact);
        }
        self.n = n - 1;
    }

    /// `(points, dimensions)` the cache was filled for.
    #[cfg(test)]
    pub(crate) fn shape(&self) -> (usize, usize) {
        (self.n, self.d)
    }

    /// Number of stored values.
    #[cfg(test)]
    pub(crate) fn stored_len(&self) -> usize {
        match &self.data {
            ArdStore::Packed(data) => data.len(),
            ArdStore::Dense(tables) | ArdStore::Rows(tables) => tables.iter().map(Vec::len).sum(),
        }
    }

    /// Whether the cache keeps the caller's dense tables.
    #[cfg(test)]
    pub(crate) fn is_dense(&self) -> bool {
        matches!(self.data, ArdStore::Dense(_))
    }

    /// Overwrites every cached value, to show that a reader uses the cache.
    #[cfg(test)]
    pub(crate) fn poison(&mut self, value: T) {
        match &mut self.data {
            ArdStore::Packed(data) => data.fill(value),
            ArdStore::Dense(tables) | ArdStore::Rows(tables) => {
                tables.iter_mut().for_each(|t| t.fill(value));
            }
        }
    }

    pub(crate) fn view(&self) -> ArdSqDiff<'_, T> {
        let data = match &self.data {
            ArdStore::Packed(data) => {
                StoreRef::Packed(data, data.len().checked_div(self.d).unwrap_or(0))
            }
            ArdStore::Dense(tables) => StoreRef::Dense(tables),
            ArdStore::Rows(rows) => StoreRef::Rows(rows),
        };
        ArdSqDiff {
            data,
            n: self.n,
            d: self.d,
        }
    }
}

/// Borrowed raw `(Δx_d)²` cache of [`ArdSqDiffBuf`].
///
/// [`Self::get`] reads any pair, in either order. A cache of column runs
/// hands out [`Self::lower`], one of row runs [`Self::rows`]: the loops
/// that read whole runs take the layout's own.
#[derive(Clone, Copy, Debug)]
pub struct ArdSqDiff<'a, T> {
    data: StoreRef<'a, T>,
    n: usize,
    d: usize,
}

/// The borrowed layout of an [`ArdSqDiff`].
#[derive(Clone, Copy, Debug)]
enum StoreRef<'a, T> {
    /// Packed lower triangles, with the entries per dimension.
    Packed(&'a [T], usize),
    /// Dense `n × n` tables, one per dimension.
    Dense(&'a [Vec<T>]),
    /// Dense `n × n` tables, one per dimension, one after another.
    Flat(&'a [T]),
    /// Row runs, one buffer per dimension.
    Rows(&'a [Vec<T>]),
}

/// One pair of a cache in every dimension ([`ArdSqDiff::pair`]): dimension
/// `dim` is `data[dim * stride + at]`, or `tables[dim][at]`.
#[derive(Clone, Copy, Debug)]
pub(crate) enum PairAt<'a, T> {
    /// One buffer, the dimensions `stride` values apart.
    Strided {
        data: &'a [T],
        stride: usize,
        at: usize,
    },
    /// One buffer per dimension.
    Tables { tables: &'a [Vec<T>], at: usize },
}

/// The column runs of a cache: column `col` holds rows `col..n`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct LowerRuns<'a, T> {
    data: ColumnRef<'a, T>,
    n: usize,
}

/// The layouts that hold column runs.
#[derive(Clone, Copy, Debug)]
enum ColumnRef<'a, T> {
    /// Packed lower triangles, with the entries per dimension.
    Packed(&'a [T], usize),
    /// Dense `n × n` tables, one per dimension.
    Dense(&'a [Vec<T>]),
    /// Dense `n × n` tables, one per dimension, one after another.
    Flat(&'a [T]),
}

/// The runs a cache holds, in its own order: a loop over whole runs takes
/// the arm of its layout, so no layout is read as another.
#[derive(Clone, Copy, Debug)]
pub(crate) enum Runs<'a, T> {
    /// Column runs (packed triangles or dense tables).
    Lower(LowerRuns<'a, T>),
    /// Row runs (an online model's).
    Rows(RowRuns<'a, T>),
}

/// The row runs of a cache: row `i` holds columns `0..=i`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct RowRuns<'a, T> {
    rows: &'a [Vec<T>],
    n: usize,
}

impl<'a, T> LowerRuns<'a, T> {
    /// `(x_row,dim − x_col,dim)²` for rows `col..n`, in row order.
    #[inline]
    pub(crate) fn column(&self, dim: usize, col: usize) -> &'a [T] {
        let n = self.n;
        match self.data {
            ColumnRef::Packed(data, block) => {
                let start = dim * block + packed_col_offset(n, col);
                &data[start..start + (n - col)]
            }
            ColumnRef::Dense(tables) => &tables[dim][col * n + col..(col + 1) * n],
            ColumnRef::Flat(data) => {
                &data[dim * n * n + col * n + col..dim * n * n + (col + 1) * n]
            }
        }
    }

    /// Every stored value of dimension `dim`, when the cache is packed:
    /// the lower triangle, column by column.
    #[inline]
    pub(crate) fn packed_block(&self, dim: usize) -> Option<&'a [T]> {
        match self.data {
            ColumnRef::Packed(data, block) => Some(&data[dim * block..(dim + 1) * block]),
            ColumnRef::Dense(_) | ColumnRef::Flat(_) => None,
        }
    }
}

impl<'a, T> RowRuns<'a, T> {
    /// Number of points.
    pub(crate) fn n(&self) -> usize {
        self.n
    }

    /// Every stored value of dimension `dim`: row after row.
    #[inline]
    pub(crate) fn buffer(&self, dim: usize) -> &'a [T] {
        &self.rows[dim][..row_offset(self.n)]
    }

    /// `(x_i,dim − x_col,dim)²` for columns `0..=i`, in column order.
    #[inline]
    pub(crate) fn row(&self, dim: usize, i: usize) -> &'a [T] {
        let start = row_offset(i);
        &self.rows[dim][start..start + i + 1]
    }
}

impl<'a, T: KernelScalar> ArdSqDiff<'a, T> {
    /// The `d` dense checked `n × n` squares of `data`, one after another
    /// (only the lower triangles are read).
    pub(crate) fn flat(data: &'a [T], n: usize) -> Self {
        let d = data.len().checked_div(n * n).unwrap_or(0);
        Self {
            data: StoreRef::Flat(data),
            n,
            d,
        }
    }

    /// Number of points.
    pub(crate) fn n(&self) -> usize {
        self.n
    }

    /// Number of dimensions.
    pub(crate) fn d(&self) -> usize {
        self.d
    }

    /// The runs the cache holds.
    #[inline]
    pub(crate) fn runs(&self) -> Runs<'a, T> {
        let n = self.n;
        match self.data {
            StoreRef::Packed(data, block) => Runs::Lower(LowerRuns {
                data: ColumnRef::Packed(data, block),
                n,
            }),
            StoreRef::Dense(tables) => Runs::Lower(LowerRuns {
                data: ColumnRef::Dense(tables),
                n,
            }),
            StoreRef::Flat(data) => Runs::Lower(LowerRuns {
                data: ColumnRef::Flat(data),
                n,
            }),
            StoreRef::Rows(rows) => Runs::Rows(RowRuns { rows, n }),
        }
    }

    /// The column runs, when the cache holds them (packed or dense).
    #[inline]
    pub(crate) fn lower(&self) -> Option<LowerRuns<'a, T>> {
        match self.runs() {
            Runs::Lower(lower) => Some(lower),
            Runs::Rows(_) => None,
        }
    }

    /// The row runs, when the cache holds them.
    #[inline]
    pub(crate) fn rows(&self) -> Option<RowRuns<'a, T>> {
        match self.runs() {
            Runs::Rows(rows) => Some(rows),
            Runs::Lower(_) => None,
        }
    }

    /// Where the pair `(row, col)` sits in every dimension, the layout
    /// read once: a loop over the dimensions then only indexes.
    #[inline(always)]
    pub(crate) fn pair(&self, row: usize, col: usize) -> PairAt<'a, T> {
        let (row, col) = if row >= col { (row, col) } else { (col, row) };
        let n = self.n;
        match self.data {
            StoreRef::Packed(data, block) => PairAt::Strided {
                data,
                stride: block,
                at: packed_col_offset(n, col) + row - col,
            },
            StoreRef::Flat(data) => PairAt::Strided {
                data,
                stride: n * n,
                at: col * n + row,
            },
            StoreRef::Dense(tables) => PairAt::Tables {
                tables,
                at: col * n + row,
            },
            StoreRef::Rows(rows) => PairAt::Tables {
                tables: rows,
                at: row_offset(row) + col,
            },
        }
    }

    /// `(x_row,dim − x_col,dim)²` for any pair. A loop over the dimensions
    /// of one pair reads [`Self::pair`] instead, which matches the layout
    /// once.
    #[inline]
    pub(crate) fn get(&self, dim: usize, row: usize, col: usize) -> T {
        let (row, col) = if row >= col { (row, col) } else { (col, row) };
        // One flat match on the layout, the packed cache of a fit first: a
        // read per pair and dimension sits in the kernels' inner loops.
        let n = self.n;
        match self.data {
            StoreRef::Packed(data, block) => {
                data[dim * block + packed_col_offset(n, col) + row - col]
            }
            StoreRef::Rows(rows) => rows[dim][row_offset(row) + col],
            StoreRef::Dense(tables) => tables[dim][col * n + row],
            StoreRef::Flat(data) => data[dim * n * n + col * n + row],
        }
    }

    /// The first stored pair `(row, col)`, `row ≥ col`, of dimension `dim`
    /// whose value `bad` picks, in the layout's own order.
    pub(crate) fn position(&self, dim: usize, bad: impl Fn(T) -> bool) -> Option<(usize, usize)> {
        match self.runs() {
            Runs::Rows(rows) => (0..self.n).find_map(|i| {
                rows.row(dim, i)
                    .iter()
                    .position(|&v| bad(v))
                    .map(|j| (i, j))
            }),
            Runs::Lower(lower) => (0..self.n).find_map(|j| {
                lower
                    .column(dim, j)
                    .iter()
                    .position(|&v| bad(v))
                    .map(|k| (j + k, j))
            }),
        }
    }

    /// The same cache as `f64` when `T` is `f64`, for the SIMD paths.
    pub(crate) fn as_f64(self) -> Option<ArdSqDiff<'a, f64>> {
        let data = match self.data {
            StoreRef::Packed(data, block) => StoreRef::Packed(T::as_f64_slice(data)?, block),
            StoreRef::Dense(tables) => StoreRef::Dense(T::as_f64_vecs(tables)?),
            StoreRef::Flat(data) => StoreRef::Flat(T::as_f64_slice(data)?),
            StoreRef::Rows(rows) => StoreRef::Rows(T::as_f64_vecs(rows)?),
        };
        Some(ArdSqDiff {
            data,
            n: self.n,
            d: self.d,
        })
    }
}

/// Whether the values of an [`ArdBlocks`] were checked when they were bound.
///
/// [`Checked`] blocks (the training triangles, a cast, a repaired or
/// filled table) are read as they are. [`Unchecked`] blocks (a caller's
/// prediction block that an `f64` model reads in place) are checked as
/// they are read, so the caller's values are read once: their values come
/// out only through [`ArdBlocks::read`], [`ArdBlocks::column`], and
/// [`ArdBlocks::gates`], each of which checks what it hands out.
pub trait BlockState: Copy + fmt::Debug + Send + Sync + 'static + sealed::Sealed {
    /// Whether the values were checked when bound.
    const CHECKED: bool;
}

/// Blocks whose values were checked when bound.
#[derive(Clone, Copy, Debug)]
pub enum Checked {}

/// Blocks whose values are checked as they are read.
#[derive(Clone, Copy, Debug)]
pub enum Unchecked {}

mod sealed {
    pub trait Sealed {}
    impl Sealed for super::Checked {}
    impl Sealed for super::Unchecked {}
}

impl BlockState for Checked {
    const CHECKED: bool = true;
}

impl BlockState for Unchecked {
    const CHECKED: bool = false;
}

/// Raw `(Δ_d)²` of a rectangular block (`rows × cols`), one dense
/// column-major block per dimension: `(row, col)` of dimension `k` is
/// `block(k)[row + col * rows]`. `S` says whether the values were checked
/// when bound ([`BlockState`]).
#[derive(Clone, Copy, Debug)]
pub struct ArdBlocks<'a, T, S = Checked> {
    /// One block per dimension.
    blocks: BlockList<'a, T>,
    rows: usize,
    cols: usize,
    /// First column of the stored blocks this view starts at.
    col0: usize,
    /// Distance between the starts of two columns of a block: `rows`, or
    /// more for a block with room for more rows (an online sparse model's).
    ld: usize,
    /// The slot's place in the kernel's slots, which an error of an
    /// unchecked value names ([`Self::of_slot`]).
    slot: Option<usize>,
    state: PhantomData<S>,
}

/// The per-dimension blocks of an [`ArdBlocks`]: a caller's borrowed or
/// moved blocks, blocks packed one after another (a fill, a repair, or a
/// cast), or one block repeated.
#[derive(Clone, Copy, Debug)]
pub(crate) enum BlockList<'a, T> {
    Slices(&'a [&'a [T]]),
    /// Owned blocks a caller moved in.
    Vecs(&'a [Vec<T>]),
    /// `dims` blocks of `len` values each, one after another.
    Packed(&'a [T], usize, usize),
    /// One block for each of `dims` dimensions.
    Repeat(&'a [T], usize),
    /// The packed training triangles: pair `(row, col)` of the square,
    /// read in either order. No dense block exists.
    Triangles(ArdSqDiff<'a, T>),
}

impl<'a, T: KernelScalar, S: BlockState> ArdBlocks<'a, T, S> {
    /// `blocks` of `rows × cols` pairs in the state `S`. The type does not
    /// prove the state: only binding code makes a `Checked` block, from
    /// values it checked, cast, or packed.
    pub(crate) fn new(blocks: BlockList<'a, T>, rows: usize, cols: usize, col0: usize) -> Self {
        Self::strided(blocks, rows, cols, col0, rows)
    }

    /// As [`Self::new`], with column `c` of each block starting at `c · ld`
    /// (`ld ≥ rows`).
    pub(crate) fn strided(
        blocks: BlockList<'a, T>,
        rows: usize,
        cols: usize,
        col0: usize,
        ld: usize,
    ) -> Self {
        Self {
            blocks,
            rows,
            cols,
            col0,
            ld,
            slot: None,
            state: PhantomData,
        }
    }

    /// These blocks as those of the slot at `place` in the kernel's slots.
    pub(crate) fn of_slot(self, place: usize) -> Self {
        Self {
            slot: Some(place),
            ..self
        }
    }

    /// `err` of these blocks, named by their slot when it is known.
    pub(crate) fn locate(&self, err: GprError) -> GprError {
        match self.slot {
            Some(place) => err.in_slot(place),
            None => err,
        }
    }

    /// Columns `start..start + len` of these blocks.
    pub(crate) fn subcols(self, start: usize, len: usize) -> Self {
        Self {
            cols: len,
            col0: self.col0 + start,
            ..self
        }
    }

    /// Number of dimensions.
    pub(crate) fn d(&self) -> usize {
        match self.blocks {
            BlockList::Slices(blocks) => blocks.len(),
            BlockList::Vecs(blocks) => blocks.len(),
            BlockList::Packed(_, _, dims) | BlockList::Repeat(_, dims) => dims,
            BlockList::Triangles(cache) => cache.d(),
        }
    }

    /// Rows of the view.
    pub(crate) fn rows(&self) -> usize {
        self.rows
    }

    /// Columns of the view.
    pub(crate) fn cols(&self) -> usize {
        self.cols
    }

    /// The dense block of dimension `dim`, or an empty slice when the
    /// blocks are packed triangles. Raw: only [`Checked`] blocks expose it.
    fn raw_block(&self, dim: usize) -> &'a [T] {
        match self.blocks {
            BlockList::Slices(blocks) => blocks[dim],
            BlockList::Vecs(blocks) => &blocks[dim],
            BlockList::Packed(all, len, _) => &all[dim * len..(dim + 1) * len],
            BlockList::Repeat(block, _) => block,
            BlockList::Triangles(_) => &[],
        }
    }

    fn raw_get(&self, dim: usize, row: usize, col: usize) -> T {
        match self.blocks {
            BlockList::Triangles(cache) => cache.get(dim, row, col + self.col0),
            _ => self.raw_block(dim)[row + (col + self.col0) * self.ld],
        }
    }

    /// `(Δ_dim)²` of the pair `(row, col)`, checked unless the blocks were.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::InvalidDistance`] if the value is not finite or
    /// is negative, at its place in the caller's table.
    #[inline]
    pub(crate) fn read(&self, dim: usize, row: usize, col: usize) -> Result<T, GprError> {
        let v = self.raw_get(dim, row, col);
        if S::CHECKED || super::sources::valid(v.to_f64()) {
            Ok(v)
        } else {
            let err = super::sources::invalid_value(v.to_f64(), row, col + self.col0);
            Err(self.locate(err.in_dim(dim)))
        }
    }

    /// Column `col` of dimension `dim` (every row) as an `f64` slice,
    /// checked unless the blocks were; `None` when the blocks are not dense
    /// `f64` columns.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::InvalidDistance`] at the first invalid value.
    pub(crate) fn column(&self, dim: usize, col: usize) -> Result<Option<&'a [f64]>, GprError> {
        let Some(run) = self.f64_column(dim, col) else {
            return Ok(None);
        };
        if !S::CHECKED && !super::simd::all_valid_distances(run) {
            let err = super::sources::first_invalid_from(run, self.rows, self.col0 + col);
            return Err(self.locate(err.in_dim(dim)));
        }
        Ok(Some(run))
    }

    /// Whether every block is a dense `f64` block holding all the view's
    /// columns, as [`Self::column`] and [`Gates::gate`] read them.
    pub(crate) fn dense_f64(&self) -> bool {
        let end = match self.cols {
            0 => 0,
            cols => (self.col0 + cols - 1) * self.ld + self.rows,
        };
        !matches!(self.blocks, BlockList::Triangles(_))
            && T::as_f64_slice(&[]).is_some()
            && (0..self.d()).all(|dim| self.raw_block(dim).len() >= end)
    }

    fn f64_column(&self, dim: usize, col: usize) -> Option<&'a [f64]> {
        if matches!(self.blocks, BlockList::Triangles(_)) {
            return None;
        }
        let start = (self.col0 + col) * self.ld;
        T::as_f64_slice(self.raw_block(dim).get(start..start + self.rows)?)
    }

    /// The dense `f64` blocks of every dimension, from which a SIMD loop
    /// takes one [`Gate`] per column; `None` when the blocks are not dense
    /// `f64` columns or have more than [`GATE_DIMS`] dimensions.
    pub(crate) fn gates(&self) -> Option<Gates<'a, S>> {
        let d = self.d();
        if d > GATE_DIMS || !self.dense_f64() {
            return None;
        }
        let mut blocks: [&'a [f64]; GATE_DIMS] = [&[]; GATE_DIMS];
        for (dim, block) in blocks.iter_mut().enumerate().take(d) {
            *block = T::as_f64_slice(self.raw_block(dim))?;
        }
        Some(Gates {
            blocks,
            d,
            rows: self.rows,
            col0: self.col0,
            ld: self.ld,
            state: PhantomData,
        })
    }
}

impl<'a, T: KernelScalar> ArdBlocks<'a, T, Checked> {
    /// The dense block of dimension `dim` (`rows` rows per column), or an
    /// empty slice when the blocks are packed triangles. Only a block whose
    /// columns lie back to back ([`Self::new`]) reads this way.
    pub(crate) fn block(&self, dim: usize) -> &'a [T] {
        self.raw_block(dim)
    }
}

/// The dense `f64` blocks of an [`ArdBlocks`] ([`ArdBlocks::gates`]).
#[derive(Clone, Copy)]
pub(crate) struct Gates<'a, S> {
    blocks: [&'a [f64]; GATE_DIMS],
    d: usize,
    rows: usize,
    col0: usize,
    /// Column stride of the blocks ([`ArdBlocks::strided`]).
    ld: usize,
    state: PhantomData<S>,
}

impl<'a, S: BlockState> Gates<'a, S> {
    /// The column `col` of every dimension, for one pass of a SIMD loop that
    /// reads each value once: a [`Gate`] folds the check of every value it
    /// sums (unless the blocks were checked), and [`Gate::verdict`] gives
    /// the result.
    #[inline]
    pub(crate) fn gate(&self, col: usize) -> Gate<'a, S> {
        let start = (self.col0 + col) * self.ld;
        let mut runs: [&'a [f64]; GATE_DIMS] = [&[]; GATE_DIMS];
        for (run, block) in runs.iter_mut().zip(&self.blocks[..self.d]) {
            *run = &block[start..start + self.rows];
        }
        Gate {
            runs,
            d: self.d,
            nonfinite: f64x4::ZERO,
            least: f64x4::ZERO,
            rows: self.rows,
            col: self.col0 + col,
            state: PhantomData,
        }
    }
}

/// Dimensions a [`Gate`] holds.
pub(crate) const GATE_DIMS: usize = 16;

/// One column of every dimension of an [`ArdBlocks`], handed to a SIMD loop
/// that reads each value once. The loop gets only weighted sums of the
/// values ([`Self::weighted4`], [`Self::weighted1`]); for [`Unchecked`]
/// blocks each value is folded into the check as it is summed (two lane
/// sums without a branch: `v · 0`, which stays `0` unless a value is `NaN`
/// or infinite, and the least value, which stays `≥ 0` unless one is
/// negative), and [`Self::verdict`] reports the result.
#[must_use = "a gate's sums are valid only once its verdict is Ok"]
pub(crate) struct Gate<'a, S> {
    runs: [&'a [f64]; GATE_DIMS],
    d: usize,
    nonfinite: f64x4,
    least: f64x4,
    rows: usize,
    col: usize,
    state: PhantomData<S>,
}

impl<S: BlockState> Gate<'_, S> {
    /// `Σ_d w_d src_d` of rows `i..i + 4`.
    #[inline(always)]
    pub(crate) fn weighted4(&mut self, w: &[f64], i: usize) -> f64x4 {
        let mut r2 = f64x4::ZERO;
        for (run, &wd) in self.runs[..self.d].iter().zip(w) {
            let v = super::simd::load4(run, i);
            if !S::CHECKED {
                self.nonfinite += v * f64x4::ZERO;
                self.least = self.least.fast_min(v);
            }
            r2 += v * f64x4::splat(wd);
        }
        r2
    }

    /// `Σ_d w_d src_d` of row `i`.
    #[inline(always)]
    pub(crate) fn weighted1(&mut self, w: &[f64], i: usize) -> f64 {
        let mut r2 = 0.0;
        for (run, &wd) in self.runs[..self.d].iter().zip(w) {
            let v = run[i];
            if !S::CHECKED {
                self.nonfinite += f64x4::splat(v * 0.0);
                self.least = self.least.fast_min(f64x4::splat(v));
            }
            r2 += v * wd;
        }
        r2
    }

    /// Whether every value summed was valid.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::InvalidDistance`] at the first invalid value of
    /// the column, when one was summed.
    pub(crate) fn verdict(self) -> Result<(), GprError> {
        if S::CHECKED {
            return Ok(());
        }
        // Exact, not a tolerance: a sum of `v · 0` is `0` or `NaN`.
        let valid =
            self.nonfinite.reduce_add() == 0.0 && self.least.to_array().iter().all(|&l| l >= 0.0);
        if valid {
            return Ok(());
        }
        for (dim, run) in self.runs[..self.d].iter().enumerate() {
            if !super::simd::all_valid_distances(run) {
                let err = super::sources::first_invalid_from(run, self.rows, self.col);
                return Err(err.in_dim(dim));
            }
        }
        // The lanes failed, yet no run holds an invalid value: the two
        // checks disagree. Refuse without claiming a place.
        Err(super::sources::unlocated())
    }
}

/// Writes raw `(Δx_d)²` into `cache`, packed as [`ArdSqDiffBuf`] stores it.
/// Columns are filled in parallel.
pub(crate) fn fill_ard_squared_diff(x: MatRef<'_, f64>, cache: &mut [f64]) {
    let n = x.nrows();
    let d = x.ncols();
    if n == 0 || d == 0 {
        return;
    }
    debug_assert_eq!(cache.len(), d * n * (n + 1) / 2);
    let mut columns: Vec<(usize, usize, &mut [f64])> = Vec::with_capacity(n * d);
    let mut rest = cache;
    for dim in 0..d {
        for col in 0..n {
            let (head, tail) = rest.split_at_mut(n - col);
            columns.push((dim, col, head));
            rest = tail;
        }
    }
    columns.into_par_iter().for_each(|(dim, col, dest)| {
        if !try_fill_ard_column(x, dim, col, dest) {
            for (offset, slot) in dest.iter_mut().enumerate() {
                let diff = x[(col + offset, dim)] - x[(col, dim)];
                *slot = diff * diff;
            }
        }
    });
}

/// Requires `cache` to hold `n` points in `d` dimensions.
pub(crate) fn require_ard_sq_diff_shape<T: KernelScalar>(
    cache: ArdSqDiff<'_, T>,
    n: usize,
    d: usize,
) -> Result<(), GprError> {
    if cache.n() == n && cache.d() == d {
        Ok(())
    } else {
        Err(GprError::ShapeMismatch {
            reason: format!(
                "ARD cache holds {} points in {} dimensions, expected {n} in {d}",
                cache.n(),
                cache.d()
            ),
        })
    }
}

/// Writes rectangular squared distances `k(x_train, x_test)`.
pub(crate) fn fill_squared_euclidean_cross(
    x_train: MatRef<'_, f64>,
    x_test: MatRef<'_, f64>,
    mut dist: MatMut<'_, f64>,
    thread_scratch: &mut [Mat<f64>],
) {
    let m = x_test.nrows();
    if x_train.nrows() == 0 || m == 0 {
        return;
    }
    if m == 1 {
        fill_cross_chunk(x_train, x_test, dist.rb_mut(), 0);
        return;
    }
    let n_parts = partition_count(thread_scratch);
    dist.rb_mut()
        .par_col_partition_mut(n_parts)
        .enumerate()
        .for_each(|(chunk_idx, part)| {
            let (start, _) = col_chunk(m, chunk_idx, n_parts);
            fill_cross_chunk(x_train, x_test, part, start);
        });
}

/// Rows `col..n` of column `col` of the squared distances of `x`.
fn fill_lower_col(x: MatRef<'_, f64>, col: usize, mut rows: ColMut<'_, f64>) {
    if let Some(dest) = rows.rb_mut().try_as_col_major_mut()
        && try_fill_lower_col(x, col, dest.as_slice_mut())
    {
        return;
    }
    for (i, row) in (col..x.nrows()).enumerate() {
        let mut sum = 0.0;
        for dim in 0..x.ncols() {
            let diff = x[(row, dim)] - x[(col, dim)];
            sum += diff * diff;
        }
        rows[i] = sum;
    }
}

/// The train–test squared distances of the test points
/// `start..start + dist_chunk.ncols()`.
fn fill_cross_chunk(
    x_train: MatRef<'_, f64>,
    x_test: MatRef<'_, f64>,
    mut dist_chunk: MatMut<'_, f64>,
    start: usize,
) {
    if try_fill_cross_chunk(x_train, x_test, dist_chunk.rb_mut(), start) {
        return;
    }
    for local in 0..dist_chunk.ncols() {
        let col = start + local;
        for row in 0..x_train.nrows() {
            let mut sum = 0.0;
            for dim in 0..x_train.ncols() {
                let diff = x_train[(row, dim)] - x_test[(col, dim)];
                sum += diff * diff;
            }
            dist_chunk[(row, local)] = sum;
        }
    }
}

fn copy_lower_to_upper(mut dist: MatMut<'_, f64>) {
    let n = dist.nrows();
    for col in 1..n {
        for row in 0..col {
            dist[(row, col)] = dist[(col, row)];
        }
    }
}

/// `f32` squared distances: the same formula as the `f64` fill, in scalar.
pub(crate) fn fill_squared_scalar(x: MatRef<'_, f32>, mut dist: MatMut<'_, f32>) {
    let n = x.nrows();
    let d = x.ncols();
    for col in 0..n {
        for row in 0..n {
            let mut sum = 0.0f32;
            for dim in 0..d {
                let diff = x[(row, dim)] - x[(col, dim)];
                sum += diff * diff;
            }
            dist[(row, col)] = sum;
        }
    }
}

pub(crate) fn fill_cross_scalar(
    x_train: MatRef<'_, f32>,
    x_test: MatRef<'_, f32>,
    mut dist: MatMut<'_, f32>,
) {
    let n = x_train.nrows();
    let m = x_test.nrows();
    let d = x_train.ncols();
    for col in 0..m {
        for row in 0..n {
            let mut sum = 0.0f32;
            for dim in 0..d {
                let diff = x_train[(row, dim)] - x_test[(col, dim)];
                sum += diff * diff;
            }
            dist[(row, col)] = sum;
        }
    }
}

pub(crate) fn fill_ard_scalar(x: MatRef<'_, f32>, cache: &mut [f32]) {
    let n = x.nrows();
    let mut slots = cache.iter_mut();
    for dim in 0..x.ncols() {
        for col in 0..n {
            for row in col..n {
                let diff = x[(row, dim)] - x[(col, dim)];
                if let Some(slot) = slots.next() {
                    *slot = diff * diff;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        ArdSqDiffBuf, col_chunk, fill_squared_euclidean, fill_squared_euclidean_cross,
        par_lower_fold, worker_count,
    };
    use faer::Mat;

    fn sequential_sq(x: faer::MatRef<'_, f64>) -> Mat<f64> {
        let n = x.nrows();
        let d = x.ncols();
        let mut dist = Mat::zeros(n, n);
        for col in 0..n {
            for row in col..n {
                let mut sum = 0.0;
                for dim in 0..d {
                    let diff = x[(row, dim)] - x[(col, dim)];
                    sum += diff * diff;
                }
                dist[(row, col)] = sum;
                dist[(col, row)] = sum;
            }
        }
        dist
    }

    #[test]
    fn col_chunks_cover_n() {
        for n in [1, 2, 5, 7, 256] {
            for chunks in [1, 2, 3, 4, 8, worker_count()] {
                let chunks = chunks.max(1);
                let mut covered = 0;
                for idx in 0..chunks {
                    let (_start, len) = col_chunk(n, idx, chunks);
                    covered += len;
                }
                assert_eq!(covered, n, "n={n} chunks={chunks}");
            }
        }
    }

    #[test]
    fn lower_blocks_cover_every_column_once_with_balanced_area() {
        for n in [0, 1, 2, 3, 7, 64, 550, 1001] {
            for blocks in [1, 2, 3, 4, 8, 16, 37] {
                let starts: Vec<usize> = (0..=blocks)
                    .map(|i| super::lower_block_start(n, i, blocks))
                    .collect();
                assert_eq!(starts[0], 0);
                assert_eq!(starts[blocks], n);
                assert!(
                    starts.windows(2).all(|w| w[0] <= w[1]),
                    "n={n} blocks={blocks}"
                );
                let areas: Vec<u128> = starts
                    .windows(2)
                    .map(|w| super::lower_area(n, w[1]) - super::lower_area(n, w[0]))
                    .collect();
                // Each boundary is the column nearest its share, so every
                // block is within one column (≤ n entries) of the even share.
                let (k, total) = (blocks as u128, super::lower_area(n, n));
                for area in &areas {
                    assert!(
                        (area * k).abs_diff(total) <= n as u128 * k,
                        "n={n} blocks={blocks} areas={areas:?}"
                    );
                }
            }
        }
    }

    #[test]
    fn par_lower_blocks_hands_every_column_to_one_block() {
        for n in [1, 5, 9, 130] {
            for blocks in [1, 3, 4, 16] {
                let mut out = Mat::<f64>::zeros(n, n);
                super::par_lower_blocks(out.as_mut(), blocks, &|start,
                                                                mut part: faer::MatMut<
                    '_,
                    f64,
                >| {
                    for local in 0..part.ncols() {
                        for row in start + local..n {
                            part[(row, local)] += (start + local) as f64 + 1.0;
                        }
                    }
                    Ok::<(), std::convert::Infallible>(())
                })
                .expect("infallible");
                for col in 0..n {
                    for row in 0..n {
                        let want = if row >= col { col as f64 + 1.0 } else { 0.0 };
                        assert_eq!(out[(row, col)].to_bits(), want.to_bits());
                    }
                }
            }
        }
    }

    /// Column sums added left to right, including pairs that do not commute
    /// under rounding. A balanced join of area blocks fails this.
    #[test]
    fn par_lower_fold_matches_a_serial_column_sum() {
        fn term(col: usize) -> f64 {
            if col.is_multiple_of(2) {
                1.0e16
            } else {
                -1.0e16 + col as f64
            }
        }
        for n in [0, 1, 2, 15, 16, 17, 64, 256] {
            let got = par_lower_fold(
                n,
                &|start, end| {
                    let mut sum = 0.0;
                    for col in start..end {
                        sum += term(col);
                    }
                    Ok::<f64, std::convert::Infallible>(sum)
                },
                &|a, b| a + b,
            )
            .expect("infallible");
            let mut expect = 0.0;
            for col in 0..n {
                expect += term(col);
            }
            assert_eq!(got.to_bits(), expect.to_bits(), "n={n}");
        }
    }

    #[test]
    fn col_chunk_five_by_four_covers_remainder() {
        // Remainder split: first `rem` chunks get `chunk_size + 1` columns.
        // n=5, n_chunks=4 → sizes 2, 1, 1, 1 (not four length-1 chunks).
        assert_eq!(col_chunk(5, 0, 4), (0, 2));
        assert_eq!(col_chunk(5, 1, 4), (2, 1));
        assert_eq!(col_chunk(5, 2, 4), (3, 1));
        assert_eq!(col_chunk(5, 3, 4), (4, 1));
        let covered: usize = (0..4).map(|idx| col_chunk(5, idx, 4).1).sum();
        assert_eq!(covered, 5);
    }

    #[test]
    fn fill_when_more_chunks_than_columns_matches_sequential() {
        let x = Mat::from_fn(5, 3, |r, c| (r as f64) * 0.1 + (c as f64) * 0.3);
        let expected = sequential_sq(x.as_ref());
        let mut dist = Mat::zeros(5, 5);
        let mut scratches = vec![Mat::<f64>::zeros(0, 0); 8];
        fill_squared_euclidean(x.as_ref(), dist.as_mut(), &mut scratches);
        for col in 0..5 {
            for row in 0..5 {
                assert!((dist[(row, col)] - expected[(row, col)]).abs() <= 1e-15);
            }
        }
    }

    #[test]
    fn parallel_fill_matches_sequential() {
        let x = Mat::from_fn(5, 3, |r, c| (r as f64) * 0.1 + (c as f64) * 0.3);
        let expected = sequential_sq(x.as_ref());
        let mut dist = Mat::zeros(5, 5);
        let mut scratches = vec![Mat::<f64>::zeros(0, 0); worker_count()];
        fill_squared_euclidean(x.as_ref(), dist.as_mut(), &mut scratches);
        for col in 0..5 {
            for row in 0..5 {
                assert!((dist[(row, col)] - expected[(row, col)]).abs() <= 1e-15);
            }
        }
    }

    #[test]
    fn parallel_cross_matches_sequential() {
        let x = Mat::from_fn(4, 2, |r, c| r as f64 + c as f64);
        let xs = Mat::from_fn(3, 2, |r, c| (r as f64) * 0.5 + c as f64);
        let mut expected = Mat::zeros(4, 3);
        for col in 0..3 {
            for row in 0..4 {
                let mut sum = 0.0;
                for dim in 0..2 {
                    let diff = x[(row, dim)] - xs[(col, dim)];
                    sum += diff * diff;
                }
                expected[(row, col)] = sum;
            }
        }
        let mut dist = Mat::zeros(4, 3);
        fill_squared_euclidean_cross(x.as_ref(), xs.as_ref(), dist.as_mut(), &mut []);
        for col in 0..3 {
            for row in 0..4 {
                assert!((dist[(row, col)] - expected[(row, col)]).abs() <= 1e-15);
            }
        }
    }

    #[test]
    fn ard_fill_matches_per_dim_squared_diff() {
        let x = Mat::from_fn(5, 3, |r, c| {
            (r as f64) * 0.1 + (c as f64) * 0.3 + (r * c) as f64
        });
        let n = 5;
        let d = 3;
        let cache = ArdSqDiffBuf::new(x.as_ref()).expect("size");
        let view = cache.view();
        assert_eq!((view.n(), view.d()), (n, d));
        // The lower triangle of each dimension only: d · n(n+1)/2, not d · n².
        assert_eq!(cache.stored_len(), d * n * (n + 1) / 2);
        for dim in 0..d {
            for col in 0..n {
                assert_eq!(view.lower().expect("lower").column(dim, col).len(), n - col);
                for row in 0..n {
                    let diff = x[(row, dim)] - x[(col, dim)];
                    assert!((view.get(dim, row, col) - diff * diff).abs() <= 1e-15);
                }
            }
        }
        let x32 = Mat::from_fn(5, 3, |r, c| x[(r, c)] as f32);
        let cache32 = ArdSqDiffBuf::new(x32.as_ref()).expect("size");
        for dim in 0..d {
            for col in 0..n {
                for row in 0..n {
                    let diff = x32[(row, dim)] - x32[(col, dim)];
                    assert!((cache32.view().get(dim, row, col) - diff * diff).abs() <= 1e-6);
                }
            }
        }
    }

    /// Coordinate `k` of pool point `p`, and the pair of two pool points.
    fn pool_pair(k: usize, a: usize, b: usize) -> f64 {
        let at = |p: usize| (p as f64 * (0.41 + 0.17 * k as f64)).sin() * (1.0 + k as f64);
        let diff = at(a) - at(b);
        diff * diff
    }

    /// Every pair of `cache` is the pair of the pool points `live` names.
    fn assert_holds(cache: &ArdSqDiffBuf<f64>, live: &[usize], d: usize) {
        let view = cache.view();
        assert_eq!((view.n(), view.d()), (live.len(), d));
        for k in 0..d {
            for (i, &a) in live.iter().enumerate() {
                for (j, &b) in live.iter().enumerate() {
                    assert_eq!(
                        view.get(k, i, j).to_bits(),
                        pool_pair(k, a, b).to_bits(),
                        "{k} ({i}, {j})"
                    );
                }
            }
        }
    }

    /// Inserts grow the row runs in their reserved room, and deletes at
    /// the head, the middle, and the tail compact them in place, from a
    /// cache of column runs and from dense tables.
    #[test]
    fn row_runs_grow_and_shrink_in_place() {
        let d = 3;
        let start: Vec<usize> = (0..5).collect();
        let packed = ArdSqDiffBuf::<f64>::from_pairs(5, d, pool_pair).expect("cache");
        let tables: Vec<Vec<f64>> = (0..d)
            .map(|k| (0..25).map(|at| pool_pair(k, at % 5, at / 5)).collect())
            .collect();
        let dense = ArdSqDiffBuf::<f64>::from_tables(tables, 5);
        for mut cache in [packed, dense] {
            let mut live = start.clone();
            assert_holds(&cache, &live, d);
            let mut next = 5;
            for step in 0..12 {
                if step % 3 == 2 {
                    let index = [0, live.len() / 2, live.len() - 1][step % 9 / 3];
                    cache.ready_to_change().expect("rows");
                    assert!(cache.can_remove());
                    cache.remove_point(index);
                    live.remove(index);
                } else {
                    cache.reserve_point().expect("room");
                    let columns: Vec<Vec<f64>> = (0..d)
                        .map(|k| live.iter().map(|&a| pool_pair(k, a, next)).collect())
                        .collect();
                    assert!(cache.can_push());
                    cache.push_point(|k| &columns[k]);
                    live.push(next);
                    next += 1;
                }
                assert!(cache.view().rows().is_some());
                assert_holds(&cache, &live, d);
            }
        }
    }

    /// A clone keeps the room its original reserved; a cache not yet laid
    /// out as row runs with room says so before anything is written.
    #[test]
    fn row_runs_report_their_room_and_clone_it() {
        let mut cache = ArdSqDiffBuf::<f64>::from_pairs(4, 2, pool_pair).expect("cache");
        assert!(!cache.can_push());
        assert!(!cache.can_remove());
        cache.reserve_point().expect("room");
        assert!(cache.can_push() && cache.can_remove());
        let mut copy = cache.clone();
        let columns: Vec<Vec<f64>> = (0..2)
            .map(|k| (0..4).map(|a| pool_pair(k, a, 4)).collect())
            .collect();
        assert!(copy.can_push(), "the clone has room");
        copy.push_point(|k| &columns[k]);
        assert_holds(&copy, &[0, 1, 2, 3, 4], 2);
        assert_holds(&cache, &[0, 1, 2, 3], 2);
        // A cast keeps the row runs.
        let cast = copy.map(|v| v as f32);
        assert!(cast.view().rows().is_some());
        assert_eq!(
            cast.view().get(1, 4, 2).to_bits(),
            (pool_pair(1, 4, 2) as f32).to_bits()
        );
        assert_eq!(
            copy.view()
                .position(1, |v| v.to_bits() == pool_pair(1, 4, 2).to_bits()),
            Some((4, 2))
        );
    }
}