qfilter 0.2.5

Efficient bloom filter like datastructure, based on the Rank Select Quotient Filter (RSQF)
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
//! Approximate Membership Query Filter ([AMQ-Filter](https://en.wikipedia.org/wiki/Approximate_Membership_Query_Filter))
//! based on the [Rank Select Quotient Filter (RSQF)](https://dl.acm.org/doi/pdf/10.1145/3035918.3035963).
//!
//! This is a small and flexible general-purpose AMQ-Filter, it not only supports approximate membership testing like a bloom filter
//! but also deletions, merging, resizing and [serde](https://crates.io/crates/serde) serialization.
//!
//! ### Example
//!
//! ```rust
//! let mut f = qfilter::Filter::new(1000000, 0.01).unwrap();
//! for i in 0..1000 {
//!     f.insert(i).unwrap();
//! }
//! for i in 0..1000 {
//!     assert!(f.contains(i));
//! }
//! ```
//!
//! ### Hasher
//!
//! The hashing algorithm used is [xxhash3](https://crates.io/crates/xxhash-rust)
//! which offers both high performance and stability across platforms.
//!
//! ### Filter size
//!
//! For a given capacity and error probability the RSQF may require significantly less space than the equivalent bloom filter or other AMQ-Filters.
//!
//! | Bits per item | Error probability when full | Bits per item (cont.) | Error (cont.) |
//! |:---:|:---:|:---:|---|
//! | 3.125 | 0.362 | 19.125 | 6.87e-06 |
//! | 4.125 | 0.201 | 20.125 | 3.43e-06 |
//! | 5.125 | 0.106 | 21.125 | 1.72e-06 |
//! | 6.125 | 0.0547 | 22.125 | 8.58e-07 |
//! | 7.125 | 0.0277 | 23.125 | 4.29e-07 |
//! | 8.125 | 0.014 | 24.125 | 2.15e-07 |
//! | 9.125 | 0.00701 | 25.125 | 1.07e-07 |
//! | 10.125 | 0.00351 | 26.125 | 5.36e-08 |
//! | 11.125 | 0.00176 | 27.125 | 2.68e-08 |
//! | 12.125 | 0.000879 | 28.125 | 1.34e-08 |
//! | 13.125 | 0.000439 | 29.125 | 6.71e-09 |
//! | 14.125 | 0.00022 | 30.125 | 3.35e-09 |
//! | 15.125 | 0.00011 | 31.125 | 1.68e-09 |
//! | 16.125 | 5.49e-05 | 32.125 | 8.38e-10 |
//! | 17.125 | 2.75e-05 | .. | .. |
//! | 18.125 | 1.37e-05 | .. | .. |
//!
//! ### Legacy x86_64 CPUs support
//!
//! The implementation assumes the `popcnt` instruction (equivalent to `integer.count_ones()`) is present
//! when compiling for x86_64 targets. This is theoretically not guaranteed as the instruction in only
//! available on AMD/Intel CPUs released after 2007/2008. If that's not the case the Filter constructor will panic.
//!
//! Support for such legacy x86_64 CPUs can be optionally enabled with the `legacy_x86_64_support`
//! which incurs a ~10% performance penalty.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

use std::{
    cmp::Ordering,
    hash::{Hash, Hasher},
    num::{NonZeroU64, NonZeroU8},
    ops::{RangeBounds, RangeFrom},
};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use stable_hasher::StableHasher;

mod stable_hasher;

/// Approximate Membership Query Filter (AMQ-Filter) based on the Rank Select Quotient Filter (RSQF).
///
/// This data structure is similar to a hash table that stores fingerprints in a very compact way.
/// Fingerprints are similar to a hash values, but are possibly truncated.
/// The reason for false positives is that multiple items can map to the same fingerprint.
/// For more information see the [quotient filter Wikipedia page](https://en.wikipedia.org/wiki/Quotient_filter)
/// that describes a similar but less optimized version of the data structure.
/// The actual implementation is based on the [Rank Select Quotient Filter (RSQF)](https://dl.acm.org/doi/pdf/10.1145/3035918.3035963).
///
/// The public API also exposes a fingerprint API, which can be used to succinctly store u64
/// hash values.
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct Filter {
    #[cfg_attr(
        feature = "serde",
        serde(
            rename = "b",
            serialize_with = "serde_bytes::serialize",
            deserialize_with = "serde_bytes::deserialize"
        )
    )]
    buffer: Box<[u8]>,
    #[cfg_attr(feature = "serde", serde(rename = "l"))]
    len: u64,
    #[cfg_attr(feature = "serde", serde(rename = "q"))]
    qbits: NonZeroU8,
    #[cfg_attr(feature = "serde", serde(rename = "r"))]
    rbits: NonZeroU8,
    #[cfg_attr(
        feature = "serde",
        serde(rename = "g", skip_serializing_if = "Option::is_none", default)
    )]
    max_qbits: Option<NonZeroU8>,
}

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// The filter cannot fit another fingerprint
    CapacityExceeded,
    /// The fingerprint sizes are not compatible
    IncompatibleFingerprintSize,
    /// The specified filter cannot be constructed with 64 bit hashes
    NotEnoughFingerprintBits,
    /// Capacity is too large. Filter::MAX_CAPACITY = 2^59 * 19 / 20.
    CapacityTooLarge,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

impl std::error::Error for Error {}

#[derive(Debug)]
struct Block {
    offset: u64,
    occupieds: u64,
    runends: u64,
}

trait BitExt {
    fn is_bit_set(&self, i: usize) -> bool;
    fn set_bit(&mut self, i: usize);
    fn clear_bit(&mut self, i: usize);
    fn shift_right(&self, bits: usize, b: &Self, b_start: usize, b_end: usize) -> Self;
    fn shift_left(&self, bits: usize, b: &Self, b_start: usize, b_end: usize) -> Self;
    /// Number of set bits (1s) in the range
    fn popcnt(&self, range: impl RangeBounds<u64>) -> u64;
    /// Index of nth set bits in the range
    fn select(&self, range: RangeFrom<u64>, n: u64) -> Option<u64>;

    #[inline]
    fn update_bit(&mut self, i: usize, value: bool) {
        if value {
            self.set_bit(i)
        } else {
            self.clear_bit(i)
        }
    }
}

impl BitExt for u64 {
    #[inline]
    fn is_bit_set(&self, i: usize) -> bool {
        (*self & (1 << i)) != 0
    }

    #[inline]
    fn set_bit(&mut self, i: usize) {
        *self |= 1 << i
    }

    #[inline]
    fn clear_bit(&mut self, i: usize) {
        *self &= !(1 << i)
    }

    #[inline]
    fn shift_right(&self, bits: usize, b: &Self, b_start: usize, b_end: usize) -> Self {
        let bitmask = |n| !u64::MAX.checked_shl(n).unwrap_or(0);
        let a_component = *self >> (64 - bits); // select the highest `bits` from A to become lowest
        let b_shifted_mask = bitmask((b_end - b_start) as u32) << b_start;
        let b_shifted = ((b_shifted_mask & b) << bits) & b_shifted_mask;
        let b_mask = !b_shifted_mask;

        a_component | b_shifted | (b & b_mask)
    }

    #[inline]
    fn shift_left(&self, bits: usize, b: &Self, b_start: usize, b_end: usize) -> Self {
        let bitmask = |n| !u64::MAX.checked_shl(n).unwrap_or(0);
        let a_component = *self << (64 - bits); // select the lowest `bits` from A to become highest
        let b_shifted_mask = bitmask((b_end - b_start) as u32) << b_start;
        let b_shifted = ((b_shifted_mask & b) >> bits) & b_shifted_mask;
        let b_mask = !b_shifted_mask;

        a_component | b_shifted | (b & b_mask)
    }

