From 0a5f16ee81299cb0334d8142b719c99c94691aad Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 4 Feb 2013 10:24:23 +0900 Subject: [PATCH] support hash: pair, value_holder, optional, variant, tribool --- sprout/array/hash.hpp | 3 ++ sprout/bitset/hash.hpp | 3 ++ sprout/functional/hash/hash.hpp | 2 +- sprout/functional/hash/hash_fwd.hpp | 14 ++++++++ sprout/logic/tribool.hpp | 1 + sprout/logic/tribool/hash.hpp | 28 +++++++++++++++ sprout/logic/tribool/io.hpp | 6 ++-- sprout/optional.hpp | 1 + sprout/optional/hash.hpp | 22 ++++++++++++ sprout/pit/hash.hpp | 3 ++ sprout/sub_array/hash.hpp | 3 ++ sprout/utility/pair.hpp | 1 + sprout/utility/pair/hash.hpp | 20 +++++++++++ sprout/utility/value_holder.hpp | 1 + sprout/utility/value_holder/hash.hpp | 28 +++++++++++++++ sprout/utility/value_holder/value_holder.hpp | 3 ++ sprout/variant.hpp | 1 + sprout/variant/hash.hpp | 36 ++++++++++++++++++++ 18 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 sprout/logic/tribool/hash.hpp create mode 100644 sprout/optional/hash.hpp create mode 100644 sprout/utility/pair/hash.hpp create mode 100644 sprout/utility/value_holder/hash.hpp create mode 100644 sprout/variant/hash.hpp diff --git a/sprout/array/hash.hpp b/sprout/array/hash.hpp index 0abd56f9..a0c2ff53 100644 --- a/sprout/array/hash.hpp +++ b/sprout/array/hash.hpp @@ -7,6 +7,9 @@ #include namespace sprout { + // + // hash_value + // template inline SPROUT_CONSTEXPR std::size_t hash_value(sprout::array const& v) { diff --git a/sprout/bitset/hash.hpp b/sprout/bitset/hash.hpp index 1b271c75..2178dc72 100644 --- a/sprout/bitset/hash.hpp +++ b/sprout/bitset/hash.hpp @@ -8,6 +8,9 @@ namespace sprout { // 20.5.3 hash support + // + // hash_value + // template inline SPROUT_CONSTEXPR std::size_t hash_value(sprout::bitset const& v) { diff --git a/sprout/functional/hash/hash.hpp b/sprout/functional/hash/hash.hpp index 9c4b027f..29e00293 100644 --- a/sprout/functional/hash/hash.hpp +++ b/sprout/functional/hash/hash.hpp @@ -5,8 +5,8 @@ #include #include #include -#include #include +#include #include namespace sprout { diff --git a/sprout/functional/hash/hash_fwd.hpp b/sprout/functional/hash/hash_fwd.hpp index b37aa6f1..1227a3d9 100644 --- a/sprout/functional/hash/hash_fwd.hpp +++ b/sprout/functional/hash/hash_fwd.hpp @@ -30,6 +30,20 @@ namespace sprout { SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last); template SPROUT_CONSTEXPR std::size_t hash_range(std::size_t seed, Iterator first, Iterator last); + + // + // hash_values_combine + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_values_combine(std::size_t seed, Args const&... args); + + // + // hash_values + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_values(Args const&... args); } // namespace sprout #endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_FWD_HPP diff --git a/sprout/logic/tribool.hpp b/sprout/logic/tribool.hpp index 56894b26..ae21d22a 100644 --- a/sprout/logic/tribool.hpp +++ b/sprout/logic/tribool.hpp @@ -5,5 +5,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_LOGIC_TRIBOOL_HPP diff --git a/sprout/logic/tribool/hash.hpp b/sprout/logic/tribool/hash.hpp new file mode 100644 index 00000000..74635deb --- /dev/null +++ b/sprout/logic/tribool/hash.hpp @@ -0,0 +1,28 @@ +#ifndef SPROUT_LOGIC_TRIBOOL_HASH_HPP +#define SPROUT_LOGIC_TRIBOOL_HASH_HPP + +#include +#include +#include +#include + +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 diff --git a/sprout/logic/tribool/io.hpp b/sprout/logic/tribool/io.hpp index 5b0a2a0e..fc47e96a 100644 --- a/sprout/logic/tribool/io.hpp +++ b/sprout/logic/tribool/io.hpp @@ -1,5 +1,5 @@ -#ifndef SPROUT_LOGIC_TRIBOOL_TRIBOOL_IO_HPP -#define SPROUT_LOGIC_TRIBOOL_TRIBOOL_IO_HPP +#ifndef SPROUT_LOGIC_TRIBOOL_IO_HPP +#define SPROUT_LOGIC_TRIBOOL_IO_HPP #include #include @@ -206,4 +206,4 @@ namespace sprout { } // namespace logic } // namespace sprout -#endif // #ifndef SPROUT_LOGIC_TRIBOOL_TRIBOOL_IO_HPP +#endif // #ifndef SPROUT_LOGIC_TRIBOOL_IO_HPP diff --git a/sprout/optional.hpp b/sprout/optional.hpp index 3b548a5d..2597d707 100644 --- a/sprout/optional.hpp +++ b/sprout/optional.hpp @@ -8,5 +8,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_OPTIONAL_HPP diff --git a/sprout/optional/hash.hpp b/sprout/optional/hash.hpp new file mode 100644 index 00000000..37bf9cbd --- /dev/null +++ b/sprout/optional/hash.hpp @@ -0,0 +1,22 @@ +#ifndef SPROUT_OPTIONAL_HASH_HPP +#define SPROUT_OPTIONAL_HASH_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // hash_value + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_value(sprout::optional const& v) { + return v.is_initialized() ? sprout::to_hash(*v) + : 0 + ; + } +} // namespace sprout + +#endif // #ifndef SPROUT_OPTIONAL_HASH_HPP diff --git a/sprout/pit/hash.hpp b/sprout/pit/hash.hpp index 1d6accd0..426bacc4 100644 --- a/sprout/pit/hash.hpp +++ b/sprout/pit/hash.hpp @@ -7,6 +7,9 @@ #include namespace sprout { + // + // hash_value + // template inline SPROUT_CONSTEXPR std::size_t hash_value(sprout::pit const& v) { diff --git a/sprout/sub_array/hash.hpp b/sprout/sub_array/hash.hpp index b6d2b262..d56b9cf6 100644 --- a/sprout/sub_array/hash.hpp +++ b/sprout/sub_array/hash.hpp @@ -6,6 +6,9 @@ #include namespace sprout { + // + // hash_value + // template SPROUT_CONSTEXPR std::size_t hash_value(sprout::sub_array const& v) { diff --git a/sprout/utility/pair.hpp b/sprout/utility/pair.hpp index ae82b5f2..8dbdf4e5 100644 --- a/sprout/utility/pair.hpp +++ b/sprout/utility/pair.hpp @@ -7,5 +7,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_UTILITY_PAIR_HPP diff --git a/sprout/utility/pair/hash.hpp b/sprout/utility/pair/hash.hpp new file mode 100644 index 00000000..a29d922e --- /dev/null +++ b/sprout/utility/pair/hash.hpp @@ -0,0 +1,20 @@ +#ifndef SPROUT_UTILITY_PAIR_HASH_HPP +#define SPROUT_UTILITY_PAIR_HASH_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // hash_value + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_value(sprout::pair const& v) { + return sprout::hash_values(v.first, v.second); + } +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_PAIR_HASH_HPP diff --git a/sprout/utility/value_holder.hpp b/sprout/utility/value_holder.hpp index 9105852b..ce4fc3c4 100644 --- a/sprout/utility/value_holder.hpp +++ b/sprout/utility/value_holder.hpp @@ -4,5 +4,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP diff --git a/sprout/utility/value_holder/hash.hpp b/sprout/utility/value_holder/hash.hpp new file mode 100644 index 00000000..ab6633b4 --- /dev/null +++ b/sprout/utility/value_holder/hash.hpp @@ -0,0 +1,28 @@ +#ifndef SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP +#define SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // hash_value + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_value(sprout::value_holder const& v) { + return sprout::to_hash(*v) + ; + } + template + inline SPROUT_CONSTEXPR std::size_t + hash_value(sprout::value_holder const& v) { + return v.is_initialized() ? sprout::to_hash(*v) + : 0 + ; + } +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP diff --git a/sprout/utility/value_holder/value_holder.hpp b/sprout/utility/value_holder/value_holder.hpp index 26d7bee3..f9ed1e14 100644 --- a/sprout/utility/value_holder/value_holder.hpp +++ b/sprout/utility/value_holder/value_holder.hpp @@ -196,6 +196,9 @@ namespace sprout { SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const SPROUT_NOEXCEPT { return get_pointer(); } + SPROUT_CONSTEXPR bool is_initialized() const SPROUT_NOEXCEPT { + return !!get_pointer(); + } }; // diff --git a/sprout/variant.hpp b/sprout/variant.hpp index 8da23dae..ebadbee3 100644 --- a/sprout/variant.hpp +++ b/sprout/variant.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include diff --git a/sprout/variant/hash.hpp b/sprout/variant/hash.hpp new file mode 100644 index 00000000..9e1c6bdb --- /dev/null +++ b/sprout/variant/hash.hpp @@ -0,0 +1,36 @@ +#ifndef SPROUT_VARIANT_HASH_HPP +#define SPROUT_VARIANT_HASH_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + class variant_hash_visitor + : public sprout::static_visitor + { + public: + template + SPROUT_CONSTEXPR std::size_t operator()(T const& t) const { + return sprout::to_hash(t); + } + }; + } // namespace detail + // + // hash_value + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_value(sprout::variant 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