iword-rs 0.1.11

High-speed keyword search — Rust implementation of iWord
Documentation
/// Rolling hash used for O(N) text scanning.
/// Algorithm ported from iWord (imos, 2009).
#[derive(Clone, Copy, Default)]
pub struct Hash {
    pub a: u64,
    pub b: u64,
    pub f: u8,
}

impl Hash {
    pub fn feed(&mut self, c: u8) {
        self.a = self.a
            .wrapping_mul(0x41f9_3a76_1943_ba01)
            .wrapping_add((c as u64).wrapping_mul(0xa37b_830b_1a83_7477));
        self.b = self.b
            .wrapping_mul(0x9736_1b83_a957_c129)
            .wrapping_add((c as u64).wrapping_mul(0x19b4_f817_4920_1013));
    }

    pub fn from_bytes(s: &[u8]) -> Self {
        let mut h = Hash::default();
        for &c in s { h.feed(c); }
        h
    }

    /// Masked b value used for binary search in the index.
    pub fn b_masked(self) -> u64 { self.b & !0xf }
}