util: switch from Boost to std::unordered set/map

This commit replaces the ue2::unordered_{set,map} types with their STL
versions, with some new hashing utilities in util/hash.h. The new types
ue2_unordered_set<T> and ue2_unordered_map<Key, T> default to using the
ue2_hasher.

The header util/ue2_containers.h has been removed, and the flat_set/map
containers moved to util/flat_containers.h.
This commit is contained in:
Justin Viiret
2017-07-14 14:59:52 +10:00
committed by Matthew Barr
parent a425bb9b7c
commit 9cf66b6ac9
123 changed files with 1048 additions and 772 deletions

View File

@@ -34,16 +34,126 @@
#ifndef UTIL_HASH_H
#define UTIL_HASH_H
#include <iterator>
#include <boost/functional/hash/hash_fwd.hpp>
#include <functional>
#include <type_traits>
#include <utility>
namespace ue2 {
namespace hash_detail {
inline
void hash_combine_impl(size_t &seed, size_t value) {
// Note: constants explicitly truncated on 32-bit platforms.
const size_t a = (size_t)0x0b4e0ef37bc32127ULL;
const size_t b = (size_t)0x318f07b0c8eb9be9ULL;
seed ^= value * a;
seed += b;
}
/** \brief Helper that determines whether std::begin() exists for T. */
template<typename T>
struct is_container_check {
private:
template<typename C>
static auto has_begin_function(const C &obj) -> decltype(std::begin(obj)) {
return std::begin(obj);
}
static void has_begin_function(...) {
return;
}
using has_begin_type = decltype(has_begin_function(std::declval<T>()));
public:
static const bool value = !std::is_void<has_begin_type>::value;
};
/** \brief Type trait to enable on whether T is a container. */
template<typename T>
struct is_container
: public ::std::integral_constant<bool, is_container_check<T>::value> {};
/** \brief Helper that determines whether T::hash() exists. */
template<typename T>
struct has_hash_member_check {
private:
template<typename C>
static auto has_hash_member_function(const C &obj) -> decltype(obj.hash()) {
return obj.hash();
}
static void has_hash_member_function(...) {
return;
}
using has_hash = decltype(has_hash_member_function(std::declval<T>()));
public:
static const bool value = !std::is_void<has_hash>::value;
};
/** \brief Type trait to enable on whether T::hash() exists. */
template<typename T>
struct has_hash_member
: public ::std::integral_constant<bool, has_hash_member_check<T>::value> {};
/** \brief Default hash: falls back to std::hash. */
template<typename T, typename Enable = void>
struct ue2_hash {
using decayed_type = typename std::decay<T>::type;
size_t operator()(const T &obj) const {
return std::hash<decayed_type>()(obj);
}
};
/** \brief Hash for std::pair. */
template<typename A, typename B>
struct ue2_hash<std::pair<A, B>, void> {
size_t operator()(const std::pair<A, B> &p) const {
size_t v = 0;
hash_combine_impl(v, ue2_hash<A>()(p.first));
hash_combine_impl(v, ue2_hash<B>()(p.second));
return v;
}
};
/** \brief Hash for any type that has a hash() member function. */
template<typename T>
struct ue2_hash<T, typename std::enable_if<has_hash_member<T>::value>::type> {
size_t operator()(const T &obj) const {
return obj.hash();
}
};
/** \brief Hash for any container type that supports std::begin(). */
template<typename T>
struct ue2_hash<T, typename std::enable_if<is_container<T>::value &&
!has_hash_member<T>::value>::type> {
size_t operator()(const T &obj) const {
size_t v = 0;
for (const auto &elem : obj) {
using element_type = typename std::decay<decltype(elem)>::type;
hash_combine_impl(v, ue2_hash<element_type>()(elem));
}
return v;
}
};
/** \brief Hash for enum types. */
template<typename T>
struct ue2_hash<T, typename std::enable_if<std::is_enum<T>::value>::type> {
size_t operator()(const T &obj) const {
using utype = typename std::underlying_type<T>::type;
return ue2_hash<utype>()(static_cast<utype>(obj));
}
};
template<typename T>
void hash_combine(size_t &seed, const T &obj) {
hash_combine_impl(seed, ue2_hash<T>()(obj));
}
template<typename T>
void hash_build(size_t &v, const T &obj) {
boost::hash_combine(v, obj);
hash_combine(v, obj);
}
template<typename T, typename... Args>
@@ -54,6 +164,21 @@ void hash_build(size_t &v, const T &obj, Args&&... args) {
} // namespace hash_detail
using hash_detail::hash_combine;
/**
* \brief Hasher for general use.
*
* Provides operators for most standard containers and falls back to
* std::hash<T>.
*/
struct ue2_hasher {
template<typename T>
size_t operator()(const T &obj) const {
return hash_detail::ue2_hash<T>()(obj);
}
};
/**
* \brief Computes the combined hash of all its arguments.
*
@@ -70,15 +195,6 @@ size_t hash_all(Args&&... args) {
return v;
}
/**
* \brief Compute the hash of all the elements of any range on which we can
* call std::begin() and std::end().
*/
template<typename Range>
size_t hash_range(const Range &r) {
return boost::hash_range(std::begin(r), std::end(r));
}
} // namespace ue2
#endif // UTIL_HASH_H