    #[inline]
    fn popcnt(&self, range: impl RangeBounds<u64>) -> u64 {
        let mut v = match range.start_bound() {
            std::ops::Bound::Included(&i) => *self >> i << i,
            std::ops::Bound::Excluded(&i) => *self >> (i + 1) << (i + 1),
            _ => *self,
        };
        v = match range.end_bound() {
            std::ops::Bound::Included(&i) if i < 63 => v & ((2 << i) - 1),
            std::ops::Bound::Excluded(&i) if i <= 63 => v & ((1 << i) - 1),
            _ => v,
        };

        #[cfg(all(
            target_arch = "x86_64",
            not(feature = "legacy_x86_64_support"),
            not(target_feature = "popcnt")
        ))]
        let result = unsafe {
            // Using intrinsics introduce a function call, and the resulting code
            // ends up slower than the inline assembly below.
            // Any calls to is_x86_feature_detected also significantly affect performance.
            // Given this is available on all x64 cpus starting 2008 we assume it's present
            // (unless legacy_x86_64_support is set) and panic elsewhere otherwise.
            let popcnt;
            std::arch::asm!(
                "popcnt {popcnt}, {v}",
                v = in(reg) v,
                popcnt = out(reg) popcnt,
                options(pure, nomem, nostack)
            );
            popcnt
        };
        #[cfg(any(
            not(target_arch = "x86_64"),
            feature = "legacy_x86_64_support",
            target_feature = "popcnt"
        ))]
        let result = v.count_ones() as u64;

        result
    }

    #[inline]
    fn select(&self, range: RangeFrom<u64>, n: u64) -> Option<u64> {
        debug_assert!(range.start < 64);
        let v = *self >> range.start << range.start;

        #[cfg_attr(target_arch = "x86_64", cold)]
        #[cfg_attr(not(target_arch = "x86_64"), inline)]
        fn fallback(mut v: u64, n: u64) -> Option<u64> {
            for _ in 0..n / 8 {
                for _ in 0..8 {
                    v &= v.wrapping_sub(1); // remove the least significant bit
                }
            }
            for _ in 0..n % 8 {
                v &= v.wrapping_sub(1); // remove the least significant bit
            }

            if v == 0 {
                None
            } else {
                Some(v.trailing_zeros() as u64)
            }
        }

        #[cfg(target_arch = "x86_64")]
        let result = {
            // TODO: AMD CPUs up to Zen2 have slow BMI implementations
            if std::is_x86_feature_detected!("bmi2") {
                // This is the equivalent intrinsics version of the inline assembly below.
                // #[target_feature(enable = "bmi1")]
                // #[target_feature(enable = "bmi2")]
                // #[inline]
                // unsafe fn select_bmi2(x: u64, k: u64) -> Option<u64> {
                //     use std::arch::x86_64::{_pdep_u64, _tzcnt_u64};
                //     let result = _tzcnt_u64(_pdep_u64(1 << k, x));
                //     if result != 64 {
                //         Some(result)
                //     } else {
                //         None
                //     }
                // }
                // unsafe { select_bmi2(v, n) }

                let result: u64;
                unsafe {
                    std::arch::asm!(
                        "mov     {tmp}, 1",
                        "shlx    {tmp}, {tmp}, {n}",
                        "pdep    {tmp}, {tmp}, {v}",
                        "tzcnt   {tmp}, {tmp}",
                        n = in(reg) n,
                        v = in(reg) v,
                        tmp = out(reg) result,
                        options(pure, nomem, nostack)
                    );
                }
                if result != 64 {
                    Some(result)
                } else {
                    None
                }
            } else {
                fallback(v, n)
            }
        };
        #[cfg(not(target_arch = "x86_64"))]
        let result = fallback(v, n);

        result
    }
}

trait CastNonZeroU8 {
    fn u64(&self) -> u64;
    fn usize(&self) -> usize;
}

impl CastNonZeroU8 for NonZeroU8 {
    #[inline]
    fn u64(&self) -> u64 {
        self.get() as u64
    }

    #[inline]
    fn usize(&self) -> usize {
        self.get() as usize
    }
}

/// An iterator over the fingerprints of a `Filter`.
pub struct FingerprintIter<'a> {
    filter: &'a Filter,
    q_bucket_idx: u64,
    r_bucket_idx: u64,
    remaining: u64,
}

impl<'a> FingerprintIter<'a> {
    fn new(filter: &'a Filter) -> Self {
        let mut iter = FingerprintIter {
            filter,
            q_bucket_idx: 0,
            r_bucket_idx: 0,
            remaining: filter.len,
        };
        if !filter.is_empty() {
            while !filter.is_occupied(iter.q_bucket_idx) {
                iter.q_bucket_idx += 1;
            }
            iter.r_bucket_idx = filter.run_start(iter.q_bucket_idx);
        }
        iter
    }
}

impl Iterator for FingerprintIter<'_> {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(r) = self.remaining.checked_sub(1) {
            self.remaining = r;
        } else {
            return None;
        }
        let hash = (self.q_bucket_idx << self.filter.rbits.get())
            | self.filter.get_remainder(self.r_bucket_idx);

        if self.filter.is_runend(self.r_bucket_idx) {
            self.q_bucket_idx += 1;
            while !self.filter.is_occupied(self.q_bucket_idx) {
                self.q_bucket_idx += 1;
            }
            self.r_bucket_idx = (self.r_bucket_idx + 1).max(self.q_bucket_idx);
        } else {
            self.r_bucket_idx += 1;
        }

        Some(hash)
    }
}

impl Filter {
    /// Maximum log2 number of slots that can be used in the filter.
    /// Effectively, the largest power of 2 that can be multiplied by 19 without overflowing u64.
    const MAX_QBITS: u8 = 59;

    /// Maximum number of items that can be stored in the filter: ceil(2^59 * 19 / 20)
    pub const MAX_CAPACITY: u64 = (2u64.pow(Self::MAX_QBITS as u32) * 19).div_ceil(20);

    /// Creates a new filter that can hold at least `capacity` items
    /// and with a desired error rate of `fp_rate` (clamped to (0, 0.5]).
    ///
    /// Errors if capacity is too large if the specified filter isn't achievable using 64 bit hashes.
    #[inline]
    pub fn new(capacity: u64, fp_rate: f64) -> Result<Self, Error> {
        Self::new_resizeable(capacity, capacity, fp_rate)
    }

    /// Calculates the number of slots needed to fit the desired fingerprints with 95% occupation.
    /// Returns the number of slots needed rounded to the next power of two, but always >= 64.
    fn calculate_needed_slots(desired: u64) -> Result<u64, Error> {
        let mut slots = desired
            .checked_next_power_of_two()
            .ok_or(Error::CapacityTooLarge)?
            .max(64);
        loop {
            let capacity = slots
                .checked_mul(19)
                .ok_or(Error::CapacityTooLarge)?
                .div_ceil(20);
            if capacity >= desired {
                return Ok(slots);
            }
            slots = slots.checked_mul(2).ok_or(Error::CapacityTooLarge)?;
        }
    }

    /// Creates a new filter that can hold at least `initial_capacity` items initially
    /// and can resize to hold at least `max_capacity` when fully grown.
    /// The desired error rate `fp_rate` (clamped to (0, 0.5]) applies to the fully grown filter.
    ///
    /// This works by storing fingerprints large enough to satisfy the maximum requirements,
    /// so smaller filters will actually have lower error rates, which will increase
    /// (up to `fp_rate`) as the filter grows. In practice every time the filter doubles in
    /// capacity its error rate also doubles.
    ///
    /// Errors if max_capacity is too large or if the specified filter isn't achievable using 64 bit hashes.
    pub fn new_resizeable(
        initial_capacity: u64,
        max_capacity: u64,
        fp_rate: f64,
    ) -> Result<Self, Error> {
        assert!(max_capacity >= initial_capacity);
        let slots_for_capacity = Self::calculate_needed_slots(initial_capacity)?;
        let qbits = slots_for_capacity.trailing_zeros() as u8;
        let slots_for_max_capacity = Self::calculate_needed_slots(max_capacity)?;
        let max_qbits = slots_for_max_capacity.trailing_zeros() as u8;
        let fp_rate = fp_rate.clamp(f64::MIN_POSITIVE, 0.5);
        let rbits = (-fp_rate.log2()).round().max(1.0) as u8 + (max_qbits - qbits);
        let mut result = Self::with_qr(qbits.try_into().unwrap(), rbits.try_into().unwrap())?;
        if max_qbits > qbits {
            result.max_qbits = Some(max_qbits.try_into().unwrap());
        }
        Ok(result)
    }

    /// Creates a new resizeable filter that can hold at least `initial_capacity` items initially while
    /// utilizing a fingerprint bit size of `fingerprint_bits` (7..=64). Normally this function is only
    /// useful if the filter is being used to manually store fingerprints.
    pub fn with_fingerprint_size(
        initial_capacity: u64,
        fingerprint_bits: u8,
    ) -> Result<Filter, Error> {
        if !(7..=64).contains(&fingerprint_bits) {
            return Err(Error::NotEnoughFingerprintBits);
        }
        let slots_for_capacity = Self::calculate_needed_slots(initial_capacity)?;
        let qbits = slots_for_capacity.trailing_zeros() as u8;
        if fingerprint_bits <= qbits {
            return Err(Error::NotEnoughFingerprintBits);
        }
        let rbits = fingerprint_bits - qbits;
        let mut result = Self::with_qr(qbits.try_into().unwrap(), rbits.try_into().unwrap())?;
        if rbits > 1 {
            result.max_qbits = Some((qbits + rbits - 1).min(Self::MAX_QBITS).try_into().unwrap());
        }
        Ok(result)
    }

    fn with_qr(qbits: NonZeroU8, rbits: NonZeroU8) -> Result<Filter, Error> {
        Self::check_cpu_support();
        if qbits.get() + rbits.get() > 64 {
            return Err(Error::NotEnoughFingerprintBits);
        }
        let num_slots = 1 << qbits.get();
        let num_blocks = num_slots / 64;
        assert_ne!(num_blocks, 0);
        let block_bytes_size = 1 + 16 + 64 * rbits.u64() / 8;
        let buffer_bytes = num_blocks * block_bytes_size;
        let buffer = vec![0u8; buffer_bytes.try_into().unwrap()].into_boxed_slice();
        Ok(Self {
            buffer,
            qbits,
            rbits,
            len: 0,
            max_qbits: None,
        })
    }

    fn check_cpu_support() {
        #[cfg(all(
            target_arch = "x86_64",
            not(feature = "legacy_x86_64_support"),
            not(target_feature = "popcnt")
        ))]
        assert!(
            std::is_x86_feature_detected!("popcnt"),
            "CPU doesn't support the popcnt instruction"
        );
    }

    /// The internal fingerprint size in bits.
    #[inline]
    pub fn fingerprint_size(&self) -> u8 {
        self.qbits.get() + self.rbits.get()
    }

    /// Whether the filter is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Current number of fingerprints admitted to the filter.
    #[inline]
    pub fn len(&self) -> u64 {
        self.len
    }

    /// Current memory usage in bytes.
    #[inline]
    pub fn memory_usage(&self) -> usize {
        self.buffer.len()
    }

    /// Resets/Clears the filter.
    pub fn clear(&mut self) {
        self.buffer.fill(0);
        self.len = 0;
    }

    /// Maximum filter capacity.
    #[inline]
    pub fn capacity_resizeable(&self) -> u64 {
        // Overflow is not possible here as it'd have overflowed in the constructor.
        ((1u64 << self.max_qbits.unwrap_or(self.qbits).get()) * 19).div_ceil(20)
    }

    /// Current filter capacity.
    #[inline]
    pub fn capacity(&self) -> u64 {
        if cfg!(fuzzing) {
            // 100% occupancy is not realistic but stresses the algorithm much more.
            // To generate real counter examples this "pessimisation" must be removed.
            self.total_buckets().get()
        } else {
            // Up to 95% occupancy
            // 19/20 == 0.95
            // Overflow is not possible here as it'd have overflowed in the constructor.
            (self.total_buckets().get() * 19).div_ceil(20)
        }
    }

    /// Max error ratio when at the resizeable capacity (len == resizeable_capacity).
    pub fn max_error_ratio_resizeable(&self) -> f64 {
        let extra_rbits = self.max_qbits.unwrap_or(self.qbits).get() - self.qbits.get();
        2f64.powi(-((self.rbits.get() - extra_rbits) as i32))
    }

    /// Max error ratio when at full capacity (len == capacity).
    pub fn max_error_ratio(&self) -> f64 {
        2f64.powi(-(self.rbits.get() as i32))
    }

    /// Current error ratio at the current occupancy.
    pub fn current_error_ratio(&self) -> f64 {
        let occupancy = self.len as f64 / self.total_buckets().get() as f64;
        1.0 - std::f64::consts::E.powf(-occupancy / 2f64.powi(self.rbits.get() as i32))
    }

    #[inline]
    fn block_byte_size(&self) -> usize {
        1 + 8 + 8 + 64 * self.rbits.usize() / 8
    }

    #[inline]
    fn set_block_runends(&mut self, block_num: u64, runends: u64) {
        let block_num = block_num % self.total_blocks();
        let block_start = block_num as usize * self.block_byte_size();
        let block_bytes: &mut [u8; 1 + 8 + 8] = (&mut self.buffer[block_start..][..1 + 8 + 8])
            .try_into()
            .unwrap();
        block_bytes[1 + 8..1 + 8 + 8].copy_from_slice(&runends.to_le_bytes());
    }

    #[inline]
    fn raw_block(&self, block_num: u64) -> Block {
        let block_num = block_num % self.total_blocks();
        let block_start = block_num as usize * self.block_byte_size();
        let block_bytes: &[u8; 1 + 8 + 8] =
            &self.buffer[block_start..][..1 + 8 + 8].try_into().unwrap();
        Block {
            offset: block_bytes[0] as u64,
            occupieds: u64::from_le_bytes(block_bytes[1..1 + 8].try_into().unwrap()),
            runends: u64::from_le_bytes(block_bytes[1 + 8..1 + 8 + 8].try_into().unwrap()),
        }
    }

    #[inline]
    fn block(&self, block_num: u64) -> Block {
        let block_num = block_num % self.total_blocks();
        let block_start = block_num as usize * self.block_byte_size();
        let block_bytes: &[u8; 1 + 8 + 8] =
            &self.buffer[block_start..][..1 + 8 + 8].try_into().unwrap();
        let offset = {
            if block_bytes[0] < u8::MAX {
                block_bytes[0] as u64
            } else {
                self.calc_offset(block_num)
            }
        };
        Block {
            offset,
            occupieds: u64::from_le_bytes(block_bytes[1..1 + 8].try_into().unwrap()),
            runends: u64::from_le_bytes(block_bytes[1 + 8..1 + 8 + 8].try_into().unwrap()),
        }
    }

    #[inline]
    fn adjust_block_offset(&mut self, block_num: u64, inc: bool) {
        let block_num = block_num % self.total_blocks();
        let block_start = block_num as usize * self.block_byte_size();
        let offset = &mut self.buffer[block_start];
        if inc {
            *offset = offset.saturating_add(1);
        } else if *offset != u8::MAX {
            *offset -= 1;
        } else {
            self.buffer[block_start] = self.calc_offset(block_num).try_into().unwrap_or(u8::MAX);
        }
    }

    #[inline]
    fn inc_offsets(&mut self, start_bucket: u64, end_bucket: u64) {
        let original_block = start_bucket / 64;
        let mut last_affected_block = end_bucket / 64;
        if end_bucket < start_bucket {
            last_affected_block += self.total_blocks().get();
        }
        for b in original_block + 1..=last_affected_block {
            self.adjust_block_offset(b, true);
        }
    }

    #[inline]
    fn dec_offsets(&mut self, start_bucket: u64, end_bucket: u64) {
        let original_block = start_bucket / 64;
        let mut last_affected_block = end_bucket / 64;
        if end_bucket < start_bucket {
            last_affected_block += self.total_blocks().get();
        }

        // As an edge case we may decrement the offsets of 2+ blocks and the block B' offset
        // may be saturated and depend on a previous Block B" with a non saturated offset.
        // But B" offset may also(!) be affected by the decremented operation, so we must
        // decrement B" offset first before the remaining offsets.
        if last_affected_block - original_block >= 2
            && self.raw_block(original_block + 1).offset >= u8::MAX as u64
        {
            // last affected block offset is always <= 64 (BLOCK SIZE)
            // otherwise the decrement operation would be to affecting a subsequent block
            debug_assert!(self.raw_block(last_affected_block).offset <= 64);
            self.adjust_block_offset(last_affected_block, false);
            last_affected_block -= 1;
        }
        for b in original_block + 1..=last_affected_block {
            self.adjust_block_offset(b, false);
        }

        #[cfg(fuzzing)]
        self.validate_offsets(original_block, last_affected_block);
    }

    #[cfg(any(fuzzing, test))]
    fn validate_offsets(&mut self, original_block: u64, last_affected_block: u64) {
        for b in original_block..=last_affected_block {
            let raw_offset = self.raw_block(b).offset;
            let offset = self.calc_offset(b);
            debug_assert!(
                (raw_offset >= u8::MAX as u64 && offset >= u8::MAX as u64)
                    || (offset == raw_offset),
                "block {} offset {} calc {}",
                b,
                raw_offset,
                offset,
            );
        }
    }

    #[inline(always)]
    fn is_occupied(&self, hash_bucket_idx: u64) -> bool {
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let block_start = (hash_bucket_idx / 64) as usize * self.block_byte_size();
        let occupieds = u64::from_le_bytes(self.buffer[block_start + 1..][..8].try_into().unwrap());
        occupieds.is_bit_set((hash_bucket_idx % 64) as usize)
    }

    #[inline(always)]
    fn set_occupied(&mut self, hash_bucket_idx: u64, value: bool) {
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let block_start = (hash_bucket_idx / 64) as usize * self.block_byte_size();
        let mut occupieds =
            u64::from_le_bytes(self.buffer[block_start + 1..][..8].try_into().unwrap());
        occupieds.update_bit((hash_bucket_idx % 64) as usize, value);
        self.buffer[block_start + 1..][..8].copy_from_slice(&occupieds.to_le_bytes());
    }

    #[inline(always)]
    fn is_runend(&self, hash_bucket_idx: u64) -> bool {
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let block_start = (hash_bucket_idx / 64) as usize * self.block_byte_size();
        let runends =
            u64::from_le_bytes(self.buffer[block_start + 1 + 8..][..8].try_into().unwrap());
        runends.is_bit_set((hash_bucket_idx % 64) as usize)
    }

    #[inline(always)]
    fn set_runend(&mut self, hash_bucket_idx: u64, value: bool) {
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let block_start = (hash_bucket_idx / 64) as usize * self.block_byte_size();
        let mut runends =
            u64::from_le_bytes(self.buffer[block_start + 1 + 8..][..8].try_into().unwrap());
        runends.update_bit((hash_bucket_idx % 64) as usize, value);
        self.buffer[block_start + 1 + 8..][..8].copy_from_slice(&runends.to_le_bytes());
    }

    #[inline(always)]
    fn get_remainder(&self, hash_bucket_idx: u64) -> u64 {
        debug_assert!(self.rbits.get() > 0 && self.rbits.get() < 64);
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let remainders_start = (hash_bucket_idx / 64) as usize * self.block_byte_size() + 1 + 8 + 8;
        let start_bit_idx = self.rbits.usize() * (hash_bucket_idx % 64) as usize;
        let end_bit_idx = start_bit_idx + self.rbits.usize();
        let start_u64 = start_bit_idx / 64;
        let num_rem_parts = 1 + (end_bit_idx > (start_u64 + 1) * 64) as usize;
        let rem_parts_bytes = &self.buffer[remainders_start + start_u64 * 8..][..num_rem_parts * 8];
        let extra_low = start_bit_idx - start_u64 * 64;
        let extra_high = ((start_u64 + 1) * 64).saturating_sub(end_bit_idx);
        let rem_part = u64::from_le_bytes(rem_parts_bytes[..8].try_into().unwrap());
        // zero high bits & truncate low bits
        let mut remainder = (rem_part << extra_high) >> (extra_high + extra_low);
        if let Some(rem_part) = rem_parts_bytes.get(8..16) {
            let remaining_bits = end_bit_idx - (start_u64 + 1) * 64;
            let rem_part = u64::from_le_bytes(rem_part.try_into().unwrap());
            remainder |=
                (rem_part & !(u64::MAX << remaining_bits)) << (self.rbits.usize() - remaining_bits);
        }
        debug_assert!(remainder.leading_zeros() >= 64 - self.rbits.get() as u32);
        remainder
    }

    #[inline(always)]
    fn set_remainder(&mut self, hash_bucket_idx: u64, remainder: u64) {
        debug_assert!(self.rbits.get() > 0 && self.rbits.get() < 64);
        debug_assert!(remainder.leading_zeros() >= 64 - self.rbits.get() as u32);
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let remainders_start = (hash_bucket_idx / 64) as usize * self.block_byte_size() + 1 + 8 + 8;
        let start_bit_idx = self.rbits.usize() * (hash_bucket_idx % 64) as usize;
        let end_bit_idx = start_bit_idx + self.rbits.usize();
        let start_u64 = start_bit_idx / 64;
        let num_rem_parts = 1 + (end_bit_idx > (start_u64 + 1) * 64) as usize;
        let rem_parts_bytes =
            &mut self.buffer[remainders_start + start_u64 * 8..][..num_rem_parts * 8];
        let mut rem_part = u64::from_le_bytes(rem_parts_bytes[..8].try_into().unwrap());
        let extra_low = start_bit_idx - start_u64 * 64;
        let extra_high = ((start_u64 + 1) * 64).saturating_sub(end_bit_idx);
        // zero region we'll copy remainder bits in
        rem_part &= !((u64::MAX << extra_low) & (u64::MAX >> extra_high));
        let low_bits_to_copy = 64 - extra_high - extra_low;
        rem_part |= (remainder & !(u64::MAX << low_bits_to_copy)) << extra_low;
        rem_parts_bytes[..8].copy_from_slice(&rem_part.to_le_bytes());
        if rem_parts_bytes.len() < 16 {
            return;
        }

        let remaining_bits = end_bit_idx - (start_u64 + 1) * 64;
        rem_part = u64::from_le_bytes(rem_parts_bytes[8..16].try_into().unwrap());
        // zero region we'll copy remainder bits in
        rem_part &= u64::MAX << remaining_bits;
        rem_part |= remainder >> (self.rbits.usize() - remaining_bits);
        rem_parts_bytes[8..16].copy_from_slice(&rem_part.to_le_bytes());
    }

    #[inline]
    fn get_rem_u64(&self, rem_u64: u64) -> u64 {
        let rbits = NonZeroU64::from(self.rbits);
        let bucket_block_idx = (rem_u64 / rbits) % self.total_blocks();
        let bucket_rem_u64 = (rem_u64 % rbits) as usize;
        let bucket_rem_start = (bucket_block_idx as usize * self.block_byte_size()) + 1 + 8 + 8;
        u64::from_le_bytes(
            self.buffer[bucket_rem_start + bucket_rem_u64 * 8..][..8]
                .try_into()
                .unwrap(),
        )
    }

    #[inline]
    fn set_rem_u64(&mut self, rem_u64: u64, rem: u64) {
        let rbits = NonZeroU64::from(self.rbits);
        let bucket_block_idx = (rem_u64 / rbits) % self.total_blocks();
        let bucket_rem_u64 = (rem_u64 % rbits) as usize;
        let bucket_rem_start = (bucket_block_idx as usize * self.block_byte_size()) + 1 + 8 + 8;
        self.buffer[bucket_rem_start + bucket_rem_u64 * 8..][..8]
            .copy_from_slice(&rem.to_le_bytes());
    }

    fn shift_remainders_by_1(&mut self, start: u64, end_inc: u64) {
        let end = if end_inc < start {
            end_inc + self.total_buckets().get() + 1
        } else {
            end_inc + 1
        };
        let mut end_u64 = end * self.rbits.u64() / 64;
        let mut bend = (end * self.rbits.u64() % 64) as usize;
        let start_u64 = start * self.rbits.u64() / 64;
        let bstart = (start * self.rbits.u64() % 64) as usize;
        while end_u64 != start_u64 {
            let prev_rem_u64 = self.get_rem_u64(end_u64 - 1);
            let mut rem_u64 = self.get_rem_u64(end_u64);
            rem_u64 = prev_rem_u64.shift_right(self.rbits.usize(), &rem_u64, 0, bend);
            self.set_rem_u64(end_u64, rem_u64);
            end_u64 -= 1;
            bend = 64;
        }
        let mut rem_u64 = self.get_rem_u64(start_u64);
        rem_u64 = 0u64.shift_right(self.rbits.usize(), &rem_u64, bstart, bend);
        self.set_rem_u64(start_u64, rem_u64);
    }

    fn shift_remainders_back_by_1(&mut self, start: u64, end_inc: u64) {
        let end = if end_inc < start {
            end_inc + self.total_buckets().get() + 1
        } else {
            end_inc + 1
        };
        let end_u64 = end * self.rbits.u64() / 64;
        let bend = (end * self.rbits.u64() % 64) as usize;
        let mut start_u64 = start * self.rbits.u64() / 64;
        let mut bstart = (start * self.rbits.u64() % 64) as usize;
        while end_u64 != start_u64 {
            let next_rem_u64 = self.get_rem_u64(start_u64 + 1);
            let mut rem_u64 = self.get_rem_u64(start_u64);
            rem_u64 = next_rem_u64.shift_left(self.rbits.usize(), &rem_u64, bstart, 64);
            self.set_rem_u64(start_u64, rem_u64);
            start_u64 += 1;
            bstart = 0;
        }
        let mut rem_u64 = self.get_rem_u64(end_u64);
        rem_u64 = 0u64.shift_left(self.rbits.usize(), &rem_u64, bstart, bend);
        self.set_rem_u64(end_u64, rem_u64);
    }

    fn shift_runends_by_1(&mut self, start: u64, end_inc: u64) {
        let end = if end_inc < start {
            end_inc + self.total_buckets().get() + 1
        } else {
            end_inc + 1
        };
        let mut end_block = end / 64;
        let mut bend = (end % 64) as usize;
        let start_block = start / 64;
        let bstart = (start % 64) as usize;
        while end_block != start_block {
            let prev_block_runends = self.raw_block(end_block - 1).runends;
            let mut block_runends = self.raw_block(end_block).runends;
            block_runends = prev_block_runends.shift_right(1, &block_runends, 0, bend);
            self.set_block_runends(end_block, block_runends);
            end_block -= 1;
            bend = 64;
        }
        let mut block_runends = self.raw_block(start_block).runends;
        block_runends = 0u64.shift_right(1, &block_runends, bstart, bend);
        self.set_block_runends(start_block, block_runends);
    }

    fn shift_runends_back_by_1(&mut self, start: u64, end_inc: u64) {
        let end = if end_inc < start {
            end_inc + self.total_buckets().get() + 1
        } else {
            end_inc + 1
        };
        let end_block = end / 64;
        let bend = (end % 64) as usize;
        let mut start_block = start / 64;
        let mut bstart = (start % 64) as usize;
        while start_block != end_block {
            let next_block_runends = self.raw_block(start_block + 1).runends;
            let mut block_runends = self.raw_block(start_block).runends;
            block_runends = next_block_runends.shift_left(1, &block_runends, bstart, 64);
            self.set_block_runends(start_block, block_runends);
            start_block += 1;
            bstart = 0;
        }
        let mut block_runends = self.raw_block(end_block).runends;
        block_runends = 0u64.shift_left(1, &block_runends, bstart, bend);
        self.set_block_runends(end_block, block_runends);
    }

    #[cold]
    #[inline(never)]
    fn calc_offset(&self, block_num: u64) -> u64 {
        // The block offset can be calculated as the difference between its position and runstart.
        let block_start = (block_num * 64) % self.total_buckets();
        let mut run_start = self.run_start(block_start);
        if run_start < block_start {
            run_start += self.total_buckets().get();
        }
        run_start - block_start
    }

    /// Start idx of of the run (inclusive)
    #[inline]
    fn run_start(&self, hash_bucket_idx: u64) -> u64 {
        // runstart is equivalent to the runend of the previous bucket + 1.
        let prev_bucket = hash_bucket_idx.wrapping_sub(1) % self.total_buckets();
        (self.run_end(prev_bucket) + 1) % self.total_buckets()
    }

    /// End idx of the end of the run (inclusive).
    fn run_end(&self, hash_bucket_idx: u64) -> u64 {
        let hash_bucket_idx = hash_bucket_idx % self.total_buckets();
        let bucket_block_idx = hash_bucket_idx / 64;
        let bucket_intrablock_offset = hash_bucket_idx % 64;
        let bucket_block = self.block(bucket_block_idx);
        let bucket_intrablock_rank = bucket_block.occupieds.popcnt(..=bucket_intrablock_offset);
        // No occupied buckets all the way to bucket_intrablock_offset
        // which also means hash_bucket_idx isn't occupied
        if bucket_intrablock_rank == 0 {
            return if bucket_block.offset <= bucket_intrablock_offset {
                // hash_bucket_idx points to an empty bucket unaffected by block offset,
                // thus end == start
                hash_bucket_idx
            } else {
                // hash_bucket_idx fall within the section occupied by the offset,
                // thus end == last bucket of offset section
                (bucket_block_idx * 64 + bucket_block.offset - 1) % self.total_buckets()
            };
        }

        // Must search runends to figure out the end of the run
        let mut runend_block_idx = bucket_block_idx + bucket_block.offset / 64;
        let mut runend_ignore_bits = bucket_block.offset % 64;
        let mut runend_block = self.raw_block(runend_block_idx);
        // Try to find the runend for the bucket in this block.
        // We're looking for the runend_rank'th bit set (0 based)
        let mut runend_rank = bucket_intrablock_rank - 1;
        let mut runend_block_offset = runend_block
            .runends
            .select(runend_ignore_bits.., runend_rank);

        if let Some(runend_block_offset) = runend_block_offset {
            let runend_idx = runend_block_idx * 64 + runend_block_offset;
            return runend_idx.max(hash_bucket_idx) % self.total_buckets();
        }
        // There were not enough runend bits set, keep looking...
        loop {
            // subtract any runend bits found
            runend_rank -= runend_block.runends.popcnt(runend_ignore_bits..);
            // move to the next block
            runend_block_idx += 1;
            runend_ignore_bits = 0;
            runend_block = self.raw_block(runend_block_idx);
            runend_block_offset = runend_block
                .runends
                .select(runend_ignore_bits.., runend_rank);

            if let Some(runend_block_offset) = runend_block_offset {
                let runend_idx = runend_block_idx * 64 + runend_block_offset;
                return runend_idx.max(hash_bucket_idx) % self.total_buckets();
            }
        }
    }

    /// Returns whether item is present (probabilistically) in the filter.
    pub fn contains<T: Hash>(&self, item: T) -> bool {
        self.contains_fingerprint(self.hash(item))
    }

    /// Returns whether the fingerprint is present (probabilistically) in the filter.
    pub fn contains_fingerprint(&self, hash: u64) -> bool {
        let (hash_bucket_idx, hash_remainder) = self.calc_qr(hash);
        if !self.is_occupied(hash_bucket_idx) {
            return false;
        }
        let mut runstart_idx = self.run_start(hash_bucket_idx);
        loop {
            if hash_remainder == self.get_remainder(runstart_idx) {
                return true;
            }
            if self.is_runend(runstart_idx) {
                return false;
            }
            runstart_idx += 1;
        }
    }

    /// Returns the number of times the item appears (probabilistically) in the filter.
    pub fn count<T: Hash>(&mut self, item: T) -> u64 {
        self.count_fingerprint(self.hash(item))
    }

    /// Returns the amount of times the fingerprint appears (probabilistically) in the filter.
    pub fn count_fingerprint(&mut self, hash: u64) -> u64 {
        let (hash_bucket_idx, hash_remainder) = self.calc_qr(hash);
        if !self.is_occupied(hash_bucket_idx) {
            return 0;
        }

        let mut count = 0u64;
        let mut runstart_idx = self.run_start(hash_bucket_idx);
        loop {
            if hash_remainder == self.get_remainder(runstart_idx) {
                count += 1;
            }
            if self.is_runend(runstart_idx) {
                return count;
            }
            runstart_idx += 1;
        }
    }

    #[inline]
    fn offset_lower_bound(&self, hash_bucket_idx: u64) -> u64 {
        let bucket_block_idx = hash_bucket_idx / 64;
        let bucket_intrablock_offset = hash_bucket_idx % 64;
        let bucket_block = self.raw_block(bucket_block_idx);
        let num_occupied = bucket_block.occupieds.popcnt(..=bucket_intrablock_offset);
        if bucket_block.offset <= bucket_intrablock_offset {
            num_occupied
                - bucket_block
                    .runends
                    .popcnt(bucket_block.offset..bucket_intrablock_offset)
        } else {
            bucket_block.offset + num_occupied - bucket_intrablock_offset
        }
    }

    fn find_first_empty_slot(&self, mut hash_bucket_idx: u64) -> u64 {
        loop {
            let olb = self.offset_lower_bound(hash_bucket_idx);
            if olb == 0 {
                return hash_bucket_idx % self.total_buckets();
            }
            hash_bucket_idx += olb;
        }
    }

    fn find_first_not_shifted_slot(&self, mut hash_bucket_idx: u64) -> u64 {
        loop {
            let run_end = self.run_end(hash_bucket_idx);
            if run_end == hash_bucket_idx {
                return hash_bucket_idx;
            }
            hash_bucket_idx = run_end;
        }
    }

    /// Removes `item` from the filter.
    /// Returns whether item was actually found and removed.
    ///
    /// Note that removing an item who wasn't previously added to the filter
    /// may introduce **false negatives**. This is because it could be removing
    /// fingerprints from a colliding item!
    pub fn remove<T: Hash>(&mut self, item: T) -> bool {
        self.remove_fingerprint(self.hash(item))
    }

    /// Removes the fingerprint specified by `hash` was from the filter.
    /// Returns whether a fingerprint was actually found and removed.
    ///
    /// Note that removing a fingerprint that wasn't previously added to the filter
    /// may introduce false negatives. This is because it could be removing
    /// fingerprints from a colliding hash!
    pub fn remove_fingerprint(&mut self, hash: u64) -> bool {
        let (hash_bucket_idx, hash_remainder) = self.calc_qr(hash);
        if !self.is_occupied(hash_bucket_idx) {
            return false;
        }
        let mut run_start = self.run_start(hash_bucket_idx);
        // adjust run_start so we can have
        // hash_bucket_idx <= run_start <= found_idx <= run_end
        if run_start < hash_bucket_idx {
            run_start += self.total_buckets().get();
        }
        let mut run_end = run_start;
        let mut found_idx = None;
        let found_idx = loop {
            if hash_remainder == self.get_remainder(run_end) {
                found_idx = Some(run_end);
            }
            if self.is_runend(run_end) {
                if let Some(i) = found_idx {
                    break i;
                } else {
                    return false;
                };
            }
            run_end += 1;
        };

        let mut last_bucket_shifted_run_end = run_end;
        if last_bucket_shifted_run_end != hash_bucket_idx {
            last_bucket_shifted_run_end = self.find_first_not_shifted_slot(run_end);
            if last_bucket_shifted_run_end < run_end {
                last_bucket_shifted_run_end += self.total_buckets().get();
            }
        }

        // run_end points to the end of the run (inc) which contains the target remainder (found_idx)
        // If we had a single remainder in the run the run is no more
        if run_end == run_start {
            self.set_occupied(hash_bucket_idx, false);
        } else {
            // More than one remainder in the run.
            // If the removed rem is the last one in the run
            // the before last remainder becomes the new runend.
            if found_idx == run_end {
                self.set_runend(run_end - 1, true);
            }
        }
        if found_idx != last_bucket_shifted_run_end {
            self.set_remainder(found_idx, 0);
            self.shift_remainders_back_by_1(found_idx, last_bucket_shifted_run_end);
            self.shift_runends_back_by_1(found_idx, last_bucket_shifted_run_end);
        }
        self.set_runend(last_bucket_shifted_run_end, false);
        self.set_remainder(last_bucket_shifted_run_end, 0);
        self.dec_offsets(hash_bucket_idx, last_bucket_shifted_run_end);
        self.len -= 1;
        true
    }

    /// Inserts `item` in the filter, even if already appears to be in the filter.
    /// This works by inserting a possibly duplicated fingerprint in the filter.
    ///
    /// This function should be used when the filter is also subject to removals
    /// and the item is known to not have been added to the filter before (or was removed).
    ///
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot admit the new item.
    #[inline]
    pub fn insert_duplicated<T: Hash>(&mut self, item: T) -> Result<(), Error> {
        self.insert_counting(u64::MAX, item).map(|_| ())
    }

    /// Inserts `item` in the filter if it's not already present (probabilistically).
    /// Note that membership is probabilistic, so this function may return false positives
    /// but never false negatives.
    ///
    /// Returns `Ok(true)` if the item was successfully added to the filter.
    /// Returns `Ok(false)` if the item is already contained (probabilistically) in the filter.
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot admit the new item.
    #[inline]
    pub fn insert<T: Hash>(&mut self, item: T) -> Result<bool, Error> {
        self.insert_counting(1, item).map(|count| count == 0)
    }

    /// Inserts `item` in the filter, even if already appears to be in the filter.
    /// This works by inserting a possibly duplicated fingerprint in the filter.
    /// The argument `max_count` specifies how many duplicates can be inserted.
    ///
    /// Returns `Ok(count)` of how many equal fingerprints _were_ in the filter. So if the item
    /// was already in the filter `C` times, another insertion was performed if `C < max_count`.
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot admit the new item.
    pub fn insert_counting<T: Hash>(&mut self, max_count: u64, item: T) -> Result<u64, Error> {
        let hash = self.hash(item);
        match self.insert_impl(max_count, hash) {
            Ok(count) => Ok(count),
            Err(_) => {
                self.grow_if_possible()?;
                self.insert_impl(max_count, hash)
            }
        }
    }

    /// Inserts the fingerprint specified by `hash` in the filter.
    /// `duplicate` specifies if the fingerprint should be added even if it's already in the filter.
    ///
    /// Note that this function will automatically grow the filter if needed.
    /// The implementation uses the first [`Self::fingerprint_size`] bits of `hash` to place the fingerprint in the appropriate slot.
    /// The remaining bits are ignored and will be returned as 0 if the fingerprint is retrieved via [`Self::fingerprints`].
    ///
    /// Returns `Ok(true)` if the item was successfully added to the filter.
    /// Returns `Ok(false)` if the item is already contained (probabilistically) in the filter. Possible if `duplicate` is `false`.
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot admit the new item.
    #[inline]
    pub fn insert_fingerprint(&mut self, duplicate: bool, hash: u64) -> Result<bool, Error> {
        let max_count = if duplicate { u64::MAX } else { 1 };
        self.insert_fingerprint_counting(max_count, hash)
            .map(|count| count < max_count)
    }

    /// Inserts the fingerprint specified by `hash` in the filter.
    /// `max_count` specifies how many occurences of the fingerprint can be added to the filter.
    ///
    /// Note that this function will automatically grow the filter if needed.
    /// The implementation uses the first [`Self::fingerprint_size`] bits of `hash` to place the fingerprint in the appropriate slot.
    /// The remaining bits are ignored and will be returned as 0 if the fingerprint is retrieved via [`Self::fingerprints`].
    ///
    /// Returns `Ok(count)` of how many equal fingerprints _were_ in the filter. So if the item
    /// was already in the filter `C` times, another insertion was performed if `C < max_count`.
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot admit the new item.
    pub fn insert_fingerprint_counting(&mut self, max_count: u64, hash: u64) -> Result<u64, Error> {
        match self.insert_impl(max_count, hash) {
            Ok(count) => Ok(count),
            Err(_) => {
                self.grow_if_possible()?;
                self.insert_impl(max_count, hash)
            }
        }
    }

    /// Inserts the fingerprint specified by `hash` in the filter.
    /// `max_count` specifies how many occurences of the fingerprint can be added to the filter.
    /// It's up to the caller to grow the filter if needed and retry the insert.
    ///
    /// Returns `Ok(count)` of how many equal fingerprints _were_ in the filter.
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot admit the new item.
    fn insert_impl(&mut self, max_count: u64, hash: u64) -> Result<u64, Error> {
        enum Operation {
            NewRun,
            BeforeRunend,
            NewRunend,
        }

        let (hash_bucket_idx, hash_remainder) = self.calc_qr(hash);
        if self.offset_lower_bound(hash_bucket_idx) == 0 {
            if self.len >= self.capacity() {
                return Err(Error::CapacityExceeded);
            }
            debug_assert!(!self.is_occupied(hash_bucket_idx));
            debug_assert!(!self.is_runend(hash_bucket_idx));
            self.set_occupied(hash_bucket_idx, true);
            self.set_runend(hash_bucket_idx, true);
            self.set_remainder(hash_bucket_idx, hash_remainder);
            self.len += 1;
            return Ok(0);
        }

        let mut runstart_idx = self.run_start(hash_bucket_idx);
        let mut runend_idx = self.run_end(hash_bucket_idx);
        let mut fingerprint_count = 0;
        let insert_idx;
        let operation;
        if self.is_occupied(hash_bucket_idx) {
            // adjust runend so its >= runstart even if it wrapped around
            if runend_idx < runstart_idx {
                runend_idx += self.total_buckets().get();
            }
            while runstart_idx <= runend_idx {
                match self.get_remainder(runstart_idx).cmp(&hash_remainder) {
                    Ordering::Equal => {
                        fingerprint_count += 1;
                        if fingerprint_count >= max_count {
                            return Ok(fingerprint_count);
                        }
                    }
                    Ordering::Greater => break,
                    Ordering::Less => (),
                }

                runstart_idx += 1;
            }

            if runstart_idx > runend_idx {
                /* new remainder is >= than any remainder in the run. */
                operation = Operation::NewRunend;
                insert_idx = runstart_idx % self.total_buckets();
            } else {
                /* there are larger remainders already in the run. */
                operation = Operation::BeforeRunend; /* Inserting */
                insert_idx = runstart_idx % self.total_buckets();
            }
        } else {
            insert_idx = (runend_idx + 1) % self.total_buckets();
            operation = Operation::NewRun; /* Insert into empty bucket */
        }

        if self.len >= self.capacity() {
            return Err(Error::CapacityExceeded);
        }
        let empty_slot_idx = self.find_first_empty_slot(runend_idx + 1);
        if insert_idx != empty_slot_idx {
            self.shift_remainders_by_1(insert_idx, empty_slot_idx);
            self.shift_runends_by_1(insert_idx, empty_slot_idx);
        }
        self.set_remainder(insert_idx, hash_remainder);
        match operation {
            Operation::NewRun => {
                /* Insert into empty bucket */
                self.set_runend(insert_idx, true);
                self.set_occupied(hash_bucket_idx, true);
            }
            Operation::NewRunend => {
                /*  new remainder it is >= than any remainder in the run. */
                self.set_runend(insert_idx.wrapping_sub(1) % self.total_buckets(), false);
                self.set_runend(insert_idx, true);
            }
            Operation::BeforeRunend => { /* there are larger remainders already in the run. */ }
        }

        self.inc_offsets(hash_bucket_idx, empty_slot_idx);
        self.len += 1;
        Ok(fingerprint_count)
    }

    /// Returns an iterator over the fingerprints stored in the filter.
    ///
    /// Fingerprints will be returned in ascending order.
    pub fn fingerprints(&self) -> FingerprintIter {
        FingerprintIter::new(self)
    }

    /// Shrinks the capacity of the filter as much as possible while preserving
    /// the false positive ratios and fingerprint size.
    pub fn shrink_to_fit(&mut self) {
        if self.total_blocks().get() > 1 && self.len() <= self.capacity() / 2 {
            let mut new = Self::with_qr(
                (self.qbits.get() - 1).try_into().unwrap(),
                (self.rbits.get() + 1).try_into().unwrap(),
            )
            .unwrap();
            new.max_qbits = self.max_qbits;
            for hash in self.fingerprints() {
                let _ = new.insert_fingerprint(true, hash);
            }
            debug_assert_eq!(new.len, self.len);
            debug_assert_eq!(new.fingerprint_size(), self.fingerprint_size());
            *self = new;
        }
    }

    /// Merges `other` filter into `self`.
    ///
    /// `keep_duplicates` specifies whether duplicated fingerprints should be store,
    /// this is normally only useful is the filter is being used for counting.
    ///
    /// Note that the `other` filter must have a fingerprint >= `self` fingerprint size,
    /// otherwise the function will fail with `Err(Error::IncompatibleFingerprintSize)`.
    /// This is the case for filters created with the same parameters or if the `other`
    /// filter has a lower target false positive ratio.
    ///
    /// Returns `Err(Error::CapacityExceeded)` if the filter cannot merge all items.
    /// Note that in this case items could have already been added and the filter is left
    /// full but in an otherwise valid state.
    pub fn merge(&mut self, keep_duplicates: bool, other: &Self) -> Result<(), Error> {
        if other.fingerprint_size() < self.fingerprint_size() {
            return Err(Error::IncompatibleFingerprintSize);
        }
        let max_count = if keep_duplicates { u64::MAX } else { 1 };
        for hash in other.fingerprints() {
            self.insert_impl(max_count, hash)?;
        }
        Ok(())
    }

    #[inline]
    fn grow_if_possible(&mut self) -> Result<(), Error> {
        if let Some(m) = self.max_qbits {
            if m > self.qbits {
                self.grow();
                return Ok(());
            }
        }
        Err(Error::CapacityExceeded)
    }

    #[cold]
    #[inline(never)]
    fn grow(&mut self) {
        let qbits = self.qbits.checked_add(1).unwrap();
        let rbits = NonZeroU8::new(self.rbits.get() - 1).unwrap();
        let mut new = Self::with_qr(qbits, rbits).unwrap();
        new.max_qbits = self.max_qbits;
        for hash in self.fingerprints() {
            new.insert_fingerprint(true, hash).unwrap();
        }
        assert_eq!(self.len, new.len);
        *self = new;
    }

    #[inline]
    fn hash<T: Hash>(&self, item: T) -> u64 {
        let mut hasher = StableHasher::new();
        item.hash(&mut hasher);
        hasher.finish()
    }

    #[inline]
    fn calc_qr(&self, hash: u64) -> (u64, u64) {
        let hash_bucket_idx = (hash >> self.rbits.get()) & ((1 << self.qbits.get()) - 1);
        let remainder = hash & ((1 << self.rbits.get()) - 1);
        (hash_bucket_idx, remainder)
    }

    #[inline]
    fn total_blocks(&self) -> NonZeroU64 {
        // The way this is calculated ensures the compilers sees that the result is both != 0 and a power of 2,
        // both of which allow the optimizer to generate much faster division/remainder code.
        #[cfg(any(debug_assertions, fuzzing))]
        {
            NonZeroU64::new((1u64 << self.qbits.get()) / 64).unwrap()
        }
        #[cfg(not(any(debug_assertions, fuzzing)))]
        {
            // Safety: All filter have at least 1 block (which have 64 slots each)
            unsafe { NonZeroU64::new_unchecked((1u64 << self.qbits.get()) / 64) }
        }
    }

    #[inline]
    fn total_buckets(&self) -> NonZeroU64 {
        NonZeroU64::new(1 << self.qbits.get()).unwrap()
    }

    #[doc(hidden)]
    #[cfg(any(fuzzing, test))]
    pub fn printout(&self) {
        eprintln!(
            "=== q {} r {} len {} cap {} ===",
            self.qbits,
            self.rbits,
            self.len(),
            self.capacity()
        );
        for b in 0..self.total_blocks().get() {
            let block = self.raw_block(b);
            eprintln!(
                "block {} offset {:?}\noccup {:064b}\nrunen {:064b}",
                b, block.offset, block.occupieds, block.runends
            );
            eprintln!(
                "      3210987654321098765432109876543210987654321098765432109876543210 {}",
                b * 64
            );
            eprint!("rem   ");
            for i in (0..64).rev() {
                let r = self.get_remainder(b * 64 + i);
                eprint!("{}", r % 100 / 10);
            }
            eprint!("\nrem   ");
            for i in (0..64).rev() {
                let r = self.get_remainder(b * 64 + i);
                eprint!("{}", r % 10);
            }
            println!();
        }
        eprintln!("===");
    }
}

