Commit 33f1f3d1 authored by Tom Jackson's avatar Tom Jackson Committed by Sara Golemon

hash_range

Test Plan: Unit test

Reviewed By: andrei.alexandrescu@fb.com

Subscribers: shilin

FB internal diff: D1497250
parent a8218a39
......@@ -59,6 +59,19 @@ inline size_t hash_combine_generic() {
return 0;
}
template <
class Iter,
class Hash = std::hash<typename std::iterator_traits<Iter>::value_type>>
uint64_t hash_range(Iter begin,
Iter end,
uint64_t hash = 0,
Hash hasher = Hash()) {
for (; begin != end; ++begin) {
hash = hash_128_to_64(hash, hasher(*begin));
}
return hash;
}
template <class Hasher, typename T, typename... Ts>
size_t hash_combine_generic(const T& t, const Ts&... ts) {
size_t seed = Hasher::hash(t);
......
......@@ -238,6 +238,19 @@ TEST(Hash, std_tuple) {
EXPECT_EQ("bar", m[t]);
}
namespace {
template <class T>
size_t hash_vector(const std::vector<T>& v) {
return hash_range(v.begin(), v.end());
}
}
TEST(Hash, hash_range) {
EXPECT_EQ(hash_vector<int32_t>({1, 2}), hash_vector<int16_t>({1, 2}));
EXPECT_NE(hash_vector<int>({2, 1}), hash_vector<int>({1, 2}));
EXPECT_EQ(hash_vector<int>({}), hash_vector<float>({}));
}
TEST(Hash, std_tuple_different_hash) {
typedef std::tuple<int64_t, std::string, int32_t> tuple3;
tuple3 t1(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