From 8d679e7e000398affda21d9aa15a216a354a5857 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 2 Oct 2021 16:23:08 +0200 Subject: [PATCH] util/IntrusiveList: add IntrusiveList::swap() --- src/util/IntrusiveList.hxx | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/util/IntrusiveList.hxx b/src/util/IntrusiveList.hxx index b606d7f36..a016c63e0 100644 --- a/src/util/IntrusiveList.hxx +++ b/src/util/IntrusiveList.hxx @@ -167,6 +167,29 @@ public: IntrusiveList &operator=(IntrusiveList &&) = delete; + friend void swap(IntrusiveList &a, IntrusiveList &b) noexcept { + using std::swap; + + if (a.empty()) { + if (b.empty()) + return; + + a.head = b.head; + a.head.next->prev = &a.head; + a.head.prev->next = &a.head; + + b.head = {&b.head, &b.head}; + } else { + swap(a.head, b.head); + + a.head.next->prev = &a.head; + a.head.prev->next = &a.head; + + b.head.next->prev = &b.head; + b.head.prev->next = &b.head; + } + } + constexpr bool empty() const noexcept { return head.next == &head; }