Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75606 - in trunk: boost/variant boost/variant/detail libs/variant/test
From: antoshkka_at_[hidden]
Date: 2011-11-22 11:05:45


Author: apolukhin
Date: 2011-11-22 11:05:44 EST (Tue, 22 Nov 2011)
New Revision: 75606
URL: http://svn.boost.org/trac/boost/changeset/75606

Log:
Fixes #6163 (hash function for variant added)
Added:
   trunk/boost/variant/detail/hash_variant.hpp (contents, props changed)
   trunk/libs/variant/test/hash_variant_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/variant/variant.hpp | 1 +
   trunk/libs/variant/test/Jamfile.v2 | 1 +
   2 files changed, 2 insertions(+), 0 deletions(-)

Added: trunk/boost/variant/detail/hash_variant.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/variant/detail/hash_variant.hpp 2011-11-22 11:05:44 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,48 @@
+//-----------------------------------------------------------------------------
+// boost variant/detail/hash_variant.hpp header file
+// See http://www.boost.org for updates, documentation, and revision history.
+//-----------------------------------------------------------------------------
+//
+// Copyright (c) 2011
+// Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+
+#ifndef BOOST_HASH_VARIANT_FUNCTION_HPP
+#define BOOST_HASH_VARIANT_FUNCTION_HPP
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <boost/variant/variant_fwd.hpp>
+#include <boost/variant/static_visitor.hpp>
+#include <boost/variant/apply_visitor.hpp>
+#include <boost/functional/hash_fwd.hpp>
+
+namespace boost {
+
+ namespace detail { namespace variant {
+ struct variant_hasher: public boost::static_visitor<std::size_t> {
+ template <class T>
+ std::size_t operator()(T const& val) const {
+ using namespace boost;
+ hash<T> hasher;
+ return hasher(val);
+ }
+ };
+ }}
+
+ template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
+ std::size_t hash_value(variant< BOOST_VARIANT_ENUM_PARAMS(T) > const& val) {
+ std::size_t seed = boost::apply_visitor(detail::variant::variant_hasher(), val);
+ hash_combine(seed, val.which());
+ return seed;
+ }
+}
+
+#endif
+

Modified: trunk/boost/variant/variant.hpp
==============================================================================
--- trunk/boost/variant/variant.hpp (original)
+++ trunk/boost/variant/variant.hpp 2011-11-22 11:05:44 EST (Tue, 22 Nov 2011)
@@ -32,6 +32,7 @@
 #include "boost/variant/detail/make_variant_list.hpp"
 #include "boost/variant/detail/over_sequence.hpp"
 #include "boost/variant/detail/visitation_impl.hpp"
+#include "boost/variant/detail/hash_variant.hpp"
 
 #include "boost/variant/detail/generic_result_type.hpp"
 #include "boost/variant/detail/has_nothrow_move.hpp"

Modified: trunk/libs/variant/test/Jamfile.v2
==============================================================================
--- trunk/libs/variant/test/Jamfile.v2 (original)
+++ trunk/libs/variant/test/Jamfile.v2 2011-11-22 11:05:44 EST (Tue, 22 Nov 2011)
@@ -32,6 +32,7 @@
     [ run variant_reference_test.cpp ]
     [ run variant_comparison_test.cpp ]
     [ run variant_visit_test.cpp ]
+ [ run hash_variant_test.cpp ]
    ;
 
 

Added: trunk/libs/variant/test/hash_variant_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/variant/test/hash_variant_test.cpp 2011-11-22 11:05:44 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,34 @@
+// Copyright (c) 2011
+// Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#include "boost/test/minimal.hpp"
+#include "boost/variant/detail/hash_variant.hpp"
+#include "boost/variant.hpp"
+#include "boost/functional/hash/hash.hpp"
+
+void run()
+{
+ typedef boost::variant<bool, int, unsigned int, double> variant_type;
+ boost::hash<variant_type> hasher;
+
+ variant_type bool_variant1 = true;
+ variant_type bool_variant2 = false;
+ variant_type int_variant = 1;
+ variant_type float_variant = 1.0;
+ variant_type uint_variant = static_cast<unsigned int>(1);
+
+ BOOST_CHECK(hasher(bool_variant1) != hasher(bool_variant2));
+ BOOST_CHECK(hasher(bool_variant1) == hasher(bool_variant1));
+ BOOST_CHECK(hasher(int_variant) != hasher(uint_variant));
+ BOOST_CHECK(hasher(float_variant) != hasher(uint_variant));
+}
+
+int test_main(int , char* [])
+{
+ run();
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk