diskann-quantization 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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use std::{marker::PhantomData, ops::RangeInclusive, ptr::NonNull};

use diskann_utils::{Reborrow, ReborrowMut};
use thiserror::Error;

use super::{
    length::{Dynamic, Length},
    packing,
    ptr::{AsMutPtr, AsPtr, MutSlicePtr, Precursor, SlicePtr},
};
use crate::{
    alloc::{AllocatorCore, AllocatorError, GlobalAllocator, Poly},
    utils,
};

//////////////////////
// Retrieval Traits //
//////////////////////

/// Representation of `NBITS` bit numbers in the associated domain.
pub trait Representation<const NBITS: usize> {
    /// The type of the domain accepted by this representation.
    type Domain: Iterator<Item = i64>;

    /// Encode `value` into the lower order bits of a byte. Returns the encoded value on
    /// success, or an `EncodingError` if the value is unencodable.
    fn encode(value: i64) -> Result<u8, EncodingError>;

    /// Encode `value` into the lower order bits of a byte without checking if `value`
    /// is encodable. This function is not marked as unsafe because in-and-of itself, it
    /// won't cause memory safety issues.
    ///
    /// This may panic in debug mode when `value` is outside of this representation's
    /// domain.
    fn encode_unchecked(value: i64) -> u8;

    /// Decode a previously encoded value. The result will be in the range
    /// `[Self::MIN, Self::MAX]`.
    ///
    /// # Panics
    ///
    /// May panic in debug builds if `raw` is not a valid pattern emitted by `encode`.
    fn decode(raw: u8) -> i64;

    /// Check whether or not the argument is in the domain.
    fn check(value: i64) -> bool;

    /// Return an iterator over the domain of representable values.
    fn domain() -> Self::Domain;
}

#[derive(Debug, Error, Clone, Copy)]
#[error("value {} is not in the encodable range of {}", got, domain)]
pub struct EncodingError {
    got: i64,
    // Question: Why is this a ref-ref??
    //
    // Answer: I have a personal vendetta to keep this struct within 16-bytes with a
    // niche-optimization. A `&'static src` is 16 bytes in and of itself. But a
    // `&'static &'static str`, now *that's* just 8 bytes.
    domain: &'static &'static str,
}

impl EncodingError {
    fn new(got: i64, domain: &'static &'static str) -> Self {
        Self { got, domain }
    }
}

//////////////
// Unsigned //
//////////////

/// Storage unsigned integers in slices.
///
/// For a bit count of `NBITS`, the `Unsigned` type can store unsigned integers in
/// the range `[0, 2^NBITS - 1]`.
#[derive(Debug, Clone, Copy)]
pub struct Unsigned;

impl Unsigned {
    /// Return the dynamic range of an `Unsigned` encoding for `NBITS`.
    pub const fn domain_const<const NBITS: usize>() -> std::ops::RangeInclusive<i64> {
        0..=2i64.pow(NBITS as u32) - 1
    }

    #[allow(clippy::panic)]
    const fn domain_str(nbits: usize) -> &'static &'static str {
        match nbits {
            8 => &"[0, 255]",
            7 => &"[0, 127]",
            6 => &"[0, 63]",
            5 => &"[0, 31]",
            4 => &"[0, 15]",
            3 => &"[0, 7]",
            2 => &"[0, 3]",
            1 => &"[0, 1]",
            _ => panic!("unimplemented"),
        }
    }
}

macro_rules! repr_unsigned {
    ($N:literal) => {
        impl Representation<$N> for Unsigned {
            type Domain = RangeInclusive<i64>;

            fn encode(value: i64) -> Result<u8, EncodingError> {
                if !<Self as Representation<$N>>::check(value) {
                    // Even with the macro gymnastics - we still have to manually inline
                    // this computation :(
                    let domain = Self::domain_str($N);
                    Err(EncodingError::new(value, domain))
                } else {
                    Ok(<Self as Representation<$N>>::encode_unchecked(value))
                }
            }

            fn encode_unchecked(value: i64) -> u8 {
                debug_assert!(<Self as Representation<$N>>::check(value));
                value as u8
            }

            fn decode(raw: u8) -> i64 {
                // Feed through the value un-modified.
                let raw: i64 = raw.into();
                debug_assert!(<Self as Representation<$N>>::check(raw));
                raw
            }

            fn check(value: i64) -> bool {
                <Self as Representation<$N>>::domain().contains(&value)
            }

            fn domain() -> Self::Domain {
                Self::domain_const::<$N>()
            }
        }
    };
    ($N:literal, $($Ns:literal),+) => {
        repr_unsigned!($N);
        $(repr_unsigned!($Ns);)+
    };
}

repr_unsigned!(1, 2, 3, 4, 5, 6, 7, 8);

////////////
// Binary //
////////////

/// A 1-bit binary quantization mapping `-1` to `0` and `1` to `1`.
#[derive(Debug, Clone, Copy)]
pub struct Binary;

impl Representation<1> for Binary {
    type Domain = std::array::IntoIter<i64, 2>;

    fn encode(value: i64) -> Result<u8, EncodingError> {
        if !Self::check(value) {
            const DOMAIN: &str = "{-1, 1}";
            Err(EncodingError::new(value, &DOMAIN))
        } else {
            Ok(Self::encode_unchecked(value))
        }
    }

    fn encode_unchecked(value: i64) -> u8 {
        debug_assert!(Self::check(value));
        // The use of `clamp` here is a quick way of sending `-1` to `0` and `1` to `1`.
        value.clamp(0, 1) as u8
    }

    fn decode(raw: u8) -> i64 {
        // Raw is either 0 or 1. We want to map it to -1 or 1.
        // We can do this by multiplying by 2 and subtracting 1.
        let raw: i64 = raw.into();
        (raw << 1) - 1
    }

    fn check(value: i64) -> bool {
        value == -1 || value == 1
    }

    /// Return the domain of the encoding.
    ///
    /// The domain is the set `{-1, 1}`.
    fn domain() -> Self::Domain {
        [-1, 1].into_iter()
    }
}

////////////////////////
// Permutation Traits //
////////////////////////

/// A enable the dimensions within a BitSlice to be permuted in an arbitrary way.
///
/// # Safety
///
/// This provides the computation for the number of bytes required to store a given number
/// of `NBITS` bit-packed values. Improper implementation will result in out-of-bounds
/// accesses being made.
///
/// The following must hold:
///
/// For all counts values `c`, let `b = Self::bytes(c)` be requested number of bytes for `c`
/// for this permutation strategy and `s` be a slice of bytes with length `c`. Then, for all
/// `i < c`, `Self::pack(s, i, _)` and `Self::unpack(s, i)` must only access `s` in-bounds.
///
/// This implementation must be such that unsafe code can rely on this property holding.
pub unsafe trait PermutationStrategy<const NBITS: usize> {
    /// Return the number of bytes required to store `count` values of with `NBITS`.
    fn bytes(count: usize) -> usize;

