Merge pull request #60 from cmazakas/multimap-heterogeneous-find

Multimap heterogeneous `find()`
This commit is contained in:
Peter Dimov
2021-12-17 21:06:16 +02:00
committed by GitHub
3 changed files with 96 additions and 37 deletions

View File

@ -701,18 +701,23 @@ namespace boost {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Type checkers used for the transparent member functions added by C++20 and up // Type checkers used for the transparent member functions added by C++20 and up
template <class, class, class = void> template <class, class = void> struct is_transparent : public false_type
struct is_transparent : public false_type
{ {
}; };
template <class X, class T> template <class T>
struct is_transparent<X, T, struct is_transparent<T,
typename boost::make_void<typename T::is_transparent>::type> typename boost::make_void<typename T::is_transparent>::type>
: public true_type : public true_type
{ {
}; };
template <class, class A, class B> struct are_transparent
{
static bool const value =
is_transparent<A>::value && is_transparent<B>::value;
};
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Explicitly call a destructor // Explicitly call a destructor

View File

@ -432,8 +432,7 @@ namespace boost {
template <class Key> template <class Key>
typename boost::enable_if_c< typename boost::enable_if_c<
detail::is_transparent<Key, H>::value && detail::are_transparent<Key, H, P>::value &&
detail::is_transparent<Key, P>::value &&
!boost::is_convertible<Key, iterator>::value && !boost::is_convertible<Key, iterator>::value &&
!boost::is_convertible<Key, const_iterator>::value, !boost::is_convertible<Key, const_iterator>::value,
node_type>::type node_type>::type
@ -725,8 +724,7 @@ namespace boost {
template <class Key> template <class Key>
typename boost::enable_if_c< typename boost::enable_if_c<
detail::is_transparent<Key, H>::value && detail::are_transparent<Key, H, P>::value &&
detail::is_transparent<Key, P>::value &&
!boost::is_convertible<Key, iterator>::value && !boost::is_convertible<Key, iterator>::value &&
!boost::is_convertible<Key, const_iterator>::value, !boost::is_convertible<Key, const_iterator>::value,
size_type>::type size_type>::type
@ -774,8 +772,7 @@ namespace boost {
const_iterator find(const key_type&) const; const_iterator find(const key_type&) const;
template <class Key> template <class Key>
typename boost::enable_if_c<detail::is_transparent<Key, H>::value && typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
detail::is_transparent<Key, P>::value,
iterator>::type iterator>::type
find(const Key& key) find(const Key& key)
{ {
@ -785,8 +782,7 @@ namespace boost {
} }
template <class Key> template <class Key>
typename boost::enable_if_c<detail::is_transparent<Key, H>::value && typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
detail::is_transparent<Key, P>::value,
const_iterator>::type const_iterator>::type
find(const Key& key) const find(const Key& key) const
{ {
@ -808,8 +804,7 @@ namespace boost {
size_type count(const key_type&) const; size_type count(const key_type&) const;
template <class Key> template <class Key>
typename boost::enable_if_c<detail::is_transparent<Key, H>::value && typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
detail::is_transparent<Key, P>::value,
size_type>::type size_type>::type
count(const Key& k) const count(const Key& k) const
{ {
@ -828,8 +823,7 @@ namespace boost {
const key_type&) const; const key_type&) const;
template <class Key> template <class Key>
typename boost::enable_if_c<detail::is_transparent<Key, H>::value && typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
detail::is_transparent<Key, P>::value,
std::pair<iterator, iterator> >::type std::pair<iterator, iterator> >::type
equal_range(const Key& key) equal_range(const Key& key)
{ {
@ -842,8 +836,7 @@ namespace boost {
} }
template <class Key> template <class Key>
typename boost::enable_if_c<detail::is_transparent<Key, H>::value && typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
detail::is_transparent<Key, P>::value,
std::pair<const_iterator, const_iterator> >::type std::pair<const_iterator, const_iterator> >::type
equal_range(const Key& key) const equal_range(const Key& key) const
{ {
@ -1448,6 +1441,26 @@ namespace boost {
iterator find(const key_type&); iterator find(const key_type&);
const_iterator find(const key_type&) const; const_iterator find(const key_type&) const;
template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
iterator>::type
find(const Key& key)
{
return iterator(table_.find_node_impl(
table::policy::apply_hash(this->hash_function(), key), key,
this->key_eq()));
}
template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
const_iterator>::type
find(const Key& key) const
{
return const_iterator(table_.find_node_impl(
table::policy::apply_hash(this->hash_function(), key), key,
this->key_eq()));
}
template <class CompatibleKey, class CompatibleHash, template <class CompatibleKey, class CompatibleHash,
class CompatiblePredicate> class CompatiblePredicate>
iterator find(CompatibleKey const&, CompatibleHash const&, iterator find(CompatibleKey const&, CompatibleHash const&,

View File

@ -164,11 +164,10 @@ template <class UnorderedMap> void test_transparent_find()
int n = 5; int n = 5;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
map[key(i)] = i; map.insert(std::make_pair(i, i));
} }
int const expected_key_count = 2 * n; int const expected_key_count = key::count_;
BOOST_TEST(key::count_ == expected_key_count);
// explicitly test `find()` and `find() const` separately // explicitly test `find()` and `find() const` separately
// //
@ -183,14 +182,14 @@ template <class UnorderedMap> void test_transparent_find()
pair const& p = *pos; pair const& p = *pos;
int const v = p.second; int const v = p.second;
BOOST_TEST(v == i); BOOST_TEST_EQ(v, i);
} }
BOOST_TEST(key::count_ == expected_key_count); BOOST_TEST_EQ(key::count_, expected_key_count);
map_iterator pos = m.find(1337); map_iterator pos = m.find(1337);
BOOST_TEST(pos == m.end()); BOOST_TEST(pos == m.end());
BOOST_TEST(key::count_ == expected_key_count); BOOST_TEST_EQ(key::count_, expected_key_count);
} }
{ {
@ -206,11 +205,11 @@ template <class UnorderedMap> void test_transparent_find()
BOOST_TEST(v == i); BOOST_TEST(v == i);
} }
BOOST_TEST(key::count_ == expected_key_count); BOOST_TEST_EQ(key::count_, expected_key_count);
map_iterator pos = m.find(1337); map_iterator pos = m.find(1337);
BOOST_TEST(pos == m.end()); BOOST_TEST(pos == m.end());
BOOST_TEST(key::count_ == expected_key_count); BOOST_TEST_EQ(key::count_, expected_key_count);
} }
} }
@ -226,12 +225,10 @@ template <class UnorderedMap> void test_non_transparent_find()
int n = 5; int n = 5;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
map[key(i)] = i; map.insert(std::make_pair(i, i));
} }
int key_count = 2 * n; int key_count = key::count_;
BOOST_TEST(key::count_ == key_count);
// explicitly test `find()` and `find() const` separately // explicitly test `find()` and `find() const` separately
// //
@ -246,13 +243,13 @@ template <class UnorderedMap> void test_non_transparent_find()
pair const& p = *pos; pair const& p = *pos;
int const v = p.second; int const v = p.second;
BOOST_TEST(v == i); BOOST_TEST_EQ(v, i);
} }
BOOST_TEST(key::count_ == n + key_count); BOOST_TEST_EQ(key::count_, n + key_count);
map_iterator pos = m.find(1337); map_iterator pos = m.find(1337);
BOOST_TEST(pos == m.end()); BOOST_TEST(pos == m.end());
BOOST_TEST(key::count_ == 1 + n + key_count); BOOST_TEST_EQ(key::count_, 1 + n + key_count);
key_count = key::count_; key_count = key::count_;
} }
@ -267,13 +264,13 @@ template <class UnorderedMap> void test_non_transparent_find()
pair const& p = *pos; pair const& p = *pos;
int const v = p.second; int const v = p.second;
BOOST_TEST(v == i); BOOST_TEST_EQ(v, i);
} }
BOOST_TEST(key::count_ == n + key_count); BOOST_TEST_EQ(key::count_, n + key_count);
map_iterator pos = m.find(1337); map_iterator pos = m.find(1337);
BOOST_TEST(pos == m.end()); BOOST_TEST(pos == m.end());
BOOST_TEST(key::count_ == 1 + n + key_count); BOOST_TEST_EQ(key::count_, 1 + n + key_count);
} }
} }
@ -738,7 +735,8 @@ template <class UnorderedMap> void test_non_transparent_extract()
BOOST_TEST_EQ(key::count_, key_count); BOOST_TEST_EQ(key::count_, key_count);
} }
UNORDERED_AUTO_TEST (unordered_map_transparent_count) { void test_unordered_map()
{
{ {
typedef boost::unordered_map<key, int, transparent_hasher, typedef boost::unordered_map<key, int, transparent_hasher,
transparent_key_equal> transparent_key_equal>
@ -790,4 +788,47 @@ UNORDERED_AUTO_TEST (unordered_map_transparent_count) {
} }
} }
void test_unordered_multimap()
{
{
typedef boost::unordered_multimap<key, int, transparent_hasher,
transparent_key_equal>
unordered_multimap;
test_transparent_find<unordered_multimap>();
}
{
// non-transparent Hash, non-transparent KeyEqual
//
typedef boost::unordered_multimap<key, int, hasher, key_equal>
unordered_multimap;
test_non_transparent_find<unordered_multimap>();
}
{
// transparent Hash, non-transparent KeyEqual
//
typedef boost::unordered_multimap<key, int, transparent_hasher, key_equal>
unordered_multimap;
test_non_transparent_find<unordered_multimap>();
}
{
// non-transparent Hash, transparent KeyEqual
//
typedef boost::unordered_multimap<key, int, hasher, transparent_key_equal>
unordered_multimap;
test_non_transparent_find<unordered_multimap>();
}
}
UNORDERED_AUTO_TEST (transparent_ops) {
test_unordered_map();
test_unordered_multimap();
}
RUN_TESTS() RUN_TESTS()