support hash: pair, value_holder, optional, variant, tribool

This commit is contained in:
bolero-MURAKAMI 2013-02-04 10:24:23 +09:00
parent f3b405ad0f
commit 0a5f16ee81
18 changed files with 172 additions and 4 deletions

View file

@ -7,6 +7,9 @@
#include <sprout/array/array.hpp> #include <sprout/array/array.hpp>
namespace sprout { namespace sprout {
//
// hash_value
//
template<typename T, std::size_t N> template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR std::size_t inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::array<T, N> const& v) { hash_value(sprout::array<T, N> const& v) {

View file

@ -8,6 +8,9 @@
namespace sprout { namespace sprout {
// 20.5.3 hash support // 20.5.3 hash support
//
// hash_value
//
template<std::size_t N> template<std::size_t N>
inline SPROUT_CONSTEXPR std::size_t inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::bitset<N> const& v) { hash_value(sprout::bitset<N> const& v) {

View file

@ -5,8 +5,8 @@
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/functional/hash/hash_fwd.hpp> #include <sprout/functional/hash/hash_fwd.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/type_traits/enabler_if.hpp> #include <sprout/type_traits/enabler_if.hpp>
namespace sprout { namespace sprout {

View file

@ -30,6 +30,20 @@ namespace sprout {
SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last); SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last);
template<typename Iterator> template<typename Iterator>
SPROUT_CONSTEXPR std::size_t hash_range(std::size_t seed, Iterator first, Iterator last); SPROUT_CONSTEXPR std::size_t hash_range(std::size_t seed, Iterator first, Iterator last);
//
// hash_values_combine
//
template<typename... Args>
inline SPROUT_CONSTEXPR std::size_t
hash_values_combine(std::size_t seed, Args const&... args);
//
// hash_values
//
template<typename... Args>
inline SPROUT_CONSTEXPR std::size_t
hash_values(Args const&... args);
} // namespace sprout } // namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_FWD_HPP #endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_FWD_HPP

View file

@ -5,5 +5,6 @@
#include <sprout/logic/tribool/tribool_fwd.hpp> #include <sprout/logic/tribool/tribool_fwd.hpp>
#include <sprout/logic/tribool/tribool.hpp> #include <sprout/logic/tribool/tribool.hpp>
#include <sprout/logic/tribool/io.hpp> #include <sprout/logic/tribool/io.hpp>
#include <sprout/logic/tribool/hash.hpp>
#endif // #ifndef SPROUT_LOGIC_TRIBOOL_HPP #endif // #ifndef SPROUT_LOGIC_TRIBOOL_HPP

View file

@ -0,0 +1,28 @@
#ifndef SPROUT_LOGIC_TRIBOOL_HASH_HPP
#define SPROUT_LOGIC_TRIBOOL_HASH_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/functional/hash/hash.hpp>
#include <sprout/logic/tribool/tribool.hpp>
namespace sprout {
//
// hash_value
//
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::logic::indeterminate_keyword_t) {
return sprout::logic::tribool::indeterminate_value;
}
namespace logic {
//
// hash_value
//
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::logic::tribool const& v) {
return sprout::to_hash(v.value);
}
} // namespace logic
} // namespace sprout
#endif // #ifndef SPROUT_LOGIC_TRIBOOL_HASH_HPP

View file

@ -1,5 +1,5 @@
#ifndef SPROUT_LOGIC_TRIBOOL_TRIBOOL_IO_HPP #ifndef SPROUT_LOGIC_TRIBOOL_IO_HPP
#define SPROUT_LOGIC_TRIBOOL_TRIBOOL_IO_HPP #define SPROUT_LOGIC_TRIBOOL_IO_HPP
#include <locale> #include <locale>
#include <string> #include <string>
@ -206,4 +206,4 @@ namespace sprout {
} // namespace logic } // namespace logic
} // namespace sprout } // namespace sprout
#endif // #ifndef SPROUT_LOGIC_TRIBOOL_TRIBOOL_IO_HPP #endif // #ifndef SPROUT_LOGIC_TRIBOOL_IO_HPP

View file

@ -8,5 +8,6 @@
#include <sprout/optional/nullopt.hpp> #include <sprout/optional/nullopt.hpp>
#include <sprout/optional/make_optional.hpp> #include <sprout/optional/make_optional.hpp>
#include <sprout/optional/get.hpp> #include <sprout/optional/get.hpp>
#include <sprout/optional/hash.hpp>
#endif // #ifndef SPROUT_OPTIONAL_HPP #endif // #ifndef SPROUT_OPTIONAL_HPP

22
sprout/optional/hash.hpp Normal file
View file

@ -0,0 +1,22 @@
#ifndef SPROUT_OPTIONAL_HASH_HPP
#define SPROUT_OPTIONAL_HASH_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/functional/hash/hash.hpp>
#include <sprout/optional/optional.hpp>
namespace sprout {
//
// hash_value
//
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::optional<T> const& v) {
return v.is_initialized() ? sprout::to_hash(*v)
: 0
;
}
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_HASH_HPP

View file

@ -7,6 +7,9 @@
#include <sprout/pit/pit.hpp> #include <sprout/pit/pit.hpp>
namespace sprout { namespace sprout {
//
// hash_value
//
template<typename Container> template<typename Container>
inline SPROUT_CONSTEXPR std::size_t inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::pit<Container> const& v) { hash_value(sprout::pit<Container> const& v) {

View file

@ -6,6 +6,9 @@
#include <sprout/sub_array/sub_array.hpp> #include <sprout/sub_array/sub_array.hpp>
namespace sprout { namespace sprout {
//
// hash_value
//
template<typename Container> template<typename Container>
SPROUT_CONSTEXPR std::size_t SPROUT_CONSTEXPR std::size_t
hash_value(sprout::sub_array<Container> const& v) { hash_value(sprout::sub_array<Container> const& v) {

View file

@ -7,5 +7,6 @@
#include <sprout/utility/pair/tuple.hpp> #include <sprout/utility/pair/tuple.hpp>
#include <sprout/utility/pair/make_pair.hpp> #include <sprout/utility/pair/make_pair.hpp>
#include <sprout/utility/pair/access.hpp> #include <sprout/utility/pair/access.hpp>
#include <sprout/utility/pair/hash.hpp>
#endif // #ifndef SPROUT_UTILITY_PAIR_HPP #endif // #ifndef SPROUT_UTILITY_PAIR_HPP

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_UTILITY_PAIR_HASH_HPP
#define SPROUT_UTILITY_PAIR_HASH_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/functional/hash/hash.hpp>
#include <sprout/utility/pair/pair.hpp>
namespace sprout {
//
// hash_value
//
template<typename T1, typename T2>
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::pair<T1, T2> const& v) {
return sprout::hash_values(v.first, v.second);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_PAIR_HASH_HPP

View file

@ -4,5 +4,6 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/utility/value_holder/value_holder.hpp> #include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/value_holder/get.hpp> #include <sprout/utility/value_holder/get.hpp>
#include <sprout/utility/value_holder/hash.hpp>
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP #endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP

View file

@ -0,0 +1,28 @@
#ifndef SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP
#define SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/functional/hash/hash.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
namespace sprout {
//
// hash_value
//
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::value_holder<T> const& v) {
return sprout::to_hash(*v)
;
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::value_holder<T&> const& v) {
return v.is_initialized() ? sprout::to_hash(*v)
: 0
;
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP

View file

@ -196,6 +196,9 @@ namespace sprout {
SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const SPROUT_NOEXCEPT { SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const SPROUT_NOEXCEPT {
return get_pointer(); return get_pointer();
} }
SPROUT_CONSTEXPR bool is_initialized() const SPROUT_NOEXCEPT {
return !!get_pointer();
}
}; };
// //

View file

@ -6,6 +6,7 @@
#include <sprout/variant/io.hpp> #include <sprout/variant/io.hpp>
#include <sprout/variant/tuple.hpp> #include <sprout/variant/tuple.hpp>
#include <sprout/variant/get.hpp> #include <sprout/variant/get.hpp>
#include <sprout/variant/hash.hpp>
#include <sprout/variant/static_visitor.hpp> #include <sprout/variant/static_visitor.hpp>
#include <sprout/variant/static_variant_visitor.hpp> #include <sprout/variant/static_variant_visitor.hpp>
#include <sprout/variant/as_visitor.hpp> #include <sprout/variant/as_visitor.hpp>

36
sprout/variant/hash.hpp Normal file
View file

@ -0,0 +1,36 @@
#ifndef SPROUT_VARIANT_HASH_HPP
#define SPROUT_VARIANT_HASH_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/functional/hash/hash.hpp>
#include <sprout/variant/variant.hpp>
#include <sprout/variant/static_visitor.hpp>
#include <sprout/variant/apply_visitor.hpp>
namespace sprout {
namespace detail {
class variant_hash_visitor
: public sprout::static_visitor<std::size_t>
{
public:
template<typename T>
SPROUT_CONSTEXPR std::size_t operator()(T const& t) const {
return sprout::to_hash(t);
}
};
} // namespace detail
//
// hash_value
//
template<typename... Types>
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::variant<Types...> const& v) {
return sprout::hash_combine(
sprout::apply_visitor(sprout::detail::variant_hash_visitor(), v),
v.which()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_VARIANT_HASH_HPP