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
BitVec.c:106:
}
bool BitVecResize(BitVec *bitvec, u64 new_size) {
ValidateBitVec(bitvec);
if (new_size > bitvec->capacity) {- In
BitVec.c:233:
}
if (!BitVecReserve(out, bv->length) || !BitVecResize(out, bv->length)) {
BitVecDeinit(out);
*out = BitVecInit(bv->allocator);- In
BitVec.c:302:
}
if (!BitVecResize(bitvec, bitvec->length + 1)) {
return false;
}- In
BitVec.c:315:
}
bool value = BitVecGet(bitvec, bitvec->length - 1);
BitVecResize(bitvec, bitvec->length - 1);
return value;
}- In
BitVec.c:347:
u64 old_length = bv->length;
if (!BitVecResize(bv, old_length + count)) {
return false;
}- In
BitVec.c:375:
u64 old_length = bv->length;
if (!BitVecResize(bv, old_length + other->length)) {
return false;
}- In
BitVec.c:403:
u64 old_length = bv->length;
if (!BitVecResize(bv, old_length + pattern_bits)) {
return false;
}- In
BitVec.c:436:
}
BitVecResize(bv, bv->length - 1);
return removed_bit;
}- In
BitVec.c:461:
}
BitVecResize(bv, bv->length - count);
}- In
BitVec.c:508:
}
BitVecResize(bv, write_idx);
return removed_count;- In
BitVec.c:537:
u64 min_len = MIN2(a->length, b->length);
if (!BitVecResize(result, min_len)) {
return;
}- In
BitVec.c:554:
u64 max_len = MAX2(a->length, b->length);
if (!BitVecResize(result, max_len)) {
return;
}- In
BitVec.c:571:
u64 max_len = MAX2(a->length, b->length);
if (!BitVecResize(result, max_len)) {
return;
}- In
BitVec.c:586:
ValidateBitVec(bitvec);
if (!BitVecResize(result, bitvec->length)) {
return;
}- In
BitVec.c:982:
}
if (!BitVecReserve(out, bit_len) || !BitVecResize(out, bit_len)) {
BitVecDeinit(out);
*out = BitVecInit(alloc);- In
BitVec.c:1040:
}
if (!BitVecReserve(out, bits) || !BitVecResize(out, bits)) {
BitVecDeinit(out);
*out = BitVecInit(alloc);- In
Int.c:219:
static void int_normalize(Int *value) {
ValidateInt(value);
BitVecResize(INT_BITS(value), int_significant_bits(value));
}- In
Int.c:1009:
if (positions == 0) {
return BitVecResize(INT_BITS(value), bits);
}
if (bits == 0) {- In
Int.c:1016:
}
if (!BitVecResize(INT_BITS(value), bits + positions)) {
return false;
}- In
Int.c:1037:
if (positions == 0) {
return BitVecResize(INT_BITS(value), bits);
}
if (bits == 0 || positions >= bits) {- In
Int.c:1048:
}
if (!BitVecResize(INT_BITS(value), bits - positions)) {
return false;
}- In
Int.c:1403:
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);- In
Init.h:45:
#define BitVecMustResize(bv, n) \
do { \
if (!BitVecResize((bv), (n))) { \
LOG_FATAL("BitVecMustResize failed"); \
} \
Last updated on