BitVecForeachIdx

Table of Contents

BitVecForeachIdx

Description

Iterate over each bit var of given bitvector bv at each index idx into the bitvector. The variables var and idx declared and defined by this macro. idx will start from 0 and will go till bv->length - 1

Parameters

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

Success

The body is executed for each bit of the bitvector bv from the beginning to the end.

Failure

If the bitvector bv is NULL or its length is zero, the loop body will not be executed. Any failures within the BitVecForeachIdx macro (like invalid index access) will result in a fatal log message and program termination.

Usage example (Cross-references)

    ///           index access) will result in a fatal log message and program termination.
    ///
    #define BitVecForeach(bv, var, body) BitVecForeachIdx((bv), var, ____iter___, {body})
    
    ///
    // Test BitVecForeachIdx macro
    bool test_bitvec_foreach_idx(void) {
    printf("Testing BitVecForeachIdx macro\n");
    
    BitVec bv = BitVecInit();
    bool pattern_correct = true;
    
    BitVecForeachIdx(&bv, bit, idx, {
    if (idx == 0 || idx == 2) {
    pattern_correct = pattern_correct && (bit == true);
    
    // Test foreach idx on empty bitvec
    BitVecForeachIdx(&bv, bit, idx, {
    (void)bit;
    result = false; // Should not execute
    // Test foreach idx on single element
    BitVecPush(&bv, false);
    BitVecForeachIdx(&bv, bit, idx, {
    result   = result && (idx == 0);
    result   = result && (bit == false);
    
    u64 expected_idx = 0;
    BitVecForeachIdx(&bv, bit, idx, {
    (void)bit;
    result = result && (idx == expected_idx);
    count1++;
    });
    BitVecForeachIdx(&bv, bitval, i, {
    (void)bitval;
    count2++;
    // Test BitVecForeachIdx macro
    bool test_bitvec_foreach_idx(void) {
    printf("Testing BitVecForeachIdx macro\n");
    
    BitVec bv = BitVecInit();
    bool pattern_correct = true;
    
    BitVecForeachIdx(&bv, bit, idx, {
    if (idx == 0 || idx == 2) {
    pattern_correct = pattern_correct && (bit == true);
    
    // Test foreach idx on empty bitvec
    BitVecForeachIdx(&bv, bit, idx, {
    (void)bit;
    result = false; // Should not execute
    // Test foreach idx on single element
    BitVecPush(&bv, false);
    BitVecForeachIdx(&bv, bit, idx, {
    result   = result && (idx == 0);
    result   = result && (bit == false);
    
    u64 expected_idx = 0;
    BitVecForeachIdx(&bv, bit, idx, {
    (void)bit;
    result = result && (idx == expected_idx);
    count1++;
    });
    BitVecForeachIdx(&bv, bitval, i, {
    (void)bitval;
    count2++;

Share :