ListForeachReverseIdx
Description
Iterate over each element var of the given list l in reverse order, with index idx. The variable var is declared and defined by this macro.
Iteration starts from the tail and moves backward using the prev pointers. The variable idx will contain the zero-based index from the head: index length-1 corresponds to the tail, length-2 to the previous node, and so on down to 0 (head).
The macro supports iteration using relative traversal if random access is required. This means user code can change
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 to hold the value during iteration. |
idx |
out | Variable that will track the index from the head. |
Success
The loop body runs once for each node walking tail to head with idx counting down from l->length - 1 to 0 and var bound to a copy of l[idx]’s data. The body may reassign idx to perform random access. 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:275:
if (ListLen(list) > 0) {
int sum = 0;
ListForeachReverseIdx(list, item, idx) {
sum += item + (int)idx;
} }
ListForeachReverseIdx(&list, value, idx) {
reverse_idx_sum += (int)idx;
reverse_values[reverse_i++] = value; }
ListForeachReverseIdx(&list, value, idx) {
reverse_values[reverse_i++] = value;
if (idx == 4) { }
ListForeachReverseIdx(&list, value, idx) {
(void)value;
(void)idx;
Last updated on