impl std::fmt::Debug for Filter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Filter")
            .field("buffer", &"[..]")
            .field("len", &self.len)
            .field("qbits", &self.qbits)
            .field("rbits", &self.rbits)
            .field("max_qbits", &self.max_qbits)
            .finish()
    }
}

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

    #[test]
    fn run_end_simple() {
        let mut f = Filter::new(50, 0.01).unwrap();
        f.set_occupied(5, true);
        f.set_runend(5, true);
        assert_eq!(f.run_end(4), 4);
        assert_eq!(f.run_end(5), 5);
        assert_eq!(f.run_end(6), 6);

        f.set_occupied(6, true);
        f.set_runend(6, true);
        assert_eq!(f.run_end(4), 4);
        assert_eq!(f.run_end(5), 5);
        assert_eq!(f.run_end(6), 6);

        f.set_runend(6, false);
        f.set_runend(7, true);
        assert_eq!(f.run_end(4), 4);
        assert_eq!(f.run_end(5), 5);
        assert_eq!(f.run_end(6), 7);

        f.set_runend(7, false);
        f.set_runend(8, true);
        assert_eq!(f.run_end(4), 4);
        assert_eq!(f.run_end(5), 5);
        assert_eq!(f.run_end(6), 8);

        f.set_occupied(10, true);
        f.set_runend(12, true);
        f.set_occupied(12, true);
        f.set_runend(13, true);
        assert_eq!(f.run_end(10), 12);
        assert_eq!(f.run_end(12), 13);

        f.set_occupied(11, true);
        f.set_runend(14, true);
        assert_eq!(f.run_end(10), 12);
        assert_eq!(f.run_end(11), 13);
        assert_eq!(f.run_end(12), 14);
    }

    #[test]
    fn run_end_eob() {
        let mut f = Filter::new(50, 0.01).unwrap();
        assert_eq!(f.total_buckets().get(), 64);
        f.set_occupied(63, true);
        f.set_runend(63, true);
        assert_eq!(f.run_end(62), 62);
        assert_eq!(f.run_end(63), 63);
        assert_eq!(f.find_first_empty_slot(62), 62);
        assert_eq!(f.find_first_empty_slot(63), 0);
    }

    #[test]
    fn run_end_crossing() {
        let mut f = Filter::new(50, 0.01).unwrap();
        f.set_occupied(0, true);
        f.set_runend(0, true);
        f.set_occupied(63, true);
        f.set_runend(63, true);
        assert_eq!(f.run_end(0), 0);
        assert_eq!(f.run_end(1), 1);
        assert_eq!(f.run_end(62), 62);
        assert_eq!(f.run_end(63), 63);

        f.set_runend(63, false);
        f.set_runend(1, true);
        f.adjust_block_offset(1, true);
        assert_eq!(f.run_end(0), 1);
        assert_eq!(f.run_end(1), 1);
        assert_eq!(f.run_end(62), 62);
        assert_eq!(f.run_end(63), 0);

        f.set_runend(1, false);
        f.set_runend(2, true);
        assert_eq!(f.run_end(63), 0);
        assert_eq!(f.run_end(0), 2);
        assert_eq!(f.run_end(1), 2);

        f.set_runend(2, false);
        f.set_runend(3, true);
        assert_eq!(f.run_end(63), 0);
        assert_eq!(f.run_end(1), 3);
        assert_eq!(f.run_end(2), 3);

        f.set_occupied(65, true);
        f.set_runend(68, true);
        assert_eq!(f.run_end(63), 0);
        assert_eq!(f.run_end(0), 3);
        assert_eq!(f.run_end(1), 4);
    }

    #[test]
    fn test_insert_duplicated() {
        for cap in [100, 200, 500, 1000] {
            let mut f = Filter::new(cap, 0.01).unwrap();
            for i in 0..f.capacity() / 2 {
                f.insert_duplicated(-1).unwrap();
                f.insert_duplicated(i).unwrap();
                assert!(f.count(-1) >= i);
                assert!(f.count(i) >= 1);
            }
        }
    }

    #[test]
    fn test_insert_duplicated_two() {
        for s in 0..10 {
            for c in [200, 800, 1500] {
                let mut f = Filter::new(c, 0.001).unwrap();
                for i in 0..f.capacity() / 2 {
                    f.insert_duplicated(-1).unwrap();
                    assert_eq!(f.count(-1), i + 1);
                    assert_eq!(f.count(s), i);
                    f.insert_duplicated(s).unwrap();
                    assert_eq!(f.count(-1), i + 1);
                    assert_eq!(f.count(s), i + 1);
                }
            }
        }
    }

    #[test]
    fn test_insert_duplicated_one() {
        for s in 0..10 {
            for cap in [100, 200, 500, 1000] {
                let mut f = Filter::new(cap, 0.01).unwrap();
                for i in 0..f.capacity() {
                    f.insert_duplicated(s).unwrap();
                    assert!(f.count(s) > i);
                }
                assert_eq!(f.count(s), f.capacity());
            }
        }
    }

    #[test]
    fn test_auto_resize_two() {
        let mut f = Filter::new_resizeable(50, 1000, 0.01).unwrap();
        for _ in 0..50 {
            f.insert_duplicated(0).unwrap();
        }
        for _ in 0..3 {
            f.insert_duplicated(1).unwrap();
        }
        f.grow();
        f.grow();
        f.grow();
        assert_eq!(f.count(0), 50);
        assert_eq!(f.count(1), 3);
    }

    #[test]
    fn test_new_resizeable() {
        let mut f = Filter::new_resizeable(100, 100, 0.01).unwrap();
        assert!(f.grow_if_possible().is_err());
        let mut f = Filter::new_resizeable(0, 100, 0.01).unwrap();
        assert!(f.grow_if_possible().is_ok());
    }

    #[test]
    #[should_panic]
    fn test_new_capacity_overflow() {
        Filter::new_resizeable(100, u64::MAX, 0.01).unwrap();
    }

    #[test]
    #[should_panic]
    fn test_new_hash_overflow() {
        Filter::new_resizeable(100, u64::MAX / 20, 0.01).unwrap();
    }

    #[test]
    fn test_auto_resize_one() {
        let mut f = Filter::new_resizeable(100, 500, 0.01).unwrap();
        for i in 0u64.. {
            if f.insert_duplicated(i).is_err() {
                assert_eq!(f.len(), i);
                break;
            }
        }
        assert!(f.len() >= 500);
        for i in 0u64..f.len() {
            assert!(f.contains(i), "{}", i);
        }
    }

    #[test]
    fn test_remainders_and_shifts() {
        let mut f = Filter::new(200, 0.01).unwrap();
        let c = f.capacity();
        for j in 0..c {
            f.set_remainder(j, 0b1011101);
            assert_eq!(f.get_remainder(j), 0b1011101);
            f.set_runend(j, true);
            assert!(f.is_runend(j));
        }
        for j in 0..c {
            f.set_remainder(j, 0b1111111);
            assert_eq!(f.get_remainder(j), 0b1111111);
            f.set_runend(j, false);
            assert!(!f.is_runend(j));
        }
        for j in 0..c {
            f.set_remainder(j, 0b1101101);
            assert_eq!(f.get_remainder(j), 0b1101101);
            f.set_runend(j, true);
            assert!(f.is_runend(j));
        }
        f.shift_remainders_by_1(0, c);
        f.shift_runends_by_1(0, c);

        for j in 1..=c {
            assert_eq!(f.get_remainder(j), 0b1101101);
        }
        assert!(!f.is_runend(0));
        for j in 1..=c {
            assert_eq!(f.get_remainder(j), 0b1101101);
            assert!(f.is_runend(j));
        }
    }

    #[test]
    fn test_remove() {
        for fp in [0.0001, 0.00001, 0.000001] {
            for cap in [0, 100, 200, 400, 1000] {
                let mut f = Filter::new(cap, fp).unwrap();
                dbg!(f.rbits, f.capacity());
                let c = f.capacity();
                for i in 0..c {
                    assert!(f.insert(i).unwrap());
                }
                assert_eq!(f.len(), c);
                for i in 0..c {
                    for j in 0..c {
                        assert_eq!(f.count(j), (j >= i) as u64, "{}", j);
                    }
                    // f.printout();
                    assert!(f.remove(i));
                    // f.printout();
                }
                assert!(f.is_empty());
            }
        }
    }
    #[test]
    fn test_remove_dup_one() {
        for s in 0..10 {
            for cap in [0, 100, 200, 500, 1000] {
                let mut f = Filter::new(cap, 0.0001).unwrap();
                let c = f.capacity();
                for _ in 0..c {
                    f.insert_duplicated(s).unwrap();
                }
                assert_eq!(f.len(), c);
                for i in 0..c {
                    assert_eq!(f.count(s), c - i);
                    assert!(f.remove(s));
                }
                assert!(f.is_empty());
            }
        }
    }
    #[test]
    fn test_remove_dup_two() {
        for s in 0..10 {
            dbg!(s);
            for cap in [100, 200, 500, 1000] {
                let mut f = Filter::new(cap, 0.0001).unwrap();
                let c = f.capacity();
                for _ in 0..c / 2 {
                    f.insert_duplicated(-1).unwrap();
                    f.insert_duplicated(s).unwrap();
                }
                assert_eq!(f.count(-1), c / 2);
                assert_eq!(f.count(s), c / 2);
                for i in 0..c / 2 {
                    assert_eq!(f.count(-1), c / 2 - i);
                    assert_eq!(f.count(s), c / 2 - i);
                    assert!(f.remove(-1));
                    assert_eq!(f.count(-1), c / 2 - i - 1);
                    assert_eq!(f.count(s), c / 2 - i);
                    assert!(f.remove(s));
                    assert_eq!(f.count(-1), c / 2 - i - 1);
                    assert_eq!(f.count(s), c / 2 - i - 1);
                }
                assert!(f.is_empty());
            }
        }
    }

    #[test]
    fn test_it_works() {
        for fp_rate_arg in [0.01, 0.001, 0.0001] {
            let mut f = Filter::new(100_000, fp_rate_arg).unwrap();
            assert!(!f.contains(0));
            assert_eq!(f.len(), 0);
            for i in 0..f.capacity() {
                f.insert_duplicated(i).unwrap();
            }
            for i in 0..f.capacity() {
                assert!(f.contains(i));
            }
            let est_fp_rate =
                (0..).take(50_000).filter(|i| f.contains(i)).count() as f64 / 50_000.0;
            dbg!(f.max_error_ratio(), est_fp_rate);
            assert!(est_fp_rate <= f.max_error_ratio());
        }
    }

    #[test]
    fn test_with_fingerprint_size_resizes() {
        let mut f = Filter::with_fingerprint_size(0, 8).unwrap();
        assert_eq!(f.fingerprint_size(), 8);
        assert_eq!(f.capacity_resizeable(), (128u64 * 19).div_ceil(20));
        assert_eq!(f.capacity(), (64u64 * 19).div_ceil(20));
        for i in 0..f.capacity_resizeable() {
            f.insert_fingerprint(false, i).unwrap();
        }
        assert_eq!(f.len(), f.capacity_resizeable());
        assert!(f
            .insert_fingerprint(false, f.capacity_resizeable())
            .is_err());
    }

    #[test]
    fn test_with_fingerprint_size() {
        let fingerprints = [
            0u64,
            0,
            1,
            1,
            1,
            1,
            1,
            0x777777777777,
            u32::MAX as u64 - 1,
            u32::MAX as u64 - 1,
            u32::MAX as u64,
            u64::MAX - 1,
            u64::MAX - 1,
            u64::MAX,
            u64::MAX,
        ];
        for fip_size in [7, 16, 24, 31, 49, 64] {
            let mut filter = Filter::with_fingerprint_size(1, fip_size).unwrap();
            for h in fingerprints {
                filter.insert_fingerprint(true, h).unwrap();
            }
            let out: Vec<u64> = filter.fingerprints().collect::<Vec<_>>();
            let mut expect = fingerprints.map(|h| h << (64 - fip_size) >> (64 - fip_size));
            expect.sort_unstable();
            assert_eq!(out, expect);
        }
    }

    #[test]
    fn test_merge() {
        fn test(mut f1: Filter, mut f2: Filter, mut f3: Filter) {
            assert!(f1.merge(true, &f1.clone()).is_ok());
            assert!(f1.merge(true, &f2).is_ok());
            assert!(f1.merge(true, &f3).is_ok());
            assert!(f2.merge(true, &f1).is_err());
            assert!(f2.merge(true, &f2.clone()).is_ok());
            assert!(f2.merge(true, &f3).is_ok());
            assert!(f3.merge(true, &f1).is_err());
            assert!(f3.merge(true, &f2).is_err());
            assert!(f3.merge(true, &f3.clone()).is_ok());

            f1.insert_fingerprint(true, 1).unwrap();
            f2.insert_fingerprint(true, 1).unwrap();
            f2.insert_fingerprint(true, 2).unwrap();
            f3.insert_fingerprint(true, 1).unwrap();
            f3.insert_fingerprint(true, 2).unwrap();
            f3.insert_fingerprint(true, 3).unwrap();
            assert_eq!(f1.len(), 1);
            assert_eq!(f2.len(), 2);
            assert_eq!(f3.len(), 3);

            f1.merge(false, &f1.clone()).unwrap();
            assert_eq!(f1.len(), 1);
            f1.merge(true, &f2.clone()).unwrap();
            assert_eq!(f1.len(), 3);
            f1.merge(false, &f3.clone()).unwrap();
            assert_eq!(f1.len(), 4);

            for _ in f1.len()..f1.capacity() {
                f1.insert_fingerprint(true, 1).unwrap();
            }
            assert_eq!(f1.len(), f1.capacity());
            assert!(matches!(
                f1.insert_impl(u64::MAX, 1),
                Err(Error::CapacityExceeded)
            ));
            assert!(matches!(
                f1.merge(true, &f1.clone()),
                Err(Error::CapacityExceeded)
            ));
            assert!(matches!(f1.insert_fingerprint(false, 1), Ok(false)));
            assert!(matches!(f1.merge(false, &f1.clone()), Ok(())));
        }
        test(
            Filter::with_fingerprint_size(1, 10).unwrap(),
            Filter::with_fingerprint_size(1, 11).unwrap(),
            Filter::with_fingerprint_size(1, 12).unwrap(),
        );
        test(
            Filter::new(1, 0.01).unwrap(),
            Filter::new(1, 0.001).unwrap(),
            Filter::new(1, 0.0001).unwrap(),
        );
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde() {
        for capacity in [100, 1000, 10000] {
            for fp_ratio in [0.2, 0.1, 0.01, 0.001, 0.0001] {
                let mut f = Filter::new(capacity, fp_ratio).unwrap();
                for i in 0..f.capacity() {
                    f.insert(i).unwrap();
                }

                let ser = serde_cbor::to_vec(&f).unwrap();
                f = serde_cbor::from_slice(&ser).unwrap();
                for i in 0..f.capacity() {
                    f.contains(i);
                }
                dbg!(
                    f.current_error_ratio(),
                    f.max_error_ratio(),
                    f.capacity(),
                    f.len(),
                    ser.len()
                );
            }
        }
    }

    #[test]
    fn test_dec_offset_edge_case() {
        // case found in fuzz testing
        #[rustfmt::skip]
        let sample = [(0u16, 287), (2u16, 1), (9u16, 2), (10u16, 1), (53u16, 5), (61u16, 5), (127u16, 2), (232u16, 1), (255u16, 21), (314u16, 2), (317u16, 2), (384u16, 2), (511u16, 3), (512u16, 2), (1599u16, 2), (2303u16, 5), (2559u16, 2), (2568u16, 3), (2815u16, 2), (6400u16, 2), (9211u16, 2), (9728u16, 2), (10790u16, 1), (10794u16, 94), (10797u16, 2), (10999u16, 2), (11007u16, 2), (11520u16, 1), (12800u16, 4), (12842u16, 2), (13823u16, 1), (14984u16, 2), (15617u16, 2), (15871u16, 4), (16128u16, 3), (16383u16, 2), (16394u16, 1), (18167u16, 2), (23807u16, 1), (32759u16, 2)];
        let mut f = Filter::new(400, 0.1).unwrap();
        for (i, c) in sample {
            for _ in 0..c {
                f.insert_duplicated(i).unwrap();
            }
        }
        assert_eq!(f.raw_block(2).offset, 3);
        assert_eq!(f.raw_block(3).offset, u8::MAX as u64);
        f.validate_offsets(0, f.total_buckets().get());
        f.remove(0u16);
        assert_eq!(f.raw_block(2).offset, 2);
        assert_eq!(f.raw_block(3).offset, 254);
        f.validate_offsets(0, f.total_buckets().get());
    }

    #[test]
    fn test_capacity_edge_cases() {
        for n in 1..32 {
            let base = (1 << n) * 19 / 20;
            // Test numbers around the edge
            for i in [base - 1, base, base + 1] {
                let filter = Filter::new(i, 0.01).unwrap();
                assert!(
                    filter.capacity() >= i,
                    "Requested capacity {} but got {}",
                    i,
                    filter.capacity()
                );
                assert_eq!(filter.capacity(), filter.capacity_resizeable());
            }
        }
    }

    #[test]
    fn test_max_capacity() {
        for i in 7..=64 {
            let f = Filter::with_fingerprint_size(0, i).unwrap();
            assert!(f.capacity() <= f.capacity_resizeable());
            assert_eq!(
                f.capacity_resizeable(),
                ((1u64 << (i - 1).min(Filter::MAX_QBITS)) * 19).div_ceil(20)
            );
        }
        for i in 1..Filter::MAX_QBITS {
            let f = Filter::new_resizeable(0, 2u64.pow(i as u32), 0.5).unwrap();
            assert_eq!(f.capacity(), 61);
            assert!(f.capacity() <= f.capacity_resizeable());
        }
        // Test the maximum capacity
        let f = Filter::new_resizeable(0, Filter::MAX_CAPACITY, 0.5).unwrap();
        assert_eq!(f.capacity(), 61);
        assert_eq!(f.capacity_resizeable(), Filter::MAX_CAPACITY);
        // Test the maximum capacity + 1, which should fail
        Filter::new_resizeable(0, Filter::MAX_CAPACITY + 1, 0.5).unwrap_err();
    }
}