mirror of
https://github.com/boostorg/unordered.git
synced 2025-07-30 03:17:15 +02:00
Merge pull request #62 from cmazakas/multimap-heterogeneous-erase
Multimap Heterogeneous `erase()`
This commit is contained in:
@ -36,6 +36,7 @@
|
|||||||
#include <boost/type_traits/integral_constant.hpp>
|
#include <boost/type_traits/integral_constant.hpp>
|
||||||
#include <boost/type_traits/is_base_of.hpp>
|
#include <boost/type_traits/is_base_of.hpp>
|
||||||
#include <boost/type_traits/is_class.hpp>
|
#include <boost/type_traits/is_class.hpp>
|
||||||
|
#include <boost/type_traits/is_convertible.hpp>
|
||||||
#include <boost/type_traits/is_empty.hpp>
|
#include <boost/type_traits/is_empty.hpp>
|
||||||
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
|
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
|
||||||
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
|
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
|
||||||
@ -718,6 +719,19 @@ namespace boost {
|
|||||||
is_transparent<A>::value && is_transparent<B>::value;
|
is_transparent<A>::value && is_transparent<B>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <class Key, class UnorderedMap> struct transparent_non_iterable
|
||||||
|
{
|
||||||
|
typedef typename UnorderedMap::hasher hash;
|
||||||
|
typedef typename UnorderedMap::key_equal key_equal;
|
||||||
|
typedef typename UnorderedMap::iterator iterator;
|
||||||
|
typedef typename UnorderedMap::const_iterator const_iterator;
|
||||||
|
|
||||||
|
static bool const value =
|
||||||
|
are_transparent<Key, hash, key_equal>::value &&
|
||||||
|
!boost::is_convertible<Key, iterator>::value &&
|
||||||
|
!boost::is_convertible<Key, const_iterator>::value;
|
||||||
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
// Explicitly call a destructor
|
// Explicitly call a destructor
|
||||||
|
|
||||||
@ -3800,14 +3814,16 @@ namespace boost {
|
|||||||
//
|
//
|
||||||
// no throw
|
// no throw
|
||||||
|
|
||||||
std::size_t erase_key_equiv(const_key_type& k)
|
template <class KeyEqual, class Key>
|
||||||
|
std::size_t erase_key_equiv_impl(KeyEqual const& key_eq, Key const& k)
|
||||||
{
|
{
|
||||||
if (!this->size_)
|
if (!this->size_)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
std::size_t key_hash = this->hash(k);
|
std::size_t key_hash = policy::apply_hash(this->hash_function(), k);
|
||||||
std::size_t bucket_index = this->hash_to_bucket(key_hash);
|
std::size_t bucket_index = this->hash_to_bucket(key_hash);
|
||||||
link_pointer prev = this->find_previous_node(k, bucket_index);
|
link_pointer prev =
|
||||||
|
this->find_previous_node_impl(key_eq, k, bucket_index);
|
||||||
if (!prev)
|
if (!prev)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -3825,6 +3841,11 @@ namespace boost {
|
|||||||
return deleted_count;
|
return deleted_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t erase_key_equiv(const_key_type& k)
|
||||||
|
{
|
||||||
|
return this->erase_key_equiv_impl(this->key_eq(), k);
|
||||||
|
}
|
||||||
|
|
||||||
link_pointer erase_nodes_equiv(node_pointer i, node_pointer j)
|
link_pointer erase_nodes_equiv(node_pointer i, node_pointer j)
|
||||||
{
|
{
|
||||||
std::size_t bucket_index = this->node_bucket(i);
|
std::size_t bucket_index = this->node_bucket(i);
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
#include <boost/functional/hash.hpp>
|
#include <boost/functional/hash.hpp>
|
||||||
#include <boost/move/move.hpp>
|
#include <boost/move/move.hpp>
|
||||||
#include <boost/type_traits/is_constructible.hpp>
|
#include <boost/type_traits/is_constructible.hpp>
|
||||||
#include <boost/type_traits/is_convertible.hpp>
|
|
||||||
#include <boost/unordered/detail/map.hpp>
|
#include <boost/unordered/detail/map.hpp>
|
||||||
|
|
||||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||||
@ -432,9 +431,7 @@ namespace boost {
|
|||||||
|
|
||||||
template <class Key>
|
template <class Key>
|
||||||
typename boost::enable_if_c<
|
typename boost::enable_if_c<
|
||||||
detail::are_transparent<Key, H, P>::value &&
|
detail::transparent_non_iterable<Key, unordered_map>::value,
|
||||||
!boost::is_convertible<Key, iterator>::value &&
|
|
||||||
!boost::is_convertible<Key, const_iterator>::value,
|
|
||||||
node_type>::type
|
node_type>::type
|
||||||
extract(BOOST_FWD_REF(Key) k)
|
extract(BOOST_FWD_REF(Key) k)
|
||||||
{
|
{
|
||||||
@ -724,9 +721,7 @@ namespace boost {
|
|||||||
|
|
||||||
template <class Key>
|
template <class Key>
|
||||||
typename boost::enable_if_c<
|
typename boost::enable_if_c<
|
||||||
detail::are_transparent<Key, H, P>::value &&
|
detail::transparent_non_iterable<Key, unordered_map>::value,
|
||||||
!boost::is_convertible<Key, iterator>::value &&
|
|
||||||
!boost::is_convertible<Key, const_iterator>::value,
|
|
||||||
size_type>::type
|
size_type>::type
|
||||||
erase(BOOST_FWD_REF(Key) k)
|
erase(BOOST_FWD_REF(Key) k)
|
||||||
{
|
{
|
||||||
@ -1404,6 +1399,16 @@ namespace boost {
|
|||||||
size_type erase(const key_type&);
|
size_type erase(const key_type&);
|
||||||
iterator erase(const_iterator, const_iterator);
|
iterator erase(const_iterator, const_iterator);
|
||||||
|
|
||||||
|
template <class Key>
|
||||||
|
typename boost::enable_if_c<
|
||||||
|
detail::transparent_non_iterable<Key, unordered_multimap>::value,
|
||||||
|
size_type>::type
|
||||||
|
erase(BOOST_FWD_REF(Key) k)
|
||||||
|
{
|
||||||
|
return table_.erase_key_equiv_impl(
|
||||||
|
this->key_eq(), boost::forward<Key>(k));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_UNORDERED_DEPRECATED("Use erase instead")
|
BOOST_UNORDERED_DEPRECATED("Use erase instead")
|
||||||
void quick_erase(const_iterator it) { erase(it); }
|
void quick_erase(const_iterator it) { erase(it); }
|
||||||
BOOST_UNORDERED_DEPRECATED("Use erase instead")
|
BOOST_UNORDERED_DEPRECATED("Use erase instead")
|
||||||
|
@ -554,7 +554,7 @@ typedef boost::unordered_map<int, int, transparent_hasher,
|
|||||||
// test that in the presence of the member function template `erase()`, we still
|
// test that in the presence of the member function template `erase()`, we still
|
||||||
// invoke the correct iterator overloads when the type is implicitly convertible
|
// invoke the correct iterator overloads when the type is implicitly convertible
|
||||||
//
|
//
|
||||||
transparent_unordered_map::iterator erase_overload_compile_test()
|
transparent_unordered_map::iterator map_erase_overload_compile_test()
|
||||||
{
|
{
|
||||||
convertible_to_iterator<transparent_unordered_map> c;
|
convertible_to_iterator<transparent_unordered_map> c;
|
||||||
transparent_unordered_map map;
|
transparent_unordered_map map;
|
||||||
@ -563,7 +563,8 @@ transparent_unordered_map::iterator erase_overload_compile_test()
|
|||||||
return map.erase(c);
|
return map.erase(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
transparent_unordered_map::const_iterator erase_const_overload_compile_test()
|
transparent_unordered_map::const_iterator
|
||||||
|
map_erase_const_overload_compile_test()
|
||||||
{
|
{
|
||||||
convertible_to_const_iterator<transparent_unordered_map> c;
|
convertible_to_const_iterator<transparent_unordered_map> c;
|
||||||
transparent_unordered_map map;
|
transparent_unordered_map map;
|
||||||
@ -572,6 +573,29 @@ transparent_unordered_map::const_iterator erase_const_overload_compile_test()
|
|||||||
return map.erase(c);
|
return map.erase(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef boost::unordered_multimap<int, int, transparent_hasher,
|
||||||
|
transparent_key_equal>
|
||||||
|
transparent_unordered_multimap;
|
||||||
|
|
||||||
|
transparent_unordered_multimap::iterator multimap_erase_overload_compile_test()
|
||||||
|
{
|
||||||
|
convertible_to_iterator<transparent_unordered_multimap> c;
|
||||||
|
transparent_unordered_multimap map;
|
||||||
|
transparent_unordered_multimap::iterator pos = map.begin();
|
||||||
|
pos = c;
|
||||||
|
return map.erase(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
transparent_unordered_multimap::const_iterator
|
||||||
|
multimap_erase_const_overload_compile_test()
|
||||||
|
{
|
||||||
|
convertible_to_const_iterator<transparent_unordered_multimap> c;
|
||||||
|
transparent_unordered_multimap map;
|
||||||
|
transparent_unordered_multimap::const_iterator pos = map.begin();
|
||||||
|
pos = c;
|
||||||
|
return map.erase(c);
|
||||||
|
}
|
||||||
|
|
||||||
template <class UnorderedMap> void test_transparent_erase()
|
template <class UnorderedMap> void test_transparent_erase()
|
||||||
{
|
{
|
||||||
count_reset();
|
count_reset();
|
||||||
@ -585,18 +609,20 @@ template <class UnorderedMap> void test_transparent_erase()
|
|||||||
BOOST_TEST_EQ(num_erased, 0);
|
BOOST_TEST_EQ(num_erased, 0);
|
||||||
BOOST_TEST_EQ(key::count_, 0);
|
BOOST_TEST_EQ(key::count_, 0);
|
||||||
|
|
||||||
map[key(0)] = 1337;
|
map.insert(std::make_pair(0, 1337));
|
||||||
map[key(1)] = 1338;
|
map.insert(std::make_pair(1, 1338));
|
||||||
map[key(2)] = 1339;
|
map.insert(std::make_pair(2, 1339));
|
||||||
|
map.insert(std::make_pair(0, 1340));
|
||||||
|
map.insert(std::make_pair(0, 1341));
|
||||||
|
map.insert(std::make_pair(0, 1342));
|
||||||
|
|
||||||
BOOST_TEST_EQ(map.size(), 3);
|
|
||||||
BOOST_TEST(map.find(0) != map.end());
|
BOOST_TEST(map.find(0) != map.end());
|
||||||
|
|
||||||
int const expected_key_count = static_cast<int>(2 * map.size());
|
int const expected_key_count = key::count_;
|
||||||
BOOST_TEST_EQ(key::count_, expected_key_count);
|
int const expected_num_erased = static_cast<int>(map.size() - 2);
|
||||||
|
|
||||||
num_erased = map.erase(0);
|
num_erased = map.erase(0);
|
||||||
BOOST_TEST_EQ(num_erased, 1);
|
BOOST_TEST_EQ(num_erased, expected_num_erased);
|
||||||
BOOST_TEST_EQ(map.size(), 2);
|
BOOST_TEST_EQ(map.size(), 2);
|
||||||
BOOST_TEST(map.find(0) == map.end());
|
BOOST_TEST(map.find(0) == map.end());
|
||||||
|
|
||||||
@ -620,20 +646,23 @@ template <class UnorderedMap> void test_non_transparent_erase()
|
|||||||
BOOST_TEST_EQ(num_erased, 0);
|
BOOST_TEST_EQ(num_erased, 0);
|
||||||
BOOST_TEST_EQ(key::count_, 1);
|
BOOST_TEST_EQ(key::count_, 1);
|
||||||
|
|
||||||
map[key(0)] = 1337;
|
map.insert(std::make_pair(0, 1337));
|
||||||
map[key(1)] = 1338;
|
map.insert(std::make_pair(1, 1338));
|
||||||
map[key(2)] = 1339;
|
map.insert(std::make_pair(2, 1339));
|
||||||
|
map.insert(std::make_pair(0, 1340));
|
||||||
|
map.insert(std::make_pair(0, 1341));
|
||||||
|
map.insert(std::make_pair(0, 1342));
|
||||||
|
|
||||||
|
int const expected_num_erased = static_cast<int>(map.size() - 2);
|
||||||
|
|
||||||
BOOST_TEST_EQ(map.size(), 3);
|
|
||||||
BOOST_TEST(map.find(0) != map.end());
|
BOOST_TEST(map.find(0) != map.end());
|
||||||
|
|
||||||
int key_count = 2 + static_cast<int>(2 * map.size());
|
int key_count = key::count_;
|
||||||
BOOST_TEST_EQ(key::count_, key_count);
|
|
||||||
|
|
||||||
num_erased = map.erase(0);
|
num_erased = map.erase(0);
|
||||||
++key_count;
|
++key_count;
|
||||||
BOOST_TEST_EQ(key::count_, key_count);
|
BOOST_TEST_EQ(key::count_, key_count);
|
||||||
BOOST_TEST_EQ(num_erased, 1);
|
BOOST_TEST_EQ(num_erased, expected_num_erased);
|
||||||
BOOST_TEST_EQ(map.size(), 2);
|
BOOST_TEST_EQ(map.size(), 2);
|
||||||
|
|
||||||
BOOST_TEST(map.find(0) == map.end());
|
BOOST_TEST(map.find(0) == map.end());
|
||||||
@ -811,6 +840,7 @@ void test_unordered_multimap()
|
|||||||
|
|
||||||
test_transparent_find<unordered_multimap>();
|
test_transparent_find<unordered_multimap>();
|
||||||
test_transparent_equal_range<unordered_multimap>();
|
test_transparent_equal_range<unordered_multimap>();
|
||||||
|
test_transparent_erase<unordered_multimap>();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -821,6 +851,7 @@ void test_unordered_multimap()
|
|||||||
|
|
||||||
test_non_transparent_find<unordered_multimap>();
|
test_non_transparent_find<unordered_multimap>();
|
||||||
test_non_transparent_equal_range<unordered_multimap>();
|
test_non_transparent_equal_range<unordered_multimap>();
|
||||||
|
test_non_transparent_erase<unordered_multimap>();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -831,6 +862,7 @@ void test_unordered_multimap()
|
|||||||
|
|
||||||
test_non_transparent_find<unordered_multimap>();
|
test_non_transparent_find<unordered_multimap>();
|
||||||
test_non_transparent_equal_range<unordered_multimap>();
|
test_non_transparent_equal_range<unordered_multimap>();
|
||||||
|
test_non_transparent_erase<unordered_multimap>();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -841,6 +873,7 @@ void test_unordered_multimap()
|
|||||||
|
|
||||||
test_non_transparent_find<unordered_multimap>();
|
test_non_transparent_find<unordered_multimap>();
|
||||||
test_non_transparent_equal_range<unordered_multimap>();
|
test_non_transparent_equal_range<unordered_multimap>();
|
||||||
|
test_non_transparent_erase<unordered_multimap>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user