    /// Pack the lower `NBITS` bits of `value` into `s` at logical index `i`.
    ///
    /// # Safety
    ///
    /// This is a tricky function to call with several subtle requirements.
    ///
    /// * Let `s` be a slice of length `c` where `c = Self::bytes(b)` for some `b`. Then
    ///   this function is safe to call if `i` is in `[0, b)`.
    unsafe fn pack(s: &mut [u8], i: usize, value: u8);

    /// Unpack the value stored at logical index `i` and return it as the lower `NBITS` bits
    /// in the return value.
    ///
    /// # Safety
    ///
    /// This is a tricky function to call with several subtle requirements.
    ///
    /// * Let `s` be a slice of length `c` where `c = Self::bytes(b)` for some `b`. Then
    ///   this function is safe to call if `i` is in `[0, b)`.
    unsafe fn unpack(s: &[u8], i: usize) -> u8;
}

/// The identity permutation strategy.
///
/// All values are densly packed.
#[derive(Debug, Clone, Copy)]
pub struct Dense;

impl Dense {
    fn bytes<const NBITS: usize>(count: usize) -> usize {
        utils::div_round_up(NBITS * count, 8)
    }
}

/// Safety: For all `0 <= i < count`, `NBITS * i <= 8 * ceil((NBITS * count) / 8)`.
unsafe impl<const NBITS: usize> PermutationStrategy<NBITS> for Dense {
    fn bytes(count: usize) -> usize {
        Self::bytes::<NBITS>(count)
    }

    unsafe fn pack(data: &mut [u8], i: usize, encoded: u8) {
        let bitaddress = NBITS * i;

        let bytestart = bitaddress / 8;
        let bytestop = (bitaddress + NBITS - 1) / 8;
        let bitstart = bitaddress - 8 * bytestart;
        debug_assert!(bytestop < data.len());

        if bytestart == bytestop {
            // SAFETY: This is safe for the following:
            // ```
            // data.len() >= ceil(NBITS * i / 8)        from `pack`'s safety requirements.
            //            >= floor(NBITS * i / 8)
            //            = bytestart
            //
            // Since we are only reading one byte - this is in-bounds.
            // ```
            let raw = unsafe { data.as_ptr().add(bytestart).read() };
            let packed = packing::pack_u8::<NBITS>(raw, encoded, bitstart);

            // SAFETY: See previous argument for in-bounds access.
            // For writing, we are the only writers in this function and we have a mutable
            // reference to `data`.
            unsafe { data.as_mut_ptr().add(bytestart).write(packed) };
        } else {
            // SAFETY: This is safe for the following reason:
            // ```
            // data.len() >= ceil(NBITS * i / 8)        from `pack`'s safety requirements.
            //            = bytestop
            //            = bytestart + 1
            // ```
            // Therefore, it is safe to read 2-bytes starting at `bytestart`.
            let raw = unsafe { data.as_ptr().add(bytestart).cast::<u16>().read_unaligned() };
            let packed = packing::pack_u16::<NBITS>(raw, encoded, bitstart);

            // SAFETY: See previous argument for in-bounds access.
            // For writing, we are the only writers in this function and we have a mutable
            // reference to `data`.
            unsafe {
                data.as_mut_ptr()
                    .add(bytestart)
                    .cast::<u16>()
                    .write_unaligned(packed)
            };
        }
    }

    unsafe fn unpack(data: &[u8], i: usize) -> u8 {
        let bitaddress = NBITS * i;

        let bytestart = bitaddress / 8;
        let bytestop = (bitaddress + NBITS - 1) / 8;
        debug_assert!(bytestop < data.len());
        if bytestart == bytestop {
            // SAFETY: See the safety argument in `pack` for in-bounds.
            let raw = unsafe { data.as_ptr().add(bytestart).read() };
            packing::unpack_u8::<NBITS>(raw, bitaddress - 8 * bytestart)
        } else {
            // SAFETY: See the safety argument in `pack` for in-bounds.
            let raw = unsafe { data.as_ptr().add(bytestart).cast::<u16>().read_unaligned() };
            packing::unpack_u16::<NBITS>(raw, bitaddress - 8 * bytestart)
        }
    }
}

/// A layout specialized for performing multi-bit operations with 1-bit scalar quantization.
///
/// The layout provided by this struct is as follows. Assume we are compressing `N` bit data.
/// Then, the store the data in blocks of `64 * N` bits (where 64 comes from the native CPU
/// word size).
///
/// Each block can contain 64 values, stored in `N` 64-bit words. The 0th bit of each value
/// is stored in word 0, the 1st bit is stored in word 1, etc.
///
/// # Partially Filled Blocks
///
/// This strategy always requests data in blocks. For partially filled blocks, the lower
/// bits in the last block will be used.
#[derive(Debug, Clone, Copy)]
pub struct BitTranspose;

/// Safety: We ask for bytes in multiples of 32. Furthermore, the accesses to the packed
/// data in `pack` and `unpack` use checked accesses, so out-of-bounds reads will panic.
unsafe impl PermutationStrategy<4> for BitTranspose {
    fn bytes(count: usize) -> usize {
        32 * utils::div_round_up(count, 64)
    }

    unsafe fn pack(data: &mut [u8], i: usize, encoded: u8) {
        // Compute the byte-address of the block containing `i`.
        let block_start = 32 * (i / 64);
        // Compute the offset within the block to find the first byte containing `i`.
        let byte_start = block_start + (i % 64) / 8;
        // Finally, compute the bit within the byte that we are interested in.
        let bit = i % 8;

        let mask: u8 = 0x1 << bit;
        for p in 0..4 {
            let mut v = data[byte_start + 8 * p];
            v = (v & !mask) | (((encoded >> p) & 0x1) << bit);
            data[byte_start + 8 * p] = v;
        }
    }

    unsafe fn unpack(data: &[u8], i: usize) -> u8 {
        // Compute the byte-address of the block containing `i`.
        let block_start = 32 * (i / 64);
        // Compute the offset within the block to find the first byte containing `i`.
        let byte_start = block_start + (i % 64) / 8;
        // Finally, compute the bit within the byte that we are interested in.
        let bit = i % 8;

        let mut output: u8 = 0;
        for p in 0..4 {
            let v = data[byte_start + 8 * p];
            output |= ((v >> bit) & 0x1) << p
        }
        output
    }
}

////////////
// Errors //
////////////

#[derive(Debug, Error, Clone, Copy)]
#[error("input span has length {got} bytes but expected {expected}")]
pub struct ConstructionError {
    got: usize,
    expected: usize,
}

#[derive(Debug, Error, Clone, Copy)]
#[error("index {index} exceeds the maximum length of {len}")]
pub struct IndexOutOfBounds {
    index: usize,
    len: usize,
}

impl IndexOutOfBounds {
    fn new(index: usize, len: usize) -> Self {
        Self { index, len }
    }
}

