ListForeachPtrReverseIdx
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).
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 pointer to 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 the in-node data address. Use this form when the body mutates elements in place. 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:297:
if (ListLen(list) > 0) {
int sum = 0;
ListForeachPtrReverseIdx(list, item_ptr, idx) {
sum += *item_ptr + (int)idx;
} }
ListForeachPtrReverseIdx(&list, value_ptr, idx) {
*value_ptr -= (int)idx;
} }
ListForeachPtrReverseIdx(&list, value_ptr, idx) {
*value_ptr -= (int)idx;
if (idx == 4) { }
ListForeachPtrReverseIdx(&list, value_ptr, idx) {
(void)value_ptr;
(void)idx;