BitVecShrinkToFit

Table of Contents

BitVecShrinkToFit

Description

Reduce bitvector capacity to match its current length. Frees any unused memory allocated beyond the current length.

Parameters

NameDirectionDescription
bvinBitvector to shrink

Usage example (from documentation)

  BitVecShrinkToFit(&flags);  // Free unused capacity

Usage example (Cross-references)

    }
    
    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;

Share :

Related Posts

BitVecPush

BitVecPush Description Push a bit to the end of bitvector. Grows the bitvector if necessary.

Read More

BitVecReserve

BitVecReserve Description Reserve space for at least n bits in bitvector. Does not change the length, only ensures capacity.

Read More

BitVecInsertMultiple

BitVecInsertMultiple Description Insert all bits from another bitvector at a specific position. All existing bits at and after the position are shifted right.

Read More