From 4f3dad4bea7a1b17739ebf3ccaed86bdee665bd0 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 1 Oct 2018 21:22:19 +0200 Subject: [PATCH] Add _reverse for I-LIST --- m-i-list.h | 16 ++++++++++++++++ tests/test-milist.c | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/m-i-list.h b/m-i-list.h index c42e6236..6563e7d4 100644 --- a/m-i-list.h +++ b/m-i-list.h @@ -570,6 +570,22 @@ typedef struct ilist_head_s { e->name.prev->next = &e->name; \ ILISTI_CONTRACT(name, d); \ ILISTI_CONTRACT(name, e); \ + } \ + \ + static inline void \ + M_C(name, _reverse)(list_t list) \ + { \ + ILISTI_CONTRACT(name, list); \ + struct ilist_head_s *next, *it; \ + for(it = list->name.next ; it != &list->name; it = next) { \ + next = it->next; \ + it->next = it->prev; \ + it->prev = next; \ + } \ + next = it->next; \ + it->next = it->prev; \ + it->prev = next; \ + ILISTI_CONTRACT(name, list); \ } \ #endif diff --git a/tests/test-milist.c b/tests/test-milist.c index 658f5445..ba544f19 100644 --- a/tests/test-milist.c +++ b/tests/test-milist.c @@ -174,6 +174,13 @@ static void test2(void) n++; } assert (n == NUM); + ilist_tname_reverse(list1); + for M_EACH(item, list1, ILIST_OPLIST(ilist_tname)) { + n--; + assert (n == item->n); + } + assert (n == 0); + } M_LET(list1, list2, ILIST_OPLIST(ilist_tname)) { for(int i = 0; i < NUM; i++)