BitVecFindLast

Table of Contents

BitVecFindLast

Description

Find index of last occurrence of a specific bit value.

Parameters

NameDirectionDescription
bvinBitvector to search in
valueinBit value to find (true or false)

Usage example (from documentation)

  u64 index = BitVecFindLast(&flags, false);

Usage example (Cross-references)

    ValidateBitVec(bv);
    
    u64 pos = BitVecFindLast(bv, value);
    if (pos != SIZE_MAX) {
    BitVecRemove(bv, pos);
    }
    
    u64 BitVecFindLast(BitVec *bv, bool value) {
    ValidateBitVec(bv);
    
    // Test BitVecFindLast
    result = result && (BitVecFindLast(&bv, true) == 7);  // Last true at index 7
    result = result && (BitVecFindLast(&bv, false) == 6); // Last false at index 6
    // Test BitVecFindLast
    result = result && (BitVecFindLast(&bv, true) == 7);  // Last true at index 7
    result = result && (BitVecFindLast(&bv, false) == 6); // Last false at index 6
    
    // Test with all same values
    }
    result = result && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 4);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == 4);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    
    BitVecDeinit(&bv);
    result = result && (BitVecFind(&bv, true) == SIZE_MAX);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    
    // Test single element
    BitVecPush(&bv, true);
    result = result && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 0);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == 0);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    
    // Test with large bitvector
    }
    result = result && (BitVecFind(&bv, true) == 500);
    result = result && (BitVecFindLast(&bv, true) == 999);
    
    BitVecDeinit(&bv);
    // Test BitVecFind and BitVecFindLast functions
    bool test_bitvec_find_functions(void) {
    printf("Testing BitVecFind and BitVecFindLast functions\n");
    
    BitVec bv     = BitVecInit();
    
    // Test finding last true (should be at index 5)
    result = result && (BitVecFindLast(&bv, true) == 5);
    
    // Test finding last false (should be at index 7)
    
    // Test finding last false (should be at index 7)
    result = result && (BitVecFindLast(&bv, false) == 7);
    
    // Test with all same values
    
    result = result && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 2);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == 2);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    
    BitVecDeinit(&bv);
    result = result && (BitVecFind(&bv, true) == SIZE_MAX);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    
    // Test single element
    BitVecPush(&bv, true);
    result = result && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 0);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == 0);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    
    // Test with large bitvector
    }
    result = result && (BitVecFind(&bv, true) == 500);
    result = result && (BitVecFindLast(&bv, true) == 999);
    
    BitVecDeinit(&bv);

Share :

Related Posts

BitVecGet

BitVecGet Description Get the value of bit at given index in bitvector.

Read More

BitVecFindLastPattern

BitVecFindLastPattern Description Find last occurrence of a bit pattern in the bitvector.

Read More

BitVecSet

BitVecSet Description Set the value of bit at given index in bitvector.

Read More