forked from boostorg/unordered
Merge pull request #51 from LeonineKing1199/heterogeneous-find
Heterogeneous `find()`
This commit is contained in:
@ -745,6 +745,28 @@ namespace boost {
|
||||
iterator find(const key_type&);
|
||||
const_iterator find(const key_type&) const;
|
||||
|
||||
template <class Key>
|
||||
typename boost::enable_if_c<detail::is_transparent<Key, H>::value &&
|
||||
detail::is_transparent<Key, 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::is_transparent<Key, H>::value &&
|
||||
detail::is_transparent<Key, 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,
|
||||
class CompatiblePredicate>
|
||||
iterator find(CompatibleKey const&, CompatibleHash const&,
|
||||
|
@ -147,24 +147,169 @@ template <class UnorderedMap> void test_non_transparent_count()
|
||||
BOOST_TEST(key::count_ == 5);
|
||||
}
|
||||
|
||||
template <class UnorderedMap> void test_transparent_find()
|
||||
{
|
||||
count_reset();
|
||||
|
||||
typedef typename UnorderedMap::const_iterator map_iterator;
|
||||
typedef typename UnorderedMap::value_type pair;
|
||||
|
||||
UnorderedMap map;
|
||||
|
||||
int n = 5;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
map[key(i)] = i;
|
||||
}
|
||||
|
||||
int const expected_key_count = 2 * n;
|
||||
BOOST_TEST(key::count_ == expected_key_count);
|
||||
|
||||
// explicitly test `find()` and `find() const` separately
|
||||
//
|
||||
|
||||
{
|
||||
UnorderedMap& m = map;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
map_iterator pos = m.find(i);
|
||||
BOOST_TEST(pos != m.end());
|
||||
|
||||
pair const& p = *pos;
|
||||
int const v = p.second;
|
||||
|
||||
BOOST_TEST(v == i);
|
||||
}
|
||||
|
||||
BOOST_TEST(key::count_ == expected_key_count);
|
||||
|
||||
map_iterator pos = m.find(1337);
|
||||
BOOST_TEST(pos == m.end());
|
||||
BOOST_TEST(key::count_ == expected_key_count);
|
||||
}
|
||||
|
||||
{
|
||||
UnorderedMap const& m = map;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
map_iterator pos = m.find(i);
|
||||
BOOST_TEST(pos != m.end());
|
||||
|
||||
pair const& p = *pos;
|
||||
int const v = p.second;
|
||||
|
||||
BOOST_TEST(v == i);
|
||||
}
|
||||
|
||||
BOOST_TEST(key::count_ == expected_key_count);
|
||||
|
||||
map_iterator pos = m.find(1337);
|
||||
BOOST_TEST(pos == m.end());
|
||||
BOOST_TEST(key::count_ == expected_key_count);
|
||||
}
|
||||
}
|
||||
|
||||
template <class UnorderedMap> void test_non_transparent_find()
|
||||
{
|
||||
count_reset();
|
||||
|
||||
typedef typename UnorderedMap::const_iterator map_iterator;
|
||||
typedef typename UnorderedMap::value_type pair;
|
||||
|
||||
UnorderedMap map;
|
||||
|
||||
int n = 5;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
map[key(i)] = i;
|
||||
}
|
||||
|
||||
int key_count = 2 * n;
|
||||
|
||||
BOOST_TEST(key::count_ == key_count);
|
||||
|
||||
// explicitly test `find()` and `find() const` separately
|
||||
//
|
||||
|
||||
{
|
||||
UnorderedMap& m = map;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
map_iterator pos = m.find(i);
|
||||
BOOST_TEST(pos != m.end());
|
||||
|
||||
pair const& p = *pos;
|
||||
int const v = p.second;
|
||||
|
||||
BOOST_TEST(v == i);
|
||||
}
|
||||
BOOST_TEST(key::count_ == n + key_count);
|
||||
|
||||
map_iterator pos = m.find(1337);
|
||||
BOOST_TEST(pos == m.end());
|
||||
BOOST_TEST(key::count_ == 1 + n + key_count);
|
||||
|
||||
key_count = key::count_;
|
||||
}
|
||||
|
||||
{
|
||||
UnorderedMap const& m = map;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
map_iterator pos = m.find(i);
|
||||
BOOST_TEST(pos != m.end());
|
||||
|
||||
pair const& p = *pos;
|
||||
int const v = p.second;
|
||||
|
||||
BOOST_TEST(v == i);
|
||||
}
|
||||
BOOST_TEST(key::count_ == n + key_count);
|
||||
|
||||
map_iterator pos = m.find(1337);
|
||||
BOOST_TEST(pos == m.end());
|
||||
BOOST_TEST(key::count_ == 1 + n + key_count);
|
||||
}
|
||||
}
|
||||
|
||||
UNORDERED_AUTO_TEST (unordered_map_transparent_count) {
|
||||
test_transparent_count<boost::unordered_map<key, int, transparent_hasher,
|
||||
transparent_key_equal> >();
|
||||
{
|
||||
typedef boost::unordered_map<key, int, transparent_hasher,
|
||||
transparent_key_equal>
|
||||
unordered_map;
|
||||
|
||||
// non-transparent Hash, non-transparent KeyEqual
|
||||
//
|
||||
test_non_transparent_count<
|
||||
boost::unordered_map<key, int, hasher, key_equal> >();
|
||||
test_transparent_count<unordered_map>();
|
||||
test_transparent_find<unordered_map>();
|
||||
}
|
||||
|
||||
// transparent Hash, non-transparent KeyEqual
|
||||
//
|
||||
test_non_transparent_count<
|
||||
boost::unordered_map<key, int, transparent_hasher, key_equal> >();
|
||||
{
|
||||
// non-transparent Hash, non-transparent KeyEqual
|
||||
//
|
||||
typedef boost::unordered_map<key, int, hasher, key_equal> unordered_map;
|
||||
|
||||
// non-transparent Hash, transparent KeyEqual
|
||||
//
|
||||
test_non_transparent_count<
|
||||
boost::unordered_map<key, int, hasher, transparent_key_equal> >();
|
||||
test_non_transparent_count<unordered_map>();
|
||||
test_non_transparent_find<unordered_map>();
|
||||
}
|
||||
|
||||
{
|
||||
// transparent Hash, non-transparent KeyEqual
|
||||
//
|
||||
typedef boost::unordered_map<key, int, transparent_hasher, key_equal>
|
||||
unordered_map;
|
||||
|
||||
test_non_transparent_count<unordered_map>();
|
||||
test_non_transparent_find<unordered_map>();
|
||||
}
|
||||
|
||||
{
|
||||
// non-transparent Hash, transparent KeyEqual
|
||||
//
|
||||
typedef boost::unordered_map<key, int, hasher, transparent_key_equal>
|
||||
unordered_map;
|
||||
|
||||
test_non_transparent_count<unordered_map>();
|
||||
test_non_transparent_find<unordered_map>();
|
||||
}
|
||||
}
|
||||
|
||||
RUN_TESTS()
|
||||
|
Reference in New Issue
Block a user