diskann 0.51.0

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
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
/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT license.
 */

//! The default implementation of a [Working Set](super).
//!
//! This is meant to work out of the box with enough knobs to cover common cases.
//! Supported workflows include:
//!
//! * Controlling reuse of the working set across multiple prunes.
//! * Zero-copy seeding from batches passed to
//!   [`multi_insert`](crate::graph::DiskANNIndex::multi_insert).
//!
//! # Guidance
//!
//! The data structure here has a lot of knobs. This section contains guidance for what
//! defaults to use for developers depending on needs.
//!
//! ### Single insert, no need to tweak working set fills
//!
//! Use [`Map`] with the default projection. For example, if your internal ID type is `u32`
//! and your [`Accessor`] element type is convertible to a `Box<[T]>`, then use
//! `Map<u32, Box<[T]>>`, constructed as follows:
//!
//! ```
//! use diskann::graph::workingset::{Map, map::{Builder, Capacity}};
//!
//! let capacity = 10;
//! let mut map: Map<u32, Box<[f32]>> = Builder::new(Capacity::Default).build(capacity);
//! map.insert(1, Box::new([1.0]));
//! assert_eq!(&**map.get(&1).unwrap(), &[1.0]);
//! ```
//!
//! ### Multi insert, element types are slice-like
//!
//! If using [`multi_insert`](crate::graph::DiskANNIndex::multi_insert) and your elements
//! look like slices, then the [`Ref`] [projection](Projection) can be used. See the section
//! on element projection down below.
//!
//! # Concepts
//!
//! ## Working Set Reuse
//!
//! The core indexing algorithm will reuse the same working-set object (e.g. [`Map`]) across
//! multiple rounds of prune. Users can take advantage of this reuse to use the working set
//! as a cache, reusing vector retrieved from a previous round to reduce the total number of
//! vectors retrieved. This needs to be balanced with the potential extra memory overhead of
//! the cache.
//!
//! To that end, [`Map`] provides four different operating modes, controlled by the
//! [`Capacity`] enum:
//!
//! * [`Capacity::None`]: Do not reuse any vectors. In this case, calls to [`Map::fill`] will
//!   completely clear the set before any vectors are added.
//!
//! * [`Capacity::Default`]: Set the capacity of the [`Map`] to
//!   [`max_occlusion_size`](crate::graph::Config::max_occlusion_size). This allows
//!   some amount of reuse while keeping an upper bound on the amount of memory occupied by
//!   the map to that which prune can use anyways.
//!
//! * [`Capacity::Some`]: Override the allowed capacity of the [`Map`]. Enabling more
//!   aggressive caching than [`Capacity::Default`] while still limiting memory growth.
//!
//! * [`Capacity::Unbounded`]: Never clear entries from the cache. This can cause unbounded
//!   growth, so use with care.
//!
//! In any case where the working set is used as a cache - there's the possibility of stale
//! entries being resident in the working set. The core algorithm will only retain working
//! sets for the duration of a logical operation (adding of back-edges, for example) and will
//! not persist these sets across operations. This minimizes the risk of stale entries
//! negatively impacting graph quality.
//!
//! ### Eviction Policy
//!
//! [`Map`] uses a scheme to evict older entries during [`fill`](Map::fill)
//! (see also [`Map::prepare`]). First, the elements in the provided
//! iterator that already exist in the map are pinned. Unpinned entries will be evicted
//! until there is enough room to store all entries in the iterator. Finally, all missing
//! entries will be fetched.
//!
//! ## Zero Copy Batch Overlays
//!
//! [`glue::MultiInsertStrategy::finish`] provides the batch of inserted vectors for use as a
//! "seed" for the final working set. In situations where
//! [intra-batch candidates](`crate::graph::config::IntraBatchCandidates`) are used, this
//! provides an opportunity to use the existing data provided to multi-insert directly, rather
//! than fetching vectors from the underlying provider every time.
//!
//! This module provides functionality in the form of [`Overlay`]. The simplest constructor
//! to use is [`Overlay::from_batch`] which is designed to work natively with the
//! [`glue::Batch`] trait required by multi-insert. This can be fed to a [`Builder`] which
//! ultimately creates a [`Map`].
//!
//! ### Element Projection
//!
//! One fundamental problem of the overlay is that the way data is stored in the overlay and
//! the way it is stored natively in [`Map`] might be different. To accommodate this, the
//! [`Projection`] trait is introduced. Briefly, this allows decoupling of the storage in
//! [`Map`] and the overlay:
//!
//! ```
//! use std::sync::Arc;
//! use diskann_utils::views::Matrix;
//!
//! use diskann::graph::workingset::{Map, View, map::{Overlay, Ref, Builder, Capacity}};
//!
//! let batch = Matrix::<u32>::row_vector(Box::new([1, 2, 3]));
//!
//! // Construct an "overlay" with just one id.
//! //
//! // The overlay uses the `Ref` projection, yielding slices.
//! let overlay = Overlay::<u32, Ref<[u32]>>::from_batch(Arc::new(batch), [5]);
//! assert_eq!(overlay.get(&5).unwrap(), &[1, 2, 3]);
//!
//! // Next - we build a `Map` that stores its entries as `Box<[u32]>`.
//! let mut map: Map<_, Box<[u32]>, _> = Builder::new(Capacity::Default)
//!     .with_overlay(overlay)
//!     .build(5);
//!
//! map.insert(10, Box::new([10, 20, 30]));
//!
//! // Since we have an overlay, the map has an entry for id "5" as well as the "10" we
//! // just inserted.
//! let v = map.view();
//! assert_eq!(v.get(5).unwrap(), &[1, 2, 3]);
//! assert_eq!(v.get(10).unwrap(), &[10, 20, 30]);
//! ```

use std::{fmt::Debug, hash::Hash, sync::Arc};

use diskann_utils::{Reborrow, future::SendFuture};
use hashbrown::hash_map;

use crate::{
    error::{RankedError, ToRanked, TransientError},
    graph::glue,
    provider::Accessor,
};

use super::{AsWorkingSet, Fill};

/////////
// Map //
/////////

/// Default [working set](super) state backed by a `HashMap` with an optional overlay for
/// [`multi_insert`](crate::graph::glue::MultiInsertStrategy).
///
/// This struct uses [`Builder`] as its constructor, which implements [`AsWorkingSet`] for
/// multi-insert compatibility.
///
/// Additionally, [working set reuse](super#reuse-in-a-workingset) is optionally supported and
/// is controlled by the [`Capacity`] enum. See the [module level docs](self#working-set-reuse)
/// for more details.
///
/// When working set reuse is enabled, older entries are evicted using an unspecified
/// algorithm with the guarantee that items of the [`Fill`] iterator that are already present
/// will not be evicted.
///
/// For zero-copy multi-insert compatibility, an [`Overlay`] should be provided to the [`Builder`].
#[derive(Debug)]
pub struct Map<K, V, P: Projection = Reborrowed<V>> {
    /// Entries local to this `Map`.
    map: hashbrown::HashMap<K, Generation<V>>,

    /// Potential seed from an input batch.
    overlay: Option<Overlay<K, P>>,

    /// The target capacity to trim down each generation. The `map` may exceed this
    /// capacity if a call to `fill` exceeds the capacity on its own.
    ///
    /// Note the following:
    ///
    /// * `capacity` of zero triggers a clear on every fill operation.
    /// * `capacity` of `None` results in an unbounded map.
    capacity: Option<usize>,

    /// The current generation. We use this to control eviction of older entries.
    generation: u32,
}

impl<K, V, P: Projection> Map<K, V, P> {
    fn new_generation(&mut self) {
        self.generation = self.generation.wrapping_add(1);
    }
}

