| 1 | // Copyright 2019 The Rust Project Developers. See the COPYRIGHT |
| 2 | // file at the top-level directory of this distribution and at |
| 3 | // http://rust-lang.org/COPYRIGHT. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | |
| 11 | //! Support for lookups based on minimal perfect hashing. |
| 12 | |
| 13 | // This function is based on multiplication being fast and is "good enough". Also |
| 14 | // it can share some work between the unsalted and salted versions. |
| 15 | #[inline ] |
| 16 | fn my_hash(key: u32, salt: u32, n: usize) -> usize { |
| 17 | let y: u32 = key.wrapping_add(salt).wrapping_mul(2654435769); |
| 18 | let y: u32 = y ^ key.wrapping_mul(0x31415926); |
| 19 | (((y as u64) * (n as u64)) >> 32) as usize |
| 20 | } |
| 21 | |
| 22 | /// Do a lookup using minimal perfect hashing. |
| 23 | /// |
| 24 | /// The table is stored as a sequence of "salt" values, then a sequence of |
| 25 | /// values that contain packed key/value pairs. The strategy is to hash twice. |
| 26 | /// The first hash retrieves a salt value that makes the second hash unique. |
| 27 | /// The hash function doesn't have to be very good, just good enough that the |
| 28 | /// resulting map is unique. |
| 29 | #[inline ] |
| 30 | pub(crate) fn mph_lookup<KV, V, FK, FV>( |
| 31 | x: u32, |
| 32 | salt: &[u16], |
| 33 | kv: &[KV], |
| 34 | fk: FK, |
| 35 | fv: FV, |
| 36 | default: V, |
| 37 | ) -> V |
| 38 | where |
| 39 | KV: Copy, |
| 40 | FK: Fn(KV) -> u32, |
| 41 | FV: Fn(KV) -> V, |
| 42 | { |
| 43 | let s: u32 = salt[my_hash(key:x, salt:0, n:salt.len())] as u32; |
| 44 | let key_val: KV = kv[my_hash(key:x, salt:s, n:salt.len())]; |
| 45 | if x == fk(key_val) { |
| 46 | fv(key_val) |
| 47 | } else { |
| 48 | default |
| 49 | } |
| 50 | } |
| 51 | |