VecForeachIdx

Table of Contents

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

NameDirectionDescription
vin,outVector to iterate over.
varinName of variable to be used which’ll contain value at iterated index idx
idxinName of variable to be used for iterating over indices.

Usage example (Cross-references)

    // 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, vec.length, 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, vec.length, val);
    
    Vec(int) vi = VecInit();
    VecForeachIdx(&vi, val, i) {
    WriteFmtLn("{}", val);
    }
    /// TAGS: Foreach, Vec, Iteration, Loop
    ///
    #define VecForeach(v, var) VecForeachIdx((v), (var), UNPL(iter))
    
    ///
    /// idx[in]     : Name of the variable to be used for iterating over indices (i64).
    ///
    #define StrForeachIdx(str, chr, idx) VecForeachIdx((str), (chr), idx)
    
    ///
    if (VecLen(vec) > 0) {
    int sum = 0;
    VecForeachIdx(vec, item, idx) {
    sum += item + (int)idx;
    }
    if (VecLen(vec) > 0) {
    size_t total_len = 0;
    VecForeachIdx(vec, str, idx) {
    total_len += strlen(str.data) + idx;
    }
    if (VecLen(vec) > 0) {
    size_t total_len = 0;
    VecForeachIdx(vec, str, idx) {
    total_len += strlen(str) + idx;
    }

Share :

Related Posts

VecInitAlignedWithDeepCopyStack

VecInitAlignedWithDeepCopyStack Description Initialize given vector with given alignment. It is mandatory to initialize vectors before use. Not doing so is undefined behaviour. Provided alignment is used to keep all objects at an aligned memory location, avoiding UB in some cases. It’s recommended to use aligned vector when dealing with structs containing unions. These vectors are best used where user doesn’t get a chance to or does not want to deinit vector, given that no data in vector needs to be deinitialized. Example includes, but does not limit to a Vec(i8), Vec(f32), etc…

Read More

VecInitWithDeepCopy

VecInitWithDeepCopy Description Initialize vector. Default alignment is 1 It is mandatory to initialize vectors before use. Not doing so is undefined behaviour.

Read More

VecInitAlignedWithDeepCopy

VecInitAlignedWithDeepCopy Description Initialize vector with given alignment. It is mandatory to initialize vectors before use. Not doing so is undefined behaviour. Provided alignment is used to keep all objects at an aligned memory location, avoiding UB in some cases. It’s recommended to use aligned vector when dealing with structs containing unions.

Read More