impl<K, V, P> Map<K, V, P>
where
    K: Hash + Eq,
    P: Projection,
{
    /// Promote any items in `itr` to the current generation - returning the number of
    /// items that were promoted.
    fn promote<Itr>(&mut self, itr: Itr) -> usize
    where
        Itr: ExactSizeIterator<Item = K>,
    {
        itr.filter(|k| {
            if let Some(tagged) = self.map.get_mut(k) {
                tagged.generation = self.generation;
                true
            } else {
                false
            }
        })
        .count()
    }

    /// Evict up to `count` items from the map that do not belong to the current generation.
    ///
    /// Returns the number of items actually evicted.
    fn evict(&mut self, count: usize) -> usize {
        if count == 0 {
            return 0;
        }

        let mut remaining = count;
        self.map.retain(|_, v| {
            if remaining == 0 {
                // Done with eviction - now we just need to ride out the rest of the `retain`
                // process.
                true
            } else if v.generation != self.generation {
                // Cannot underflow.
                remaining -= 1;
                false
            } else {
                // This item belong to the current generation - keep it.
                true
            }
        });

        count - remaining
    }

    /// Prepare the map for the next [`fill`](Self::fill) cycle.
    ///
    /// Pins entries whose keys appear in `itr` and evicts unpinned entries as needed
    /// to stay within the configured [`Capacity`]. Called automatically by [`fill`](Self::fill);
    /// only call directly if using [`fill_with`](Self::fill_with).
    pub fn prepare<Itr>(&mut self, itr: Itr)
    where
        Itr: ExactSizeIterator<Item = K>,
    {
        self.new_generation();

        // Only bother if a capacity was specified. Otherwise, we don't need to bother at
        // all with generations.
        if let Some(capacity) = self.capacity {
            if capacity == 0 {
                // Do not retain **any** items from previous iterations.
                self.map.clear();
            } else {
                let len = itr.len();
                let promoted = self.promote(itr);

                // This operation cannot underflow since `promoted` should be at most `len`.
                //
                // However, incorrect implementations of `ExactSizeIterator` could lie about
                // the length. This will catch such degenerate implementations in debug builds.
                //
                // In release builds - this is still fine because the wrapped result is used
                // as an upper bound, limited by the current size of `map` which is **not**
                // degenerate.
                let extra_needed = len - promoted;
                let final_size = self.map.len().saturating_add(extra_needed);

                if let Some(to_evict) = final_size.checked_sub(capacity) {
                    self.evict(to_evict);
                }
            }
        }
    }
}

impl<K, V, P> Map<K, V, P>
where
    K: Copy + Hash + Eq + Send + Sync,
    V: Send + Sync,
    P: Projection,
{
    /// Fill using `accessor.get_element` for each missing key.
    ///
    /// Calls [`prepare`](Self::prepare) to pin and evict entries, then fetches any
    /// keys still missing. For incremental fills that skip preparation, use
    /// [`fill_with`](Self::fill_with) directly.
    ///
    /// Transient errors are acknowledged and skipped. Only critical errors are propagated.
    pub fn fill<'a, A, Itr>(
        &'a mut self,
        accessor: &'a mut A,
        itr: Itr,
    ) -> impl SendFuture<Result<View<'a, K, V, P>, <A::GetError as ToRanked>::Error>>
    where
        A: for<'b> Accessor<Id = K, ElementRef<'b>: Into<V>>,
        Itr: ExactSizeIterator<Item = K> + Clone + Send + Sync,
    {
        self.prepare(itr.clone());
        self.fill_with(accessor, itr, |element| element.into())
    }

    /// Fill using a projection from `Accessor::Element` to `V`.
    ///
    /// Transient errors are acknowledged and skipped. Only critical errors are propagated.
    pub fn fill_with<'a, A, Itr, F>(
        &'a mut self,
        accessor: &'a mut A,
        itr: Itr,
        f: F,
    ) -> impl SendFuture<Result<View<'a, K, V, P>, <A::GetError as ToRanked>::Error>>
    where
        A: Accessor<Id = K>,
        Itr: ExactSizeIterator<Item = K> + Send + Sync,
        F: Fn(A::ElementRef<'_>) -> V + Send,
    {
        async move {
            for i in itr {
                match self.entry(i) {
                    Entry::Seeded(_) | Entry::Occupied(_) => { /* nothing to do */ }
                    Entry::Vacant(vacant) => match accessor.get_element(i).await {
                        Ok(element) => {
                            vacant.insert(f(element.reborrow()));
                        }
                        Err(local_error) => match local_error.to_ranked() {
                            RankedError::Transient(transient) => {
                                transient.acknowledge(
                                    "transient error during fill; element will be absent from working set",
                                );
                            }
                            RankedError::Error(critical) => {
                                return Err(critical);
                            }
                        },
                    },
                }
            }
            Ok(self.view())
        }
    }
}

impl<K, V, P> Map<K, V, P>
where
    K: Hash + Eq,
    P: Projection,
{
    /// Look up an element in the fill (mutable) layer only.
    ///
    /// Does NOT check the seed overlay. For unified lookups, use
    /// [`view()`](Self::view) which returns a [`View`].
    pub fn get(&self, key: &K) -> Option<&V> {
        self.map.get(key).map(|v| v.value())
    }

    /// Mutable access to the map's own entries. Returns `None` for seed-only keys.
    ///
    /// NOTE: Using this interface sets the generation of the associated value (if any)
    /// to the current generation.
    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
        self.map.get_mut(key).map(|v| {
            v.generation = self.generation;
            v.value_mut()
        })
    }

    /// Remove all entries from the mutable layer, preserving capacity configuration.
    ///
    /// Callers should prefer [`prepare`](Self::prepare) instead if cache-like behavior
    /// is needed.
    pub fn clear(&mut self) {
        self.map.clear()
    }

    /// Insert into the mutable map layer. If the key exists in the seed, this is
    /// a no-op (seed keys and map keys are disjoint).
    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        if self.overlay.as_ref().is_some_and(|s| s.contains_key(&key)) {
            return None;
        }
        self.map
            .insert(key, Generation::new(value, self.generation))
            .map(|v| v.into_inner())
    }

    /// Returns `true` if the key is in the seed overlay or the map.
    pub fn contains_key(&self, key: &K) -> bool {
        self.overlay.as_ref().is_some_and(|s| s.contains_key(key)) || self.map.contains_key(key)
    }

    /// Seed-aware entry API.
    ///
    /// Returns [`Entry::Seeded`] if the key is in the seed overlay (carrying the
    /// projected element), [`Entry::Occupied`] if it is in the mutable map layer, or
    /// [`Entry::Vacant`] if absent from both.
    pub fn entry(&mut self, key: K) -> Entry<'_, K, V, P> {
        if let Some(element) = self.overlay.as_ref().and_then(|s| s.get(&key)) {
            return Entry::Seeded(element);
        }
        let generation = self.generation;
        match self.map.entry(key) {
            hash_map::Entry::Occupied(mut o) => {
                // Side effect: Set the entry's generation to the current generation.
                //
                // Most of the time, this will happen after a call to `prepare()` and so
                // the generation should already match.
                //
                // Doing it this way decreases the workload on the caller needing to manually
                // bump the generation.
                o.get_mut().generation = self.generation;
                Entry::Occupied(OccupiedEntry { entry: o })
            }
            hash_map::Entry::Vacant(v) => Entry::Vacant(VacantEntry {
                entry: v,
                generation,
            }),
        }
    }

    /// Borrow as a [`View`].
    pub fn view(&self) -> View<'_, K, V, P> {
        View { map: self }
    }

    //-----------------------//
    // Internal Test Helpers //
    //-----------------------//

    #[cfg(test)]
    fn generation_of(&self, k: K) -> u32 {
        self.map.get(&k).unwrap().generation
    }
}

#[derive(Debug)]
struct Generation<T> {
    generation: u32,
    value: T,
}

impl<T> Generation<T> {
    fn new(value: T, generation: u32) -> Self {
        Self { generation, value }
    }

    fn value(&self) -> &T {
        &self.value
    }

    fn value_mut(&mut self) -> &mut T {
        &mut self.value
    }

    fn into_inner(self) -> T {
        self.value
    }
}

/// Result of [`Map::entry`].
///
/// This is a three-state entry:
/// - [`Seeded`](Entry::Seeded): present in the immutable seed overlay. Carries the
///   projected element (`P::Element`).
/// - [`Occupied`](Entry::Occupied): present in the mutable map layer. The value can be
///   read and written.
/// - [`Vacant`](Entry::Vacant): absent from both layers.
pub enum Entry<'a, K, V, P: Projection> {
    /// The key exists in the immutable seed overlay.
    Seeded(P::Element<'a>),
    /// The key exists in the mutable map layer.
    Occupied(OccupiedEntry<'a, K, V>),
    /// The key is absent from both layers.
    Vacant(VacantEntry<'a, K, V>),
}

/// An entry in the mutable map layer that already has a value.
pub struct OccupiedEntry<'a, K, V> {
    entry: hash_map::OccupiedEntry<'a, K, Generation<V>>,
}

impl<'a, K, V> OccupiedEntry<'a, K, V> {
    /// Returns a reference to the value.
    pub fn get(&self) -> &V {
        self.entry.get().value()
    }

    /// Returns a mutable reference to the value.
    pub fn get_mut(&mut self) -> &mut V {
        self.entry.get_mut().value_mut()
    }

    /// Converts into a mutable reference to the value with the entry's lifetime.
    pub fn into_mut(self) -> &'a mut V {
        self.entry.into_mut().value_mut()
    }
}

