BitVecShrinkToFit
- Function
- August 22, 2025
Table of Contents
BitVecShrinkToFit
BitVecShrinkToFit
Description
Reduce bitvector capacity to match its current length. Frees any unused memory allocated beyond the current length.
Parameters
Name | Direction | Description |
---|---|---|
bv | in | Bitvector to shrink |
Usage example (from documentation)
BitVecShrinkToFit(&flags); // Free unused capacity
Usage example (Cross-references)
- In
BitVec.c:108
:
}
void BitVecShrinkToFit(BitVec *bv) {
ValidateBitVec(bv);
if (bv->length == 0) {
// Test BitVecShrinkToFit function
bool test_bitvec_shrink_to_fit(void) {
printf("Testing BitVecShrinkToFit\n");
BitVec bv = BitVecInit();
// Shrink to fit
BitVecShrinkToFit(&bv);
// Check that capacity is now closer to length
// Edge case tests
bool test_bitvec_shrink_to_fit_edge_cases(void) {
printf("Testing BitVecShrinkToFit edge cases\n");
BitVec bv = BitVecInit();
// Test shrink on empty bitvec
BitVecShrinkToFit(&bv);
result = result && (bv.length == 0) && (bv.capacity >= 0);
// Test shrink on single element
BitVecPush(&bv, true);
BitVecShrinkToFit(&bv);
result = result && (bv.length == 1) && (bv.capacity >= 1);
result = result && (BitVecGet(&bv, 0) == true);
// Test multiple shrinks (should be safe)
BitVecShrinkToFit(&bv);
BitVecShrinkToFit(&bv);
result = result && (bv.length == 1);
// Test multiple shrinks (should be safe)
BitVecShrinkToFit(&bv);
BitVecShrinkToFit(&bv);
result = result && (bv.length == 1);
BitVecReserve(&bv, 1000);
BitVecClear(&bv);
BitVecShrinkToFit(&bv);
result = result && (bv.length == 0);
// Reu64 operations
BitVecReserve(&bv1, cycle * 20);
BitVecShrinkToFit(&bv2);
// Verify data integrity
// Test NULL bitvec pointer - should abort
BitVecShrinkToFit(NULL);
return false;