Commit 3751f5de authored by Den Raskovalov's avatar Den Raskovalov Committed by Facebook Github Bot

add folly:hasher<bool>

Summary:
hasher<bool> is a trivial thing, but it is useful, if we implement a hash function for a class by writing something like:
  return folly::Hash()(boolField1_, boolField2_, intField3_, boolField4_, someField5_)

Reviewed By: ot

Differential Revision: D4535563

fbshipit-source-id: 5e6672c2eeaebbbac80a8a066a6a0ec4838810f4
parent 53367378
......@@ -361,6 +361,14 @@ struct Hash {
}
};
template <>
struct hasher<bool> {
size_t operator()(bool key) const {
// Make sure that all the output bits depend on the input.
return -static_cast<size_t>(key);
}
};
template<> struct hasher<int32_t> {
size_t operator()(int32_t key) const {
return hash::jenkins_rev_mix32(uint32_t(key));
......
......@@ -229,6 +229,39 @@ TEST(Hash, hash_combine) {
EXPECT_NE(hash_combine(1, 2), hash_combine(2, 1));
}
TEST(Hash, hash_bool) {
const auto hash = folly::Hash();
EXPECT_NE(hash(true), hash(false));
}
TEST(Hash, hash_bool10) {
const auto hash = folly::Hash();
std::set<size_t> values;
for (bool b1 : {false, true}) {
for (bool b2 : {false, true}) {
for (bool b3 : {false, true}) {
for (bool b4 : {false, true}) {
for (bool b5 : {false, true}) {
for (bool b6 : {false, true}) {
for (bool b7 : {false, true}) {
for (bool b8 : {false, true}) {
for (bool b9 : {false, true}) {
for (bool b10 : {false, true}) {
values.insert(
hash(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10));
}
}
}
}
}
}
}
}
}
}
EXPECT_EQ(values.size(), 1 << 10);
}
TEST(Hash, std_tuple) {
typedef std::tuple<int64_t, std::string, int32_t> tuple3;
tuple3 t(42, "foo", 1);
......
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