Skip to content

Commit

Permalink
Add 'bool contains(...)' methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tessil committed Jun 20, 2020
1 parent 333f257 commit ab30ba4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/tsl/ordered_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,17 @@ class ordered_hash: private Hash, private KeyEqual {
return (it_bucket != m_buckets_data.cend())?const_iterator(m_values.begin() + it_bucket->index()):end();
}


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 find(key, hash) != cend();
}


template<class K>
std::pair<iterator, iterator> equal_range(const K& key) {
Expand Down
30 changes: 30 additions & 0 deletions include/tsl/ordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,36 @@ class ordered_map {



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). Useful 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). Useful 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); }

/**
Expand Down
30 changes: 30 additions & 0 deletions include/tsl/ordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,36 @@ class ordered_set {



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). Useful 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). Useful 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); }

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/ordered_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,18 @@ BOOST_AUTO_TEST_CASE(test_at) {
}


/**
* contains
*/
BOOST_AUTO_TEST_CASE(test_contains) {
tsl::ordered_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
*/
Expand Down Expand Up @@ -1453,6 +1465,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_OH_CHECK_THROW(map.at(""), std::out_of_range);
TSL_OH_CHECK_THROW(map.at("test"), std::out_of_range);

Expand Down Expand Up @@ -1493,6 +1508,15 @@ BOOST_AUTO_TEST_CASE(test_precalculated_hash) {
BOOST_REQUIRE_NE(map.hash_function()(2), map.hash_function()(3));
TSL_OH_CHECK_THROW(map.at(3, map.hash_function()(2)), std::out_of_range);

/**
* count
*/
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
*/
Expand Down

0 comments on commit ab30ba4

Please sign in to comment.