Expand description
ยงByte Perfect Hash Function Internals
This module contains a perfect hash function (PHF) designed for a fast, compact perfect hash over 1 to 256 nodes (bytes).
The PHF uses the following variables:
- A single parameter
p
, which is 0 in about 98% of cases. - A list of
N
parametersq_t
, one per bucket - The
N
keys in an arbitrary order determined by the PHF
Reading a key
from the PHF uses the following algorithm:
- Let
t
, the bucket index, bef1(key, p)
. - Let
i
, the key index, bef2(key, q_t)
. - If
key == k_i
, returnSome(i)
; else returnNone
.
The functions f1
and f2
are internal to the PHF but should remain stable across
serialization versions of ZeroTrie
. They are very fast, constant-time operations as long
as p
<= P_FAST_MAX
and q
<= Q_FAST_MAX
. In practice, nearly 100% of parameter
values are in the fast range.
use zerotrie::_internal::PerfectByteHashMap;
let phf_example_bytes = [
// `p` parameter
1, // `q` parameters, one for each of the N buckets
0, 0, 1, 1, // Exact keys to be compared with the input
b'e', b'a', b'c', b'g',
];
let phf = PerfectByteHashMap::from_bytes(&phf_example_bytes);
// The PHF returns the index of the key or `None` if not found.
assert_eq!(phf.get(b'a'), Some(1));
assert_eq!(phf.get(b'b'), None);
assert_eq!(phf.get(b'c'), Some(2));
assert_eq!(phf.get(b'd'), None);
assert_eq!(phf.get(b'e'), Some(0));
assert_eq!(phf.get(b'f'), None);
assert_eq!(phf.get(b'g'), Some(3));
Re-exportsยง
pub use cached_owned::PerfectByteHashMapCacheOwned;
Modulesยง
- builder ๐
- cached_
owned ๐
Structsยง
- A constant-time map from bytes to unique indices.
Constantsยง
- P_
FAST_ ๐MAX The cutoff for the fast version off1
. - P_
REAL_ ๐MAX The maximum allowable value ofp
. This could be raised if found to be necessary. Values exceeding P_FAST_MAX could use a differentp
algorithm by modifyingf1
. - Q_
FAST_ ๐MAX The cutoff for the fast version off2
. - Q_
REAL_ ๐MAX The maximum allowable value ofq
. This could be raised if found to be necessary.
Functionsยง
- Calculates the function
f1
for the PHF. For the exact formula, please read the code. - Calculates the function
f2
for the PHF. For the exact formula, please read the code.