From: Lennart Poettering Date: Tue, 27 Jan 2015 23:34:58 +0000 (+0100) Subject: list: add macro for iterating through a list an item is in, skipping the item X-Git-Tag: v219~259 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=7663df377016cf7b95001aec893006647175ae4a list: add macro for iterating through a list an item is in, skipping the item --- diff --git a/src/shared/list.h b/src/shared/list.h index f0458b54e..7ed63188b 100644 --- a/src/shared/list.h +++ b/src/shared/list.h @@ -138,6 +138,16 @@ #define LIST_FOREACH_AFTER(name,i,p) \ for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next) +/* Iterate through all the members of the list p is included in, but skip over p */ +#define LIST_FOREACH_OTHERS(name,i,p) \ + for (({ \ + (i) = (p); \ + while ((i) && (i)->name##_prev) \ + (i) = (i)->name##_prev; \ + }); \ + (i); \ + (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next) + /* Loop starting from p->next until p->prev. p can be adjusted meanwhile. */ #define LIST_LOOP_BUT_ONE(name,i,head,p) \ diff --git a/src/test/test-list.c b/src/test/test-list.c index e9d47f050..399ec8e58 100644 --- a/src/test/test-list.c +++ b/src/test/test-list.c @@ -38,6 +38,13 @@ int main(int argc, const char *argv[]) { LIST_PREPEND(item, head, &items[i]); } + i = 0; + LIST_FOREACH_OTHERS(item, cursor, &items[2]) { + i++; + assert_se(cursor != &items[2]); + } + assert_se(i == ELEMENTSOF(items)-1); + assert_se(!LIST_JUST_US(item, head)); assert_se(items[0].item_next == NULL); @@ -125,7 +132,7 @@ int main(int argc, const char *argv[]) { assert_se(items[3].item_prev == &items[2]); for (i = 0; i < ELEMENTSOF(items); i++) - LIST_REMOVE(item, head, &items[i]); + LIST_REMOVE(item, head, &items[i]); assert_se(head == NULL);