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)
    #define BitVecMustResize(bv, n)                                                                                        \
        do {                                                                                                               \
            if (!BitVecResize((bv), (n))) {                                                                                \
                LOG_FATAL("BitVecMustResize failed");                                                                      \
            }                                                                                                              \
    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) {
        u64 new_bits = bits + positions;
    
        if (!BitVecResize(INT_BITS(value), new_bits)) {
            return false;
        }
    
        if (positions == 0) {
            return BitVecResize(INT_BITS(value), bits);
        }
        if (bits == 0 || positions >= bits) {
        }
    
        if (!BitVecResize(INT_BITS(value), new_bits)) {
            return false;
        }
    
        // a + b < 2^(max_bits+1), so max_bits+1 bits always hold the sum (incl. carry).
        if (!BitVecResize(INT_BITS(result), max_bits + 1)) {
            return false;
        }
        u64 b_bits = IntBitLength(b);
    
        if (!BitVecResize(INT_BITS(result), a_bits)) {
            return false;
        }
            // so every limb store is in bounds and accumulation starts clean.
            IntClear(result);
            if (!BitVecResize(INT_BITS(result), r_words * 64u)) {
                goto cleanup;
            }
            IntClear(quotient);
            IntClear(remainder);
            if (!BitVecResize(INT_BITS(quotient), dividend_bits) || !BitVecResize(INT_BITS(remainder), r_words * 64u)) {
                goto cleanup;
            }
    }
    
    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 out_len = MIN2(a_len, b_len);
    
        if (!BitVecResize(result, out_len)) {
            return;
        }
        u64 out_len = MAX2(a_len, b_len);
    
        if (!BitVecResize(result, out_len)) {
            return;
        }
        u64 out_len = MAX2(a_len, b_len);
    
        if (!BitVecResize(result, out_len)) {
            return;
        }
        u64 bytes = BYTES_FOR_BITS(len);
    
        if (!BitVecResize(result, len)) {
            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);
        // Shrink length below the byte boundary -- the high bits stay set in
        // the backing byte but are now past the logical length.
        BitVecResize(&bv, 5);
    
        // Grow back to 8. The grow path must mask off the stale 5..7 bits.
    
        // Grow back to 8. The grow path must mask off the stale 5..7 bits.
        BitVecResize(&bv, 8);
    
        bool result = (BitVecLen(&bv) == 8);
    // Kills: 107:5 remove_void_call (ValidateBitVec dropped).
    bool test_resize_null_aborts(void) {
        WriteFmt("Testing BitVecResize NULL handle aborts\n");
    
        BitVecResize(NULL, 5);
        WriteFmt("Testing BitVecResize NULL handle aborts\n");
    
        BitVecResize(NULL, 5);
    
        return false; // Should never reach here
        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);
    
        // 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
        }
        HeapAllocator va = HeapAllocatorInit();
        BitVec        v  = BitVecInit(ALLOCATOR_OF(&va));
        BitVecResize(&v, 12);
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", v) && (StrLen(&out) > 0);
Last updated on