Add 'bool contains(...)' methods.

This commit is contained in:
Tessil 2019-09-25 22:02:22 +02:00
parent f66212139c
commit 425a213b17
4 changed files with 96 additions and 0 deletions

View File

@ -1016,6 +1016,17 @@ public:
}
template<class K>
bool contains(const K& key) const {
return contains(key, hash_key(key));
}
template<class K>
bool contains(const K& key, std::size_t hash) const {
return count(key, hash) != 0;
}
template<class K>
std::pair<iterator, iterator> equal_range(const K& key) {
return equal_range(key, hash_key(key));

View File

@ -538,6 +538,37 @@ public:
bool contains(const Key& key) const { return m_ht.contains(key); }
/**
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
* as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
*/
bool contains(const Key& key, std::size_t precalculated_hash) const {
return m_ht.contains(key, precalculated_hash);
}
/**
* This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
* If so, K must be hashable and comparable to Key.
*/
template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
bool contains(const K& key) const { return m_ht.contains(key); }
/**
* @copydoc contains(const K& key) const
*
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
* as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
*/
template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
bool contains(const K& key, std::size_t precalculated_hash) const {
return m_ht.contains(key, precalculated_hash);
}
std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
/**

View File

@ -405,6 +405,37 @@ public:
bool contains(const Key& key) const { return m_ht.contains(key); }
/**
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
* as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
*/
bool contains(const Key& key, std::size_t precalculated_hash) const {
return m_ht.contains(key, precalculated_hash);
}
/**
* This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
* If so, K must be hashable and comparable to Key.
*/
template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
bool contains(const K& key) const { return m_ht.contains(key); }
/**
* @copydoc contains(const K& key) const
*
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
* as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
*/
template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
bool contains(const K& key, std::size_t precalculated_hash) const {
return m_ht.contains(key, precalculated_hash);
}
std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
/**

View File

@ -928,6 +928,17 @@ BOOST_AUTO_TEST_CASE(test_at) {
TSL_RH_CHECK_THROW(map.at(1), std::out_of_range);
}
/**
* contains
*/
BOOST_AUTO_TEST_CASE(test_contains) {
tsl::robin_map<std::int64_t, std::int64_t> map = {{0, 10}, {-2, 20}};
BOOST_CHECK(map.contains(0));
BOOST_CHECK(map.contains(-2));
BOOST_CHECK(!map.contains(-3));
}
/**
* equal_range
*/
@ -1148,6 +1159,9 @@ BOOST_AUTO_TEST_CASE(test_empty_map) {
BOOST_CHECK_EQUAL(map.count(""), 0);
BOOST_CHECK_EQUAL(map.count("test"), 0);
BOOST_CHECK(!map.contains(""));
BOOST_CHECK(!map.contains("test"));
TSL_RH_CHECK_THROW(map.at(""), std::out_of_range);
TSL_RH_CHECK_THROW(map.at("test"), std::out_of_range);
@ -1188,6 +1202,15 @@ BOOST_AUTO_TEST_CASE(test_precalculated_hash) {
BOOST_REQUIRE_NE(map.hash_function()(2), map.hash_function()(3));
TSL_RH_CHECK_THROW(map.at(3, map.hash_function()(2)), std::out_of_range);
/**
* contains
*/
BOOST_CHECK(map.contains(3, map.hash_function()(3)));
BOOST_CHECK(map_const.contains(3, map_const.hash_function()(3)));
BOOST_REQUIRE_NE(map.hash_function()(2), map.hash_function()(3));
BOOST_CHECK(!map.contains(3, map.hash_function()(2)));
/**
* count
*/