From 96d5b6fe57521bd8de6387933dac3388037d0d2b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 20 Feb 2023 18:14:22 -0800 Subject: [PATCH] ircd::ctx::list: Add mid-list insertion suite to interface. --- include/ircd/ctx/list.h | 3 ++ ircd/ctx.cc | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/include/ircd/ctx/list.h b/include/ircd/ctx/list.h index f737a2d84..c532e0275 100644 --- a/include/ircd/ctx/list.h +++ b/include/ircd/ctx/list.h @@ -64,8 +64,11 @@ struct ircd::ctx::list bool empty() const noexcept; size_t size() const noexcept; + void push_before(ctx *, ctx * = current) noexcept; + void push_after(ctx *, ctx * = current) noexcept; void push_front(ctx * = current) noexcept; void push_back(ctx * = current) noexcept; + void push_sort(ctx * = current) noexcept; // weak/partial void push(ctx * = current) noexcept; // push_back ctx *pop_front() noexcept; diff --git a/ircd/ctx.cc b/ircd/ctx.cc index 33f75cf95..6a2cf4f1b 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -3074,6 +3074,21 @@ noexcept return head; } +void +ircd::ctx::list::push_sort(ctx *const c) +noexcept +{ + assert(c); + for(ctx *o{head}; o; o = next(o)) + if(ircd::ctx::id(*c) < ircd::ctx::id(*o)) + { + push_before(o, c); + return; + } + + push_back(c); +} + [[gnu::hot]] void ircd::ctx::list::push_front(ctx *const c) @@ -3118,6 +3133,52 @@ noexcept tail = c; } +[[gnu::hot]] +void +ircd::ctx::list::push_after(ctx *const o, + ctx *const c) +noexcept +{ + assert(o != c); + assert(next(c) == nullptr); + assert(prev(c) == nullptr); + assert(prev(o) || o == head); + assert(next(o) || o == tail); + + prev(c) = o; + next(c) = next(o); + next(o) = c; + + if(next(c)) + prev(next(c)) = c; + + if(tail == o) + tail = c; +} + +[[gnu::hot]] +void +ircd::ctx::list::push_before(ctx *const o, + ctx *const c) +noexcept +{ + assert(o != c); + assert(next(c) == nullptr); + assert(prev(c) == nullptr); + assert(prev(o) || o == head); + assert(next(o) || o == tail); + + next(c) = o; + prev(c) = prev(o); + prev(o) = c; + + if(prev(c)) + next(prev(c)) = c; + + if(head == o) + head = c; +} + size_t ircd::ctx::list::size() const noexcept