libcruft-util/cruft/util/hash/tuple.hpp

39 lines
1008 B
C++

#pragma once
#include "../hash.hpp"
#include "./mix.hpp"
#include <tuple>
namespace cruft::hash {
/// Computes a hash for a tuple type by elementwise hashing, then variadic mixing.
///
/// \tparam ElementT The per-element hash type
/// \tparam MixT The mixing hash type. Must support variadic operator()
template <
typename ElementT,
typename MixT = cruft::hash::mixer
>
class tuple {
public:
using is_transparent = void;
template <typename ...ValueT>
auto
operator() (std::tuple<ValueT...> const &val) const
{
return compute (val, std::make_index_sequence<sizeof... (ValueT)> {});
}
private:
template <typename ...ValueT, std::size_t ...IndexV>
auto
compute (std::tuple<ValueT...> const &val, std::index_sequence<IndexV...>) const
{
return MixT {} (
ElementT {} (std::get<IndexV> (val))...
);
}
};
}