diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index a9387ef3..aed6619b 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -757,6 +757,8 @@ namespace boost { size_type count(const key_type&) const; + template size_type count(const TransparentKey&) const; + std::pair equal_range(const key_type&); std::pair equal_range( const key_type&) const; @@ -1837,6 +1839,21 @@ namespace boost { return table_.find_node(k) ? 1 : 0; } + template + template + typename unordered_map::size_type + unordered_map::count(const TransparentKey& k) const + { + std::size_t const key_hash = + table::policy::apply_hash(this->hash_function(), k); + + P const& eq = this->key_eq(); + + node_pointer p = table_.find_node_impl(key_hash, k, eq); + + return (p ? 1 : 0); + } + template std::pair::iterator, typename unordered_map::iterator> diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index e87886aa..e91cfc98 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -65,6 +65,7 @@ test-suite unordered [ run unordered/detail_tests.cpp ] [ run unordered/deduction_tests.cpp ] [ run unordered/scoped_allocator.cpp /boost/filesystem//boost_filesystem ] + [ run unordered/transparent_tests.cpp ] [ run unordered/compile_set.cpp : : : BOOST_UNORDERED_USE_MOVE diff --git a/test/unordered/transparent_tests.cpp b/test/unordered/transparent_tests.cpp new file mode 100644 index 00000000..22c20905 --- /dev/null +++ b/test/unordered/transparent_tests.cpp @@ -0,0 +1,64 @@ +// Copyright 2021 Christian Mazakas. +// 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) + +// clang-format off +#include "../helpers/prefix.hpp" +#include +#include +#include "../helpers/postfix.hpp" +// clang-format on + +#include "../helpers/test.hpp" + +#include + +struct key { + int x_; + static int count_; + + explicit key(int x) : x_(x) { + ++count_; + } +}; + +int key::count_; + +UNORDERED_AUTO_TEST(transparent_count) { + key::count_ = 0; + + struct hasher { + std::size_t operator()(key const& k) const { + return boost::hash()(k.x_); + } + + std::size_t operator()(int const k) const { + return boost::hash()(k); + } + }; + + struct key_equal { + bool operator()(key const& k1, key const& k2) const { + return k1.x_ == k2.x_; + } + + bool operator()(key const& k1, int const x) const { + return k1.x_ == x; + } + + bool operator()(int const x, key const& k1 ) const { + return k1.x_ == x; + } + }; + + boost::unordered_map map; + map.insert({key(0), 1337}); + BOOST_TEST(key::count_ == 1); + + std::size_t const count = map.count(0); + + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 1); +} + +RUN_TESTS()