pub struct NGram(/* private fields */);Expand description
A compact n-gram identifier. See the module-level documentation for the bit layout.
Note: we could store n-grams up to length 8 verbatim in a u64. However, that would explode
the number of distinct keys in a search dictionary. For that reason we compress n-grams into a
u32, which puts a more reasonable upper bound on the number of dictionary keys.
Note: by storing the length explicitly, we ensure that only n-grams of the same length can collide. This is important because there are exponentially more long n-grams than short ones. At the same time, longer n-grams occur less frequently, so colliding long n-grams won’t increase the false-positive rate too much.
§Construction
Use NGram::from_bytes for one-off hashing, or the rolling 8-byte window helper inside the
extraction loop for amortised O(1) computation per n-gram.
Implementations§
Source§impl NGram
impl NGram
Sourcepub fn from_bytes(src: &[u8]) -> Self
pub fn from_bytes(src: &[u8]) -> Self
Build an NGram by hashing the given byte slice from scratch.
§Panics
In debug builds, panics if src.len() is not in 2..=MAX_SPARSE_GRAM_SIZE.
Trait Implementations§
impl Copy for NGram
Source§impl Debug for NGram
The encoded u32 representation is not human readable. This formatter improves the situation
at least for short ascii grams.
impl Debug for NGram
The encoded u32 representation is not human readable. This formatter improves the situation
at least for short ascii grams.