#[derive(Debug, Error, Clone, Copy)]
#[error("error setting index in bitslice")]
#[non_exhaustive]
pub enum SetError {
    IndexError(#[from] IndexOutOfBounds),
    EncodingError(#[from] EncodingError),
}

#[derive(Debug, Error, Clone, Copy)]
#[error("error getting index in bitslice")]
pub enum GetError {
    IndexError(#[from] IndexOutOfBounds),
}

//////////////
// BitSlice //
//////////////

/// A generalized representation for packed small bit integer encodings over a contiguous
/// span of memory.
///
/// Think of this as a Rust slice, but supporting integer elements with fewer than 8-bits.
/// The borrowed representations [`BitSlice`] and [`MutBitSlice`] consist of just a pointer
/// and a length and are therefore just 16-bytes in size and amenable to the niche
/// optimization.
///
/// # Parameters
///
/// * `NBITS`: The number of bits occupied by each entry in the vector.
///
/// * `Repr`: The storage representation for each collection of 8-bits. This representation
///   defines the domain of the encoding (i.e., range of realized values) as well as how
///   this domain is mapped into `NBITS` bits.
///
/// * `Ptr`: The storage type for the contiguous memory. Possible representations are:
///   - `diskann_quantization::bits::SlicePtr<'_, u8>`: For immutable views.
///   - `diskann_quantization::bits::MutSlicePtr<'_, u8>`: For mutable views.
///   - `Box<[u8]>`: For standalone vectors.
///
/// * `Perm`: By default, this type uses a dense storage strategy where the least significant
///   bit of the value at index `i` occurs directly after the most significant bit of
///   the value at index `i-1`.
///
///   Different permutations can be used to enable faster distance computations between
///   compressed vectors and full-precision vectors by enabling faster SIMD unpacking.
///
/// * `Len`: The representation for the length of the vector. This may only be one of the
///   two families of types:
///   - `diskann_quantization::bits::Dynamic`: For instances with a run-time length.
///   - `diskann_quantization::bits::Static<N>`: For instances with a compile-time known length
///     of `N`.
///
/// # Examples
///
/// ## Canonical Bit Slice
///
/// The canonical `BitSlice` stores unsigned integers of `NBITS` densely in memory.
/// That is, for a type `BitSliceBase<3, Unsigned, _>`, the layout is as follows:
/// ```text
/// |<--LSB-- byte 0 --MSB--->|<--LSB-- byte 1 --MSB--->|
/// | a0 a1 a2 b0 b1 b2 c0 c1 | c2 d0 d1 d2 e0 e1 e2 f0 |
/// |<-- A -->|<-- B ->|<--- C -->|<-- D ->|<-- E ->|<- F
/// ```
/// An example is shown below:
///
/// ```rust
/// use diskann_quantization::bits::{BoxedBitSlice, Unsigned};
/// // Create a new boxed bit-slice with capacity for 10 dimensions.
/// let mut x = BoxedBitSlice::<3, Unsigned>::new_boxed(10);
/// assert_eq!(x.len(), 10);
/// // The number of bytes in the canonical representation is computed by
/// // ceil((len * NBITS) / 8);
/// assert_eq!(x.bytes(), 4);
///
/// // Assign values.
/// x.set(0, 1).unwrap(); // assign the value 1 to index 0
/// x.set(1, 5).unwrap(); // assign the value 5 to index 1
/// assert_eq!(x.get(0).unwrap(), 1); // retrieve the value at index 0
/// assert_eq!(x.get(1).unwrap(), 5); // retrieve the value at index 1
///
/// // Assigning out-of-bounds will result in an error.
/// let err = x.set(1, 10).unwrap_err();
/// assert!(matches!(diskann_quantization::bits::SetError::EncodingError, err));
/// // The old value is left untouched.
/// assert_eq!(x.get(1).unwrap(), 5);
///
/// // `BoxedBitSlice` allows itself to be consumed, returning the underlying storage.
/// let y = x.into_inner();
/// assert_eq!(y.len(), BoxedBitSlice::<3, Unsigned>::bytes_for(10));
/// ```
///
/// The above example demonstrates a boxed bit slice - a type that owns its underlying
/// memory. However, this is not always ergonomic when interfacing with data stores.
/// For this, the viewing interface can be used.
/// ```rust
/// use diskann_quantization::bits::{MutBitSlice, Unsigned};
///
/// let mut x: Vec<u8> = vec![0; 4];
/// let mut slice = MutBitSlice::<3, Unsigned>::new(x.as_mut_slice(), 10).unwrap();
/// assert_eq!(slice.len(), 10);
/// assert_eq!(slice.bytes(), 4);
///
/// // The slice reference behaves just like boxed slice.
/// slice.set(0, 5).unwrap();
/// assert_eq!(slice.get(0).unwrap(), 5);
///
/// // Note - if the number of bytes required for the provided dimensions does not match
/// // the length of the provided span, than slice construction will return an error.
/// let err = MutBitSlice::<3, Unsigned>::new(x.as_mut_slice(), 11).unwrap_err();
/// assert_eq!(err.to_string(), "input span has length 4 bytes but expected 5");
/// ```
#[derive(Debug, Clone, Copy)]
pub struct BitSliceBase<const NBITS: usize, Repr, Ptr, Perm = Dense, Len = Dynamic>
where
    Repr: Representation<NBITS>,
    Ptr: AsPtr<Type = u8>,
    Perm: PermutationStrategy<NBITS>,
    Len: Length,
{
    ptr: Ptr,
    len: Len,
    repr: PhantomData<Repr>,
    packing: PhantomData<Perm>,
}

impl<const NBITS: usize, Repr, Ptr, Perm, Len> BitSliceBase<NBITS, Repr, Ptr, Perm, Len>
where
    Repr: Representation<NBITS>,
    Ptr: AsPtr<Type = u8>,
    Perm: PermutationStrategy<NBITS>,
    Len: Length,
{
    /// Check that NBITS is in the interval [1, 8].
    const _CHECK: () = assert!(NBITS > 0 && NBITS <= 8);

    /// Return the exact number of bytes required to store `count` values.
    pub fn bytes_for(count: usize) -> usize {
        Perm::bytes(count)
    }

    /// Return a new `BitSlice` over the data behind `ptr`.
    ///
    /// # Safety
    ///
    /// It's the callers responsibility to ensure that all the invariants required for
    /// `std::slice::from_raw_parts(ptr.as_ptr(), len.value)` hold.
    unsafe fn new_unchecked_internal(ptr: Ptr, len: Len) -> Self {
        Self {
            ptr,
            len,
            repr: PhantomData,
            packing: PhantomData,
        }
    }

    /// Construct a new `BitSlice` without checking preconditions.
    ///
    /// # Safety
    ///
    /// Requires the following to avoid undefined behavior:
    ///
    /// * `precursor.precursor_len() == Self::bytes_for(<Count as Into<Len>>::into(count).value())`.
    ///
    /// This is checked in debug builds.
    pub unsafe fn new_unchecked<Pre, Count>(precursor: Pre, count: Count) -> Self
    where
        Count: Into<Len>,
        Pre: Precursor<Ptr>,
    {
        let count: Len = count.into();
        debug_assert_eq!(precursor.precursor_len(), Self::bytes_for(count.value()));

        // SAFETY: Inherited from the caller.
        unsafe { Self::new_unchecked_internal(precursor.precursor_into(), count) }
    }

    /// Construct a new `BitSlice` from the `precursor` capable of holding `count` encoded
    /// elements of size `NBITS.
    ///
    /// # Requirements
    ///
    /// The number of bytes pointed to by the precursor must be equal to the number of bytes
    /// required by the layout. That is:
    ///
    /// * `precursor.precursor_len() == Self::bytes_for(<Count as Into<Len>>::into(count).value())`.
    pub fn new<Pre, Count>(precursor: Pre, count: Count) -> Result<Self, ConstructionError>
    where
        Count: Into<Len>,
        Pre: Precursor<Ptr>,
    {
        // Allow callers to pass in `usize` as the count when using dynamic
        let count: Len = count.into();

        // Make sure that the slice has the correct length.
        if precursor.precursor_len() != Self::bytes_for(count.value()) {
            Err(ConstructionError {
                got: precursor.precursor_len(),
                expected: Self::bytes_for(count.value()),
            })
        } else {
            // SAFETY: We have checked that `precursor` has the correct number of bytes.
            // The only implementations of `Precursor` are those we defined for slices, so we
            // don't have to worry about downstream users inserting their own, incorrectly
            // implemented implementation.
            Ok(unsafe { Self::new_unchecked(precursor, count) })
        }
    }

    /// Return the number of elements contained in the slice.
    pub fn len(&self) -> usize {
        self.len.value()
    }

    /// Return whether or not the slice is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Return the number of bytes occupied by this slice.
    pub fn bytes(&self) -> usize {
        Self::bytes_for(self.len())
    }

    /// Return the value at logical index `i`.
    pub fn get(&self, i: usize) -> Result<i64, GetError> {
        if i >= self.len() {
            Err(IndexOutOfBounds::new(i, self.len()).into())
        } else {
            // SAFETY: We've performed the bounds check.
            Ok(unsafe { self.get_unchecked(i) })
        }
    }

    /// Return the value at logical index `i`.
    ///
    /// # Safety
    ///
    /// Argument `i` must be in bounds: `0 <= i < self.len()`.
    pub unsafe fn get_unchecked(&self, i: usize) -> i64 {
        debug_assert!(i < self.len());
        debug_assert_eq!(self.as_slice().len(), Perm::bytes(self.len()));

        // SAFETY: We maintain the invariant that
        // `self.as_slice().len() == Perm::bytes(self.len())`.
        //
        // So, `i < self.len()` implies we uphold the safety requirements of `unpack`.
        Repr::decode(unsafe { Perm::unpack(self.as_slice(), i) })
    }

    /// Encode and assign `value` to logical index `i`.
    pub fn set(&mut self, i: usize, value: i64) -> Result<(), SetError>
    where
        Ptr: AsMutPtr<Type = u8>,
    {
        if i >= self.len() {
            return Err(IndexOutOfBounds::new(i, self.len()).into());
        }

        let encoded = Repr::encode(value)?;

        // SAFETY: We've performed the bounds check.
        unsafe { self.set_unchecked(i, encoded) }
        Ok(())
    }

    /// Assign `value` to logical index `i`.
    ///
    /// # Safety
    ///
    /// Argument `i` must be in bounds: `0 <= i < self.len()`.
    pub unsafe fn set_unchecked(&mut self, i: usize, encoded: u8)
    where
        Ptr: AsMutPtr<Type = u8>,
    {
        debug_assert!(i < self.len());
        debug_assert_eq!(self.as_slice().len(), Perm::bytes(self.len()));

        // SAFETY: We maintain the invariant that
        // `self.as_slice().len() == Perm::bytes(self.len())`.
        //
        // So, `i < self.len()` implies we uphold the safety requirements of `unpack`.
        unsafe { Perm::pack(self.as_mut_slice(), i, encoded) }
    }

    /// Return the domain of acceptable values.
    pub fn domain(&self) -> Repr::Domain {
        Repr::domain()
    }

    pub(crate) fn as_slice(&self) -> &'_ [u8] {
        // SAFETY: This class has the invariant that the backing storage must be initialized
        // and exist in a single allocation containing at least
        // `[self.ptr.as_ptr(), self.ptr_ptr() + self.bytes())`.
        unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.bytes()) }
    }

