Skip to content

Commit

Permalink
Tightening up collective operation semantics
Browse files Browse the repository at this point in the history
- flyby: small_vector tweaks
  • Loading branch information
hkaiser committed Jan 11, 2024
1 parent 7b35448 commit 50b3cc3
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ namespace hpx::detail {
}

// only void* is allowed to be converted to uintptr_t
void* ptr = ::operator new(offset_to_data + sizeof(T) * capacity);
void* ptr = ::operator new(mem);
if (nullptr == ptr)
{
throw std::bad_alloc();
Expand Down Expand Up @@ -319,9 +319,13 @@ namespace hpx::detail {
{
// indirect -> direct
auto* storage = indirect();
uninitialized_move_and_destroy(
storage->data(), direct_data(), storage->size());
set_direct_and_size(storage->size());
auto const data_size = storage->size();
if (data_size != 0)
{
uninitialized_move_and_destroy(
storage->data(), direct_data(), data_size);
}
set_direct_and_size(data_size);
detail::storage<T>::dealloc(storage);
}
}
Expand All @@ -332,16 +336,26 @@ namespace hpx::detail {
if (is_direct())
{
// direct -> indirect
uninitialized_move_and_destroy(data<direction::direct>(),
storage->data(), size<direction::direct>());
storage->size(size<direction::direct>());
auto const data_size = size<direction::direct>();
if (data_size != 0)
{
uninitialized_move_and_destroy(
data<direction::direct>(), storage->data(),
data_size);
}
storage->size(data_size);
}
else
{
// indirect -> indirect
uninitialized_move_and_destroy(data<direction::indirect>(),
storage->data(), size<direction::indirect>());
storage->size(size<direction::indirect>());
auto const data_size = size<direction::indirect>();
if (data_size != 0)
{
uninitialized_move_and_destroy(
data<direction::indirect>(), storage->data(),
data_size);
}
storage->size(data_size);
detail::storage<T>::dealloc(indirect());
}
set_indirect(storage);
Expand Down Expand Up @@ -657,7 +671,7 @@ namespace hpx::detail {
set_direct_and_size(0);
}

// performs a const_cast so we don't need this implementation twice
// performs a const_cast, so we don't need this implementation twice
template <direction D>
[[nodiscard]] auto at(std::size_t idx) const -> T&
{
Expand Down Expand Up @@ -785,7 +799,7 @@ namespace hpx::detail {
if (&other == this)
{
// It doesn't seem to be required to do self-check, but let's do
// it anyways to be safe
// it anyway to be safe
return *this;
}

Expand Down
73 changes: 37 additions & 36 deletions libs/core/datastructures/tests/unit/small_vector.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 Hartmut Kaiser
// Copyright (c) 2021-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -36,18 +36,18 @@ namespace test {
simple_allocator() noexcept = default;

template <typename U>
simple_allocator(simple_allocator<U> const&) noexcept
explicit simple_allocator(simple_allocator<U> const&) noexcept
{
}

T* allocate(std::size_t n)
static T* allocate(std::size_t n)
{
return reinterpret_cast<T*>(::new char[sizeof(T) * n]);
}

void deallocate(T* p, std::size_t) noexcept
static void deallocate(T* p, std::size_t) noexcept
{
delete[](reinterpret_cast<char*>(p));
delete[] (reinterpret_cast<char*>(p));
}

friend bool operator==(
Expand Down Expand Up @@ -204,7 +204,7 @@ namespace test {

void small_vector_test()
{
// basic test with less elements than static size
// basic test with fewer elements than static size
{
using sm5_t = hpx::detail::small_vector<int, 5>;
static_assert(
Expand All @@ -214,7 +214,7 @@ namespace test {
sm5.push_back(1);
HPX_TEST_EQ(sm5[0], 1);

sm5_t sm5_copy(sm5);
sm5_t const sm5_copy(sm5);
HPX_TEST(sm5 == sm5_copy);
}
{
Expand All @@ -226,7 +226,7 @@ namespace test {
sm7.push_back(1);
HPX_TEST_EQ(sm7[0], 1);

sm7_t sm7_copy(sm7);
sm7_t const sm7_copy(sm7);
HPX_TEST(sm7 == sm7_copy);
}
{
Expand All @@ -240,7 +240,7 @@ namespace test {

sm5.push_back(2);
HPX_TEST_EQ(sm5[1], 2);
HPX_TEST_EQ(sm5.size(), std::size_t(2));
HPX_TEST_EQ(sm5.size(), static_cast<std::size_t>(2));

sm5_copy = sm5;
HPX_TEST(sm5 == sm5_copy);
Expand Down Expand Up @@ -274,7 +274,7 @@ namespace test {
sm2.push_back(3);
HPX_TEST_EQ(sm2[1], 2);
HPX_TEST_EQ(sm2[2], 3);
HPX_TEST_EQ(sm2.size(), std::size_t(3));
HPX_TEST_EQ(sm2.size(), static_cast<std::size_t>(3));

sm2_copy = sm2;
HPX_TEST(sm2 == sm2_copy);
Expand Down Expand Up @@ -307,13 +307,13 @@ namespace test {

for (std::size_t i = 0, max = v.capacity() + 1; i != max; ++i)
{
v.push_back(int(i));
v.push_back(static_cast<int>(i));
}

vec w;

vec v_copy(v);
vec w_copy(w);
vec const v_copy(v);
vec const w_copy(w);

v.swap(w);
HPX_TEST(v == w_copy);
Expand All @@ -324,13 +324,13 @@ namespace test {
vec v;
for (std::size_t i = 0, max = v.capacity() - 1; i != max; ++i)
{
v.push_back(int(i));
v.push_back(static_cast<int>(i));
}

vec w;

vec v_copy(v);
vec w_copy(w);
vec const v_copy(v);
vec const w_copy(w);

v.swap(w);
HPX_TEST(v == w_copy);
Expand All @@ -341,17 +341,17 @@ namespace test {
vec v;
for (std::size_t i = 0, max = v.capacity() - 1; i != max; ++i)
{
v.push_back(int(i));
v.push_back(static_cast<int>(i));
}

vec w;
for (std::size_t i = 0, max = v.capacity() / 2; i != max; ++i)
{
w.push_back(int(i));
w.push_back(static_cast<int>(i));
}

vec v_copy(v);
vec w_copy(w);
vec const v_copy(v);
vec const w_copy(w);

v.swap(w);
HPX_TEST(v == w_copy);
Expand All @@ -362,17 +362,17 @@ namespace test {
vec v;
for (std::size_t i = 0, max = v.capacity() + 1; i != max; ++i)
{
v.push_back(int(i));
v.push_back(static_cast<int>(i));
}

vec w;
for (std::size_t i = 0, max = v.capacity() * 2; i != max; ++i)
{
w.push_back(int(i));
w.push_back(static_cast<int>(i));
}

vec v_copy(v);
vec w_copy(w);
vec const v_copy(v);
vec const w_copy(w);

v.swap(w);
HPX_TEST(v == w_copy);
Expand Down Expand Up @@ -448,7 +448,6 @@ namespace test {
void vector_test()
{
using value_type = typename Vector::value_type;
constexpr int max = 100;

test_range_insertion<Vector>();

Expand Down Expand Up @@ -480,6 +479,7 @@ namespace test {
test::check_equal_containers(vector2, v);
}
{
constexpr int max = 100;
Vector vector;
std::vector<int> v;

Expand Down Expand Up @@ -534,8 +534,9 @@ namespace test {

auto insert_it =
vector.insert(vector.end(), &aux_vect[0], aux_vect + 50);
HPX_TEST_EQ(std::size_t(std::distance(insert_it, vector.end())),
std::size_t(50));
HPX_TEST_EQ(static_cast<std::size_t>(
std::distance(insert_it, vector.end())),
static_cast<std::size_t>(50));

v.insert(v.end(), aux_vect2, aux_vect2 + 50);
test::check_equal_containers(vector, v);
Expand Down Expand Up @@ -581,9 +582,9 @@ namespace test {
{ //push_back with not enough capacity
value_type push_back_this(1);
vector.push_back(std::move(push_back_this));
v.push_back(int(1));
v.push_back(static_cast<int>(1));
vector.push_back(value_type(1));
v.push_back(int(1));
v.push_back(static_cast<int>(1));
test::check_equal_containers(vector, v);
}

Expand All @@ -601,9 +602,9 @@ namespace test {

value_type push_back_this(1);
vector.push_back(std::move(push_back_this));
v.push_back(int(1));
v.push_back(static_cast<int>(1));
vector.push_back(value_type(1));
v.push_back(int(1));
v.push_back(static_cast<int>(1));
test::check_equal_containers(vector, v);
}

Expand All @@ -617,7 +618,7 @@ namespace test {
vector.insert(vector.begin(), std::move(insert_this));
v.insert(v.begin(), i);
vector.insert(vector.begin(), value_type(i));
v.insert(v.begin(), int(i));
v.insert(v.begin(), static_cast<int>(i));
}
test::check_equal_containers(vector, v);

Expand All @@ -631,7 +632,7 @@ namespace test {

// Test insertion from list
{
std::list<int> l(50, int(1));
std::list<int> l(50, static_cast<int>(1));
auto it_insert =
vector.insert(vector.begin(), l.begin(), l.end());
HPX_TEST(vector.begin() == it_insert);
Expand All @@ -643,7 +644,7 @@ namespace test {
v.assign(l.begin(), l.end());
test::check_equal_containers(vector, v);

std::forward_list<int> fl(50, int(1));
std::forward_list<int> fl(50, static_cast<int>(1));
vector.clear();
v.clear();
vector.assign(fl.begin(), fl.end());
Expand All @@ -663,7 +664,7 @@ namespace test {
class emplace_int
{
public:
emplace_int(
explicit emplace_int(
int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) noexcept
: a_(a)
, b_(b)
Expand Down Expand Up @@ -726,7 +727,7 @@ namespace test {
}

int a_, b_, c_, d_, e_;
int padding[6];
int padding[6] = {};
};

static emplace_int expected[10];
Expand Down
Loading

0 comments on commit 50b3cc3

Please sign in to comment.