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

Added finalizer to default hash function. #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/tsl/robin_growth_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,35 @@ class prime_growth_policy {
} // namespace rh
} // namespace tsl

namespace tsl {

/**
* Wrapper around std::hash which adds hash finalizer missing in libstd-c++ and
* libc++. It should be used with power_of_two_growth_policy.
*/
template <class Key>
struct hash : public std::hash<Key> {
std::size_t operator()(const Key& key) const {
std::size_t h = std::hash<Key>::operator()(key);

#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
#if SIZE_MAX >= ULLONG_MAX
// 64-bit finalizer from MurmurHash2
h ^= h >> 47;
h *= 0xc6a4a7935bd1e995ull;
h ^= h >> 47;
#else
// 32-bit finalizer from MurmurHash2
h ^= h >> 13;
h *= 0x5bd1e995u;
h ^= h >> 15;
#endif
#endif

return h;
}
};

} // namespace tsl

#endif
2 changes: 1 addition & 1 deletion include/tsl/robin_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
is_power_of_two_policy<GrowthPolicy>::value) &&
// Don't store the hash for primitive types with default hash.
(!std::is_arithmetic<key_type>::value ||
!std::is_same<Hash, std::hash<key_type>>::value));
!std::is_same<Hash, tsl::hash<key_type>>::value));

/**
* Only use the stored hash on lookup if we are explicitly asked. We are not
Expand Down
4 changes: 2 additions & 2 deletions include/tsl/robin_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace tsl {
* insert, invalidate the iterators.
* - erase: always invalidate the iterators.
*/
template <class Key, class T, class Hash = std::hash<Key>,
template <class Key, class T, class Hash = tsl::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<Key, T>>,
bool StoreHash = false,
Expand Down Expand Up @@ -803,7 +803,7 @@ class robin_map {
* Same as `tsl::robin_map<Key, T, Hash, KeyEqual, Allocator, StoreHash,
* tsl::rh::prime_growth_policy>`.
*/
template <class Key, class T, class Hash = std::hash<Key>,
template <class Key, class T, class Hash = tsl::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<Key, T>>,
bool StoreHash = false>
Expand Down
4 changes: 2 additions & 2 deletions include/tsl/robin_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace tsl {
* insert, invalidate the iterators.
* - erase: always invalidate the iterators.
*/
template <class Key, class Hash = std::hash<Key>,
template <class Key, class Hash = tsl::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<Key>, bool StoreHash = false,
class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>>
Expand Down Expand Up @@ -657,7 +657,7 @@ class robin_set {
* Same as `tsl::robin_set<Key, Hash, KeyEqual, Allocator, StoreHash,
* tsl::rh::prime_growth_policy>`.
*/
template <class Key, class Hash = std::hash<Key>,
template <class Key, class Hash = tsl::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<Key>, bool StoreHash = false>
using robin_pg_set = robin_set<Key, Hash, KeyEqual, Allocator, StoreHash,
Expand Down
19 changes: 19 additions & 0 deletions tests/robin_set_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,23 @@ BOOST_AUTO_TEST_CASE(test_erase_fast) {
BOOST_CHECK(set.size() == 0);
}

BOOST_AUTO_TEST_CASE(test_hash_function_default_integer) {
// Brief quality check for default integer hash function.
// Most importantly, make sure it is not identity function like std::hash on GCC.
// See: https://github.com/Tessil/robin-map/issues/73
const std::size_t nb_values = 1 << 18;
const std::size_t shift = 32;

using Set = tsl::robin_set<uint64_t>;
Set set;

for (uint64_t i = 0; i < nb_values; i++) {
uint64_t hash = set.hash_function()(uint64_t(i) << shift);
set.insert(hash & 0xFFFFFF);
}

double imageRatio = double(set.size()) / nb_values;
BOOST_CHECK(imageRatio >= 0.9);
}

BOOST_AUTO_TEST_SUITE_END()