    /// Return a pointer to the beginning of the memory associated with this slice.
    ///
    /// # NOTE
    ///
    /// The memory span underlying this instances is valid for `self.bytes()`, not
    /// necessarily `self.len()`.
    pub fn as_ptr(&self) -> *const u8 {
        self.ptr.as_ptr()
    }

    /// This function is very easy to use incorrectly and hence is crate-local.
    pub(super) fn as_mut_slice(&mut self) -> &'_ mut [u8]
    where
        Ptr: AsMutPtr,
    {
        // SAFETY: This class has the invariant that the backing storage must be initialized
        // and exist in a single allocation containing at least
        // `[self.ptr.as_ptr(), self.ptr_ptr() + self.bytes())`.
        //
        // A mutable reference to self with `Ptr: AsMutPtr` attests to the fact that we
        // have an exclusive borrow over the underlying memory.
        unsafe { std::slice::from_raw_parts_mut(self.ptr.as_mut_ptr(), self.bytes()) }
    }

    /// This function is very easy to use incorrectly and hence is private.
    fn as_mut_ptr(&mut self) -> *mut u8
    where
        Ptr: AsMutPtr,
    {
        self.ptr.as_mut_ptr()
    }
}

impl<const NBITS: usize, Repr, Perm, Len>
    BitSliceBase<NBITS, Repr, Poly<[u8], GlobalAllocator>, Perm, Len>
where
    Repr: Representation<NBITS>,
    Perm: PermutationStrategy<NBITS>,
    Len: Length,
{
    /// Construct a new owning `BitSlice` capable of holding `Count` logical values.
    /// The slice is initialized in a valid but undefined state.
    ///
    /// # Example
    ///
    /// ```
    /// use diskann_quantization::bits::{BoxedBitSlice, Unsigned};
    /// let mut x = BoxedBitSlice::<3, Unsigned>::new_boxed(4);
    /// x.set(0, 0).unwrap();
    /// x.set(1, 2).unwrap();
    /// x.set(2, 4).unwrap();
    /// x.set(3, 6).unwrap();
    ///
    /// assert_eq!(x.get(0).unwrap(), 0);
    /// assert_eq!(x.get(1).unwrap(), 2);
    /// assert_eq!(x.get(2).unwrap(), 4);
    /// assert_eq!(x.get(3).unwrap(), 6);
    /// ```
    pub fn new_boxed<Count>(count: Count) -> Self
    where
        Count: Into<Len>,
    {
        let count: Len = count.into();
        let bytes = Self::bytes_for(count.value());
        let storage: Box<[u8]> = (0..bytes).map(|_| 0).collect();

        // SAFETY: We've ensured that the backing storage has the correct number of bytes
        // as required by the count and PermutationStrategy.
        //
        // Since this is owned storage, we do not need to worry about capturing lifetimes.
        unsafe { Self::new_unchecked(Poly::from(storage), count) }
    }
}

