ListForeachIdx
Description
Iterate over each element var of the given list l, with index idx. The variable var is declared and defined by this macro.
Iteration happens in forward order, starting from the head of the list. This macro also tracks the index (idx) of each element during iteration.
var will contain a copy of the value pointed to by each list node, and idx will be the zero-based index of the current element.
idx to any value in list boundaries and the macro will adjust node automatically for the new index.
Parameters
| Name | Direction | Description |
|---|---|---|
l |
in | List to iterate over. |
var |
out | Name of the variable that will hold the current value during iteration. |
idx |
out | Name of the variable that will hold the current index during iteration. |
Success
The loop body runs once for each node from head to tail with idx advancing from 0 to l->length - 1 and var bound to a copy of l[idx]’s data. The body may reassign idx to perform random access; the macro will reposition to that node. The body is skipped when l is empty.
Failure
The macro itself does not fail. LOG_FATAL via ValidateList(l) when l is uninitialised or corrupted.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
ListInt.c:231:
if (ListLen(list) > 0) {
int sum = 0;
ListForeachIdx(list, item, idx) {
sum += item + (int)idx;
} FILL_INT_LIST(&list);
ListForeachIdx(&list, value, idx) {
idx_sum += (int)idx;
value_sum += value; FILL_INT_LIST(&list);
ListForeachIdx(&list, value, idx) {
forward_values[forward_i++] = value;
if (idx == 0) { }
ListForeachIdx(&list, value, idx) {
(void)value;
(void)idx;