VecForeachPtrReverse

Table of Contents

VecForeachPtrReverse

Description

Iterate over each element var (as a pointer) of the given vector v in reverse order. This is a convenience macro that iterates backward 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)

    if (VecLen(vec) > 0) {
    size_t total_len = 0;
    VecForeachPtrReverse(vec, str_ptr) {
    total_len += strlen(*str_ptr);
    }
    if (VecLen(vec) > 0) {
    size_t total_len = 0;
    VecForeachPtrReverse(vec, str_ptr) {
    total_len += ZstrLen(str_ptr->data);
    }
    if (VecLen(vec) > 0) {
    int sum = 0;
    VecForeachPtrReverse(vec, item_ptr) {
    sum += *item_ptr;
    }
    // Test VecForeachPtrReverse macro
    bool test_vec_foreach_ptr_reverse(void) {
    WriteFmt("Testing VecForeachPtrReverse\n");
    
    // Create a vector of integers
    // Use VecForeachPtrReverse to increment values in reverse order
    int increment = 1;
    VecForeachPtrReverse(&vec, item_ptr) {
    *item_ptr += increment++;
    }
    ///               implementation (e.g., `char*`).
    ///
    #define StrForeachPtrReverse(str, chrptr) VecForeachPtrReverse((str), (chrptr))
    
    ///

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

VecForeachPtr

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.

Read More

VecForeachIdx

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

Read More