Merge pull request #45 from LeonineKing1199/transparent-count-unordered_map

Transparent count unordered map
This commit is contained in:
Peter Dimov
2021-11-24 01:50:28 +02:00
committed by GitHub
4 changed files with 203 additions and 0 deletions

View File

@ -40,6 +40,7 @@
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
#include <boost/type_traits/is_nothrow_swappable.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/make_void.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/unordered/detail/fwd.hpp>
#include <boost/utility/addressof.hpp>
@ -722,6 +723,21 @@ namespace boost {
#endif
////////////////////////////////////////////////////////////////////////////
// Type checkers used for the transparent member functions added by C++20 and up
template <class, class, class = void>
struct is_transparent : public false_type
{
};
template <class X, class T>
struct is_transparent<X, T,
typename boost::make_void<typename T::is_transparent>::type>
: public true_type
{
};
////////////////////////////////////////////////////////////////////////////
// Explicitly call a destructor

View File

@ -757,6 +757,22 @@ namespace boost {
size_type count(const key_type&) const;
template <class Key>
typename boost::enable_if_c<detail::is_transparent<Key, H>::value &&
detail::is_transparent<Key, P>::value,
size_type>::type
count(const Key& 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);
}
std::pair<iterator, iterator> equal_range(const key_type&);
std::pair<const_iterator, const_iterator> equal_range(
const key_type&) const;

View File

@ -65,6 +65,7 @@ test-suite unordered
[ run unordered/detail_tests.cpp ]
[ run unordered/deduction_tests.cpp ]
[ run unordered/scoped_allocator.cpp /boost/filesystem//boost_filesystem ]
[ run unordered/transparent_tests.cpp ]
[ run unordered/compile_set.cpp : :
: <define>BOOST_UNORDERED_USE_MOVE

View File

@ -0,0 +1,170 @@
// 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_;
key(int x) : x_(x) { ++count_; }
key(key const& k) : x_(k.x_) { ++count_; }
};
int key::count_;
struct transparent_hasher
{
typedef void is_transparent;
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 transparent_key_equal
{
typedef void is_transparent;
static bool was_called_;
bool operator()(key const& k1, key const& k2) const { return k1.x_ == k2.x_; }
bool operator()(int const x, key const& k1) const
{
was_called_ = true;
return k1.x_ == x;
}
};
bool transparent_key_equal::was_called_;
struct hasher
{
std::size_t operator()(key const& k) const
{
return boost::hash<int>()(k.x_);
}
};
struct key_equal
{
static bool was_called_;
bool operator()(key const& k1, key const& k2) const { return k1.x_ == k2.x_; }
bool operator()(int const x, key const& k1) const
{
was_called_ = true;
return k1.x_ == x;
}
};
bool key_equal::was_called_;
void count_reset()
{
key::count_ = 0;
transparent_key_equal::was_called_ = false;
key_equal::was_called_ = false;
}
template <class UnorderedMap> void test_transparent_count()
{
count_reset();
UnorderedMap map;
// initial `key(0)` expression increases the count
// then copying into the `unordered_map` increments the count again thus we
// have 2
//
map[key(0)] = 1337;
BOOST_TEST(key::count_ == 2);
// now the number of `key` objects created should be a constant and never
// touched again
//
std::size_t count = 0;
count = map.count(0);
BOOST_TEST(count == 1);
BOOST_TEST(key::count_ == 2);
BOOST_TEST(map.key_eq().was_called_);
count = map.count(1);
BOOST_TEST(count == 0);
BOOST_TEST(key::count_ == 2);
count = map.count(key(0));
BOOST_TEST(count == 1);
BOOST_TEST(key::count_ == 3);
}
template <class UnorderedMap> void test_non_transparent_count()
{
count_reset();
UnorderedMap map;
// initial `key(0)` expression increases the count
// then copying into the `unordered_map` increments the count again thus we
// have 2
//
map[key(0)] = 1337;
BOOST_TEST(key::count_ == 2);
// rely on the implicit constructor here to spawn a new object which
// increases the count
//
std::size_t count = 0;
count = map.count(0);
BOOST_TEST(count == 1);
BOOST_TEST(key::count_ == 3);
BOOST_TEST(!map.key_eq().was_called_);
count = map.count(1);
BOOST_TEST(count == 0);
BOOST_TEST(key::count_ == 4);
count = map.count(key(0));
BOOST_TEST(count == 1);
BOOST_TEST(key::count_ == 5);
}
UNORDERED_AUTO_TEST (unordered_map_transparent_count) {
test_transparent_count<boost::unordered_map<key, int, transparent_hasher,
transparent_key_equal> >();
// non-transparent Hash, non-transparent KeyEqual
//
test_non_transparent_count<
boost::unordered_map<key, int, hasher, key_equal> >();
// transparent Hash, non-transparent KeyEqual
//
test_non_transparent_count<
boost::unordered_map<key, int, transparent_hasher, key_equal> >();
// non-transparent Hash, transparent KeyEqual
//
test_non_transparent_count<
boost::unordered_map<key, int, hasher, transparent_key_equal> >();
}
RUN_TESTS()