Get initial prototype of transparent count() working

This commit is contained in:
LeonineKing1199
2021-11-19 15:29:57 -08:00
parent c8abaf32ee
commit f41b3e8295
3 changed files with 82 additions and 0 deletions

View File

@ -757,6 +757,8 @@ namespace boost {
size_type count(const key_type&) const;
template <class TransparentKey> size_type count(const TransparentKey&) const;
std::pair<iterator, iterator> equal_range(const key_type&);
std::pair<const_iterator, const_iterator> equal_range(
const key_type&) const;
@ -1837,6 +1839,21 @@ namespace boost {
return table_.find_node(k) ? 1 : 0;
}
template <class K, class T, class H, class P, class A>
template <class TransparentKey>
typename unordered_map<K, T, H, P, A>::size_type
unordered_map<K, T, H, P, A>::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 <class K, class T, class H, class P, class A>
std::pair<typename unordered_map<K, T, H, P, A>::iterator,
typename unordered_map<K, T, H, P, A>::iterator>

View File

@ -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 : :
: <define>BOOST_UNORDERED_USE_MOVE

View File

@ -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 <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>
#include "../helpers/postfix.hpp"
// clang-format on
#include "../helpers/test.hpp"
#include <boost/container_hash/hash.hpp>
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<int>()(k.x_);
}
std::size_t operator()(int const k) const {
return boost::hash<int>()(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<key, int, hasher, key_equal> 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()