impl<const NBITS: usize, Repr, Perm, Len, A> BitSliceBase<NBITS, Repr, Poly<[u8], A>, Perm, Len>
where
    Repr: Representation<NBITS>,
    Perm: PermutationStrategy<NBITS>,
    Len: Length,
    A: AllocatorCore,
{
    /// Construct a new owning `BitSlice` capable of holding `Count` logical values using
    /// the provided allocator.
    ///
    /// The slice is initialized in a valid but undefined state.
    ///
    /// # Example
    ///
    /// ```
    /// use diskann_quantization::{
    ///     alloc::GlobalAllocator,
    ///     bits::{BoxedBitSlice, Unsigned}
    /// };
    /// let mut x = BoxedBitSlice::<3, Unsigned>::new_in(4, GlobalAllocator).unwrap();
    /// x.set(0, 0).unwrap();
    /// x.set(1, 2).unwrap();
    /// x.set(2, 4).unwrap();
    /// x.set(3, 6).unwrap();
    ///
    /// assert_eq!(x.get(0).unwrap(), 0);
    /// assert_eq!(x.get(1).unwrap(), 2);
    /// assert_eq!(x.get(2).unwrap(), 4);
    /// assert_eq!(x.get(3).unwrap(), 6);
    /// ```
    pub fn new_in<Count>(count: Count, allocator: A) -> Result<Self, AllocatorError>
    where
        Count: Into<Len>,
    {
        let count: Len = count.into();
        let bytes = Self::bytes_for(count.value());
        let storage = Poly::broadcast(0, bytes, allocator)?;

        // SAFETY: We've ensured that the backing storage has the correct number of bytes
        // as required by the count and PermutationStrategy.
        //
        // Since this is owned storage, we do not need to worry about capturing lifetimes.
        Ok(unsafe { Self::new_unchecked(storage, count) })
    }

    /// Consume `self` and return the boxed allocation.
    pub fn into_inner(self) -> Poly<[u8], A> {
        self.ptr
    }
}

/// The layout for `N`-bit integers that references a raw underlying slice.
pub type BitSlice<'a, const N: usize, Repr, Perm = Dense, Len = Dynamic> =
    BitSliceBase<N, Repr, SlicePtr<'a, u8>, Perm, Len>;

/// The layout for `N`-bit integers that mutable references a raw underlying slice.
pub type MutBitSlice<'a, const N: usize, Repr, Perm = Dense, Len = Dynamic> =
    BitSliceBase<N, Repr, MutSlicePtr<'a, u8>, Perm, Len>;

/// The layout for `N`-bit integers that own the underlying slice.
pub type PolyBitSlice<const N: usize, Repr, A, Perm = Dense, Len = Dynamic> =
    BitSliceBase<N, Repr, Poly<[u8], A>, Perm, Len>;

/// The layout for `N`-bit integers that own the underlying slice.
pub type BoxedBitSlice<const N: usize, Repr, Perm = Dense, Len = Dynamic> =
    PolyBitSlice<N, Repr, GlobalAllocator, Perm, Len>;

///////////////////////////////
// Special Cased Conversions //
///////////////////////////////

impl<'a, Ptr> From<&'a BitSliceBase<8, Unsigned, Ptr>> for &'a [u8]
where
    Ptr: AsPtr<Type = u8>,
{
    fn from(slice: &'a BitSliceBase<8, Unsigned, Ptr>) -> Self {
        // SAFETY: The original pointer must have been obtained from a slice of the
        // appropriate length.
        //
        // Furthermore, the layout of this type of slice is guaranteed to be identical
        // to the layout of a `[u8]`.
        unsafe { std::slice::from_raw_parts(slice.as_ptr(), slice.len()) }
    }
}

impl<'this, const NBITS: usize, Repr, Ptr, Perm, Len> Reborrow<'this>
    for BitSliceBase<NBITS, Repr, Ptr, Perm, Len>
where
    Repr: Representation<NBITS>,
    Ptr: AsPtr<Type = u8>,
    Perm: PermutationStrategy<NBITS>,
    Len: Length,
{
    type Target = BitSlice<'this, NBITS, Repr, Perm, Len>;

    fn reborrow(&'this self) -> Self::Target {
        let ptr: *const u8 = self.as_ptr();
        debug_assert!(!ptr.is_null());

        // Safety: `AsPtr` may never return null pointers.
        // The `cast_mut()` is safe because `SlicePtr` does not provide a way of retrieving
        // a mutable pointer.
        let nonnull = unsafe { NonNull::new_unchecked(ptr.cast_mut()) };

        // Safety: By struct invariant,
        // `[self.ptr(), self.ptr() + Self::bytes_for(self.len()))` is a valid slice, so
        // the returned object will also uphold these invariants.
        //
        // The returned struct will not outlive `&'this self`, so we've attached the
        // proper lifetime.
        let ptr = unsafe { SlicePtr::new_unchecked(nonnull) };

        Self::Target {
            ptr,
            len: self.len,
            repr: PhantomData,
            packing: PhantomData,
        }
    }
}

impl<'this, const NBITS: usize, Repr, Ptr, Perm, Len> ReborrowMut<'this>
    for BitSliceBase<NBITS, Repr, Ptr, Perm, Len>
where
    Repr: Representation<NBITS>,
    Ptr: AsMutPtr<Type = u8>,
    Perm: PermutationStrategy<NBITS>,
    Len: Length,
{
    type Target = MutBitSlice<'this, NBITS, Repr, Perm, Len>;

    fn reborrow_mut(&'this mut self) -> Self::Target {
        let ptr: *mut u8 = self.as_mut_ptr();
        debug_assert!(!ptr.is_null());

        // Safety: `AsMutPtr` may never return null pointers.
        let nonnull = unsafe { NonNull::new_unchecked(ptr) };

        // Safety: By struct invariant,
        // `[self.ptr(), self.ptr() + Self::bytes_for(self.len()))` is a valid slice, so
        // the returned object will also uphold these invariants.
        //
        // The returned struct will not outlive `&'this mut self`, so we've attached the
        // proper lifetime.
        //
        // Exclusive ownership is attested by both `AsMutPtr` and the mutable refernce
        // to self.
        let ptr = unsafe { MutSlicePtr::new_unchecked(nonnull) };

        Self::Target {
            ptr,
            len: self.len,
            repr: PhantomData,
            packing: PhantomData,
        }
    }
}

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

#[cfg(test)]
mod tests {
    use rand::{
        Rng, SeedableRng,
        distr::{Distribution, Uniform},
        rngs::StdRng,
        seq::{IndexedRandom, SliceRandom},
    };

    use super::*;
    use crate::{bits::Static, test_util::AlwaysFails};

    ////////////
    // Errors //
    ////////////

    const BOUNDS: &str = "special bounds";

    #[test]
    fn test_encoding_error() {
        assert_eq!(std::mem::size_of::<EncodingError>(), 16);
        assert_eq!(
            std::mem::size_of::<Option<EncodingError>>(),
            16,
            "expected EncodingError to have the niche optimization"
        );
        let err = EncodingError::new(7, &BOUNDS);
        assert_eq!(
            err.to_string(),
            "value 7 is not in the encodable range of special bounds"
        );
    }

    // Check that a type is `Send` and `Sync`.
    fn assert_send_and_sync<T: Send + Sync>(_x: &T) {}

