Skip to content

Commit

Permalink
Add more forward_list constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
trcrsired committed Oct 13, 2024
1 parent fc35b5e commit 99fbe36
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
76 changes: 76 additions & 0 deletions include/fast_io_dsal/impl/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,34 @@ inline constexpr void forward_list_sort_common(void *firstptr, void *lastptr, Cm

#endif

#if 0
template <typename T, typename Cmp>
inline constexpr void forward_list_sort_before_common_n(void *beforefirstptr, void *beforelastptr, ::std::size_t n, Cmp cmp)
{
auto beforefirst{static_cast<::fast_io::containers::details::forward_list_node_common *>(beforefirstptr)};
switch (n)
{
case 2:
auto it1{static_cast<::fast_io::containers::details::forward_list_node_common *>(beforefirst->next)};
auto it2{static_cast<::fast_io::containers::details::forward_list_node_common *>(it1->next) 1};
if (cmp(*it2, *it1))
{
it1->next = it2->next;
it2->next = it1;
beforefirst->next = it2;
;
}
break;
case 1:
case 0:
break;
default:

}
}

#endif

} // namespace details

template <typename T, typename allocator>
Expand Down Expand Up @@ -578,6 +606,54 @@ class forward_list
node_type *imp{};
constexpr forward_list() noexcept = default;

private:
template <typename Iter, typename Sentinel>
constexpr void forward_list_range_init_common(Iter first, Sentinel last)
{
forward_list_destroyer destroyer(this);
void *beforeit{__builtin_addressof(this->imp)};
for (; first != last; ++first)
{
beforeit = this->emplace_after_impl(beforeit, *first);
}
destroyer.release();
}

public:
template <std::ranges::input_range Rg>
explicit constexpr forward_list(::fast_io::freestanding::from_range_t, Rg &&rg)
{
if constexpr (::std::ranges::contiguous_range<Rg>)
{
this->forward_list_range_init_common(::std::to_address(::std::ranges::cbegin(rg)), ::std::to_address(::std::ranges::cend(rg)));
}
else
{
this->forward_list_range_init_common(::std::ranges::cbegin(rg), ::std::ranges::cend(rg));
}
}

explicit constexpr forward_list(::std::initializer_list<value_type> ilist)
: forward_list(::fast_io::freestanding::from_range, ilist)
{
}

explicit constexpr forward_list(::std::size_t n, const_reference r) noexcept(::std::is_nothrow_copy_constructible_v<value_type>)
{
forward_list_destroyer destroyer(this);
void *beforeit{__builtin_addressof(this->imp)};
for (::std::size_t i{}; i != n; ++i)
{
beforeit = this->emplace_after_impl(beforeit, r);
}
destroyer.release();
}

explicit constexpr forward_list(::std::size_t n) noexcept(::std::is_nothrow_default_constructible_v<value_type> && ::std::is_nothrow_copy_constructible_v<value_type>)
: forward_list(n, value_type())
{
}

constexpr ~forward_list()
{
this->destroy();
Expand Down
7 changes: 4 additions & 3 deletions tests/0026.container/0011.forward_list/forward_list_sort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

int main()
{
::fast_io::forward_list<::std::size_t> vec;
constexpr ::std::size_t n{100000};
::fast_io::vector<::std::size_t> vec;
constexpr ::std::size_t n{10};
vec.reserve(n);
for (::std::size_t i{}; i != n; ++i)
{
Expand All @@ -20,5 +20,6 @@ int main()
#if 0
flst.sort();
#endif
::fast_io::io::print(::fast_io::mnp::rgvw(flst,"\n"));
::fast_io::forward_list<::std::size_t> flst2{1, 3, 5, 7, 9};
::fast_io::io::println("flst:\n", ::fast_io::mnp::rgvw(flst, "\n"), "\nflst2:\n", ::fast_io::mnp::rgvw(flst2, "\n"));
}

0 comments on commit 99fbe36

Please sign in to comment.