forked from boostorg/optional
Add std::hash specialization
This commit is contained in:
49
include/boost/optional/detail/optional_hash.hpp
Normal file
49
include/boost/optional/detail/optional_hash.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright (C) 2022 Andrzej Krzemienski.
|
||||
//
|
||||
// Use, modification, and distribution is subject to 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/optional for documentation.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// akrzemi1@gmail.com
|
||||
|
||||
#ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_HASH_AJK_20MAY2022_HPP
|
||||
#define BOOST_OPTIONAL_DETAIL_OPTIONAL_HASH_AJK_20MAY2022_HPP
|
||||
|
||||
#include <boost/optional/optional_fwd.hpp>
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if !defined(BOOST_OPTIONAL_CONFIG_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <typename T>
|
||||
struct hash<boost::optional<T> >
|
||||
{
|
||||
typedef typename hash<T>::result_type result_type;
|
||||
typedef boost::optional<T> argument_type;
|
||||
|
||||
BOOST_CONSTEXPR result_type operator()(const argument_type& arg) const {
|
||||
return arg ? std::hash<T>()(*arg) : result_type();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct hash<boost::optional<T&> >
|
||||
{
|
||||
typedef typename hash<T>::result_type result_type;
|
||||
typedef boost::optional<T&> argument_type;
|
||||
|
||||
BOOST_CONSTEXPR result_type operator()(const argument_type& arg) const {
|
||||
return arg ? std::hash<T>()(*arg) : result_type();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // !defined(BOOST_OPTIONAL_CONFIG_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
#endif // header guard
|
@ -65,6 +65,7 @@
|
||||
#include <boost/optional/detail/optional_config.hpp>
|
||||
#include <boost/optional/detail/optional_factory_support.hpp>
|
||||
#include <boost/optional/detail/optional_aligned_storage.hpp>
|
||||
#include <boost/optional/detail/optional_hash.hpp>
|
||||
|
||||
namespace boost { namespace optional_detail {
|
||||
|
||||
|
@ -26,6 +26,7 @@ import testing ;
|
||||
[ run optional_test_empty_braces.cpp ]
|
||||
[ run optional_test_make_optional.cpp ]
|
||||
[ run optional_test_flat_map.cpp ]
|
||||
[ run optional_test_hash.cpp ]
|
||||
[ run optional_test_map.cpp ]
|
||||
[ run optional_test_tie.cpp ]
|
||||
[ run optional_test_ref_assign_portable_minimum.cpp ]
|
||||
|
64
test/optional_test_hash.cpp
Normal file
64
test/optional_test_hash.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright (C) 2014 Andrzej Krzemienski.
|
||||
//
|
||||
// Use, modification, and distribution is subject to 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)
|
||||
//
|
||||
// See http://www.boost.org/lib/optional for documentation.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// akrzemi1@gmail.com
|
||||
|
||||
#include "boost/optional/optional.hpp"
|
||||
#include "boost/config.hpp"
|
||||
#include "boost/core/lightweight_test.hpp"
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) && !defined(BOOST_OPTIONAL_CONFIG_DO_NOT_SPECIALIZE_STD_HASH)
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
void test_unordered_map()
|
||||
{
|
||||
std::unordered_set<boost::optional<int> > set;
|
||||
set.insert(boost::optional<int>(1));
|
||||
set.insert(boost::optional<int>(1));
|
||||
|
||||
|
||||
BOOST_TEST(set.size() == 1u);
|
||||
BOOST_TEST(set.find(boost::optional<int>(1)) != set.end());
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void test_unordered_map()
|
||||
{}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(BOOST_OPTIONAL_CONFIG_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
void tets_hash()
|
||||
{
|
||||
std::hash<boost::optional<int> > hash_int;
|
||||
boost::optional<int> oN;
|
||||
boost::optional<int> o1(1);
|
||||
|
||||
BOOST_TEST(hash_int(oN) == hash_int(oN));
|
||||
BOOST_TEST(hash_int(o1) == hash_int(o1));
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void tets_hash()
|
||||
{}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
test_unordered_map();
|
||||
tets_hash();
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user