    ////////////
    // Binary //
    ////////////

    #[test]
    fn test_binary_repr() {
        assert_eq!(Binary::encode(-1).unwrap(), 0);
        assert_eq!(Binary::encode(1).unwrap(), 1);
        assert_eq!(Binary::decode(0), -1);
        assert_eq!(Binary::decode(1), 1);

        assert!(Binary::check(-1));
        assert!(Binary::check(1));
        assert!(!Binary::check(0));
        assert!(!Binary::check(-2));
        assert!(!Binary::check(2));

        let domain: Vec<_> = Binary::domain().collect();
        assert_eq!(domain, &[-1, 1]);
    }

    ///////////
    // Sizes //
    ///////////

    #[test]
    fn test_sizes() {
        assert_eq!(std::mem::size_of::<BitSlice<'static, 8, Unsigned>>(), 16);
        assert_eq!(std::mem::size_of::<MutBitSlice<'static, 8, Unsigned>>(), 16);

        // Ensure the borrowed slices are eligible for niche optimization.
        assert_eq!(
            std::mem::size_of::<Option<BitSlice<'static, 8, Unsigned>>>(),
            16
        );
        assert_eq!(
            std::mem::size_of::<Option<MutBitSlice<'static, 8, Unsigned>>>(),
            16
        );

        assert_eq!(
            std::mem::size_of::<BitSlice<'static, 8, Unsigned, Dense, Static<128>>>(),
            8
        );
    }

    ///////////////////
    // General Tests //
    ///////////////////

    cfg_if::cfg_if! {
        if #[cfg(miri)] {
            const MAX_DIM: usize = 160;
            const FUZZ_ITERATIONS: usize = 1;
        } else if #[cfg(debug_assertions)] {
            const MAX_DIM: usize = 128;
            const FUZZ_ITERATIONS: usize = 10;
        } else {
            const MAX_DIM: usize = 256;
            const FUZZ_ITERATIONS: usize = 100;
        }
    }

    fn test_send_and_sync<const NBITS: usize, Repr, Perm>()
    where
        Repr: Representation<NBITS> + Send + Sync,
        Perm: PermutationStrategy<NBITS> + Send + Sync,
    {
        let mut x = BoxedBitSlice::<NBITS, Repr, Perm>::new_boxed(1);
        assert_send_and_sync(&x);
        assert_send_and_sync(&x.reborrow());
        assert_send_and_sync(&x.reborrow_mut());
    }

    fn test_empty<const NBITS: usize, Repr, Perm>()
    where
        Repr: Representation<NBITS>,
        Perm: PermutationStrategy<NBITS>,
    {
        let base: &mut [u8] = &mut [];
        let mut slice = MutBitSlice::<NBITS, Repr, Perm>::new(base, 0).unwrap();
        assert_eq!(slice.len(), 0);
        assert!(slice.is_empty());

        {
            let reborrow = slice.reborrow();
            assert_eq!(reborrow.len(), 0);
            assert!(reborrow.is_empty());
        }

        {
            let reborrow = slice.reborrow_mut();
            assert_eq!(reborrow.len(), 0);
            assert!(reborrow.is_empty());
        }
    }

    // times, ensuring that values are preserved.
    fn test_construction_errors<const NBITS: usize, Repr, Perm>()
    where
        Repr: Representation<NBITS>,
        Perm: PermutationStrategy<NBITS>,
    {
        let len: usize = 10;
        let bytes = Perm::bytes(len);

        // Construction errors for Boxes
        let box_big = Poly::broadcast(0u8, bytes + 1, GlobalAllocator).unwrap();
        let box_small = Poly::broadcast(0u8, bytes - 1, GlobalAllocator).unwrap();
        let box_right = Poly::broadcast(0u8, bytes, GlobalAllocator).unwrap();

        let result = BoxedBitSlice::<NBITS, Repr, Perm>::new(box_big, len);
        match result {
            Err(ConstructionError { got, expected }) => {
                assert_eq!(got, bytes + 1);
                assert_eq!(expected, bytes);
            }
            _ => panic!("shouldn't have reached here!"),
        };

        let result = BoxedBitSlice::<NBITS, Repr, Perm>::new(box_small, len);
        match result {
            Err(ConstructionError { got, expected }) => {
                assert_eq!(got, bytes - 1);
                assert_eq!(expected, bytes);
            }
            _ => panic!("shouldn't have reached here!"),
        };

        let mut base = BoxedBitSlice::<NBITS, Repr, Perm>::new(box_right, len).unwrap();
        let ptr = base.as_ptr();
        assert_eq!(base.len(), len);

        // Successful mutable reborrow and borrow.
        {
            // Use reborrow
            let borrowed = base.reborrow_mut();
            assert_eq!(borrowed.as_ptr(), ptr);
            assert_eq!(borrowed.len(), len);

            // Go through a slice.
            let borrowed = MutBitSlice::<NBITS, Repr, Perm>::new(base.as_mut_slice(), len).unwrap();
            assert_eq!(borrowed.as_ptr(), ptr);
            assert_eq!(borrowed.len(), len);
        }

        // Successful mutable borrow.
        {
            // Try constructing from an oversized slice.
            let mut oversized = vec![0; bytes + 1];
            let result = MutBitSlice::<NBITS, Repr, Perm>::new(oversized.as_mut_slice(), len);
            match result {
                Err(ConstructionError { got, expected }) => {
                    assert_eq!(got, bytes + 1);
                    assert_eq!(expected, bytes);
                }
                _ => panic!("shouldn't have reached here!"),
            };

            let mut undersized = vec![0; bytes - 1];
            let result = MutBitSlice::<NBITS, Repr, Perm>::new(undersized.as_mut_slice(), len);
            match result {
                Err(ConstructionError { got, expected }) => {
                    assert_eq!(got, bytes - 1);
                    assert_eq!(expected, bytes);
                }
                _ => panic!("shouldn't have reached here!"),
            };
        }

        // Successful const borrow and reborrow.
        {
            // Use reborrow
            let borrowed = base.reborrow();
            assert_eq!(borrowed.as_ptr(), ptr);
            assert_eq!(borrowed.len(), len);

            // Go through a slice.
            let borrowed = BitSlice::<NBITS, Repr, Perm>::new(base.as_slice(), len).unwrap();
            assert_eq!(borrowed.as_ptr(), ptr);
            assert_eq!(borrowed.len(), len);

            // Go through a mutable slice.
            let borrowed = BitSlice::<NBITS, Repr, Perm>::new(base.as_mut_slice(), len).unwrap();
            assert_eq!(borrowed.as_ptr(), ptr);
            assert_eq!(borrowed.len(), len);
        }

        // Successful mutable borrow.
        {
            // Try constructing from an oversized slice.
            let mut oversized = vec![0; bytes + 1];
            let result = BitSlice::<NBITS, Repr, Perm>::new(oversized.as_mut_slice(), len);
            match result {
                Err(ConstructionError { got, expected }) => {
                    assert_eq!(got, bytes + 1);
                    assert_eq!(expected, bytes);
                }
                _ => panic!("shouldn't have reached here!"),
            };

            let result = BitSlice::<NBITS, Repr, Perm>::new(oversized.as_slice(), len);
            match result {
                Err(ConstructionError { got, expected }) => {
                    assert_eq!(got, bytes + 1);
                    assert_eq!(expected, bytes);
                }
                _ => panic!("shouldn't have reached here!"),
            };

            // Try constructing from an undersized slice.
            let mut undersized = vec![0; bytes - 1];
            let result = BitSlice::<NBITS, Repr, Perm>::new(undersized.as_mut_slice(), len);
            match result {
                Err(ConstructionError { got, expected }) => {
                    assert_eq!(got, bytes - 1);
                    assert_eq!(expected, bytes);
                }
                _ => panic!("shouldn't have reached here!"),
            };

            let result = BitSlice::<NBITS, Repr, Perm>::new(undersized.as_slice(), len);
            match result {
                Err(ConstructionError { got, expected }) => {
                    assert_eq!(got, bytes - 1);
                    assert_eq!(expected, bytes);
                }
                _ => panic!("shouldn't have reached here!"),
            };
        }
    }

    // This series of tests writes to all indices in the vector in random orders multiple
    // times, ensuring that values are preserved.
    fn run_overwrite_test<const NBITS: usize, Perm, Len, R>(
        base: &mut BoxedBitSlice<NBITS, Unsigned, Perm, Len>,
        num_iterations: usize,
        rng: &mut R,
    ) where
        Unsigned: Representation<NBITS, Domain = RangeInclusive<i64>>,
        Len: Length,
        Perm: PermutationStrategy<NBITS>,
        R: Rng,
    {
        let mut expected: Vec<i64> = vec![0; base.len()];
        let mut indices: Vec<usize> = (0..base.len()).collect();
        for i in 0..base.len() {
            base.set(i, 0).unwrap();
        }

        for i in 0..base.len() {
            assert_eq!(base.get(i).unwrap(), 0, "failed to initialize bit vector");
        }

        let domain = base.domain();
        assert_eq!(domain, 0..=2i64.pow(NBITS as u32) - 1);
        let distribution = Uniform::new_inclusive(*domain.start(), *domain.end()).unwrap();

        for iter in 0..num_iterations {
            // Shuffle insertion order.
            indices.shuffle(rng);

            // Insert random values.
            for &i in indices.iter() {
                let value = distribution.sample(rng);
                expected[i] = value;
                base.set(i, value).unwrap();
            }

            // Make sure values are preserved.
            for (i, &expect) in expected.iter().enumerate() {
                let value = base.get(i).unwrap();
                assert_eq!(
                    value, expect,
                    "retrieval failed on iteration {iter} at index {i}"
                );
            }

            // Make sure the reborrowed version matches.
            let borrowed = base.reborrow();
            for (i, &expect) in expected.iter().enumerate() {
                let value = borrowed.get(i).unwrap();
                assert_eq!(
                    value, expect,
                    "reborrow retrieval failed on iteration {iter} at index {i}"
                );
            }
        }
    }

    fn run_overwrite_binary_test<Perm, Len, R>(
        base: &mut BoxedBitSlice<1, Binary, Perm, Len>,
        num_iterations: usize,
        rng: &mut R,
    ) where
        Len: Length,
        Perm: PermutationStrategy<1>,
        R: Rng,
    {
        let mut expected: Vec<i64> = vec![0; base.len()];
        let mut indices: Vec<usize> = (0..base.len()).collect();
        for i in 0..base.len() {
            base.set(i, -1).unwrap();
        }

        for i in 0..base.len() {
            assert_eq!(base.get(i).unwrap(), -1, "failed to initialize bit vector");
        }

        let distribution: [i64; 2] = [-1, 1];

        for iter in 0..num_iterations {
            // Shuffle insertion order.
            indices.shuffle(rng);

            // Insert random values.
            for &i in indices.iter() {
                let value = distribution.choose(rng).unwrap();
                expected[i] = *value;
                base.set(i, *value).unwrap();
            }

            // Make sure values are preserved.
            for (i, &expect) in expected.iter().enumerate() {
                let value = base.get(i).unwrap();
                assert_eq!(
                    value, expect,
                    "retrieval failed on iteration {iter} at index {i}"
                );
            }

            // Make sure the reborrowed version matches.
            let borrowed = base.reborrow();
            for (i, &expect) in expected.iter().enumerate() {
                let value = borrowed.get(i).unwrap();
                assert_eq!(
                    value, expect,
                    "reborrow retrieval failed on iteration {iter} at index {i}"
                );
            }
        }
    }

    //////////////////////
    // Unsigned - Dense //
    //////////////////////

