BitVecFind

Table of Contents

BitVecFind

Description

Find index of first 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 = BitVecFind(&flags, true);
  if (index != SIZE_MAX) { /* found at index */ }

Usage example (Cross-references)

    ValidateBitVec(bv);
    
    u64 pos = BitVecFind(bv, value);
    if (pos != SIZE_MAX) {
    BitVecRemove(bv, pos);
    // Missing Access functions implementation
    
    u64 BitVecFind(BitVec *bv, bool value) {
    ValidateBitVec(bv);
    bool BitVecAny(BitVec *bv, bool value) {
    ValidateBitVec(bv);
    return BitVecFind(bv, value) != SIZE_MAX;
    }
    // Test BitVecFind functions (Find, FindLast)
    bool test_bitvec_find_functions(void) {
    printf("Testing BitVecFind functions\n");
    
    BitVec bv     = BitVecInit();
    
    // Test BitVecFind
    result = result && (BitVecFind(&bv, true) == 1);  // First true at index 1
    result = result && (BitVecFind(&bv, false) == 0); // First false at index 0
    // Test BitVecFind
    result = result && (BitVecFind(&bv, true) == 1);  // First true at index 1
    result = result && (BitVecFind(&bv, false) == 0); // First false at index 0
    
    // Test BitVecFindLast
    BitVecPush(&bv, true);
    }
    result = result && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 4);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    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);
    // Edge case tests for Find functions
    bool test_bitvec_find_edge_cases(void) {
    printf("Testing BitVecFind edge cases\n");
    
    BitVec bv     = BitVecInit();
    
    // Test empty bitvector
    result = result && (BitVecFind(&bv, true) == SIZE_MAX);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == SIZE_MAX);
    // Test empty bitvector
    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);
    // 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 && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 0);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    BitVecPush(&bv, i == 500 || i == 999); // Only indices 500 and 999 are true
    }
    result = result && (BitVecFind(&bv, true) == 500);
    result = result && (BitVecFindLast(&bv, true) == 999);
    // Deadend tests - testing NULL pointers and invalid conditions that should cause fatal errors
    bool test_bitvec_find_deadend_tests(void) {
    printf("Testing BitVecFind deadend scenarios\n");
    
    // This should cause LOG_FATAL and terminate the program
    
    // This should cause LOG_FATAL and terminate the program
    BitVecFind(NULL, true);
    
    return true; // Should never reach here
    // Test BitVecFind and BitVecFindLast functions
    bool test_bitvec_find_functions(void) {
    printf("Testing BitVecFind and BitVecFindLast functions\n");
    
    BitVec bv     = BitVecInit();
    
    // Test finding first true (should be at index 0)
    result = result && (BitVecFind(&bv, true) == 0);
    
    // Test finding first false (should be at index 1)
    
    // Test finding first false (should be at index 1)
    result = result && (BitVecFind(&bv, false) == 1);
    
    // Test finding last true (should be at index 5)
    BitVecPush(&bv, true);
    
    result = result && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 2);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    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);
    // Edge case tests for Find functions
    bool test_bitvec_find_edge_cases(void) {
    printf("Testing BitVecFind edge cases\n");
    
    BitVec bv     = BitVecInit();
    
    // Test empty bitvector
    result = result && (BitVecFind(&bv, true) == SIZE_MAX);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, true) == SIZE_MAX);
    // Test empty bitvector
    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);
    // 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 && (BitVecFind(&bv, true) == 0);
    result = result && (BitVecFindLast(&bv, true) == 0);
    result = result && (BitVecFind(&bv, false) == SIZE_MAX);
    result = result && (BitVecFindLast(&bv, false) == SIZE_MAX);
    BitVecPush(&bv, i == 500 || i == 999); // Only indices 500 and 999 are true
    }
    result = result && (BitVecFind(&bv, true) == 500);
    result = result && (BitVecFindLast(&bv, true) == 999);
    // Deadend tests - testing NULL pointers and invalid conditions that should cause fatal errors
    bool test_bitvec_find_deadend_tests(void) {
    printf("Testing BitVecFind deadend scenarios\n");
    
    // This should cause LOG_FATAL and terminate the program
    
    // This should cause LOG_FATAL and terminate the program
    BitVecFind(NULL, true);
    
    return true; // Should never reach here

Share :

Related Posts

BitVecFlip

BitVecFlip Description Flip the value of bit at given index in bitvector. Changes 0 to 1 and 1 to 0.

Read More

BitVecCountZeros

BitVecCountZeros Description Count number of bits set to 0 in bitvector.

Read More

BitVecFindAllPattern

BitVecFindAllPattern Description Find all occurrences of a bit pattern in the bitvector. Results array must be pre-allocated with sufficient space.

Read More