BitVecInsertRange

Table of Contents

BitVecInsertRange

Description

Insert multiple bits of the same value at a specific position. All existing bits at and after the position are shifted right.

Parameters

NameDirectionDescription
bvinBitvector to insert into
idxinPosition to insert at (0-based)
countinNumber of bits to insert
valueinValue for all inserted bits (true or false)

Usage example (from documentation)

  BitVecInsertRange(&flags, 2, 5, true);  // Insert 5 true bits at position 2

Usage example (Cross-references)

    }
    
    void BitVecInsertRange(BitVec *bv, u64 idx, u64 count, bool value) {
    ValidateBitVec(bv);
    if (idx > bv->length) {
    // Test BitVecInsertRange function
    bool test_bitvec_insert_range(void) {
    WriteFmt("Testing BitVecInsertRange\n");
    
    BitVec bv = BitVecInit();
    
    // Insert range of true bits
    BitVecInsertRange(&bv, 1, 3, true);
    
    // Check result: false, true, true, true, false
    // Edge case tests
    bool test_bitvec_insert_range_edge_cases(void) {
    WriteFmt("Testing BitVecInsertRange edge cases\n");
    
    BitVec bv     = BitVecInit();
    
    // Test inserting 0 bits (should be no-op)
    BitVecInsertRange(&bv, 0, 0, true);
    result = result && (bv.length == 0);
    // Test inserting at end
    BitVecPush(&bv, true);
    BitVecInsertRange(&bv, 1, 2, false);
    result = result && (bv.length == 3);
    result = result && (BitVecGet(&bv, 1) == false);
    // Test large range insertion
    BitVecClear(&bv);
    BitVecInsertRange(&bv, 0, 1000, true);
    result = result && (bv.length == 1000);
    result = result && (BitVecGet(&bv, 0) == true);
    
    // Test NULL bitvec pointer - should abort
    BitVecInsertRange(NULL, 0, 1, true);
    
    return false;
    
    // Test inserting beyond capacity limit - should abort
    BitVecInsertRange(&bv, SIZE_MAX, 1, true);
    
    BitVecDeinit(&bv);

Share :

Related Posts

VecForeachInRange

VecForeachInRange Description Iterate over elements in a specific range of the given vector v. This is a convenience macro that iterates over a range using an internally managed index. The variable var is declared and defined by this macro.

Read More

VecForeachPtrInRange

VecForeachPtrInRange Description Iterate over elements in a specific range of the given vector v (as pointers). This is a convenience macro that iterates over a range 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

ListForeachReverseInRange

ListForeachReverseInRange Description Iterate over each element var of the given list l in reverse, limited to index range [start, end) relative to the tail of the list. Index 0 corresponds to the tail, 1 to the previous node, and so on. The variable var is declared and defined by this macro. Since linked lists do not support indexing, this macro counts nodes from the tail and includes only those where the relative reverse index lies in [start, end).

Read More