Skip to content

BitVecResize

Description

Resize bv to exactly n bits, growing or shrinking as needed. Newly added bits are zero-initialized.

Parameters

Name Direction Description
bv in,out Bitvector to resize.
n in New bit length.

Success

Returns true; bv->length equals n and any new bits are zeroed.

Failure

Returns false on allocator OOM during growth; bv is left unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool BitVecResize(BitVec *bitvec, u64 new_size) {
        ValidateBitVec(bitvec);
        if (new_size > bitvec->capacity) {
        }
    
        if (!BitVecReserve(out, bv->length) || !BitVecResize(out, bv->length)) {
            BitVecDeinit(out);
            *out = BitVecInit(bv->allocator);
        }
    
        if (!BitVecResize(bitvec, bitvec->length + 1)) {
            return false;
        }
        }
        bool value = BitVecGet(bitvec, bitvec->length - 1);
        BitVecResize(bitvec, bitvec->length - 1);
        return value;
    }
    
        u64 old_length = bv->length;
        if (!BitVecResize(bv, old_length + count)) {
            return false;
        }
    
        u64 old_length = bv->length;
        if (!BitVecResize(bv, old_length + other->length)) {
            return false;
        }
    
        u64 old_length = bv->length;
        if (!BitVecResize(bv, old_length + pattern_bits)) {
            return false;
        }
        }
    
        BitVecResize(bv, bv->length - 1);
        return removed_bit;
    }
        }
    
        BitVecResize(bv, bv->length - count);
    }
        }
    
        BitVecResize(bv, write_idx);
    
        return removed_count;
    
        u64 min_len = MIN2(a->length, b->length);
        if (!BitVecResize(result, min_len)) {
            return;
        }
    
        u64 max_len = MAX2(a->length, b->length);
        if (!BitVecResize(result, max_len)) {
            return;
        }
    
        u64 max_len = MAX2(a->length, b->length);
        if (!BitVecResize(result, max_len)) {
            return;
        }
        ValidateBitVec(bitvec);
    
        if (!BitVecResize(result, bitvec->length)) {
            return;
        }
        }
    
        if (!BitVecReserve(out, bit_len) || !BitVecResize(out, bit_len)) {
            BitVecDeinit(out);
            *out = BitVecInit(alloc);
        }
    
        if (!BitVecReserve(out, bits) || !BitVecResize(out, bits)) {
            BitVecDeinit(out);
            *out = BitVecInit(alloc);
    static void int_normalize(Int *value) {
        ValidateInt(value);
        BitVecResize(INT_BITS(value), int_significant_bits(value));
    }
    
        if (positions == 0) {
            return BitVecResize(INT_BITS(value), bits);
        }
        if (bits == 0) {
        }
    
        if (!BitVecResize(INT_BITS(value), bits + positions)) {
            return false;
        }
    
        if (positions == 0) {
            return BitVecResize(INT_BITS(value), bits);
        }
        if (bits == 0 || positions >= bits) {
        }
    
        if (!BitVecResize(INT_BITS(value), bits - positions)) {
            return false;
        }
                    r = next;
    
                    if (IntBitLength(&q) < bit + 1 && !BitVecResize(INT_BITS(&q), bit + 1)) {
                        IntDeinit(&shifted);
                        goto cleanup;
    
        // Reserve space and set bits
        BitVecResize(&bv, 4);
        BitVecSet(&bv, 0, true);
        BitVecSet(&bv, 1, false);
    
        // Set first bit
        BitVecResize(&bv, 1);
        BitVecSet(&bv, 0, true);
        bool result = (BitVecGet(&bv, 0) == true);
        // Create pattern using different methods
        BitVecPush(&bv, true);
        BitVecResize(&bv, 5);
        BitVecSet(&bv, 1, false);
        BitVecSet(&bv, 2, true);
        // Set alternating pattern
        for (int i = 0; i < size; i++) {
            BitVecResize(&bv, i + 1);
            BitVecSet(&bv, i, i % 3 == 0); // Every third bit is true
        }
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecResize\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Test resizing to larger size
        BitVecResize(&bv, 6);
    
        // Check that length was increased and new bits have the default value
    
        // Test resizing to smaller size
        BitVecResize(&bv, 2);
    
        // Check that length was decreased and data was truncated
    
        // Test resizing to same size (should be no-op)
        BitVecResize(&bv, 2);
        result = result && (BitVecLen(&bv) == 2);
        BitVecPush(&bv, true);
        BitVecPush(&bv, false);
        BitVecResize(&bv, 0);
        result = result && (BitVecLen(&bv) == 0);
    
        // Test reu64 from 0 to non-zero
        BitVecResize(&bv, 5);
        result = result && (BitVecLen(&bv) == 5);
        // New bits should be false
    
        // Test reu64 to same size
        BitVecResize(&bv, 5);
        result = result && (BitVecLen(&bv) == 5);
    
        // Test large resize
        BitVecResize(&bv, 1000);
        result = result && (BitVecLen(&bv) == 1000);
    
        // Test shrinking from large size
        BitVecResize(&bv, 10);
        result = result && (BitVecLen(&bv) == 10);
    #define BitVecMustResize(bv, n)                                                                                        \
        do {                                                                                                               \
            if (!BitVecResize((bv), (n))) {                                                                                \
                LOG_FATAL("BitVecMustResize failed");                                                                      \
            }                                                                                                              \
Last updated on