VecPtrAt

Table of Contents

VecPtrAt

Description

Value at given index in a vector. It’s strongly recommended to always use this instead of directly accessing the data.

Parameters

NameDirectionDescription
vinVector to get data from
idxinIndex to get data at

Usage example (Cross-references)

    /// Get pointer to character at given index
    ///
    #define StrCharPtrAt(str, idx) VecPtrAt(str, idx)
    
    #ifdef __cplusplus
    if ((v)->length > 0) {                                                                                         \
    for ((idx) = 0; (idx) < (v)->length; ++(idx)) {                                                            \
    var = VecPtrAt(v, idx);                                                                                \
    body if ((idx) >= (v)->length) {                                                                       \
    LOG_FATAL("Vector range overflow : Invalid index reached during Foreach iteration.");              \
    if ((v)->length > 0) {                                                                                         \
    for ((idx) = (v)->length - 1; (idx) < (v)->length; --(idx)) {                                              \
    var = VecPtrAt(v, idx);                                                                                \
    { body }                                                                                               \
    if ((idx) >= (v)->length) {                                                                            \
    );                                                                                                 \
    }                                                                                                      \
    var = VecPtrAt(v, idx);                                                                                \
    { body }                                                                                               \
    }                                                                                                          \
    // Test VecPtrAt function
    bool test_vec_ptr_at(void) {
    printf("Testing VecPtrAt\n");
    
    // Create a vector of integers
    
    // Get pointers to elements
    int* ptr0 = VecPtrAt(&vec, 0);
    int* ptr1 = VecPtrAt(&vec, 1);
    int* ptr2 = VecPtrAt(&vec, 2);
    // Get pointers to elements
    int* ptr0 = VecPtrAt(&vec, 0);
    int* ptr1 = VecPtrAt(&vec, 1);
    int* ptr2 = VecPtrAt(&vec, 2);
    int* ptr0 = VecPtrAt(&vec, 0);
    int* ptr1 = VecPtrAt(&vec, 1);
    int* ptr2 = VecPtrAt(&vec, 2);
    
    // Check values through pointers
    printf("Testing BitVecPrefixMatch(NULL, patterns, 1) - should fatal\n");
    BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit);
    BitVecPush(VecPtrAt(&vp, 0), true);
    BitVecPrefixMatch(NULL, &vp);
    VecDeinit(&vp);
    printf("Testing BitVecSuffixMatch(NULL, patterns, 1) - should fatal\n");
    BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit);
    BitVecPush(VecPtrAt(&vp, 0), true);
    BitVecSuffixMatch(NULL, &vp);
    VecDeinit(&vp);
    BitVecPush(&source, true);
    
    BitVec* p0 = VecPtrAt(&patterns, 0);
    BitVec* p1 = VecPtrAt(&patterns, 1);
    BitVec* p2 = VecPtrAt(&patterns, 2);
    
    BitVec* p0 = VecPtrAt(&patterns, 0);
    BitVec* p1 = VecPtrAt(&patterns, 1);
    BitVec* p2 = VecPtrAt(&patterns, 2);
    BitVec* p0 = VecPtrAt(&patterns, 0);
    BitVec* p1 = VecPtrAt(&patterns, 1);
    BitVec* p2 = VecPtrAt(&patterns, 2);
    
    // Pattern 0: 111 (should not match)
    BitVecPush(&source, true);
    
    BitVec* p0 = VecPtrAt(&patterns, 0);
    BitVec* p1 = VecPtrAt(&patterns, 1);
    BitVec* p2 = VecPtrAt(&patterns, 2);
    
    BitVec* p0 = VecPtrAt(&patterns, 0);
    BitVec* p1 = VecPtrAt(&patterns, 1);
    BitVec* p2 = VecPtrAt(&patterns, 2);
    BitVec* p0 = VecPtrAt(&patterns, 0);
    BitVec* p1 = VecPtrAt(&patterns, 1);
    BitVec* p2 = VecPtrAt(&patterns, 2);
    
    // Pattern 0: 111 (should not match)

Share :