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)
- In
Init.h:45:
#define BitVecMustResize(bv, n) \
do { \
if (!BitVecResize((bv), (n))) { \
LOG_FATAL("BitVecMustResize failed"); \
} \- In
Int.c:244:
static void int_normalize(Int *value) {
ValidateInt(value);
BitVecResize(INT_BITS(value), int_significant_bits(value));
}- In
Int.c:1097:
if (positions == 0) {
return BitVecResize(INT_BITS(value), bits);
}
if (bits == 0) {- In
Int.c:1106:
u64 new_bits = bits + positions;
if (!BitVecResize(INT_BITS(value), new_bits)) {
return false;
}- In
Int.c:1137:
if (positions == 0) {
return BitVecResize(INT_BITS(value), bits);
}
if (bits == 0 || positions >= bits) {- In
Int.c:1163:
}
if (!BitVecResize(INT_BITS(value), new_bits)) {
return false;
}- In
Int.c:1275:
// 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;
}- In
Int.c:1368:
u64 b_bits = IntBitLength(b);
if (!BitVecResize(INT_BITS(result), a_bits)) {
return false;
}- In
Int.c:1459:
// so every limb store is in bounds and accumulation starts clean.
IntClear(result);
if (!BitVecResize(INT_BITS(result), r_words * 64u)) {
goto cleanup;
}- In
Int.c:1667:
IntClear(quotient);
IntClear(remainder);
if (!BitVecResize(INT_BITS(quotient), dividend_bits) || !BitVecResize(INT_BITS(remainder), r_words * 64u)) {
goto cleanup;
}- In
BitVec.c:127:
}
bool BitVecResize(BitVec *bitvec, u64 new_size) {
ValidateBitVec(bitvec);
if (new_size > bitvec->capacity) {- In
BitVec.c:254:
}
if (!BitVecReserve(out, bv->length) || !BitVecResize(out, bv->length)) {
BitVecDeinit(out);
*out = BitVecInit(bv->allocator);- In
BitVec.c:330:
}
if (!BitVecResize(bitvec, bitvec->length + 1)) {
return false;
}- In
BitVec.c:343:
}
bool value = BitVecGet(bitvec, bitvec->length - 1);
BitVecResize(bitvec, bitvec->length - 1);
return value;
}- In
BitVec.c:375:
u64 old_length = bv->length;
if (!BitVecResize(bv, old_length + count)) {
return false;
}- In
BitVec.c:403:
u64 old_length = bv->length;
if (!BitVecResize(bv, old_length + other->length)) {
return false;
}- In
BitVec.c:431:
u64 old_length = bv->length;
if (!BitVecResize(bv, old_length + pattern_bits)) {
return false;
}- In
BitVec.c:464:
}
BitVecResize(bv, bv->length - 1);
return removed_bit;
}- In
BitVec.c:489:
}
BitVecResize(bv, bv->length - count);
}- In
BitVec.c:536:
}
BitVecResize(bv, write_idx);
return removed_count;- In
BitVec.c:582:
u64 out_len = MIN2(a_len, b_len);
if (!BitVecResize(result, out_len)) {
return;
}- In
BitVec.c:603:
u64 out_len = MAX2(a_len, b_len);
if (!BitVecResize(result, out_len)) {
return;
}- In
BitVec.c:624:
u64 out_len = MAX2(a_len, b_len);
if (!BitVecResize(result, out_len)) {
return;
}- In
BitVec.c:641:
u64 bytes = BYTES_FOR_BITS(len);
if (!BitVecResize(result, len)) {
return;
}- In
BitVec.c:1037:
}
if (!BitVecReserve(out, bit_len) || !BitVecResize(out, bit_len)) {
BitVecDeinit(out);
*out = BitVecInit(alloc);- In
BitVec.c:1095:
}
if (!BitVecReserve(out, bits) || !BitVecResize(out, bits)) {
BitVecDeinit(out);
*out = BitVecInit(alloc);- In
Memory.c:534:
// 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.
- In
Memory.c:537:
// Grow back to 8. The grow path must mask off the stale 5..7 bits.
BitVecResize(&bv, 8);
bool result = (BitVecLen(&bv) == 8);- In
Memory.c:575:
// Kills: 107:5 remove_void_call (ValidateBitVec dropped).
bool test_resize_null_aborts(void) {
WriteFmt("Testing BitVecResize NULL handle aborts\n");
BitVecResize(NULL, 5);- In
Memory.c:577:
WriteFmt("Testing BitVecResize NULL handle aborts\n");
BitVecResize(NULL, 5);
return false; // Should never reach here
- In
Init.c:157:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecResize\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));- In
Init.c:167:
// Test resizing to larger size
BitVecResize(&bv, 6);
// Check that length was increased and new bits have the default value
- In
Init.c:179:
// Test resizing to smaller size
BitVecResize(&bv, 2);
// Check that length was decreased and data was truncated
- In
Init.c:187:
// Test resizing to same size (should be no-op)
BitVecResize(&bv, 2);
result = result && (BitVecLen(&bv) == 2);- In
Init.c:264:
BitVecPush(&bv, true);
BitVecPush(&bv, false);
BitVecResize(&bv, 0);
result = result && (BitVecLen(&bv) == 0);- In
Init.c:268:
// Test reu64 from 0 to non-zero
BitVecResize(&bv, 5);
result = result && (BitVecLen(&bv) == 5);
// New bits should be false
- In
Init.c:276:
// Test reu64 to same size
BitVecResize(&bv, 5);
result = result && (BitVecLen(&bv) == 5);- In
Init.c:280:
// Test large resize
BitVecResize(&bv, 1000);
result = result && (BitVecLen(&bv) == 1000);- In
Init.c:284:
// 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
}- In
Write.c:5716:
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