Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PhfHash for integral arrays of any size. #247

Merged
merged 3 commits into from Feb 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 20 additions & 38 deletions phf_shared/src/lib.rs
Expand Up @@ -364,62 +364,44 @@ impl PhfHash for char {
}

// minimize duplicated code since formatting drags in quite a bit
fn fmt_array(array: &[u8], f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt_array<T: std::fmt::Debug>(array: &[T], f: &mut fmt::Formatter<'_>) -> fmt::Result {
turbocool3r marked this conversation as resolved.
Show resolved Hide resolved
write!(f, "{:?}", array)
}

macro_rules! array_impl (
($t:ty, $n:expr) => (
impl PhfHash for [$t; $n] {
($t:ty) => (
impl<const N: usize> PhfHash for [$t; N] {
#[inline]
fn phf_hash<H: Hasher>(&self, state: &mut H) {
state.write(self);
for v in self {
v.phf_hash(state);
}
}
}

impl FmtConst for [$t; $n] {
impl<const N: usize> FmtConst for [$t; N] {
turbocool3r marked this conversation as resolved.
Show resolved Hide resolved
fn fmt_const(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_array(self, f)
}
}

impl PhfBorrow<[$t]> for [$t; $n] {
impl<const N: usize> PhfBorrow<[$t]> for [$t; N] {
fn borrow(&self) -> &[$t] {
self
}
}
)
);

array_impl!(u8, 1);
array_impl!(u8, 2);
array_impl!(u8, 3);
array_impl!(u8, 4);
array_impl!(u8, 5);
array_impl!(u8, 6);
array_impl!(u8, 7);
array_impl!(u8, 8);
array_impl!(u8, 9);
array_impl!(u8, 10);
array_impl!(u8, 11);
array_impl!(u8, 12);
array_impl!(u8, 13);
array_impl!(u8, 14);
array_impl!(u8, 15);
array_impl!(u8, 16);
array_impl!(u8, 17);
array_impl!(u8, 18);
array_impl!(u8, 19);
array_impl!(u8, 20);
array_impl!(u8, 21);
array_impl!(u8, 22);
array_impl!(u8, 23);
array_impl!(u8, 24);
array_impl!(u8, 25);
array_impl!(u8, 26);
array_impl!(u8, 27);
array_impl!(u8, 28);
array_impl!(u8, 29);
array_impl!(u8, 30);
array_impl!(u8, 31);
array_impl!(u8, 32);
array_impl!(u8);
array_impl!(i8);
array_impl!(u16);
array_impl!(i16);
array_impl!(u32);
array_impl!(i32);
array_impl!(u64);
array_impl!(i64);
array_impl!(u128);
array_impl!(i128);
array_impl!(bool);
array_impl!(char);