| 1 |
//! See [the `phf` crate's documentation][phf] for details. |
| 2 |
//! |
| 3 |
//! [phf]: https://docs.rs/phf |
| 4 |
|
| 5 |
#![doc(html_root_url = "https://docs.rs/phf_generator/0.11")] |
| 6 |
use phf_shared::{HashKey, PhfHash}; |
| 7 |
use rand::distributions::Standard; |
| 8 |
use rand::rngs::SmallRng; |
| 9 |
use rand::{Rng, SeedableRng}; |
| 10 |
|
| 11 |
const DEFAULT_LAMBDA: usize = 5; |
| 12 |
|
| 13 |
const FIXED_SEED: u64 = 1234567890; |
| 14 |
|
| 15 |
pub struct HashState { |
| 16 |
pub key: HashKey, |
| 17 |
pub disps: Vec<(u32, u32)>, |
| 18 |
pub map: Vec<usize>, |
| 19 |
} |
| 20 |
|
| 21 |
pub fn generate_hash<H: PhfHash>(entries: &[H]) -> HashState { |
| 22 |
SmallRng::seed_from_u64(FIXED_SEED) |
| 23 |
.sample_iter(Standard) |
| 24 |
.find_map(|key| try_generate_hash(entries, key)) |
| 25 |
.expect("failed to solve PHF") |
| 26 |
} |
| 27 |
|
| 28 |
fn try_generate_hash<H: PhfHash>(entries: &[H], key: HashKey) -> Option<HashState> { |
| 29 |
struct Bucket { |
| 30 |
idx: usize, |
| 31 |
keys: Vec<usize>, |
| 32 |
} |
| 33 |
|
| 34 |
let hashes: Vec<_> = entries |
| 35 |
.iter() |
| 36 |
.map(|entry| phf_shared::hash(entry, &key)) |
| 37 |
.collect(); |
| 38 |
|
| 39 |
let buckets_len = (hashes.len() + DEFAULT_LAMBDA - 1) / DEFAULT_LAMBDA; |
| 40 |
let mut buckets = (0..buckets_len) |
| 41 |
.map(|i| Bucket { |
| 42 |
idx: i, |
| 43 |
keys: vec![], |
| 44 |
}) |
| 45 |
.collect::<Vec<_>>(); |
| 46 |
|
| 47 |
for (i, hash) in hashes.iter().enumerate() { |
| 48 |
buckets[(hash.g % (buckets_len as u32)) as usize] |
| 49 |
.keys |
| 50 |
.push(i); |
| 51 |
} |
| 52 |
|
| 53 |
// Sort descending |
| 54 |
buckets.sort_by(|a, b| a.keys.len().cmp(&b.keys.len()).reverse()); |
| 55 |
|
| 56 |
let table_len = hashes.len(); |
| 57 |
let mut map = vec![None; table_len]; |
| 58 |
let mut disps = vec![(0u32, 0u32); buckets_len]; |
| 59 |
|
| 60 |
// store whether an element from the bucket being placed is |
| 61 |
// located at a certain position, to allow for efficient overlap |
| 62 |
// checks. It works by storing the generation in each cell and |
| 63 |
// each new placement-attempt is a new generation, so you can tell |
| 64 |
// if this is legitimately full by checking that the generations |
| 65 |
// are equal. (A u64 is far too large to overflow in a reasonable |
| 66 |
// time for current hardware.) |
| 67 |
let mut try_map = vec![0u64; table_len]; |
| 68 |
let mut generation = 0u64; |
| 69 |
|
| 70 |
// the actual values corresponding to the markers above, as |
| 71 |
// (index, key) pairs, for adding to the main map once we've |
| 72 |
// chosen the right disps. |
| 73 |
let mut values_to_add = vec![]; |
| 74 |
|
| 75 |
'buckets: for bucket in &buckets { |
| 76 |
for d1 in 0..(table_len as u32) { |
| 77 |
'disps: for d2 in 0..(table_len as u32) { |
| 78 |
values_to_add.clear(); |
| 79 |
generation += 1; |
| 80 |
|
| 81 |
for &key in &bucket.keys { |
| 82 |
let idx = (phf_shared::displace(hashes[key].f1, hashes[key].f2, d1, d2) |
| 83 |
% (table_len as u32)) as usize; |
| 84 |
if map[idx].is_some() || try_map[idx] == generation { |
| 85 |
continue 'disps; |
| 86 |
} |
| 87 |
try_map[idx] = generation; |
| 88 |
values_to_add.push((idx, key)); |
| 89 |
} |
| 90 |
|
| 91 |
// We've picked a good set of disps |
| 92 |
disps[bucket.idx] = (d1, d2); |
| 93 |
for &(idx, key) in &values_to_add { |
| 94 |
map[idx] = Some(key); |
| 95 |
} |
| 96 |
continue 'buckets; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Unable to find displacements for a bucket |
| 101 |
return None; |
| 102 |
} |
| 103 |
|
| 104 |
Some(HashState { |
| 105 |
key, |
| 106 |
disps, |
| 107 |
map: map.into_iter().map(|i| i.unwrap()).collect(), |
| 108 |
}) |
| 109 |
} |