pub struct Xor32 {
pub seed: u64,
pub block_length: usize,
pub fingerprints: Box<[u32]>,
}
Expand description
Xor filter using 32-bit fingerprints.
An Xor32
filter uses <40 bits per entry of the set is it constructed from, and has a false
positive rate of effectively zero (1/2^32 =~ 1/4 billion). As with other probabilistic filters,
a higher number of entries decreases the bits per entry but increases the false positive rate.
An Xor32
is constructed from a set of 64-bit unsigned integers and is immutable.
use xorf::{Filter, Xor32};
const SAMPLE_SIZE: usize = 1_000_000;
let keys: Vec<u64> = (0..SAMPLE_SIZE).map(|_| rng.gen()).collect();
let filter = Xor32::from(&keys);
// no false negatives
for key in keys {
assert!(filter.contains(&key));
}
// bits per entry
let bpe = (filter.len() as f64) * 32.0 / (SAMPLE_SIZE as f64);
assert!(bpe < 40., "Bits per entry is {}", bpe);
// false positive rate
let false_positives: usize = (0..SAMPLE_SIZE)
.map(|_| rng.gen())
.filter(|n| filter.contains(n))
.count();
let fp_rate: f64 = (false_positives * 100) as f64 / SAMPLE_SIZE as f64;
assert!(fp_rate < 0.0000000000000001, "False positive rate is {}", fp_rate);
Serializing and deserializing Xor32
filters can be enabled with the serde
feature (or [bincode
] for bincode).
Fields§
§seed: u64
The seed for the filter
block_length: usize
The number of blocks in the filter
fingerprints: Box<[u32]>
The fingerprints for the filter
Implementations§
source§impl Xor32
impl Xor32
sourcepub fn from_iterator<T>(keys: T) -> Self
pub fn from_iterator<T>(keys: T) -> Self
Construct the filter from a key iterator. Can be used directly if you don’t have a contiguous array of u64 keys.
Note: the iterator will be iterated over multiple times while building the filter. If using a hash function to map the key, it may be cheaper just to create a scratch array of hashed keys that you pass in.