X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fll.h;h=269c81424df077c342a9abd143cb85ba47051a2e;hb=522f263565607b97b83d26bff49b5fa44704df33;hp=4414e2e68e0a19b4d2bae59fb433be693e278792;hpb=db8c531ad19eff86adbc11e5435f07d5f780ab4a;p=pspp diff --git a/src/libpspp/ll.h b/src/libpspp/ll.h index 4414e2e68e..269c81424d 100644 --- a/src/libpspp/ll.h +++ b/src/libpspp/ll.h @@ -1,6 +1,5 @@ /* PSPP - computes sample statistics. Copyright (C) 2006 Free Software Foundation, Inc. - Written by Ben Pfaff . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -229,7 +228,8 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1, /* Iteration helper macros. */ -/* Sets DATA to each object in LIST in turn, assuming that each +/* Sets DATA to each object in LIST in turn, in forward or + reverse order, assuming that each `struct ll' in LIST is embedded as the given MEMBER name in data type STRUCT. @@ -239,11 +239,16 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1, for (DATA = ll_head__ (STRUCT, MEMBER, LIST); \ DATA != NULL; \ DATA = ll_next__ (DATA, STRUCT, MEMBER, LIST)) +#define ll_for_each_reverse(DATA, STRUCT, MEMBER, LIST) \ + for (DATA = ll_tail__ (STRUCT, MEMBER, LIST); \ + DATA != NULL; \ + DATA = ll_prev__ (DATA, STRUCT, MEMBER, LIST)) /* Continues a iteration of LIST, starting from the object - currently in DATA and continuing forward through the remainder - of the list, assuming that each `struct ll' in LIST is - embedded as the given MEMBER name in data type STRUCT. + currently in DATA and continuing, in forward or reverse order, + through the remainder of the list, assuming that each `struct + ll' in LIST is embedded as the given MEMBER name in data type + STRUCT. Behavior is undefined if DATA is removed from the list between loop iterations. */ @@ -251,11 +256,15 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1, for (; \ DATA != NULL; \ DATA = ll_next__ (DATA, STRUCT, MEMBER, LIST)) +#define ll_for_each_reverse_continue(DATA, STRUCT, MEMBER, LIST) \ + for (; \ + DATA != NULL; \ + DATA = ll_prev__ (DATA, STRUCT, MEMBER, LIST)) -/* Sets DATA to each object in LIST in turn, assuming that each - `struct ll' in LIST is embedded as the given MEMBER name in - data type STRUCT. NEXT must be another variable of the same - type as DATA. +/* Sets DATA to each object in LIST in turn, in forward or + reverse order, assuming that each `struct ll' in LIST is + embedded as the given MEMBER name in data type STRUCT. NEXT + (or PREV) must be another variable of the same type as DATA. Behavior is well-defined even if DATA is removed from the list between iterations. */ @@ -265,12 +274,19 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1, ? (NEXT = ll_next__ (DATA, STRUCT, MEMBER, LIST), 1) \ : 0); \ DATA = NEXT) +#define ll_for_each_reverse_safe(DATA, PREV, STRUCT, MEMBER, LIST) \ + for (DATA = ll_tail__ (STRUCT, MEMBER, LIST); \ + (DATA != NULL \ + ? (PREV = ll_prev__ (DATA, STRUCT, MEMBER, LIST), 1) \ + : 0); \ + DATA = PREV) -/* Continues a iteration of LIST, starting from the object - currently in DATA and continuing forward through the remainder - of the list, assuming that each `struct ll' in LIST is - embedded as the given MEMBER name in data type STRUCT. NEXT - must be another variable of the same type as DATA. +/* Continues a iteration of LIST, in forward or reverse order, + starting from the object currently in DATA and continuing + forward through the remainder of the list, assuming that each + `struct ll' in LIST is embedded as the given MEMBER name in + data type STRUCT. NEXT (or PREV) must be another variable of + the same type as DATA. Behavior is well-defined even if DATA is removed from the list between iterations. */ @@ -280,37 +296,51 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1, ? (NEXT = ll_next__ (DATA, STRUCT, MEMBER, LIST), 1) \ : 0); \ DATA = NEXT) +#define ll_for_each_safe_reverse_continue(DATA, PREV, STRUCT, MEMBER, LIST) \ + for (; \ + (DATA != NULL \ + ? (PREV = ll_prev__ (DATA, STRUCT, MEMBER, LIST), 1) \ + : 0); \ + DATA = PREV) -/* Sets DATA to each object in LIST in turn, assuming that each - `struct ll' in LIST is embedded as the given MEMBER name in - data type STRUCT. +/* Sets DATA to each object in LIST in turn, in forward or + reverse order, assuming that each `struct ll' in LIST is + embedded as the given MEMBER name in data type STRUCT. Each object is removed from LIST before its loop iteration. */ #define ll_for_each_preremove(DATA, STRUCT, MEMBER, LIST) \ while (!ll_is_empty (LIST) \ ? (DATA = ll_data (ll_pop_head (LIST), STRUCT, MEMBER), 1) \ : 0) +#define ll_for_each_reverse_preremove(DATA, STRUCT, MEMBER, LIST) \ + while (!ll_is_empty (LIST) \ + ? (DATA = ll_data (ll_pop_tail (LIST), STRUCT, MEMBER), 1) \ + : 0) -/* Sets DATA to each object in LIST in turn, assuming that each - `struct ll' in LIST is embedded as the given MEMBER name in - data type STRUCT. +/* Sets DATA to each object in LIST in turn, in forward or + reverse order, assuming that each `struct ll' in LIST is + embedded as the given MEMBER name in data type STRUCT. At the end of each loop iteration, DATA is removed from the list. */ #define ll_for_each_postremove(DATA, STRUCT, MEMBER, LIST) \ for (; \ (DATA = ll_head__ (STRUCT, MEMBER, LIST)) != NULL; \ ll_remove (&DATA->MEMBER)) +#define ll_for_each_reverse_postremove(DATA, STRUCT, MEMBER, LIST) \ + for (; \ + (DATA = ll_tail__ (STRUCT, MEMBER, LIST)) != NULL; \ + ll_remove (&DATA->MEMBER)) /* Macros for internal use only. */ #define ll_data__(LL, STRUCT, MEMBER, LIST) \ ((LL) != ll_null (LIST) ? ll_data (LL, STRUCT, MEMBER) : NULL) #define ll_head__(STRUCT, MEMBER, LIST) \ ll_data__ (ll_head (LIST), STRUCT, MEMBER, LIST) +#define ll_tail__(STRUCT, MEMBER, LIST) \ + ll_data__ (ll_tail (LIST), STRUCT, MEMBER, LIST) #define ll_next__(DATA, STRUCT, MEMBER, LIST) \ ll_data__ (ll_next (&DATA->MEMBER), STRUCT, MEMBER, LIST) -#define ll_remove__(DATA, STRUCT, MEMBER, LIST) \ - (ll_next (&DATA->MEMBER) == ll_null (LIST) \ - ? ll_remove (&DATA->MEMBER), NULL \ - : ll_data (ll_remove (&DATA->MEMBER), STRUCT, MEMBER)) +#define ll_prev__(DATA, STRUCT, MEMBER, LIST) \ + ll_data__ (ll_prev (&DATA->MEMBER), STRUCT, MEMBER, LIST) /* Inline functions. */