From cf759983b80d4f30ed4c565b8d519dd727b59001 Mon Sep 17 00:00:00 2001 From: cyborg1811m Date: Tue, 6 Feb 2024 21:39:41 +0100 Subject: [PATCH] const_list: implemented swap and merge --- include/const_list.h | 96 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/include/const_list.h b/include/const_list.h index 503afb5..a301b6b 100644 --- a/include/const_list.h +++ b/include/const_list.h @@ -402,6 +402,102 @@ namespace cc { --_size; } + template + constexpr void const_list::swap(const_list &other) noexcept + { + auto tmp = _tail; + _tail = other._tail; + other._tail = tmp; + } + + template + constexpr void const_list::merge(const_list &other) + { + merge(std::forward(other), [](const value_type& v1, const value_type& v2) -> bool { return v1 < v2; }); + } + + template + constexpr void const_list::merge(const_list &&other) + { + merge(std::forward(other), [](const value_type& v1, const value_type& v2) -> bool { return v1 < v2; }); + } + + template + template + constexpr void const_list::merge(const_list &other, Compare comp) + { + if (std::addressof(other) == this) return; + + auto first1 = begin(); + auto last1 = end(); + auto first2 = other.begin(); + auto last2 = other.end(); + + while (first1 != last1 && first2 != last2) { + if (comp(*first2, *first1)) { + auto next = std::next(first2); + + first2._node->_owner = this; + first1._node->push_before(first2._node); + + first2 = next; + } else { + ++first1; + } + } + + while (first2 != last2) { + auto next = std::next(first2); + + first2._node->_owner = this; + _tail.push_before(first2._node); + + first2 = next; + } + + other._tail._next = &other._tail; + other._tail._prev = &other._tail; + other._size = 0; + } + + template + template + constexpr void const_list::merge(const_list &&other, Compare comp) + { + if (std::addressof(other) == this) return; + + auto first1 = begin(); + auto last1 = end(); + auto first2 = other.begin(); + auto last2 = other.end(); + + while (first1 != last1 && first2 != last2) { + if (comp(*first2, *first1)) { + auto next = std::next(first2); + + first2._node->_owner = this; + first1._node->push_before(first2._node); + + first2 = next; + } else { + ++first1; + } + } + + while (first2 != last2) { + auto next = std::next(first2); + + first2._node->_owner = this; + _tail.push_before(first2._node); + + first2 = next; + } + + other._tail._next = &other._tail; + other._tail._prev = &other._tail; + other._size = 0; + } + template constexpr const_list_node::const_list_node(const const_list_node &other) noexcept : _delete_cb(other._delete_cb)