Skip to content

Commit

Permalink
Added tag default_init_t. Added constructor and resize that take …
Browse files Browse the repository at this point in the history
…`default_init_t`.
  • Loading branch information
slavenf committed Mar 27, 2024
1 parent d661a36 commit 65a7c34
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/sfl/private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,8 @@ inline void throw_out_of_range(const char* msg)

} // namespace dtl

struct default_init_t { };

} // namespace sfl

#endif // SFL_PRIVATE_HPP_INCLUDED
41 changes: 41 additions & 0 deletions include/sfl/static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ class static_vector
);
}

static_vector(size_type n, sfl::default_init_t)
{
SFL_ASSERT(n <= capacity());

data_.last_ = sfl::dtl::uninitialized_default_construct_n
(
data_.first_,
n
);
}

static_vector(size_type n, const T& value)
{
SFL_ASSERT(n <= capacity());
Expand Down Expand Up @@ -873,6 +884,36 @@ class static_vector
}
}

void resize(size_type n, sfl::default_init_t)
{
SFL_ASSERT(n <= capacity());

const size_type size = this->size();

if (n < size)
{
const pointer new_last = data_.first_ + n;

sfl::dtl::destroy
(
new_last,
data_.last_
);

data_.last_ = new_last;
}
else if (n > size)
{
const size_type delta = n - size;

data_.last_ = sfl::dtl::uninitialized_default_construct_n
(
data_.last_,
delta
);
}
}

void resize(size_type n, const T& value)
{
SFL_ASSERT(n <= capacity());
Expand Down
50 changes: 50 additions & 0 deletions test/static_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,33 @@ void test_static_vector()
}
}

PRINT("Test resize(size_type, sfl::default_init_t)");
{
{
sfl::static_vector<xint, 100> vec;

vec.resize(4, sfl::default_init_t());

CHECK(vec.size() == 4);
CHECK(*vec.nth(0) == SFL_TEST_XINT_DEFAULT_VALUE);
CHECK(*vec.nth(1) == SFL_TEST_XINT_DEFAULT_VALUE);
CHECK(*vec.nth(2) == SFL_TEST_XINT_DEFAULT_VALUE);
CHECK(*vec.nth(3) == SFL_TEST_XINT_DEFAULT_VALUE);
}

{
sfl::static_vector<int, 100> vec;

vec.resize(4, sfl::default_init_t());

CHECK(vec.size() == 4);
// *vec.nth(0) is undetermined
// *vec.nth(1) is undetermined
// *vec.nth(2) is undetermined
// *vec.nth(3) is undetermined
}
}

PRINT("Test resize(size_type, const T&)");
{
#define CONDITION n < vec.size()
Expand Down Expand Up @@ -2524,6 +2551,29 @@ void test_static_vector()
}
}

PRINT("Test container(size_type, sfl::default_init_t)");
{
{
sfl::static_vector<xint, 100> vec(4, sfl::default_init_t());

CHECK(vec.size() == 4);
CHECK(*vec.nth(0) == SFL_TEST_XINT_DEFAULT_VALUE);
CHECK(*vec.nth(1) == SFL_TEST_XINT_DEFAULT_VALUE);
CHECK(*vec.nth(2) == SFL_TEST_XINT_DEFAULT_VALUE);
CHECK(*vec.nth(3) == SFL_TEST_XINT_DEFAULT_VALUE);
}

{
sfl::static_vector<int, 100> vec(4, sfl::default_init_t());

CHECK(vec.size() == 4);
// *vec.nth(0) is undetermined
// *vec.nth(1) is undetermined
// *vec.nth(2) is undetermined
// *vec.nth(3) is undetermined
}
}

PRINT("Test container(size_type, const T&)");
{
xint value_99(99);
Expand Down

0 comments on commit 65a7c34

Please sign in to comment.