forked from boostorg/unordered
tested boost::concurrent_flat_set
This commit is contained in:
+122
-23
@@ -1,34 +1,114 @@
|
||||
// Copyright (C) 2023 Christian Mazakas
|
||||
// Copyright (C) 2023 Joaquin M Lopez Munoz
|
||||
// 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)
|
||||
|
||||
#include "helpers.hpp"
|
||||
|
||||
#include <boost/unordered/concurrent_flat_map.hpp>
|
||||
#include <boost/unordered/concurrent_flat_set.hpp>
|
||||
|
||||
#include <boost/core/ignore_unused.hpp>
|
||||
|
||||
namespace {
|
||||
test::seed_t initialize_seed(335740237);
|
||||
|
||||
template <typename Container, typename Value>
|
||||
bool member_emplace(Container& x, Value const & v)
|
||||
{
|
||||
return x.emplace(v.x_);
|
||||
}
|
||||
|
||||
template <typename Container, typename Value>
|
||||
bool member_emplace(Container& x, Value& v)
|
||||
{
|
||||
return x.emplace(v.x_);
|
||||
}
|
||||
|
||||
template <typename Container, typename Key, typename Value>
|
||||
bool member_emplace(Container& x, std::pair<Key, Value> const & v)
|
||||
{
|
||||
return x.emplace(v.first.x_, v.second.x_);
|
||||
}
|
||||
|
||||
template <typename Container, typename Key, typename Value>
|
||||
bool member_emplace(Container& x, std::pair<Key, Value>& v)
|
||||
{
|
||||
return x.emplace(v.first.x_, v.second.x_);
|
||||
}
|
||||
|
||||
template <typename Container, typename Value, typename F>
|
||||
bool member_emplace_or_visit(Container& x, Value const & v, F f)
|
||||
{
|
||||
return x.emplace_or_visit(v.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Value, typename F>
|
||||
bool member_emplace_or_visit(Container& x, Value& v, F f)
|
||||
{
|
||||
return x.emplace_or_visit(v.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Key, typename Value, typename F>
|
||||
bool member_emplace_or_visit(
|
||||
Container& x, std::pair<Key, Value> const & v, F f)
|
||||
{
|
||||
return x.emplace_or_visit(v.first.x_, v.second.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Key, typename Value, typename F>
|
||||
bool member_emplace_or_visit(Container& x, std::pair<Key, Value>& v, F f)
|
||||
{
|
||||
return x.emplace_or_visit(v.first.x_, v.second.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Value, typename F>
|
||||
bool member_emplace_or_cvisit(Container& x, Value const & v, F f)
|
||||
{
|
||||
return x.emplace_or_cvisit(v.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Value, typename F>
|
||||
bool member_emplace_or_cvisit(Container& x, Value& v, F f)
|
||||
{
|
||||
return x.emplace_or_cvisit(v.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Key, typename Value, typename F>
|
||||
bool member_emplace_or_cvisit(
|
||||
Container& x, std::pair<Key, Value> const & v, F f)
|
||||
{
|
||||
return x.emplace_or_cvisit(v.first.x_, v.second.x_, f);
|
||||
}
|
||||
|
||||
template <typename Container, typename Key, typename Value, typename F>
|
||||
bool member_emplace_or_cvisit(Container& x, std::pair<Key, Value>& v, F f)
|
||||
{
|
||||
return x.emplace_or_cvisit(v.first.x_, v.second.x_, f);
|
||||
}
|
||||
|
||||
struct lvalue_emplacer_type
|
||||
{
|
||||
template <class T, class X> void operator()(std::vector<T>& values, X& x)
|
||||
{
|
||||
static constexpr auto value_type_cardinality =
|
||||
value_cardinality<typename X::value_type>::value;
|
||||
|
||||
std::atomic<std::uint64_t> num_inserts{0};
|
||||
thread_runner(values, [&x, &num_inserts](boost::span<T> s) {
|
||||
for (auto const& r : s) {
|
||||
bool b = x.emplace(r.first.x_, r.second.x_);
|
||||
bool b = member_emplace(x, r);
|
||||
if (b) {
|
||||
++num_inserts;
|
||||
}
|
||||
}
|
||||
});
|
||||
BOOST_TEST_EQ(num_inserts, x.size());
|
||||
BOOST_TEST_EQ(raii::default_constructor, 2 * values.size());
|
||||
BOOST_TEST_EQ(
|
||||
raii::default_constructor, value_type_cardinality * values.size());
|
||||
|
||||
BOOST_TEST_EQ(raii::copy_constructor, 0u);
|
||||
BOOST_TEST_GE(raii::move_constructor, 2 * x.size());
|
||||
BOOST_TEST_GE(raii::move_constructor, value_type_cardinality * x.size());
|
||||
|
||||
BOOST_TEST_EQ(raii::copy_constructor, 0u);
|
||||
BOOST_TEST_EQ(raii::copy_assignment, 0u);
|
||||
@@ -40,9 +120,12 @@ namespace {
|
||||
{
|
||||
template <class T, class X> void operator()(std::vector<T>& values, X& x)
|
||||
{
|
||||
static constexpr auto value_type_cardinality =
|
||||
value_cardinality<typename X::value_type>::value;
|
||||
|
||||
x.reserve(values.size());
|
||||
lvalue_emplacer_type::operator()(values, x);
|
||||
BOOST_TEST_EQ(raii::move_constructor, 2 * x.size());
|
||||
BOOST_TEST_EQ(raii::move_constructor, value_type_cardinality * x.size());
|
||||
}
|
||||
} norehash_lvalue_emplacer;
|
||||
|
||||
@@ -50,12 +133,15 @@ namespace {
|
||||
{
|
||||
template <class T, class X> void operator()(std::vector<T>& values, X& x)
|
||||
{
|
||||
static constexpr auto value_type_cardinality =
|
||||
value_cardinality<typename X::value_type>::value;
|
||||
|
||||
std::atomic<std::uint64_t> num_inserts{0};
|
||||
std::atomic<std::uint64_t> num_invokes{0};
|
||||
thread_runner(values, [&x, &num_inserts, &num_invokes](boost::span<T> s) {
|
||||
for (auto& r : s) {
|
||||
bool b = x.emplace_or_cvisit(
|
||||
r.first.x_, r.second.x_,
|
||||
bool b = member_emplace_or_cvisit(
|
||||
x, r,
|
||||
[&num_invokes](typename X::value_type const& v) {
|
||||
(void)v;
|
||||
++num_invokes;
|
||||
@@ -70,9 +156,10 @@ namespace {
|
||||
BOOST_TEST_EQ(num_inserts, x.size());
|
||||
BOOST_TEST_EQ(num_invokes, values.size() - x.size());
|
||||
|
||||
BOOST_TEST_EQ(raii::default_constructor, 2 * values.size());
|
||||
BOOST_TEST_EQ(
|
||||
raii::default_constructor, value_type_cardinality * values.size());
|
||||
BOOST_TEST_EQ(raii::copy_constructor, 0u);
|
||||
BOOST_TEST_GE(raii::move_constructor, 2 * x.size());
|
||||
BOOST_TEST_GE(raii::move_constructor, value_type_cardinality * x.size());
|
||||
BOOST_TEST_EQ(raii::move_assignment, 0u);
|
||||
BOOST_TEST_EQ(raii::copy_assignment, 0u);
|
||||
}
|
||||
@@ -82,13 +169,23 @@ namespace {
|
||||
{
|
||||
template <class T, class X> void operator()(std::vector<T>& values, X& x)
|
||||
{
|
||||
static constexpr auto value_type_cardinality =
|
||||
value_cardinality<typename X::value_type>::value;
|
||||
|
||||
// concurrent_flat_set visit is always const access
|
||||
using arg_type = typename std::conditional<
|
||||
std::is_same<typename X::key_type, typename X::value_type>::value,
|
||||
typename X::value_type const,
|
||||
typename X::value_type
|
||||
>::type;
|
||||
|
||||
std::atomic<std::uint64_t> num_inserts{0};
|
||||
std::atomic<std::uint64_t> num_invokes{0};
|
||||
thread_runner(values, [&x, &num_inserts, &num_invokes](boost::span<T> s) {
|
||||
for (auto& r : s) {
|
||||
bool b = x.emplace_or_visit(
|
||||
r.first.x_, r.second.x_,
|
||||
[&num_invokes](typename X::value_type& v) {
|
||||
bool b = member_emplace_or_visit(
|
||||
x, r,
|
||||
[&num_invokes](arg_type& v) {
|
||||
(void)v;
|
||||
++num_invokes;
|
||||
});
|
||||
@@ -102,20 +199,21 @@ namespace {
|
||||
BOOST_TEST_EQ(num_inserts, x.size());
|
||||
BOOST_TEST_EQ(num_invokes, values.size() - x.size());
|
||||
|
||||
BOOST_TEST_EQ(raii::default_constructor, 2 * values.size());
|
||||
BOOST_TEST_EQ(
|
||||
raii::default_constructor, value_type_cardinality * values.size());
|
||||
BOOST_TEST_EQ(raii::copy_constructor, 0u);
|
||||
BOOST_TEST_GE(raii::move_constructor, 2 * x.size());
|
||||
BOOST_TEST_GE(raii::move_constructor, value_type_cardinality * x.size());
|
||||
BOOST_TEST_EQ(raii::move_assignment, 0u);
|
||||
BOOST_TEST_EQ(raii::copy_assignment, 0u);
|
||||
}
|
||||
} lvalue_emplace_or_visit;
|
||||
|
||||
template <class X, class G, class F>
|
||||
void emplace(X*, G gen, F emplacer, test::random_generator rg)
|
||||
template <class X, class GF, class F>
|
||||
void emplace(X*, GF gen_factory, F emplacer, test::random_generator rg)
|
||||
{
|
||||
auto gen = gen_factory.template get<X>();
|
||||
auto values = make_random_values(1024 * 16, [&] { return gen(rg); });
|
||||
auto reference_map =
|
||||
boost::unordered_flat_map<raii, raii>(values.begin(), values.end());
|
||||
auto reference_cont = reference_container<X>(values.begin(), values.end());
|
||||
raii::reset_counts();
|
||||
|
||||
{
|
||||
@@ -123,13 +221,13 @@ namespace {
|
||||
|
||||
emplacer(values, x);
|
||||
|
||||
BOOST_TEST_EQ(x.size(), reference_map.size());
|
||||
BOOST_TEST_EQ(x.size(), reference_cont.size());
|
||||
|
||||
using value_type = typename X::value_type;
|
||||
BOOST_TEST_EQ(x.size(), x.visit_all([&](value_type const& kv) {
|
||||
BOOST_TEST(reference_map.contains(kv.first));
|
||||
BOOST_TEST_EQ(x.size(), x.visit_all([&](value_type const& v) {
|
||||
BOOST_TEST(reference_cont.contains(get_key(v)));
|
||||
if (rg == test::sequential) {
|
||||
BOOST_TEST_EQ(kv.second, reference_map[kv.first]);
|
||||
BOOST_TEST_EQ(v, *reference_cont.find(get_key(v)));
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -145,6 +243,7 @@ namespace {
|
||||
}
|
||||
|
||||
boost::unordered::concurrent_flat_map<raii, raii>* map;
|
||||
boost::unordered::concurrent_flat_set<raii>* set;
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -156,8 +255,8 @@ using test::sequential;
|
||||
|
||||
UNORDERED_TEST(
|
||||
emplace,
|
||||
((map))
|
||||
((value_type_generator)(init_type_generator))
|
||||
((map)(set))
|
||||
((value_type_generator_factory)(init_type_generator_factory))
|
||||
((lvalue_emplacer)(norehash_lvalue_emplacer)
|
||||
(lvalue_emplace_or_cvisit)(lvalue_emplace_or_visit))
|
||||
((default_generator)(sequential)(limited_range)))
|
||||
|
||||
Reference in New Issue
Block a user