list: declare iterator of LIST_FOREACH() in the loop

This commit is contained in:
Yu Watanabe
2022-03-19 08:10:29 +09:00
parent 40f35786b0
commit 03677889f0
81 changed files with 208 additions and 544 deletions
+20 -17
View File
@@ -136,33 +136,36 @@
#define LIST_JUST_US(name,item) \
(!(item)->name##_prev && !(item)->name##_next)
/* The type of the iterator 'i' is automatically determined by the type of 'head', and declared in the
* loop. Hence, do not declare the same variable in the outer scope. Sometimes, we set 'head' through
* hashmap_get(). In that case, you need to explicitly cast the result. */
#define LIST_FOREACH(name,i,head) \
for ((i) = (head); (i); (i) = (i)->name##_next)
for (typeof(*(head)) *i = (head); i; i = i->name##_next)
#define LIST_FOREACH_SAFE(name,i,n,head) \
for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
for (typeof(*(head)) *n, *i = (head); i && ((n = i->name##_next), 1); i = n)
#define LIST_FOREACH_BACKWARDS(name,i,p) \
for ((i) = (p); (i); (i) = (i)->name##_prev)
for (typeof(*(p)) *i = (p); i; i = i->name##_prev)
/* 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; \
if ((i) == (p)) \
(i) = (p)->name##_next; \
}); \
(i); \
(i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
for (typeof(*(p)) *_p = (p), *i = ({ \
typeof(*_p) *_j = _p; \
while (_j && _j->name##_prev) \
_j = _j->name##_prev; \
if (_j == _p) \
_j = _p->name##_next; \
_j; \
}); \
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. */
/* Loop starting from p->next until p->prev. p can be adjusted meanwhile. */
#define LIST_LOOP_BUT_ONE(name,i,head,p) \
for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
(i) != (p); \
(i) = (i)->name##_next ? (i)->name##_next : (head))
for (typeof(*(p)) *i = (p)->name##_next ? (p)->name##_next : (head); \
i != (p); \
i = i->name##_next ? i->name##_next : (head))
#define LIST_IS_EMPTY(head) \
(!(head))