Skip to content
VecForeachInRangeIdx

VecForeachInRangeIdx

Description

Iterate over elements in a specific range of the given vector v at each index idx. The variables var and idx are declared and defined by this macro.

idx will start from start and will go till end - 1

Parameters

Name Direction Description
v in,out Vector to iterate over.
var in Name of variable to be used which’ll contain value at iterated index idx.
idx in Name of variable to be used for iterating over indices.
start in Starting index (inclusive).
end in Ending index (exclusive).

Success

The loop body runs once for each element with var bound to VecAt(v, idx) and idx advancing from start to min(end, v->length) - 1. The body is skipped when the range is empty or v is empty.

Failure

The macro itself does not fail. LOG_FATAL via ValidateVec(v) when v is uninitialised or corrupted.

Usage example (Cross-references)

Usage examples (Cross-references)
                    if (start < end) {
                        size_t total_len = 0;
                        VecForeachInRangeIdx(vec, str, idx, start, end) {
                            total_len += ZstrLen(str) + idx;
                        }
                    if (start < end) {
                        size_t total_len = 0;
                        VecForeachInRangeIdx(vec, str, idx, start, end) {
                            total_len += StrLen(&str) + idx;
                        }
                    if (start < end) {
                        int sum = 0;
                        VecForeachInRangeIdx(vec, item, idx, start, end) {
                            sum += item + (int)idx;
                        }
    /// TAGS: Foreach, Vec, Iteration, Loop, Range
    ///
    #define VecForeachInRange(v, var, start, end) VecForeachInRangeIdx((v), (var), UNPL(iter), (start), (end))
    
    ///
    /// TAGS: Str, Foreach, Iterate, Range
    ///
    #define StrForeachInRangeIdx(str, chr, idx, start, end) VecForeachInRangeIdx((str), (chr), idx, (start), (end))
    
    ///
Last updated on