Skip to content

VecForeachIdx

Description

Iterate over each element var of given vector v at each index idx into the vector. The variables var and idx declared and defined by this macro.

idx will start from 0 and will go till v->length - 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.

Success

The loop body runs once for each element, with var bound to VecAt(v, idx) and idx advancing from 0 to v->length - 1. The body is skipped entirely when v is empty. The vector is not modified by the macro itself.

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 (VecLen(vec) > 0) {
                    size_t total_len = 0;
                    VecForeachIdx(vec, str, idx) {
                        total_len += ZstrLen(str) + idx;
                    }
                if (VecLen(vec) > 0) {
                    size_t total_len = 0;
                    VecForeachIdx(vec, str, idx) {
                        total_len += StrLen(&str) + idx;
                    }
                if (VecLen(vec) > 0) {
                    int sum = 0;
                    VecForeachIdx(vec, item, idx) {
                        sum += item + (int)idx;
                    }
    // Test VecForeachIdx macro
    bool test_vec_foreach_idx(void) {
        WriteFmt("Testing VecForeachIdx\n");
    
        // Create a vector of integers
        // Use VecForeachIdx to verify indices and values
        bool result = true;
        VecForeachIdx(&vec, item, idx) {
            result = result && (item == values[idx]);
        };
        // Use VecForeachIdx to calculate weighted sum (value * index)
        int weighted_sum = 0;
        VecForeachIdx(&vec, item, idx) {
            weighted_sum += item * idx;
        }
    // Make idx go out of bounds in VecForeachIdx by modifying vector during iteration
    bool test_vec_foreach_idx_out_of_bounds_access(void) {
        WriteFmt("Testing VecForeachIdx where idx goes out of bounds (should crash)\n");
    
        typedef Vec(int) IntVec;
    
        // VecForeachIdx has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
        VecForeachIdx(&vec, val, idx) {
            WriteFmt("Accessing idx {} (vec.length={}): {}\n", idx, VecLen(&vec), val);
    // Make idx go out of bounds in basic VecForeachIdx by modifying vector during iteration
    bool test_vec_foreach_idx_basic_out_of_bounds_access(void) {
        WriteFmt("Testing basic VecForeachIdx where idx goes out of bounds (should crash)\n");
    
        typedef Vec(int) IntVec;
    
        // Basic VecForeachIdx now has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
        VecForeachIdx(&vec, val, idx) {
            WriteFmt("Accessing idx {} (vec.length={}): {}\n", idx, VecLen(&vec), val);
    /// TAGS: Foreach, Vec, Iteration, Loop
    ///
    #define VecForeach(v, var) VecForeachIdx((v), (var), UNPL(iter))
    
    ///
    /// TAGS: Str, Foreach, Iterate
    ///
    #define StrForeachIdx(str, chr, idx) VecForeachIdx((str), (chr), idx)
    
    ///
Last updated on