Skip to content
BitVecInsertRange

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

Name Direction Description
bv in,out Bitvector to insert into.
idx in Position to insert at (0-based), in [0, length].
count in Number of bits to insert.
value in Value for all inserted bits (true or false).

Usage example (from documentation)

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

Success

Returns true. Bitvector length grows by count; the range [idx, idx + count) now holds count copies of value; previous bits at and after idx have shifted right by count. The byte buffer may have grown.

Failure

Returns false on allocation failure when capacity must grow. The bitvector is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool BitVecInsertRange(BitVec *bv, u64 idx, u64 count, bool value) {
        ValidateBitVec(bv);
        if (idx > bv->length) {
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecInsertRange\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Insert range of true bits
        BitVecInsertRange(&bv, 1, 3, true);
    
        // Check result: false, true, true, true, false
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecInsertRange edge cases\n");
    
        BitVec bv     = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Test inserting 0 bits (should be no-op)
        BitVecInsertRange(&bv, 0, 0, true);
        result = result && (BitVecLen(&bv) == 0);
        // Test inserting at end
        BitVecPush(&bv, true);
        BitVecInsertRange(&bv, 1, 2, false);
        result = result && (BitVecLen(&bv) == 3);
        result = result && (BitVecGet(&bv, 1) == false);
        // Test large range insertion
        BitVecClear(&bv);
        BitVecInsertRange(&bv, 0, 1000, true);
        result = result && (BitVecLen(&bv) == 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);
    #define BitVecMustInsertRange(bv, idx, count, value)                                                                   \
        do {                                                                                                               \
            if (!BitVecInsertRange((bv), (idx), (count), (value))) {                                                       \
                LOG_FATAL("BitVecMustInsertRange failed");                                                                 \
            }                                                                                                              \
Last updated on