Introduce liftoff_list_swap

Introduce a function to swap elements in a doubly linked list. This will
be helpful when keeping the list of output layers sorted by priority.

v2: re-define in terms of list_inserts and list_removes
This commit is contained in:
Leo Li 2023-10-02 17:54:03 -04:00 committed by Simon Ser
parent 19e56163c2
commit a08c4d1f08
2 changed files with 16 additions and 0 deletions

View file

@ -18,6 +18,9 @@ liftoff_list_insert(struct liftoff_list *list, struct liftoff_list *elm);
void
liftoff_list_remove(struct liftoff_list *elm);
void
liftoff_list_swap(struct liftoff_list *this, struct liftoff_list *other);
size_t
liftoff_list_length(const struct liftoff_list *list);

13
list.c
View file

@ -25,6 +25,19 @@ liftoff_list_remove(struct liftoff_list *elm)
elm->prev = NULL;
}
void
liftoff_list_swap(struct liftoff_list *this, struct liftoff_list *other)
{
struct liftoff_list tmp;
liftoff_list_insert(other, &tmp);
liftoff_list_remove(other);
liftoff_list_insert(this, other);
liftoff_list_remove(this);
liftoff_list_insert(&tmp, this);
liftoff_list_remove(&tmp);
}
size_t
liftoff_list_length(const struct liftoff_list *list)
{