/// An entry in the mutable map layer with no existing value.
pub struct VacantEntry<'a, K, V> {
    entry: hash_map::VacantEntry<'a, K, Generation<V>>,
    generation: u32,
}

impl<K, V> VacantEntry<'_, K, V> {
    /// Insert a value, stamped with the current generation.
    pub fn insert(self, value: V)
    where
        K: Hash + Eq,
    {
        self.entry.insert(Generation::new(value, self.generation));
    }

    /// Returns a reference to this entry's key.
    pub fn key(&self) -> &K {
        self.entry.key()
    }
}

/// Blanket implementation of [`Fill`] for [`Map`]-backed working sets.
///
/// This covers the common case where the accessor's `Extended` type is stored directly
/// in the map. Accessors that need custom fill logic (e.g. hybrid full-precision/quantized)
/// should use a different `State` type and provide their own `Fill` impl.
impl<A, V, P> Fill<Map<A::Id, V, P>> for A
where
    P: Projection,
    A: for<'a> Accessor<Id: Hash + Eq, ElementRef<'a> = P::ElementRef<'a>>,
    V: Project<P> + Send + Sync + 'static,
    for<'a> A::ElementRef<'a>: Into<V>,
{
    type Error = <A::GetError as ToRanked>::Error;
    type View<'a>
        = View<'a, A::Id, V, P>
    where
        Self: 'a;

    fn fill<'a, Itr>(
        &'a mut self,
        map: &'a mut Map<A::Id, V, P>,
        itr: Itr,
    ) -> impl SendFuture<Result<Self::View<'a>, Self::Error>>
    where
        Itr: ExactSizeIterator<Item = A::Id> + Clone + Send + Sync,
        Self: 'a,
    {
        map.fill(self, itr)
    }
}

/////////////////
// Projections //
/////////////////

/// Defines how stored values `V` are projected to element types for lookups.
///
/// A projection decouples the owned storage type (`V`) from the borrowed view type
/// returned by [`View::get`](super::View::get). For example, `Ref<[T]>` projects
/// `Box<[T]>` to `&[T]`, while `Reborrowed<V>` uses `V`'s own [`Reborrow`] impl.
pub trait Projection: Send + Sync + 'static {
    type Element<'a>: for<'b> Reborrow<'b, Target = Self::ElementRef<'b>> + Send;
    type ElementRef<'a>;
}

/// Projection that borrows stored values as `&T` slices.
///
/// Use this when `V = Box<[T]>` and the view should yield `&[T]`.
#[derive(Debug)]
pub struct Ref<T: ?Sized>(std::marker::PhantomData<T>);

impl<T> Clone for Ref<T>
where
    T: ?Sized,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Ref<T> where T: ?Sized {}

impl<T> Projection for Ref<T>
where
    T: ?Sized + Send + Sync + 'static,
{
    type Element<'a> = &'a T;
    type ElementRef<'a> = &'a T;
}

/// Default projection that uses the value's [`Reborrow`] impl.
///
/// This is the default `P` parameter for [`Map`]. It delegates to `V::reborrow()`,
/// wrapping the value in [`AsReborrowed`] so the view's element type matches
/// the accessor's `ElementRef`.
#[derive(Debug)]
pub struct Reborrowed<T>(std::marker::PhantomData<T>)
where
    T: ?Sized;

impl<T> Clone for Reborrowed<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Reborrowed<T> {}

impl<T> Projection for Reborrowed<T>
where
    T: for<'a> Reborrow<'a> + ?Sized + Send + Sync + 'static,
{
    type Element<'a> = AsReborrowed<'a, T>;
    type ElementRef<'a> = <T as Reborrow<'a>>::Target;
}

/// Wrapper that implements [`Reborrow`] by delegating to the inner value.
///
/// Used by the [`Reborrowed`] projection to provide a [`Reborrow`]-compatible element
/// type from a `&V` reference.
#[derive(Debug)]
pub struct AsReborrowed<'a, T>(&'a T)
where
    T: ?Sized;

impl<T> Clone for AsReborrowed<'_, T>
where
    T: ?Sized,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for AsReborrowed<'_, T> where T: ?Sized {}

impl<'a, T> Reborrow<'a> for AsReborrowed<'_, T>
where
    T: Reborrow<'a> + ?Sized,
{
    type Target = T::Target;
    fn reborrow(&'a self) -> Self::Target {
        self.0.reborrow()
    }
}

impl<T> Project<Reborrowed<T>> for T
where
    T: for<'a> Reborrow<'a> + ?Sized + Send + Sync + 'static,
{
    fn project(&self) -> AsReborrowed<'_, T> {
        AsReborrowed(self)
    }
}

/// Convert a stored value `V` to the projection's element type for read access.
pub trait Project<P>
where
    P: Projection,
{
    fn project(&self) -> P::Element<'_>;
}

impl<T> Project<Ref<T>> for Box<T>
where
    T: Send + Sync + 'static + ?Sized,
{
    fn project(&self) -> &T {
        self
    }
}

impl<T> Project<Ref<[T]>> for Vec<T>
where
    T: Send + Sync + 'static,
{
    fn project(&self) -> &[T] {
        self
    }
}

/////////////
// Overlay //
/////////////

/// An immutable, zero-copy layer to provide to [`Builder`] and [`Map`].
///
/// Elements in an overlay take precedence when used in [`Map`].
///
/// This struct is cheap to clone.
///
/// ```
/// use std::sync::Arc;
/// use diskann_utils::views::Matrix;
///
/// use diskann::graph::workingset::map::{Overlay, Ref};
///
/// let data = Matrix::<u32>::column_vector(Box::new([10, 20, 30]));
/// let ids = [5, 3, 2];
///
/// // The `Ref<[u32]>` projection yields slices, which is compatible with a `Matrix`.
/// let overlay = Overlay::<u32, Ref<[u32]>>::from_batch(Arc::new(data), ids);
///
/// assert_eq!(overlay.get(&5).unwrap(), &[10]);
/// assert_eq!(overlay.get(&3).unwrap(), &[20]);
/// assert_eq!(overlay.get(&2).unwrap(), &[30]);
///
/// assert!(overlay.get(&1).is_none());
/// assert!(overlay.get(&4).is_none());
/// ```
///
/// See [`Builder`] for the [`Seed`](crate::graph::glue::MultiInsertStrategy::Seed) that
/// should be used for [`Map`].
///
/// # Construction
///
/// Users will typically use [`Self::from_batch`] for multi-insert scenarios. Additional
/// construction using the [`MapLike`] trait are provided for more fine-grained control.
#[derive(Debug)]
pub struct Overlay<K, P: Projection> {
    overlay: Arc<dyn MapLike<K, P>>,
}

impl<K, P: Projection> Clone for Overlay<K, P> {
    fn clone(&self) -> Self {
        Self {
            overlay: self.overlay.clone(),
        }
    }
}

impl<K, P> Overlay<K, P>
where
    P: Projection,
{
    /// Wrap an existing [`MapLike`] implementor as an overlay.
    pub fn new(overlay: Arc<dyn MapLike<K, P>>) -> Self {
        Self { overlay }
    }

    /// Look up a key in the overlay.
    pub fn get(&self, key: &K) -> Option<P::Element<'_>> {
        self.overlay.get(key)
    }

    /// Returns `true` if the overlay contains the key.
    pub fn contains_key(&self, key: &K) -> bool {
        self.overlay.contains_key(key)
    }
}

impl<K, P> Overlay<K, P>
where
    K: Hash + Eq + Send + Sync + 'static,
    P: Projection,
{
    /// Create a zero-copy seed backed directly by a [`glue::Batch`].
    ///
    /// Lookups go through the batch via an index map — no element data is copied.
    /// Requires that batch elements convert to `P::Element` via `Into`.
    pub fn from_batch<B, I>(batch: Arc<B>, ids: I) -> Self
    where
        I: IntoIterator<Item = K, IntoIter: ExactSizeIterator>,
        B: for<'a> glue::Batch<Element<'a>: Into<P::Element<'a>>> + Debug,
        K: Debug,
    {
        let indices: hashbrown::HashMap<K, usize> =
            ids.into_iter().enumerate().map(|(i, id)| (id, i)).collect();
        Self {
            overlay: Arc::new(BatchOverlay { batch, indices }),
        }
    }
}

