diff --git a/include/fast_io_dsal/impl/forward_list.h b/include/fast_io_dsal/impl/forward_list.h index 138eb16d..f4841283 100644 --- a/include/fast_io_dsal/impl/forward_list.h +++ b/include/fast_io_dsal/impl/forward_list.h @@ -395,6 +395,34 @@ inline constexpr void forward_list_sort_common(void *firstptr, void *lastptr, Cm #endif +#if 0 +template +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 @@ -578,6 +606,54 @@ class forward_list node_type *imp{}; constexpr forward_list() noexcept = default; +private: + template + 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 + explicit constexpr forward_list(::fast_io::freestanding::from_range_t, Rg &&rg) + { + if constexpr (::std::ranges::contiguous_range) + { + 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 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) + { + 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 && ::std::is_nothrow_copy_constructible_v) + : forward_list(n, value_type()) + { + } + constexpr ~forward_list() { this->destroy(); diff --git a/tests/0026.container/0011.forward_list/forward_list_sort.cc b/tests/0026.container/0011.forward_list/forward_list_sort.cc index 4c9e430a..c751bf7f 100644 --- a/tests/0026.container/0011.forward_list/forward_list_sort.cc +++ b/tests/0026.container/0011.forward_list/forward_list_sort.cc @@ -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) { @@ -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")); }