Skip to content

Commit

Permalink
add list_splice test
Browse files Browse the repository at this point in the history
  • Loading branch information
trcrsired committed Feb 18, 2024
1 parent 9090671 commit b8b1516
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
32 changes: 25 additions & 7 deletions include/fast_io_dsal/impl/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class list_iterator

using reference = ::std::conditional_t<isconst, value_type const &, value_type &>;
using const_reference = value_type const &;

using size_type = ::std::size_t;
using difference_type = ::std::ptrdiff_t;
void *iter{};

constexpr list_iterator &operator++() noexcept
Expand Down Expand Up @@ -295,7 +298,6 @@ inline void list_debug(void *leftfirstptr, void *leftlastptr)
template <typename T, typename Cmp>
inline constexpr void list_merge_common(void *leftfirstptr, void *leftlastptr, void *rightfirstptr, void *rightlastptr, Cmp cmp)
{

for (; leftfirstptr != leftlastptr;)
{
auto rightfirst{static_cast<::fast_io::containers::details::list_node_common *>(rightfirstptr)};
Expand Down Expand Up @@ -328,12 +330,28 @@ inline constexpr void list_merge_common(void *leftfirstptr, void *leftlastptr, v
}
leftfirstptr = leftfirstnext;
}
}
list_splice_range_common(leftlastptr, rightfirstptr, rightlastptr);
#if 0
if (rightfirstptr != rightlastptr)
{

template <typename T, typename Cmp>
inline constexpr void list_sort_merge_common(void *firstptr, void *middleptr, void *lastptr, Cmp cmp)
{
list_merge_common<T, Cmp>(firstptr, middleptr, middleptr, lastptr, cmp);
auto leftlast{static_cast<::fast_io::containers::details::list_node_common *>(leftlastptr)};
auto leftlastprev{static_cast<::fast_io::containers::details::list_node_common *>(leftlast->prev)};

auto rightfirst{static_cast<::fast_io::containers::details::list_node_common *>(rightfirstptr)};
auto rightfirstprev{static_cast<::fast_io::containers::details::list_node_common *>(rightfirst->prev)};
leftlastprev->next = rightfirst;
rightfirst->prev = leftlastprev;

auto rightlast{static_cast<::fast_io::containers::details::list_node_common *>(rightlastptr)};
auto rightlastprev{static_cast<::fast_io::containers::details::list_node_common *>(rightlast->prev)};
rightlastprev->next = leftlast;
leftlast->prev = rightlastprev;

rightlast->prev = rightfirstprev;
rightfirstprev->next = rightlast;
}
#endif
}

template <typename T, typename Cmp>
Expand Down Expand Up @@ -816,7 +834,7 @@ class list

constexpr void splice(const_iterator pos, list &&other) noexcept
{
this->splice(pos, other.imp.next, __builtin_addressof(other.imp));
::fast_io::containers::details::list_splice_range_common(pos.iter, other.imp.next, __builtin_addressof(other.imp));
other.imp = {__builtin_addressof(other.imp), __builtin_addressof(other.imp)};
}

Expand Down
22 changes: 22 additions & 0 deletions tests/0026.container/0002.list/list_splice.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <fast_io.h>
#include <fast_io_dsal/list.h>
#include <iterator>

int main()
{
fast_io::list<int> list1{1, 2, 3, 4, 5};
fast_io::list<int> list2{10, 20, 30, 40, 50};

auto it = list1.begin();
std::advance(it, 2);

list1.splice(it, std::move(list2));

println("list1:", ::fast_io::mnp::rgvw(list1," "));
println("list2:", ::fast_io::mnp::rgvw(list2," "));

list2.splice(list2.begin(), it, list1.end());

println("list1:", ::fast_io::mnp::rgvw(list1," "));
println("list2:", ::fast_io::mnp::rgvw(list2," "));
}

0 comments on commit b8b1516

Please sign in to comment.