mirror of
https://github.com/boostorg/unordered.git
synced 2025-07-31 20:04:29 +02:00
Merge pull request #71 from cmazakas/set-heterogeneous-count
Set Heterogeneous `count()`
This commit is contained in:
@@ -500,6 +500,18 @@ namespace boost {
|
||||
|
||||
size_type count(const key_type&) const;
|
||||
|
||||
template <class Key>
|
||||
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
|
||||
size_type>::type
|
||||
count(const Key& k) const
|
||||
{
|
||||
node_pointer n = table_.find_node_impl(
|
||||
table::policy::apply_hash(this->hash_function(), k), k,
|
||||
this->key_eq());
|
||||
|
||||
return n ? 1 : 0;
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(
|
||||
const key_type&) const;
|
||||
|
||||
|
@@ -69,7 +69,7 @@ struct key_equal
|
||||
|
||||
void count_reset() { key::count_ = 0; }
|
||||
|
||||
template <class UnorderedMap> void test_transparent_count()
|
||||
template <class UnorderedMap> void test_map_transparent_count()
|
||||
{
|
||||
count_reset();
|
||||
|
||||
@@ -97,7 +97,7 @@ template <class UnorderedMap> void test_transparent_count()
|
||||
BOOST_TEST_EQ(key::count_, expected_key_count);
|
||||
}
|
||||
|
||||
template <class UnorderedMap> void test_non_transparent_count()
|
||||
template <class UnorderedMap> void test_map_non_transparent_count()
|
||||
{
|
||||
count_reset();
|
||||
|
||||
@@ -131,6 +131,68 @@ template <class UnorderedMap> void test_non_transparent_count()
|
||||
BOOST_TEST_EQ(key::count_, key_count);
|
||||
}
|
||||
|
||||
template <class UnorderedSet> void test_set_transparent_count()
|
||||
{
|
||||
count_reset();
|
||||
|
||||
UnorderedSet set;
|
||||
|
||||
set.insert(0);
|
||||
set.insert(1);
|
||||
set.insert(2);
|
||||
set.insert(0);
|
||||
set.insert(0);
|
||||
set.insert(0);
|
||||
|
||||
int const expected_key_count = key::count_;
|
||||
|
||||
std::size_t count = 0;
|
||||
count = set.count(0);
|
||||
BOOST_TEST_EQ(count, set.size() - 2);
|
||||
|
||||
count = set.count(1);
|
||||
BOOST_TEST_EQ(count, 1);
|
||||
|
||||
count = set.count(1337);
|
||||
BOOST_TEST_EQ(count, 0);
|
||||
|
||||
BOOST_TEST_EQ(key::count_, expected_key_count);
|
||||
}
|
||||
|
||||
template <class UnorderedSet> void test_set_non_transparent_count()
|
||||
{
|
||||
count_reset();
|
||||
|
||||
UnorderedSet set;
|
||||
|
||||
set.insert(0);
|
||||
set.insert(1);
|
||||
set.insert(2);
|
||||
set.insert(0);
|
||||
set.insert(0);
|
||||
set.insert(0);
|
||||
|
||||
int key_count = key::count_;
|
||||
|
||||
std::size_t count = 0;
|
||||
count = set.count(0);
|
||||
++key_count;
|
||||
|
||||
BOOST_TEST_EQ(count, set.size() - 2);
|
||||
BOOST_TEST_EQ(key::count_, key_count);
|
||||
|
||||
count = set.count(1);
|
||||
++key_count;
|
||||
|
||||
BOOST_TEST_EQ(count, 1);
|
||||
|
||||
count = set.count(1337);
|
||||
++key_count;
|
||||
|
||||
BOOST_TEST_EQ(count, 0);
|
||||
BOOST_TEST_EQ(key::count_, key_count);
|
||||
}
|
||||
|
||||
template <class UnorderedMap> void test_map_transparent_find()
|
||||
{
|
||||
count_reset();
|
||||
@@ -1388,7 +1450,7 @@ void test_unordered_map()
|
||||
transparent_key_equal>
|
||||
unordered_map;
|
||||
|
||||
test_transparent_count<unordered_map>();
|
||||
test_map_transparent_count<unordered_map>();
|
||||
test_map_transparent_find<unordered_map>();
|
||||
test_map_transparent_equal_range<unordered_map>();
|
||||
test_map_transparent_erase<unordered_map>();
|
||||
@@ -1400,7 +1462,7 @@ void test_unordered_map()
|
||||
//
|
||||
typedef boost::unordered_map<key, int, hasher, key_equal> unordered_map;
|
||||
|
||||
test_non_transparent_count<unordered_map>();
|
||||
test_map_non_transparent_count<unordered_map>();
|
||||
test_map_non_transparent_find<unordered_map>();
|
||||
test_map_non_transparent_equal_range<unordered_map>();
|
||||
test_map_non_transparent_erase<unordered_map>();
|
||||
@@ -1413,7 +1475,7 @@ void test_unordered_map()
|
||||
typedef boost::unordered_map<key, int, transparent_hasher, key_equal>
|
||||
unordered_map;
|
||||
|
||||
test_non_transparent_count<unordered_map>();
|
||||
test_map_non_transparent_count<unordered_map>();
|
||||
test_map_non_transparent_find<unordered_map>();
|
||||
test_map_non_transparent_equal_range<unordered_map>();
|
||||
test_map_non_transparent_erase<unordered_map>();
|
||||
@@ -1426,7 +1488,7 @@ void test_unordered_map()
|
||||
typedef boost::unordered_map<key, int, hasher, transparent_key_equal>
|
||||
unordered_map;
|
||||
|
||||
test_non_transparent_count<unordered_map>();
|
||||
test_map_non_transparent_count<unordered_map>();
|
||||
test_map_non_transparent_find<unordered_map>();
|
||||
test_map_non_transparent_equal_range<unordered_map>();
|
||||
test_map_non_transparent_erase<unordered_map>();
|
||||
@@ -1441,7 +1503,7 @@ void test_unordered_multimap()
|
||||
transparent_key_equal>
|
||||
unordered_multimap;
|
||||
|
||||
test_transparent_count<unordered_multimap>();
|
||||
test_map_transparent_count<unordered_multimap>();
|
||||
test_map_transparent_find<unordered_multimap>();
|
||||
test_map_transparent_equal_range<unordered_multimap>();
|
||||
test_map_transparent_erase<unordered_multimap>();
|
||||
@@ -1454,7 +1516,7 @@ void test_unordered_multimap()
|
||||
typedef boost::unordered_multimap<key, int, hasher, key_equal>
|
||||
unordered_multimap;
|
||||
|
||||
test_non_transparent_count<unordered_multimap>();
|
||||
test_map_non_transparent_count<unordered_multimap>();
|
||||
test_map_non_transparent_find<unordered_multimap>();
|
||||
test_map_non_transparent_equal_range<unordered_multimap>();
|
||||
test_map_non_transparent_erase<unordered_multimap>();
|
||||
@@ -1467,7 +1529,7 @@ void test_unordered_multimap()
|
||||
typedef boost::unordered_multimap<key, int, transparent_hasher, key_equal>
|
||||
unordered_multimap;
|
||||
|
||||
test_non_transparent_count<unordered_multimap>();
|
||||
test_map_non_transparent_count<unordered_multimap>();
|
||||
test_map_non_transparent_find<unordered_multimap>();
|
||||
test_map_non_transparent_equal_range<unordered_multimap>();
|
||||
test_map_non_transparent_erase<unordered_multimap>();
|
||||
@@ -1480,7 +1542,7 @@ void test_unordered_multimap()
|
||||
typedef boost::unordered_multimap<key, int, hasher, transparent_key_equal>
|
||||
unordered_multimap;
|
||||
|
||||
test_non_transparent_count<unordered_multimap>();
|
||||
test_map_non_transparent_count<unordered_multimap>();
|
||||
test_map_non_transparent_find<unordered_multimap>();
|
||||
test_map_non_transparent_equal_range<unordered_multimap>();
|
||||
test_map_non_transparent_erase<unordered_multimap>();
|
||||
@@ -1494,6 +1556,7 @@ void test_unordered_set()
|
||||
typedef boost::unordered_set<key, transparent_hasher, transparent_key_equal>
|
||||
unordered_set;
|
||||
|
||||
test_set_transparent_count<unordered_set>();
|
||||
test_set_transparent_find<unordered_set>();
|
||||
test_set_transparent_erase<unordered_set>();
|
||||
test_set_transparent_equal_range<unordered_set>();
|
||||
@@ -1504,6 +1567,7 @@ void test_unordered_set()
|
||||
//
|
||||
typedef boost::unordered_set<key, hasher, key_equal> unordered_set;
|
||||
|
||||
test_set_non_transparent_count<unordered_set>();
|
||||
test_set_non_transparent_find<unordered_set>();
|
||||
test_set_non_transparent_erase<unordered_set>();
|
||||
test_set_non_transparent_equal_range<unordered_set>();
|
||||
@@ -1515,6 +1579,7 @@ void test_unordered_set()
|
||||
typedef boost::unordered_set<key, transparent_hasher, key_equal>
|
||||
unordered_set;
|
||||
|
||||
test_set_non_transparent_count<unordered_set>();
|
||||
test_set_non_transparent_find<unordered_set>();
|
||||
test_set_non_transparent_erase<unordered_set>();
|
||||
test_set_non_transparent_equal_range<unordered_set>();
|
||||
@@ -1526,6 +1591,7 @@ void test_unordered_set()
|
||||
typedef boost::unordered_set<key, hasher, transparent_key_equal>
|
||||
unordered_set;
|
||||
|
||||
test_set_non_transparent_count<unordered_set>();
|
||||
test_set_non_transparent_find<unordered_set>();
|
||||
test_set_non_transparent_erase<unordered_set>();
|
||||
test_set_non_transparent_equal_range<unordered_set>();
|
||||
|
Reference in New Issue
Block a user