forked from boostorg/unordered
Get initial prototype of transparent count()
working
This commit is contained in:
@ -757,6 +757,8 @@ namespace boost {
|
|||||||
|
|
||||||
size_type count(const key_type&) const;
|
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<iterator, iterator> equal_range(const key_type&);
|
||||||
std::pair<const_iterator, const_iterator> equal_range(
|
std::pair<const_iterator, const_iterator> equal_range(
|
||||||
const key_type&) const;
|
const key_type&) const;
|
||||||
@ -1837,6 +1839,21 @@ namespace boost {
|
|||||||
return table_.find_node(k) ? 1 : 0;
|
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>
|
template <class K, class T, class H, class P, class A>
|
||||||
std::pair<typename unordered_map<K, T, H, P, A>::iterator,
|
std::pair<typename unordered_map<K, T, H, P, A>::iterator,
|
||||||
typename unordered_map<K, T, H, P, A>::iterator>
|
typename unordered_map<K, T, H, P, A>::iterator>
|
||||||
|
@ -65,6 +65,7 @@ test-suite unordered
|
|||||||
[ run unordered/detail_tests.cpp ]
|
[ run unordered/detail_tests.cpp ]
|
||||||
[ run unordered/deduction_tests.cpp ]
|
[ run unordered/deduction_tests.cpp ]
|
||||||
[ run unordered/scoped_allocator.cpp /boost/filesystem//boost_filesystem ]
|
[ run unordered/scoped_allocator.cpp /boost/filesystem//boost_filesystem ]
|
||||||
|
[ run unordered/transparent_tests.cpp ]
|
||||||
|
|
||||||
[ run unordered/compile_set.cpp : :
|
[ run unordered/compile_set.cpp : :
|
||||||
: <define>BOOST_UNORDERED_USE_MOVE
|
: <define>BOOST_UNORDERED_USE_MOVE
|
||||||
|
64
test/unordered/transparent_tests.cpp
Normal file
64
test/unordered/transparent_tests.cpp
Normal 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()
|
Reference in New Issue
Block a user