| 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.14.0")] |
| 6 |
use std::iter; |
| 7 |
|
| 8 |
use fastrand::Rng; |
| 9 |
use phf_shared::{HashKey, Hashes, PhfHash}; |
| 10 |
|
| 11 |
const DEFAULT_LAMBDA: usize = 3; |
| 12 |
|
| 13 |
const FIXED_SEED: u64 = 1234567890; |
| 14 |
const EMPTY_SLOT: usize = usize::MAX; |
| 15 |
|
| 16 |
#[cfg(feature = "ptrhash")] |
| 17 |
pub mod ptrhash; |
| 18 |
|
| 19 |
pub struct HashState { |
| 20 |
pub key: HashKey, |
| 21 |
pub disps: Vec<(u32, u32)>, |
| 22 |
pub map: Vec<usize>, |
| 23 |
} |
| 24 |
|
| 25 |
pub fn generate_hash<H: PhfHash>(entries: &[H]) -> HashState { |
| 26 |
generate_hash_with_hash_fn(entries, phf_shared::hash) |
| 27 |
} |
| 28 |
|
| 29 |
pub fn generate_hash_with_hash_fn<T, F>(entries: &[T], hash_fn: F) -> HashState |
| 30 |
where |
| 31 |
F: Fn(&T, &HashKey) -> Hashes, |
| 32 |
{ |
| 33 |
let mut generator = Generator::new(entries.len()); |
| 34 |
let mut rng = Rng::with_seed(FIXED_SEED); |
| 35 |
|
| 36 |
iter::repeat_with(|| rng.u64(..)) |
| 37 |
.find(|key| { |
| 38 |
let hashes = entries.iter().map(|entry| hash_fn(entry, key)); |
| 39 |
generator.reset(hashes); |
| 40 |
|
| 41 |
generator.try_generate_hash() |
| 42 |
}) |
| 43 |
.map(|key| HashState { |
| 44 |
key, |
| 45 |
disps: generator.disps, |
| 46 |
map: generator.map, |
| 47 |
}) |
| 48 |
.expect("failed to solve PHF") |
| 49 |
} |
| 50 |
|
| 51 |
struct Bucket { |
| 52 |
idx: usize, |
| 53 |
start: usize, |
| 54 |
len: usize, |
| 55 |
cursor: usize, |
| 56 |
} |
| 57 |
|
| 58 |
struct Generator { |
| 59 |
hashes: Vec<Hashes>, |
| 60 |
buckets: Vec<Bucket>, |
| 61 |
bucket_order: Vec<usize>, |
| 62 |
bucket_keys: Vec<usize>, |
| 63 |
key_buckets: Vec<usize>, |
| 64 |
disps: Vec<(u32, u32)>, |
| 65 |
map: Vec<usize>, |
| 66 |
try_map: Vec<u64>, |
| 67 |
} |
| 68 |
|
| 69 |
impl Generator { |
| 70 |
fn new(table_len: usize) -> Self { |
| 71 |
let hashes = Vec::with_capacity(table_len); |
| 72 |
|
| 73 |
let buckets_len = (table_len + DEFAULT_LAMBDA - 1) / DEFAULT_LAMBDA; |
| 74 |
let buckets: Vec<_> = (0..buckets_len) |
| 75 |
.map(|i| Bucket { |
| 76 |
idx: i, |
| 77 |
start: 0, |
| 78 |
len: 0, |
| 79 |
cursor: 0, |
| 80 |
}) |
| 81 |
.collect(); |
| 82 |
let bucket_order = (0..buckets_len).collect(); |
| 83 |
let bucket_keys = vec![0; table_len]; |
| 84 |
let key_buckets = vec![0; table_len]; |
| 85 |
let disps = vec![(0u32, 0u32); buckets_len]; |
| 86 |
|
| 87 |
let map = vec![EMPTY_SLOT; table_len]; |
| 88 |
let try_map = vec![0u64; table_len]; |
| 89 |
|
| 90 |
Self { |
| 91 |
hashes, |
| 92 |
buckets, |
| 93 |
bucket_order, |
| 94 |
bucket_keys, |
| 95 |
key_buckets, |
| 96 |
disps, |
| 97 |
map, |
| 98 |
try_map, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
fn reset<I>(&mut self, hashes: I) |
| 103 |
where |
| 104 |
I: Iterator<Item = Hashes>, |
| 105 |
{ |
| 106 |
self.buckets.iter_mut().for_each(|b| { |
| 107 |
b.start = 0; |
| 108 |
b.len = 0; |
| 109 |
b.cursor = 0; |
| 110 |
}); |
| 111 |
self.disps.fill((0, 0)); |
| 112 |
self.map.fill(EMPTY_SLOT); |
| 113 |
self.try_map.fill(0); |
| 114 |
|
| 115 |
self.hashes.clear(); |
| 116 |
self.hashes.extend(hashes); |
| 117 |
} |
| 118 |
|
| 119 |
fn try_generate_hash(&mut self) -> bool { |
| 120 |
let buckets_len = self.buckets.len() as u32; |
| 121 |
|
| 122 |
// Store bucket contents in one flat buffer instead of allocating a Vec per bucket. |
| 123 |
for (i, hash) in self.hashes.iter().enumerate() { |
| 124 |
let bucket = (hash.g % buckets_len) as usize; |
| 125 |
self.key_buckets[i] = bucket; |
| 126 |
let bucket = &mut self.buckets[bucket]; |
| 127 |
bucket.len += 1; |
| 128 |
} |
| 129 |
|
| 130 |
let mut start = 0; |
| 131 |
for bucket in &mut self.buckets { |
| 132 |
bucket.start = start; |
| 133 |
bucket.cursor = start; |
| 134 |
start += bucket.len; |
| 135 |
} |
| 136 |
|
| 137 |
for (i, &bucket) in self.key_buckets.iter().enumerate() { |
| 138 |
let bucket = &mut self.buckets[bucket]; |
| 139 |
self.bucket_keys[bucket.cursor] = i; |
| 140 |
bucket.cursor += 1; |
| 141 |
} |
| 142 |
|
| 143 |
let buckets = &self.buckets; |
| 144 |
self.bucket_order |
| 145 |
.sort_unstable_by(|&a, &b| buckets[b].len.cmp(&buckets[a].len).then_with(|| a.cmp(&b))); |
| 146 |
|
| 147 |
let table_len = self.hashes.len(); |
| 148 |
|
| 149 |
// store whether an element from the bucket being placed is |
| 150 |
// located at a certain position, to allow for efficient overlap |
| 151 |
// checks. It works by storing the generation in each cell and |
| 152 |
// each new placement-attempt is a new generation, so you can tell |
| 153 |
// if this is legitimately full by checking that the generations |
| 154 |
// are equal. (A u64 is far too large to overflow in a reasonable |
| 155 |
// time for current hardware.) |
| 156 |
let mut generation = 0u64; |
| 157 |
|
| 158 |
// the actual values corresponding to the markers above, as |
| 159 |
// (index, key) pairs, for adding to the main map once we've |
| 160 |
// chosen the right disps. |
| 161 |
let mut values_to_add = vec![]; |
| 162 |
|
| 163 |
'buckets: for &bucket_idx in &self.bucket_order { |
| 164 |
let bucket = &self.buckets[bucket_idx]; |
| 165 |
let keys = &self.bucket_keys[bucket.start..bucket.start + bucket.len]; |
| 166 |
|
| 167 |
for d1 in 0..(table_len as u32) { |
| 168 |
'disps: for d2 in 0..(table_len as u32) { |
| 169 |
values_to_add.clear(); |
| 170 |
generation += 1; |
| 171 |
|
| 172 |
for &key in keys { |
| 173 |
let idx = |
| 174 |
(phf_shared::displace(self.hashes[key].f1, self.hashes[key].f2, d1, d2) |
| 175 |
% (table_len as u32)) as usize; |
| 176 |
if self.map[idx] != EMPTY_SLOT || self.try_map[idx] == generation { |
| 177 |
continue 'disps; |
| 178 |
} |
| 179 |
self.try_map[idx] = generation; |
| 180 |
values_to_add.push((idx, key)); |
| 181 |
} |
| 182 |
|
| 183 |
// We've picked a good set of disps |
| 184 |
self.disps[bucket.idx] = (d1, d2); |
| 185 |
for &(idx, key) in &values_to_add { |
| 186 |
self.map[idx] = key; |
| 187 |
} |
| 188 |
continue 'buckets; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// Unable to find displacements for a bucket |
| 193 |
return false; |
| 194 |
} |
| 195 |
true |
| 196 |
} |
| 197 |
} |