/// Object-safe seed overlay trait, parameterized by [`Projection`].
///
/// The seed stores pre-populated elements (e.g. from a multi-insert batch) and
/// returns them as `P::Element<'_>` — the same type that [`View`] produces
/// from the fill layer via [`Project`]. This decouples the seed's storage type from
/// the fill layer's value type `V`.
pub trait MapLike<K, P: Projection>: Debug + Send + Sync {
    fn get(&self, key: &K) -> Option<P::Element<'_>>;
    fn contains_key(&self, key: &K) -> bool;
}

impl<K, V, P> MapLike<K, P> for hashbrown::HashMap<K, V>
where
    K: Hash + Eq + Debug + Send + Sync,
    V: Project<P> + Debug + Send + Sync,
    P: Projection,
{
    fn get(&self, key: &K) -> Option<P::Element<'_>> {
        hashbrown::HashMap::get(self, key).map(|v| v.project())
    }

    fn contains_key(&self, key: &K) -> bool {
        hashbrown::HashMap::contains_key(self, key)
    }
}

/// Zero-copy seed overlay backed by a [`glue::Batch`](glue::Batch).
///
/// Stores the batch behind an `Arc` and an index map from keys to batch positions.
/// Lookups convert batch elements to `P::Element` via `Into`, which is by-value and
/// avoids the double-reference problem that `Project::project(&self)` would have.
#[derive(Debug)]
struct BatchOverlay<K, B> {
    batch: Arc<B>,
    indices: hashbrown::HashMap<K, usize>,
}

impl<K, B, P> MapLike<K, P> for BatchOverlay<K, B>
where
    K: Hash + Eq + Debug + Send + Sync,
    B: for<'a> glue::Batch<Element<'a>: Into<P::Element<'a>>> + Debug,
    P: Projection,
{
    fn get(&self, key: &K) -> Option<P::Element<'_>> {
        self.indices.get(key).map(|&idx| self.batch.get(idx).into())
    }

    fn contains_key(&self, key: &K) -> bool {
        self.indices.contains_key(key)
    }
}

/////////////
// Builder //
/////////////

/// Builder for [`Map`]-backed working sets.
///
/// Doubles as the `Seed` type for [`AsWorkingSet<Map>`]: strategies return a `Builder`
/// from [`MultiInsertStrategy::finish`](crate::graph::glue::MultiInsertStrategy::finish),
/// and the index calls [`as_working_set`](AsWorkingSet::as_working_set) to materialize it.
///
/// # Examples
///
/// ```ignore
/// // Unseeded, clear every fill:
/// let map: Map<u32, Box<[f32]>> = Builder::new(Capacity::None).build(128);
///
/// // Seeded, use caller's default capacity:
/// let map: Map<u32, Box<[f32]>, Ref<[f32]>> = Builder::new(Capacity::Default)
///     .with_overlay(overlay)
///     .build(128);
/// ```
#[derive(Debug)]
pub struct Builder<K, P: Projection> {
    overlay: Option<Overlay<K, P>>,
    capacity: Capacity,
}

impl<K, P: Projection> Builder<K, P> {
    /// Create a builder with the given eviction policy.
    pub fn new(capacity: Capacity) -> Self {
        Self {
            overlay: None,
            capacity,
        }
    }

    /// Attach a zero-copy seed overlay to the resulting [`Map`].
    pub fn with_overlay(mut self, overlay: Overlay<K, P>) -> Self {
        self.overlay = Some(overlay);
        self
    }

    /// Materialize the [`Map`].
    ///
    /// `default_capacity` is used when `self.capacity` is [`Capacity::Default`].
    pub fn build<V>(self, default_capacity: usize) -> Map<K, V, P> {
        let capacity = self.capacity.resolve(default_capacity);
        Map {
            map: hashbrown::HashMap::with_capacity(capacity.unwrap_or(0)),
            overlay: self.overlay,
            capacity,
            generation: 0,
        }
    }
}

impl<K, P: Projection> Clone for Builder<K, P> {
    fn clone(&self) -> Self {
        Self {
            overlay: self.overlay.clone(),
            capacity: self.capacity,
        }
    }
}

impl<K, V, P> AsWorkingSet<Map<K, V, P>> for Builder<K, P>
where
    P: Projection,
{
    fn as_working_set(&self, capacity: usize) -> Map<K, V, P> {
        self.clone().build(capacity)
    }
}

/// Controls how a [`Builder`] resolves the capacity of the constructed [`Map`].
///
/// See [working set reuse](self#working-set-reuse) for a detailed description.
#[derive(Debug, Clone, Copy)]
pub enum Capacity {
    /// No capacity — clear the map on every fill. Equivalent to `Some(0)`.
    None,
    /// Use the default capacity provided by the caller (typically `max_occlusion_size`).
    Default,
    /// Use a specific capacity.
    Some(usize),
    /// Unbounded — never evict entries.
    Unbounded,
}

impl Capacity {
    /// Resolve to a concrete capacity. Returns [`Option::None`] for
    /// [`Unbounded`](Capacity::Unbounded).
    pub fn resolve(self, default: usize) -> Option<usize> {
        match self {
            Self::None => Some(0),
            Self::Default => Some(default),
            Self::Some(actual) => Some(actual),
            Self::Unbounded => Option::None,
        }
    }
}

//////////
// View //
//////////

/// The [`super::View`] implementation for [`Map`], constructed with [`Map::view`].
///
/// If an [`Overlay`] is used with the associated [`Map`], elements in the overlay will be
/// given precedence.
///
/// If [working set reuse](self#working-set-reuse) is enabled, then all elements within
/// the [`Map`] are visible, not just those that belong to the most recent [`Fill`].
#[derive(Debug)]
pub struct View<'a, K, V, P: Projection = Reborrowed<V>> {
    map: &'a Map<K, V, P>,
}