    fn test_unsigned_dense<const NBITS: usize, Len, R>(
        len: Len,
        minimum: i64,
        maximum: i64,
        rng: &mut R,
    ) where
        Unsigned: Representation<NBITS, Domain = RangeInclusive<i64>>,
        Dense: PermutationStrategy<NBITS>,
        Len: Length,
        R: Rng,
    {
        test_send_and_sync::<NBITS, Unsigned, Dense>();
        test_empty::<NBITS, Unsigned, Dense>();
        test_construction_errors::<NBITS, Unsigned, Dense>();
        assert_eq!(Unsigned::domain_const::<NBITS>(), Unsigned::domain(),);

        match PolyBitSlice::<NBITS, Unsigned, _, Dense, Len>::new_in(len, AlwaysFails) {
            Ok(_) => {
                if len.value() != 0 {
                    panic!("zero sized allocations don't require an allocator");
                }
            }
            Err(AllocatorError) => {
                if len.value() == 0 {
                    panic!("allocation should have failed");
                }
            }
        }

        let mut base =
            PolyBitSlice::<NBITS, Unsigned, _, Dense, Len>::new_in(len, GlobalAllocator).unwrap();
        assert_eq!(
            base.len(),
            len.value(),
            "BoxedBitSlice returned the incorrect length"
        );

        let expected_bytes = BitSlice::<'static, NBITS, Unsigned>::bytes_for(len.value());
        assert_eq!(
            base.bytes(),
            expected_bytes,
            "BoxedBitSlice has the incorrect number of bytes"
        );

        // Check that the minimum and maximum values reported by the struct are correct.
        assert_eq!(base.domain(), minimum..=maximum);

        if len.value() == 0 {
            return;
        }

        let ptr = base.as_ptr();

        // Now that we know the length is non-zero, we can try testing the interface.
        // Setting the lowest index should always work.
        {
            let mut borrowed = base.reborrow_mut();

            // Make sure the pointer is preserved.
            assert_eq!(
                borrowed.as_ptr(),
                ptr,
                "pointer was not preserved during borrowing!"
            );
            assert_eq!(
                borrowed.len(),
                len.value(),
                "borrowing did not preserve length!"
            );

            borrowed.set(0, 0).unwrap();
            assert_eq!(borrowed.get(0).unwrap(), 0);

            borrowed.set(0, 1).unwrap();
            assert_eq!(borrowed.get(0).unwrap(), 1);

            borrowed.set(0, 0).unwrap();
            assert_eq!(borrowed.get(0).unwrap(), 0);

            // Setting to an invalid value should yield an error.
            let result = borrowed.set(0, minimum - 1);
            assert!(matches!(result, Err(SetError::EncodingError { .. })));

            let result = borrowed.set(0, maximum + 1);
            assert!(matches!(result, Err(SetError::EncodingError { .. })));

            // Make sure an out-of-bounds access is caught.
            let result = borrowed.set(borrowed.len(), 0);
            assert!(matches!(result, Err(SetError::IndexError { .. })));

            // Ensure that getting out-of-bounds is an error.
            let result = borrowed.get(borrowed.len());
            assert!(matches!(result, Err(GetError::IndexError { .. })));
        }

        {
            // Reconsturct the mutable borrow directly through a slice.
            let borrowed =
                MutBitSlice::<NBITS, Unsigned, Dense, Len>::new(base.as_mut_slice(), len).unwrap();

            assert_eq!(
                borrowed.as_ptr(),
                ptr,
                "pointer was not preserved during borrowing!"
            );
            assert_eq!(
                borrowed.len(),
                len.value(),
                "borrowing did not preserve length!"
            );
        }

        {
            let borrowed = base.reborrow();

            // Make sure the pointer is preserved.
            assert_eq!(
                borrowed.as_ptr(),
                ptr,
                "pointer was not preserved during borrowing!"
            );

            assert_eq!(
                borrowed.len(),
                len.value(),
                "borrowing did not preserve length!"
            );

            // Ensure that getting out-of-bounds is an error.
            let result = borrowed.get(borrowed.len());
            assert!(matches!(result, Err(GetError::IndexError { .. })));
        }

        {
            // Reconsturct the mutable borrow directly through a slice.
            let borrowed =
                BitSlice::<NBITS, Unsigned, Dense, Len>::new(base.as_slice(), len).unwrap();

            assert_eq!(
                borrowed.as_ptr(),
                ptr,
                "pointer was not preserved during borrowing!"
            );
            assert_eq!(
                borrowed.len(),
                len.value(),
                "borrowing did not preserve length!"
            );
        }

        {
            // Reconsturct the mutable borrow directly through a slice.
            let borrowed =
                BitSlice::<NBITS, Unsigned, Dense, Len>::new(base.as_mut_slice(), len).unwrap();

            assert_eq!(
                borrowed.as_ptr(),
                ptr,
                "pointer was not preserved during borrowing!"
            );
            assert_eq!(
                borrowed.len(),
                len.value(),
                "borrowing did not preserve length!"
            );
        }

        // Now we begin the testing loop.
        run_overwrite_test(&mut base, FUZZ_ITERATIONS, rng);
    }

    macro_rules! generate_unsigned_test {
        ($name:ident, $NBITS:literal, $MIN:literal, $MAX:literal, $SEED:literal) => {
            #[test]
            fn $name() {
                let mut rng = StdRng::seed_from_u64($SEED);
                for dim in 0..MAX_DIM {
                    test_unsigned_dense::<$NBITS, Dynamic, _>(dim.into(), $MIN, $MAX, &mut rng);
                }
            }
        };
    }

    generate_unsigned_test!(test_unsigned_8bit, 8, 0, 0xff, 0xc652f2a1018f442b);
    generate_unsigned_test!(test_unsigned_7bit, 7, 0, 0x7f, 0xb732e59fec6d6c9c);
    generate_unsigned_test!(test_unsigned_6bit, 6, 0, 0x3f, 0x35d9380d0a318f21);
    generate_unsigned_test!(test_unsigned_5bit, 5, 0, 0x1f, 0xfb09895183334304);
    generate_unsigned_test!(test_unsigned_4bit, 4, 0, 0x0f, 0x38dfcf9e82c33f48);
    generate_unsigned_test!(test_unsigned_3bit, 3, 0, 0x07, 0xf9a94c8c749ee26c);
    generate_unsigned_test!(test_unsigned_2bit, 2, 0, 0x03, 0xbba03db62cecf4cf);
    generate_unsigned_test!(test_unsigned_1bit, 1, 0, 0x01, 0x54ea2a07d7c67f37);

    #[test]
    fn test_binary_dense() {
        let mut rng = StdRng::seed_from_u64(0xb3c95e8e19d3842e);
        for len in 0..MAX_DIM {
            test_send_and_sync::<1, Binary, Dense>();
            test_empty::<1, Binary, Dense>();
            test_construction_errors::<1, Binary, Dense>();

            // Create a boxed base.
            let mut base = BoxedBitSlice::<1, Binary>::new_boxed(len);
            assert_eq!(
                base.len(),
                len,
                "BoxedBitSlice returned the incorrect length"
            );

            assert_eq!(base.bytes(), len.div_ceil(8));

            let bytes = BitSlice::<'static, 1, Binary>::bytes_for(len);
            assert_eq!(
                bytes,
                len.div_ceil(8),
                "BoxedBitSlice has the incorrect number of bytes"
            );

            if len == 0 {
                continue;
            }

            // Setting to an invalid value should yield an error.
            let result = base.set(0, 0);
            assert!(matches!(result, Err(SetError::EncodingError { .. })));

            // Make sure an out-of-bounds access is caught.
            let result = base.set(base.len(), -1);
            assert!(matches!(result, Err(SetError::IndexError { .. })));

            // Ensure that getting out-of-bounds is an error.
            let result = base.get(base.len());
            assert!(matches!(result, Err(GetError::IndexError { .. })));

            // Now we begin the testing loop.
            run_overwrite_binary_test(&mut base, FUZZ_ITERATIONS, &mut rng);
        }
    }

    #[test]
    fn test_4bit_bit_transpose() {
        let mut rng = StdRng::seed_from_u64(0xb3c95e8e19d3842e);
        for len in 0..MAX_DIM {
            test_send_and_sync::<4, Unsigned, BitTranspose>();
            test_empty::<4, Unsigned, BitTranspose>();
            test_construction_errors::<4, Unsigned, BitTranspose>();

            // Create a boxed base.
            let mut base = BoxedBitSlice::<4, Unsigned, BitTranspose>::new_boxed(len);
            assert_eq!(
                base.len(),
                len,
                "BoxedBitSlice returned the incorrect length"
            );

            assert_eq!(base.bytes(), 32 * len.div_ceil(64));

            let bytes = BitSlice::<'static, 4, Unsigned, BitTranspose>::bytes_for(len);
            assert_eq!(
                bytes,
                32 * len.div_ceil(64),
                "BoxedBitSlice has the incorrect number of bytes"
            );

            if len == 0 {
                continue;
            }

            // Setting to an invalid value should yield an error.
            let result = base.set(0, -1);
            assert!(matches!(result, Err(SetError::EncodingError { .. })));

            // Make sure an out-of-bounds access is caught.
            let result = base.set(base.len(), -1);
            assert!(matches!(result, Err(SetError::IndexError { .. })));

            // Ensure that getting out-of-bounds is an error.
            let result = base.get(base.len());
            assert!(matches!(result, Err(GetError::IndexError { .. })));

            // Now we begin the testing loop.
            run_overwrite_test(&mut base, FUZZ_ITERATIONS, &mut rng);
        }
    }
}