Skip to content
ListForeachInRange

ListForeachInRange

Description

Iterate over each element var of the given list l in the index range [start, end). The variable var is declared and defined by this macro.

This macro performs forward traversal, starting at index start (inclusive) and continuing until index end (exclusive), assuming zero-based indexing.

Since linked lists are not indexable, the traversal walks node-by-node and skips nodes before start, then continues while tracking the current index.

Parameters

Name Direction Description
l in List to iterate over.
var out Name of the variable to be used which will contain the value of the current element during iteration.
start in Starting index (inclusive).
end in Ending index (exclusive).

Success

The loop body runs once for each node whose head-relative index lies in [start, end), with var bound to a copy of that node’s data. The body is skipped when the range is empty or 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)
                    if (start < end) {
                        int sum = 0;
                        ListForeachInRange(list, item, start, end) {
                            sum += item;
                        }
        FILL_INT_LIST(&list);
    
        ListForeachInRange(&list, value, 1, 4) {
            forward_range_sum += value;
        }
        FILL_INT_LIST(&list);
    
        ListForeachInRange(&list, value, 2, 2) {
            (void)value;
            count += 1;
        }
    
        ListForeachInRange(&list, value, 4, 2) {
            (void)value;
            count += 10000;
        }
    
        ListForeachInRange(&list, value, 0, 1) {
            (void)value;
            count += 10000;
Last updated on