impl<K, V, P> super::View<K> for View<'_, K, V, P>
where
    K: Hash + Eq,
    P: Projection,
    V: Project<P> + Send + Sync + 'static,
{
    type ElementRef<'a> = P::ElementRef<'a>;
    type Element<'a>
        = P::Element<'a>
    where
        Self: 'a;

    fn get(&self, id: K) -> Option<Self::Element<'_>> {
        // Check seed overlay first (zero-copy from batch), then fill layer (owned, projected).
        self.map
            .overlay
            .as_ref()
            .and_then(|s| s.get(&id))
            .or_else(|| self.map.map.get(&id).map(|v| v.value().project()))
    }
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;

    use std::{borrow::Cow, sync::Arc};

    use diskann_utils::views::Matrix;

    use crate::graph::{
        test::provider::Accessor as TestAccessor, workingset::View as WorkingSetView,
    };

    /// Convenience alias matching the test provider's working set type.
    type TestMap = Map<u32, Box<[f32]>, Ref<[f32]>>;

    /// Convenience alias matching the test provider's working set type.
    type TestMapProjected = Map<u32, Box<[f32]>, TestProjection>;

    /// Build an unbounded test map (no eviction). Convenience for unit tests.
    fn unbounded_map() -> TestMap {
        Builder::new(Capacity::Unbounded).build(0)
    }

    /// Build an unbounded projected test map. Convenience for unit tests.
    fn unbounded_map_projected() -> TestMapProjected {
        Builder::new(Capacity::Unbounded).build(0)
    }

    /// Build a seeded test map with the given overlay and capacity.
    fn seeded_map(overlay: Overlay<u32, Ref<[f32]>>, capacity: Capacity) -> TestMap {
        Builder::new(capacity).with_overlay(overlay).build(16)
    }

    /// Build a seeded projected test map with the given overlay and capacity.
    fn seeded_map_projected(
        overlay: Overlay<u32, TestProjection>,
        capacity: Capacity,
    ) -> TestMapProjected {
        Builder::new(capacity).with_overlay(overlay).build(16)
    }

    /// The `TestProjection` ensures that we can apply an arbitrary transformation on the
    /// `Map`, `View`, and `Overlay` types.
    #[derive(Debug, Clone, Copy)]
    struct TestProjection;

    #[derive(Debug, PartialEq, Clone, Copy)]
    enum Source {
        Project,
        Into,
    }

    #[derive(Debug, PartialEq)]
    struct Wrapped<'a> {
        data: &'a [f32],
        source: Source,
    }

    impl<'a> Wrapped<'a> {
        fn new(data: &'a [f32], source: Source) -> Self {
            Self { data, source }
        }
    }

    impl<'a> Reborrow<'a> for Wrapped<'_> {
        type Target = &'a [f32];
        fn reborrow(&'a self) -> Self::Target {
            self.data
        }
    }

    impl Projection for TestProjection {
        type Element<'a> = Wrapped<'a>;
        type ElementRef<'a> = &'a [f32];
    }

    impl Project<TestProjection> for Box<[f32]> {
        fn project(&self) -> Wrapped<'_> {
            Wrapped {
                data: self,
                source: Source::Project,
            }
        }
    }

    impl<'a> From<&'a [f32]> for Wrapped<'a> {
        fn from(data: &'a [f32]) -> Self {
            Self {
                data,
                source: Source::Into,
            }
        }
    }

    /// Build a 3-row × 2-col matrix for testing with the following layout:
    /// ```text
    /// 1.0  2.0
    /// 3.0  4.0
    /// 5.0  6.0
    /// ```
    fn test_matrix() -> Matrix<f32> {
        Matrix::try_from(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0].into_boxed_slice(), 3, 2).unwrap()
    }

    type TestOverlay = Overlay<u32, Ref<[f32]>>;

    fn test_overlay() -> (Arc<Matrix<f32>>, TestOverlay) {
        let batch = Arc::new(test_matrix());
        let ids = [10u32, 20, 30];
        let overlay = Overlay::from_batch(batch.clone(), ids);
        (batch, overlay)
    }

    fn test_overlay_projected() -> (Arc<Matrix<f32>>, Overlay<u32, TestProjection>) {
        let batch = Arc::new(test_matrix());
        let ids = [10u32, 20, 30];
        let overlay = Overlay::from_batch(batch.clone(), ids);
        (batch, overlay)
    }

    //----------//
    // Capacity //
    //----------//

    #[test]
    fn test_capacity() {
        let c = Capacity::None;
        assert_eq!(c.resolve(0), Some(0));
        assert_eq!(c.resolve(50), Some(0));
        assert_eq!(c.resolve(100), Some(0));

        let c = Capacity::Default;
        assert_eq!(c.resolve(0), Some(0));
        assert_eq!(c.resolve(50), Some(50));
        assert_eq!(c.resolve(100), Some(100));

        for i in [5, 10, 15] {
            let c = Capacity::Some(i);
            assert_eq!(c.resolve(0), Some(i));
            assert_eq!(c.resolve(50), Some(i));
            assert_eq!(c.resolve(100), Some(i));
        }

        let c = Capacity::Unbounded;
        assert_eq!(c.resolve(0), None);
        assert_eq!(c.resolve(50), None);
        assert_eq!(c.resolve(100), None);
    }

    //-----------------------//
    // Map basics (unseeded) //
    //-----------------------//

    #[test]
    fn insert_and_get() {
        let mut map = unbounded_map();
        assert_eq!(map.generation, 0);

        // Empty Map
        assert!(map.get(&1).is_none());
        assert!(map.get(&2).is_none());
        assert!(map.get(&3).is_none());

        assert!(!map.contains_key(&1));
        assert!(!map.contains_key(&2));
        assert!(!map.contains_key(&3));

        // Insert key 1
        assert!(map.insert(1, vec![1.0, 2.0].into_boxed_slice()).is_none());
        assert_eq!(&**map.get(&1).unwrap(), &[1.0, 2.0]);

        assert!(map.get(&2).is_none());
        assert!(map.get(&3).is_none());

        assert!(map.contains_key(&1));
        assert!(!map.contains_key(&2));
        assert!(!map.contains_key(&3));

        // -- Check generations
        assert_eq!(map.generation_of(1), 0);

        // Here - we reach into a private method to bump the generation and verify that
        // `insert` respects the generation.
        map.new_generation();
        assert_eq!(map.generation, 1);

        // Insert Key 3
        assert!(map.insert(3, vec![2.0, 3.0].into_boxed_slice()).is_none());
        assert_eq!(&**map.get(&1).unwrap(), &[1.0, 2.0]);
        assert!(map.get(&2).is_none());
        assert_eq!(&**map.get(&3).unwrap(), &[2.0, 3.0]);

        assert!(map.contains_key(&1));
        assert!(!map.contains_key(&2));
        assert!(map.contains_key(&3));

        // -- Check Gneerations
        assert_eq!(map.generation_of(1), 0);
        assert_eq!(map.generation_of(3), 1);
    }

    #[test]
    fn insert_overwrites_existing() {
        let mut map = unbounded_map();
        map.insert(1, Box::new([1.0]));
        assert_eq!(map.generation_of(1), 0);
        map.new_generation();

        let old = map.insert(1, Box::new([2.0]));
        assert_eq!(&*old.unwrap(), &[1.0]);
        assert_eq!(&**map.get(&1).unwrap(), &[2.0]);
        assert_eq!(map.generation_of(1), 1);
    }

    #[test]
    fn get_mut_modifies_in_place() {
        let mut map = unbounded_map();
        map.insert(1, Box::new([0.0, 0.0]));
        assert_eq!(map.generation_of(1), 0);

        map.new_generation();
        map.get_mut(&1).unwrap()[0] = 42.0;
        assert_eq!(&**map.get(&1).unwrap(), &[42.0, 0.0]);
        assert_eq!(
            map.generation_of(1),
            1,
            "`get_mut` implicitly bumps the generation"
        );
    }

    #[test]
    fn clear_empties_map() {
        let mut map = unbounded_map();
        map.insert(1, Box::new([1.0]));
        map.insert(2, Box::new([2.0]));
        map.clear();
        assert!(!map.contains_key(&1));
        assert!(!map.contains_key(&2));
    }

    //----------------------//
    // Entry API (unseeded) //
    //----------------------//

    #[test]
    fn entry_vacant_then_occupied() {
        let mut map = unbounded_map();
        map.new_generation();

        // Initially vacant.
        match map.entry(1) {
            Entry::Vacant(v) => {
                assert_eq!(*v.key(), 1);
                v.insert(Box::new([1.0]));
            }
            _ => panic!("expected Vacant"),
        }

        // Now occupied.
        match map.entry(1) {
            Entry::Occupied(o) => {
                assert_eq!(&**o.get(), &[1.0]);
            }
            _ => panic!("expected Occupied"),
        }

        assert_eq!(
            map.generation_of(1),
            1,
            "vacant inserts match current generation"
        );
    }

    #[test]
    fn occupied_entry_mut() {
        let mut map = unbounded_map();

        // Go through `into_mut()`.
        map.insert(1, Box::new([0.0]));
        assert_eq!(map.generation_of(1), 0);

        map.new_generation();
        match map.entry(1) {
            Entry::Occupied(o) => {
                let val = o.into_mut();
                val[0] = 99.0;
            }
            _ => panic!("expected Occupied"),
        }
        assert_eq!(&**map.get(&1).unwrap(), &[99.0]);
        assert_eq!(
            map.generation_of(1),
            1,
            "OccupiedEntry implicitly increments generation"
        );

        map.new_generation();
        match map.entry(1) {
            Entry::Occupied(mut o) => {
                let val = o.get_mut();
                val[0] = 42.0;
            }
            _ => panic!("expected Occupied"),
        }
        assert_eq!(&**map.get(&1).unwrap(), &[42.0]);
        assert_eq!(
            map.generation_of(1),
            2,
            "OccupiedEntry implicitly increments generation"
        );
    }

    //-----------------//
    // View (unseeded) //
    //-----------------//

    #[test]
    fn view_returns_projected_element() {
        // Standard Slice Decay
        {
            let mut map = unbounded_map();
            map.insert(1, Box::new([1.0, 2.0]));

            // Views don't care about generations.
            map.new_generation();
            let view = map.view();

            let element = view.get(1).unwrap();
            assert_eq!(element, &[1.0, 2.0]);
            assert!(view.get(999).is_none());
        }

        // Projection - note that the types inserted are still `Box<[f32]>`.
        {
            let mut map = unbounded_map_projected();
            map.insert(1, Box::new([1.0, 2.0]));

            // Views don't care about generations.
            map.new_generation();
            let view = map.view();

            let element = view.get(1).unwrap();
            assert_eq!(element, Wrapped::new(&[1.0, 2.0], Source::Project));
            assert!(view.get(999).is_none());
        }
    }

    //------------------------//
    // Overlay / BatchOverlay //
    //------------------------//

    #[test]
    fn overlay_from_batch_lookups() {
        // Standard Ref
        {
            let (_batch, overlay) = test_overlay();

            // Each ID maps to its row in the matrix.
            assert_eq!(overlay.get(&10).unwrap(), &[1.0, 2.0]);
            assert_eq!(overlay.get(&20).unwrap(), &[3.0, 4.0]);
            assert_eq!(overlay.get(&30).unwrap(), &[5.0, 6.0]);
            assert!(overlay.contains_key(&10));
            assert!(overlay.contains_key(&20));

            // Missing key.
            assert!(overlay.get(&99).is_none());
            assert!(!overlay.contains_key(&99));
        }

        // Projected
        {
            let (_batch, overlay) = test_overlay_projected();
            let source = Source::Into;

            // Each ID maps to its row in the matrix.
            assert_eq!(overlay.get(&10).unwrap(), Wrapped::new(&[1.0, 2.0], source));
            assert_eq!(overlay.get(&20).unwrap(), Wrapped::new(&[3.0, 4.0], source));
            assert_eq!(overlay.get(&30).unwrap(), Wrapped::new(&[5.0, 6.0], source));
            assert!(overlay.contains_key(&10));
            assert!(overlay.contains_key(&20));

            // Missing key.
            assert!(overlay.get(&99).is_none());
            assert!(!overlay.contains_key(&99));
        }
    }

    #[test]
    fn overlay_is_cheap_to_clone() {
        let (_batch, overlay) = test_overlay();

        let base = overlay.get(&10).unwrap();
        assert_eq!(base, &[1.0, 2.0]);

        let cloned = overlay.clone();
        // Both see the same data.
        let from_cloned = cloned.get(&10).unwrap();
        assert_eq!(from_cloned, &[1.0, 2.0]);
        assert_eq!(
            from_cloned.as_ptr(),
            base.as_ptr(),
            "underlying data should be the exact same"
        );
    }

    #[test]
    fn overlay_from_batch_zero_copy() {
        // Verify that the batch data is NOT copied — the overlay holds an Arc to the same
        // allocation. We can confirm by checking that the returned slices point into the
        // original matrix's memory.
        let batch = Arc::new(test_matrix());
        let overlay = Overlay::<u32, Ref<[f32]>>::from_batch(batch.clone(), [10u32, 20, 30]);

        let from_overlay: &[f32] = overlay.get(&10).unwrap();
        let from_batch: &[f32] = batch.row(0);
        assert!(std::ptr::eq(from_overlay, from_batch));
    }

    //-------------------------------------------//
    // Map with seed overlay (three-state entry) //
    //-------------------------------------------//

    #[test]
    fn seeded_map_entry_seeded() {
        let (_batch, overlay) = test_overlay_projected();
        let mut map = seeded_map_projected(overlay, Capacity::Default);
        assert_eq!(map.generation, 0);

        match map.entry(99) {
            Entry::Vacant(v) => {
                assert_eq!(*v.key(), 99);
                // Don't fill the vacant entry.
            }
            _ => panic!("expected Vacant for key not in overlay or map"),
        }

        match map.entry(10) {
            Entry::Seeded(element) => {
                assert_eq!(element, Wrapped::new(&[1.0, 2.0], Source::Into));
            }
            _ => panic!("expected Seeded for key present in overlay"),
        }

        match map.entry(99) {
            Entry::Vacant(v) => {
                v.insert(Box::new([5.0]));
            }
            _ => panic!("expected Vacant"),
        }
        assert_eq!(&**map.get(&99).unwrap(), &[5.0]);
        assert_eq!(map.generation_of(99), 0);

        map.new_generation();
        match map.entry(99) {
            Entry::Occupied(v) => {
                *v.into_mut() = Box::new([10.0]);
            }
            _ => panic!("expected Occupied"),
        }
        assert_eq!(&**map.get(&99).unwrap(), &[10.0]);
        assert_eq!(map.generation_of(99), 1);
    }

    #[test]
    fn seeded_map_insert_skips_seeded_key() {
        let (_batch, overlay) = test_overlay_projected();
        let mut map = seeded_map_projected(overlay, Capacity::Default);

        // Inserting a key that's in the seed is a no-op (returns None).
        assert!(
            map.insert(10, Box::new([99.0])).is_none(),
            "The key 10 should not exist"
        );

        // The seed value is unchanged.
        let view = map.view();
        assert_eq!(
            view.get(10).unwrap(),
            Wrapped::new(&[1.0, 2.0], Source::Into)
        );

        assert!(
            map.insert(10, Box::new([99.0])).is_none(),
            "The key 10 should not still exist"
        );
    }

    #[test]
    fn seeded_map_insert_works_for_non_seeded() {
        let (_batch, overlay) = test_overlay_projected();
        let mut map = seeded_map_projected(overlay, Capacity::Default);

        map.new_generation();
        map.insert(99, Box::new([7.0, 8.0]));
        assert_eq!(map.generation_of(99), 1);

        assert!(map.contains_key(&10));
        assert!(map.contains_key(&20));
        assert!(map.contains_key(&30));
        assert!(map.contains_key(&99));
        assert!(!map.contains_key(&1));

        assert!(map.get(&10).is_none());
        assert!(map.get(&20).is_none());
        assert!(map.get(&30).is_none());
        assert_eq!(
            map.get(&99).unwrap().as_ref(),
            &[7.0, 8.0],
            "get does not check the overlay"
        );
        assert!(!map.contains_key(&1));
    }

    //-----------------------------//
    // View with seed (seed-first) //
    //-----------------------------//

    #[test]
    fn view_seed_first_priority() {
        let (_batch, overlay) = test_overlay_projected();
        let mut map = seeded_map_projected(overlay, Capacity::Default);

        // Insert a different value for key 10 into the fill layer — but seed takes priority
        // in the view. (In practice, insert() blocks this, but we test the view's dispatch.)
        // Since insert() is a no-op for seeded keys, insert into a non-seeded key instead.
        map.insert(99, Box::new([7.0, 8.0]));

        let view = map.view();
        // Seed key — served from overlay.
        assert_eq!(
            view.get(10).unwrap(),
            Wrapped::new(&[1.0, 2.0], Source::Into)
        );
        // Map key — served from fill layer.
        assert_eq!(
            view.get(99).unwrap(),
            Wrapped::new(&[7.0, 8.0], Source::Project)
        );
        // Missing — None.
        assert!(view.get(1).is_none());
    }

    #[test]
    fn view_after_clear_still_has_seed() {
        let (_batch, overlay) = test_overlay();
        let mut map = seeded_map(overlay, Capacity::Default);
        map.insert(99, Box::new([7.0]));

        map.clear();

        let view = map.view();
        // Seed survives clear.
        assert_eq!(view.get(10).unwrap(), &[1.0, 2.0]);
        // Fill layer is gone.
        assert!(view.get(99).is_none());
    }

    //-------------//
    // Generations //
    //-------------//

    #[test]
    fn test_promote() {
        let mut map: TestMap = Builder::new(Capacity::Default).build(5);
        map.insert(1, Box::new([1.0]));
        map.insert(2, Box::new([2.0]));
        map.insert(3, Box::new([3.0]));

        // No matches - nothing to promote
        map.new_generation();
        assert_eq!(map.promote([5, 6, 7].into_iter()), 0);
        for i in [1, 2, 3] {
            assert_eq!(
                map.generation_of(i),
                0,
                "generation should remain unchanged"
            );
        }

        // All matches - everything should promote.
        //
        // Include some extra keys not present in the map as well.
        map.new_generation();
        assert_eq!(map.promote([5, 2, 6, 3, 7, 1].into_iter()), 3);
        for i in [1, 2, 3] {
            assert_eq!(map.generation_of(i), 2);
        }

        // Partial matches - some update, others don't.
        map.new_generation();
        assert_eq!(map.promote([1, 4, 2, 10].into_iter()), 2);
        assert_eq!(map.generation_of(1), 3);
        assert_eq!(map.generation_of(2), 3);
        assert_eq!(map.generation_of(3), 2, "this entry was not promoted");

        // Cross generations all get promoted to the same.
        map.new_generation();
        assert_eq!(map.promote([1, 3].into_iter()), 2);
        assert_eq!(map.generation_of(1), 4);
        assert_eq!(map.generation_of(2), 3, "this entry was not promoted");
        assert_eq!(map.generation_of(3), 4);
    }

    #[test]
    fn test_evict() {
        let mut map: TestMap = Builder::new(Capacity::Default).build(5);

        fn reset(map: &mut TestMap) {
            map.clear();
            map.insert(1, Box::new([1.0]));
            map.insert(2, Box::new([2.0]));
            map.insert(3, Box::new([3.0]));
        }

        reset(&mut map);

        // All inserted items belong to the current generation - eviction should do nothing.
        for count in 0..10 {
            assert_eq!(
                map.evict(count),
                0,
                "all items belong to the current generation"
            );
            assert_eq!(&**map.get(&1).unwrap(), [1.0]);
            assert_eq!(&**map.get(&2).unwrap(), [2.0]);
            assert_eq!(&**map.get(&3).unwrap(), [3.0]);
        }

        // If we bump the generation but request no evictions - nothing should happen.
        map.new_generation(); // generation 1
        assert_eq!(map.evict(0), 0);
        assert_eq!(map.map.len(), 3);

        // Now - we bump the generation of entry 2 and request one entry to be evicted.
        let _ = map.get_mut(&2).unwrap();
        assert_eq!(map.generation_of(2), 1);
        assert_eq!(map.evict(1), 1);
        assert_eq!(map.map.len(), 2);
        assert!(map.contains_key(&2));

        // Bump the generation of entry 2 and request two entries to be evicted.
        reset(&mut map);
        map.new_generation(); // generation 2
        let _ = map.get_mut(&2).unwrap();
        assert_eq!(map.evict(2), 2);
        assert_eq!(map.map.len(), 1);
        assert!(map.contains_key(&2));

        // Request that more gets evicted and that the count is truncated by entries not
        // belonging to the current generation.
        reset(&mut map);
        map.new_generation(); // generation 3
        let _ = map.get_mut(&1).unwrap();
        assert_eq!(map.evict(10), 2);
        assert_eq!(map.map.len(), 1);
        assert!(map.contains_key(&1));

        // Check that *all* entries can be evicted.
        reset(&mut map);
        map.new_generation(); // generation 4
        assert_eq!(map.evict(10), 3);
        assert!(map.map.is_empty());
    }

    #[test]
    fn test_prepare_no_capacity() {
        let mut map: TestMap = Builder::new(Capacity::None).build(100);

        map.insert(1, Box::new([1.0]));
        map.insert(2, Box::new([2.0]));
        map.insert(3, Box::new([3.0]));

        // Even if we pass an empty iterator - this is sufficient to clear the entries.
        map.prepare([].into_iter());
        assert!(map.map.is_empty());
    }

    #[test]
    fn test_prepare_unbounded() {
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        map.insert(1, Box::new([1.0]));
        map.insert(2, Box::new([2.0]));
        map.insert(3, Box::new([3.0]));

        // Even if we pass an empty iterator - this is sufficient to clear the entries.
        map.prepare(1..100);
        assert_eq!(map.map.len(), 3, "unbounded maps skip preparation");
    }

    #[test]
    fn test_prepare_bounded() {
        // Capacity of 4: the map can hold up to 4 entries across generations.
        let mut map: TestMap = Builder::new(Capacity::Default).build(4);

        fn reset(map: &mut TestMap) {
            map.clear();
            map.insert(1, Box::new([1.0]));
            map.insert(2, Box::new([2.0]));
            map.insert(3, Box::new([3.0]));
            map.insert(4, Box::new([4.0]));
            assert_eq!(map.map.len(), 4);
        }

        // -- Case 1: insert 4 entries (at capacity)
        //
        // In this test - we have a mix of IDs in the map and out of the map.
        reset(&mut map);

        // IDs 1 and 2 get promoted, 3 and 4 get evicted.
        map.prepare([1, 2, 5, 6].into_iter());
        assert_eq!(map.generation, 1, "prepare should increment generation");
        assert_eq!(map.map.len(), 2, "evicted 2 old-gen entries to make room");

        // The promoted entries survive.
        assert_eq!(map.generation_of(1), 1);
        assert_eq!(map.generation_of(2), 1);

        // The non-promoted entries were evicted.
        assert!(map.get(&3).is_none(), "old-gen entry 3 should be evicted");
        assert!(map.get(&4).is_none(), "old-gen entry 4 should be evicted");

        // -- Case 2: request only IDs already present
        reset(&mut map);
        map.prepare([1, 2].into_iter());
        assert_eq!(map.generation, 2);
        assert_eq!(
            map.map.len(),
            4,
            "since we don't exceed capacity - no eviction was necessary"
        );
        assert_eq!(map.generation_of(1), 2);
        assert_eq!(map.generation_of(2), 2);
        assert_eq!(map.generation_of(3), 1);
        assert_eq!(map.generation_of(4), 1);

        // -- Case 3: request more IDs than capacity
        //
        // Entries 1 and 2 get promoted, everything else gets cleaned up.
        reset(&mut map);
        map.prepare([1, 2, 10, 11, 12, 13].into_iter());
        assert_eq!(map.generation, 3);
        assert_eq!(map.map.len(), 2, "keep items present in the iterator");

        // -- Case 4: empty iterator.
        //
        // No items should be evicted if we're already at capacity.
        reset(&mut map);
        map.prepare([].into_iter());
        assert_eq!(map.generation, 4);
        assert_eq!(map.map.len(), 4, "at capacity — old entries survive");
        assert_eq!(map.generation_of(1), 3);
        assert_eq!(map.generation_of(2), 3);
        assert_eq!(map.generation_of(3), 3);
        assert_eq!(map.generation_of(4), 3);

        // -- Case 5: empty iterator, recovery
        //
        // In this situation, the hash map is already over capacity (maybe a previous `fill`
        // added too many candidates). Here - we verify that it gets shrunk down to capacity
        // even if an empty iterator is provided.
        reset(&mut map);
        map.insert(5, Box::new([5.0]));
        map.insert(6, Box::new([6.0]));
        map.insert(7, Box::new([7.0]));
        map.prepare([].into_iter());
        assert_eq!(map.generation, 5);
        assert_eq!(map.map.len(), 4, "evict down to capacity");

        // -- Case 5: Disjoint, final size exceeds capacity but itertor does not.
        reset(&mut map);
        map.prepare([20, 21, 22].into_iter());
        assert_eq!(map.generation, 6);
        assert_eq!(map.map.len(), 1, "evicted 1 old-gen entry to make room");

        // -- Case 6: New iterator completely disjoint and above capacity.
        reset(&mut map);
        map.prepare([20, 21, 22, 23, 24].into_iter());
        assert_eq!(map.generation, 7);
        assert!(map.map.is_empty());
    }

    //---------------------//
    // Projection (Ref<T>) //
    //---------------------//

    #[test]
    fn project_ref_box_slice() {
        let value: Box<[f32]> = Box::new([1.0, 2.0, 3.0]);
        let projected: &[f32] = <_ as Project<Ref<[f32]>>>::project(&value);
        assert_eq!(projected, &[1.0, 2.0, 3.0]);
    }

    #[test]
    fn project_ref_vec() {
        let value = vec![4.0f32, 5.0];
        let projected: &[f32] = <_ as Project<Ref<[f32]>>>::project(&value);
        assert_eq!(projected, &[4.0, 5.0]);
    }

    //----------------------------//
    // Projection (Reborrowed<T>) //
    //----------------------------//

    #[test]
    fn project_reborrowed_box_slice() {
        let value: Box<[f32]> = Box::new([1.0, 2.0]);
        let projected = <_ as Project<Reborrowed<Box<[f32]>>>>::project(&value);
        let reborrowed: &[f32] = projected.reborrow();
        assert_eq!(reborrowed, &[1.0, 2.0]);
    }

    //---------------------//
    // MapLike for HashMap //
    //---------------------//

    #[test]
    fn hashmap_as_maplike() {
        let mut hm = hashbrown::HashMap::<u32, Box<[f32]>>::new();
        hm.insert(1, Box::new([10.0, 20.0]));
        hm.insert(2, Box::new([30.0]));

        {
            let map_like: &dyn MapLike<u32, Ref<[f32]>> = &hm;
            assert_eq!(map_like.get(&1).unwrap(), &[10.0, 20.0]);
            assert_eq!(map_like.get(&2).unwrap(), &[30.0]);
            assert!(map_like.get(&3).is_none());
            assert!(map_like.contains_key(&1));
            assert!(!map_like.contains_key(&3));

            let overlay = Overlay::<u32, Ref<[f32]>>::new(Arc::new(hm.clone()));
            assert_eq!(overlay.get(&1).unwrap(), &[10.0, 20.0]);
            assert_eq!(overlay.get(&2).unwrap(), &[30.0]);
            assert!(overlay.get(&3).is_none());
            assert!(overlay.contains_key(&1));
            assert!(!overlay.contains_key(&3));
        }

        {
            let map_like: &dyn MapLike<u32, TestProjection> = &hm;
            let source = Source::Project;
            assert_eq!(
                map_like.get(&1).unwrap(),
                Wrapped::new(&[10.0, 20.0], source)
            );
            assert_eq!(map_like.get(&2).unwrap(), Wrapped::new(&[30.0], source));
            assert!(map_like.get(&3).is_none());
            assert!(map_like.contains_key(&1));
            assert!(!map_like.contains_key(&3));

            let overlay = Overlay::<u32, TestProjection>::new(Arc::new(hm));
            assert_eq!(
                overlay.get(&1).unwrap(),
                Wrapped::new(&[10.0, 20.0], source)
            );
            assert_eq!(overlay.get(&2).unwrap(), Wrapped::new(&[30.0], source));
            assert!(overlay.get(&3).is_none());
            assert!(overlay.contains_key(&1));
            assert!(!overlay.contains_key(&3));
        }
    }

    //---------//
    // Builder //
    //---------//

    #[test]
    fn builder_unseeded_creates_empty_map() {
        let ws: TestMap = Builder::new(Capacity::Default).build(32);
        assert!(!ws.contains_key(&1));
        // No overlay.
        let view = ws.view();
        assert!(view.get(1).is_none());
    }

    #[test]
    fn builder_as_working_set() {
        let ws: TestMap = Builder::new(Capacity::Default).as_working_set(32);
        assert!(!ws.contains_key(&1));
        let view = ws.view();
        assert!(view.get(1).is_none());
    }

    //------------//
    // Edge cases //
    //------------//

    #[test]
    fn overlay_from_batch_empty() {
        let batch = Arc::new(Matrix::try_from(Box::new([]), 0, 2).unwrap());
        let overlay = Overlay::<u32, Ref<[f32]>>::from_batch(batch, std::iter::empty());
        assert!(overlay.get(&0).is_none());
        assert!(!overlay.contains_key(&0));
    }

    #[test]
    fn overlay_from_batch_single_element() {
        let batch = Arc::new(Matrix::try_from(Box::new([1.0, 2.0]), 1, 2).unwrap());
        let overlay = Overlay::<u32, Ref<[f32]>>::from_batch(batch, [42u32]);
        assert_eq!(overlay.get(&42).unwrap(), &[1.0, 2.0]);
    }

    #[test]
    fn overlay_from_batch_duplicate_ids_last_wins() {
        // When the same ID appears multiple times, the last index wins (HashMap behavior).
        let batch = Arc::new(test_matrix()); // rows: [1,2], [3,4], [5,6]
        let overlay = Overlay::<u32, Ref<[f32]>>::from_batch(batch, [10u32, 10, 10]);
        // Last occurrence: index 2 → row [5,6]
        assert_eq!(overlay.get(&10).unwrap(), &[5.0, 6.0]);
    }

    //------------------------------//
    // Projection struct Clone/Copy //
    //------------------------------//

    #[test]
    #[expect(clippy::clone_on_copy)]
    fn ref_clone() {
        let r = Ref::<[f32]>(std::marker::PhantomData);
        let _ = r.clone();
    }

    #[test]
    #[expect(clippy::clone_on_copy)]
    fn reborrowed_clone() {
        let r = Reborrowed::<Box<[f32]>>(std::marker::PhantomData);
        let _ = r.clone();
    }

    #[test]
    #[expect(clippy::clone_on_copy, reason = "explicitly testing Clone impl")]
    fn as_reborrowed_clone() {
        let value: Box<[f32]> = Box::new([3.0, 4.0]);
        let ar = AsReborrowed(&*value);
        let _ = ar.clone();
    }

    //----------------------------------//
    // Fill / fill_with (async, Tier 1) //
    //----------------------------------//

    /// Create a grid-backed provider (1-D, size 5).
    ///
    /// IDs 0–4 have vectors `[0.0]` .. `[4.0]`, start point is `u32::MAX`.
    fn fill_provider() -> crate::graph::test::provider::Provider {
        use crate::graph::test::synthetic::Grid;
        crate::graph::test::provider::Provider::grid(Grid::One, 5).unwrap()
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_happy_path() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        let current = map.generation;
        let view = map
            .fill(&mut accessor, [0u32, 1, 2].into_iter())
            .await
            .unwrap();

        assert_eq!(view.get(0).unwrap(), &[0.0]);
        assert_eq!(view.get(1).unwrap(), &[1.0]);
        assert_eq!(view.get(2).unwrap(), &[2.0]);
        assert!(view.get(99).is_none());

        assert_eq!(
            map.generation,
            current + 1,
            "`fill` should bump the generation"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_clears_previous_entries() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: Map<u32, Box<[f32]>, Ref<[f32]>> = Builder::new(Capacity::None).build(0);

        // First fill with IDs 0 and 1.
        let view = map
            .fill(&mut accessor, [0u32, 1].into_iter())
            .await
            .unwrap();

        assert!(view.get(0).is_some());
        assert!(view.get(1).is_some());
        assert!(view.get(2).is_none());

        // Second fill with only ID 2 — previous entries should be cleared.
        let view = map.fill(&mut accessor, [2u32].into_iter()).await.unwrap();
        assert!(view.get(0).is_none(), "fill should have cleared id 0");
        assert!(view.get(1).is_none(), "fill should have cleared id 1");
        assert_eq!(view.get(2).unwrap(), &[2.0]);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_with_preserves_entries() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        // Populate with ID 0.
        let view = map
            .fill_with(&mut accessor, [0u32].into_iter(), |e| e.into())
            .await
            .unwrap();
        assert!(view.get(0).is_some());

        // fill_with with ID 1 — should NOT clear ID 0.
        let view = map
            .fill_with(&mut accessor, [1u32].into_iter(), |e| e.into())
            .await
            .unwrap();
        assert_eq!(
            view.get(0).unwrap(),
            &[0.0],
            "fill_with should preserve id 0"
        );
        assert_eq!(view.get(1).unwrap(), &[1.0]);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_skips_transient_errors() {
        let provider = fill_provider();

        let mut accessor =
            TestAccessor::flaky(&provider, Cow::Owned(std::collections::HashSet::from([1])));
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        // ID 1 is transient — should be skipped, not propagated.
        let view = map
            .fill(&mut accessor, [0u32, 1, 2].into_iter())
            .await
            .unwrap();
        assert_eq!(view.get(0).unwrap(), &[0.0]);
        assert!(view.get(1).is_none(), "transient ID should be absent");
        assert_eq!(view.get(2).unwrap(), &[2.0]);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_propagates_critical_errors() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        // ID 99 doesn't exist — critical InvalidId error.
        let err = map
            .fill(&mut accessor, [0u32, 99].into_iter())
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("99"),
            "error should mention the invalid id: {msg}"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_with_skips_occupied_entries() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        // Pre-insert a sentinel value for ID 0.
        map.insert(0, Box::new([99.0]));

        // fill_with should skip the occupied entry.
        let view = map
            .fill_with(&mut accessor, [0u32, 1].into_iter(), |e| e.into())
            .await
            .unwrap();

        // ID 0 retains its pre-inserted sentinel.
        assert_eq!(
            view.get(0).unwrap(),
            &[99.0],
            "occupied entry should be preserved"
        );
        assert_eq!(view.get(1).unwrap(), &[1.0]);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_with_skips_seeded_entries() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);

        // Seed with a batch containing a different value for ID 0.
        let batch = Arc::new(Matrix::try_from(Box::new([99.0, 88.0]), 2, 1).unwrap());
        let overlay = Overlay::<u32, Ref<[f32]>>::from_batch(batch, [0u32, 1].into_iter());
        let mut map = seeded_map(overlay, Capacity::Unbounded);

        // fill_with requests IDs 0 and 2. ID 0 is seeded → skip, ID 2 is filled.
        let view = map
            .fill_with(&mut accessor, [0u32, 2].into_iter(), |e| e.into())
            .await
            .unwrap();

        // ID 0 comes from the seed (batch row 0 = [99.0]), NOT the accessor.
        assert_eq!(view.get(0).unwrap(), &[99.0]);
        // ID 2 was filled from the accessor.
        assert_eq!(view.get(2).unwrap(), &[2.0]);
        // Verify ID 0 is NOT in the fill layer.
        assert!(
            map.get(&0).is_none(),
            "ID 0 should only be in the seed, not the fill layer"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn fill_empty_iterator() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        let view = map.fill(&mut accessor, std::iter::empty()).await.unwrap();
        assert!(view.get(0).is_none());
    }

    #[tokio::test(flavor = "current_thread")]
    async fn blanket_fill_trait() {
        let provider = fill_provider();
        let mut accessor = TestAccessor::new(&provider);
        let mut map: TestMap = Builder::new(Capacity::Unbounded).build(0);

        // Exercise the blanket Fill<Map> impl.
        let view = <_ as Fill<TestMap>>::fill(&mut accessor, &mut map, [0u32, 1, 2].into_iter())
            .await
            .unwrap();

        assert_eq!(view.get(0).unwrap(), &[0.0]);
        assert_eq!(view.get(1).unwrap(), &[1.0]);
        assert_eq!(view.get(2).unwrap(), &[2.0]);
    }
}