1#[inline]
12pub fn selector_hash(bytes: &[u8]) -> u32 {
13 let mut hash: u32 = 2_166_136_261; for &byte in bytes {
15 hash ^= byte as u32;
16 hash = hash.wrapping_mul(16_777_619); }
18 hash
19}
20
21#[inline]
26pub fn class_bloom_bit(class_bytes: &[u8]) -> u64 {
27 1u64 << (selector_hash(class_bytes) % 64)
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33
34 #[test]
35 fn hash_deterministic() {
36 assert_eq!(selector_hash(b"hello"), selector_hash(b"hello"));
37 }
38
39 #[test]
40 fn hash_different_inputs() {
41 assert_ne!(selector_hash(b"hello"), selector_hash(b"world"));
42 }
43
44 #[test]
45 fn hash_empty() {
46 assert_eq!(selector_hash(b""), 2_166_136_261);
48 }
49
50 #[test]
51 fn bloom_bit_single_bit() {
52 let bit = class_bloom_bit(b"content");
53 assert_eq!(bit.count_ones(), 1);
54 }
55
56 #[test]
57 fn bloom_bit_in_range() {
58 let bit = class_bloom_bit(b"test");
59 assert!(bit != 0);
60 assert!(bit.is_power_of_two());
61 }
62}