Commit d201571c authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Shrink integral hasher specialization implementations

Summary:
[Folly] Shrink integral `hasher` specialization implementations.

Instead of giving them bodies, just use inheritance.

Reviewed By: luciang

Differential Revision: D6067757

fbshipit-source-id: f71bd36132e1b66002493474825894b03754b34f
parent b83985c4
...@@ -371,18 +371,20 @@ inline uint32_t hsieh_hash32_str(const std::string& str) { ...@@ -371,18 +371,20 @@ inline uint32_t hsieh_hash32_str(const std::string& str) {
} // namespace hash } // namespace hash
namespace detail { namespace detail {
template <typename I> struct integral_hasher {
size_t integral_hash(I const& i) { template <typename I>
static_assert(sizeof(I) <= 8, "input type is too wide"); size_t operator()(I const& i) const {
if (sizeof(I) <= 4) { // the branch taken is known at compile time static_assert(sizeof(I) <= 8, "input type is too wide");
auto const i32 = static_cast<int32_t>(i); // as impl accident, sign-extends if (sizeof(I) <= 4) { // the branch taken is known at compile time
auto const u32 = static_cast<uint32_t>(i32); auto const i32 = static_cast<int32_t>(i); // impl accident: sign-extends
return static_cast<size_t>(hash::jenkins_rev_mix32(u32)); auto const u32 = static_cast<uint32_t>(i32);
} else { return static_cast<size_t>(hash::jenkins_rev_mix32(u32));
auto const u64 = static_cast<uint64_t>(i); } else {
return static_cast<size_t>(hash::twang_mix64(u64)); auto const u64 = static_cast<uint64_t>(i);
return static_cast<size_t>(hash::twang_mix64(u64));
}
} }
} };
} // namespace detail } // namespace detail
template <class Key, class Enable = void> template <class Key, class Enable = void>
...@@ -409,86 +411,37 @@ struct hasher<bool> { ...@@ -409,86 +411,37 @@ struct hasher<bool> {
}; };
template <> template <>
struct hasher<unsigned long long> { struct hasher<unsigned long long> : detail::integral_hasher {};
size_t operator()(unsigned long long key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<signed long long> { struct hasher<signed long long> : detail::integral_hasher {};
size_t operator()(signed long long key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<unsigned long> { struct hasher<unsigned long> : detail::integral_hasher {};
size_t operator()(unsigned long key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<signed long> { struct hasher<signed long> : detail::integral_hasher {};
size_t operator()(signed long key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<unsigned int> { struct hasher<unsigned int> : detail::integral_hasher {};
size_t operator()(unsigned int key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<signed int> { struct hasher<signed int> : detail::integral_hasher {};
size_t operator()(signed int key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<unsigned short> { struct hasher<unsigned short> : detail::integral_hasher {};
size_t operator()(unsigned short key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<signed short> { struct hasher<signed short> : detail::integral_hasher {};
size_t operator()(signed short key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<unsigned char> { struct hasher<unsigned char> : detail::integral_hasher {};
size_t operator()(unsigned char key) const {
return detail::integral_hash(key);
}
};
template <> template <>
struct hasher<signed char> { struct hasher<signed char> : detail::integral_hasher {};
size_t operator()(signed char key) const {
return detail::integral_hash(key);
}
};
// char is different type from both signed char and unsigned char. template <> // char is a different type from both signed char and unsigned char
template <> struct hasher<char> : detail::integral_hasher {};
struct hasher<char> {
using explicit_type = std::conditional<
std::is_signed<char>::value,
signed char,
unsigned char>::type;
size_t operator()(char key) const {
return detail::integral_hash(explicit_type(key));
}
};
template <> struct hasher<std::string> { template <> struct hasher<std::string> {
size_t operator()(const std::string& key) const { size_t operator()(const std::string& key) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment