VecForeachPtr

Table of Contents

VecForeachPtr

Description

Iterate over each element var of the given vector v (as a pointer). This is a convenience macro that iterates forward using an internally managed index and provides a pointer to each element. The variable var is declared and defined by this macro as a pointer to the vector’s data type.

Parameters

NameDirectionDescription
vin,outVector to iterate over.
varinName of the pointer variable to be used which will point to the current element during iteration. The type of var will be a pointer to the data type of the vector elements (obtained via VEC_DATATYPE(v) *).

Usage example (Cross-references)

    void ValidateStrs(const Strs *vs) {
    ValidateVec(vs);
    VecForeachPtr(vs, sp) {
    ValidateStr(sp);
    }
    // Test VecForeachPtr macro
    bool test_vec_foreach_ptr(void) {
    WriteFmt("Testing VecForeachPtr\n");
    
    // Create a vector of integers
    
    // Use VecForeachPtr to modify the values in the vector
    VecForeachPtr(&vec, item_ptr) {
    *item_ptr *= 2;
    }
    // Use VecForeachPtr to calculate sum
    int sum = 0;
    VecForeachPtr(&vec, item_ptr) {
    sum += *item_ptr;
    }
    
    // Use the fixed VecForeachPtr macro
    VecForeachPtr(&lines, line) {
    if (StrStartsWithZstr(line, "[.") && StrEndsWithZstr(line, "]")) {
    Str rule_name = StrInit();
    ///               implementation (e.g., `char*`).
    ///
    #define StrForeachPtr(str, chrptr) VecForeachPtr((str), (chrptr))
    
    ///
    if (VecLen(vec) > 0) {
    int sum = 0;
    VecForeachPtr(vec, item_ptr) {
    sum += *item_ptr;
    }
    if (VecLen(vec) > 0) {
    size_t total_len = 0;
    VecForeachPtr(vec, str_ptr) {
    total_len += strlen(str_ptr->data);
    }
    if (VecLen(vec) > 0) {
    size_t total_len = 0;
    VecForeachPtr(vec, str_ptr) {
    total_len += strlen(*str_ptr);
    }

Share :

Related Posts

VecForeachPtrIdx

VecForeachPtrIdx 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

Read More

VecForeach

VecForeach Description Iterate over each element var of the given vector v. This is a convenience macro that iterates forward using an internally managed index. The variable var is declared and defined by this macro.

Read More

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