add tuple hash support

This commit is contained in:
bolero-MURAKAMI 2012-07-15 23:06:29 +09:00
parent 2d039ad6b4
commit 5918f1c773
3 changed files with 64 additions and 4 deletions

View file

@ -182,16 +182,43 @@ namespace sprout {
// hash_range
//
template<typename Iterator>
inline SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last) {
return sprout::hash_range(0, first, last);
}
template<typename Iterator>
inline SPROUT_CONSTEXPR std::size_t hash_range(std::size_t seed, Iterator first, Iterator last) {
return first != last
? sprout::hash_range(sprout::hash_combine(seed, *first), sprout::next(first), last)
: seed
;
}
template<typename Iterator>
inline SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last) {
return sprout::hash_range(0, first, last);
}
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR std::size_t hash_values_combine_impl(std::size_t seed, T const& v) {
return sprout::hash_combine(seed, v);
}
template<typename Head, typename... Tail>
inline SPROUT_CONSTEXPR std::size_t hash_values_combine_impl(std::size_t seed, Head const& head, Tail const&... tail) {
return sprout::detail::hash_values_combine_impl(sprout::hash_combine(seed, head), tail...);
}
} // namespace detail
//
// hash_values_combine
//
template<typename... Args>
inline SPROUT_CONSTEXPR std::size_t hash_values_combine(std::size_t seed, Args const&... args) {
return sprout::detail::hash_values_combine_impl(seed, args...);
}
//
// hash_values
//
template<typename... Args>
inline SPROUT_CONSTEXPR std::size_t hash_values(Args const&... args) {
return sprout::hash_values_combine(0, args...);
}
//
// hash

View file

@ -9,5 +9,6 @@
#include <sprout/tuple/tuple/make_tuple.hpp>
#include <sprout/tuple/tuple/comparison.hpp>
#include <sprout/tuple/tuple/type_traits.hpp>
#include <sprout/tuple/tuple/hash.hpp>
#endif // #ifndef SPROUT_TUPLE_TUPLE_HPP

View file

@ -0,0 +1,32 @@
#ifndef SPROUT_TUPLE_TUPLE_HASH_HPP
#define SPROUT_TUPLE_TUPLE_HASH_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/functional/hash/hash.hpp>
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.hpp>
namespace sprout {
namespace detail {
template<typename... Types, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR std::size_t tuple_hash_value_impl(
sprout::tuples::tuple<Types...> const& v,
sprout::index_tuple<Indexes...>
)
{
return sprout::hash_values(sprout::tuples::get<Indexes>(v)...);
}
} // namespace detail
template<typename... Types>
inline SPROUT_CONSTEXPR std::size_t hash_value(sprout::tuples::tuple<Types...> const& v) {
return sprout::detail::tuple_hash_value_impl(
v,
sprout::index_range<0, sprout::tuples::tuple_size<sprout::tuples::tuple<Types...> >::value>::make()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_TUPLE_TUPLE_HASH_HPP