Skip to content
BitVecCapacity

BitVecCapacity

Description

Maximum number of bits the bitvector can hold without reallocation.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        *out = int_wrap(BitVecInitWithCapacity(capacity, alloc));
        if (capacity != 0 && BitVecCapacity(INT_BITS(out)) < capacity) {
            IntDeinit(out);
            *out = IntInit(alloc);
    
        // Check that capacity is larger than length
        u64  initial_capacity = BitVecCapacity(&bv);
        bool result           = (initial_capacity >= 100) && (BitVecLen(&bv) == 3);
    
        // Check that capacity is now closer to length
        result = result && (BitVecCapacity(&bv) < initial_capacity);
        result = result && (BitVecCapacity(&bv) >= BitVecLen(&bv));
        // Check that capacity is now closer to length
        result = result && (BitVecCapacity(&bv) < initial_capacity);
        result = result && (BitVecCapacity(&bv) >= BitVecLen(&bv));
    
        // Check that data is still intact
    
        // Check that capacity was set correctly
        bool result = (BitVecCapacity(&bv) >= 50) && (BitVecLen(&bv) == 2);
    
        // Check that data is still intact
    
        // Capacity should still accommodate at least the current length
        result = result && (BitVecCapacity(&bv) >= BitVecLen(&bv));
        result = result && (BitVecLen(&bv) == 2);
        // Clone should share the same Allocator* and therefore see identical
        // configuration fields on the base allocator.
        bool result = BitVecLen(&clone) == BitVecLen(&original) && BitVecCapacity(&clone) >= BitVecLen(&original) &&
                      BitVecAllocator(&clone) == BitVecAllocator(&original) &&
                      BitVecAllocator(&clone)->allocate == BitVecAllocator(&original)->allocate &&
        // Test shrink on empty bitvec
        BitVecShrinkToFit(&bv);
        result = result && (BitVecLen(&bv) == 0) && (BitVecCapacity(&bv) >= 0);
    
        // Test shrink on single element
        BitVecPush(&bv, true);
        BitVecShrinkToFit(&bv);
        result = result && (BitVecLen(&bv) == 1) && (BitVecCapacity(&bv) >= 1);
        result = result && (BitVecGet(&bv, 0) == true);
        // Test set capacity on empty bitvec
        BitVecReserve(&bv, 100);
        result = result && (BitVecCapacity(&bv) >= 100) && (BitVecLen(&bv) == 0);
    
        // BitVecReserve is grow-only; use BitVecClear to drop length to 0.
        // Test setting very large capacity
        BitVecReserve(&bv, 10000);
        result = result && (BitVecCapacity(&bv) >= 10000);
    
        BitVecDeinit(&bv);
        // Check initial state
        bool result =
            (BitVecLen(&bitvec) == 0 && BitVecCapacity(&bitvec) == 0 && BitVecData(&bitvec) == NULL &&
             BitVecByteSize(&bitvec) == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecLength and BitVecCapacity\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        result = result && (BitVecLen(&bv) == 3);
        result = result && (BitVecCapacity(&bv) >= 3);
    
        // Reserve more space
        BitVecReserve(&bv, 100);
        result = result && (BitVecLen(&bv) == 3);
        result = result && (BitVecCapacity(&bv) >= 100);
    
        BitVecDeinit(&bv);
        // Test length and capacity macros if they exist
        result = result && (BitVecLen(&bv) == 2);
        result = result && (BitVecCapacity(&bv) >= 2);
    
        // Test some bit operations
        // Check initial state
        bool result = (BitVecLen(&bv) == 0);
        result      = result && (BitVecCapacity(&bv) == 0);
        result      = result && (BitVecData(&bv) == NULL);
        result      = result && (BitVecByteSize(&bv) == 0);
        // but we can check that the structure is reset to safe values
        result = result && (BitVecLen(&bv) == 0);
        result = result && (BitVecCapacity(&bv) == 0);
        result = result && (BitVecData(&bv) == NULL);
        result = result && (BitVecByteSize(&bv) == 0);
    
        // Check that capacity was increased
        bool result = (BitVecCapacity(&bv) >= 50);
        result      = result && (BitVecLen(&bv) == 0);     // Length should still be 0
        result      = result && (BitVecData(&bv) != NULL); // Memory should be allocated
    
        result = result && (BitVecLen(&bv) == 10);
        result = result && (BitVecCapacity(&bv) >= 50); // Should still have the reserved capacity
    
        // Test reserving less than current capacity (should be no-op)
    
        // Test reserving less than current capacity (should be no-op)
        u64 original_capacity = BitVecCapacity(&bv);
        BitVecReserve(&bv, 25);
        result = result && (BitVecCapacity(&bv) == original_capacity);
        u64 original_capacity = BitVecCapacity(&bv);
        BitVecReserve(&bv, 25);
        result = result && (BitVecCapacity(&bv) == original_capacity);
    
        // Clean up
        // Check initial state
        bool result            = (BitVecLen(&bv) == 4) && (BitVecData(&bv) != NULL);
        u64  original_capacity = BitVecCapacity(&bv);
    
        // Clear the bitvector
        // Check that length is 0 but capacity and memory allocation remain
        result = result && (BitVecLen(&bv) == 0);
        result = result && (BitVecCapacity(&bv) == original_capacity);
        result = result && (BitVecData(&bv) != NULL); // Memory should still be allocated
        // Test reserving 0 (should be safe no-op)
        BitVecReserve(&bv, 0);
        result = result && (BitVecCapacity(&bv) == 0) && (BitVecData(&bv) == NULL);
    
        // Test reserving 1 bit (minimum meaningful size)
        // Test reserving 1 bit (minimum meaningful size)
        BitVecReserve(&bv, 1);
        result = result && (BitVecCapacity(&bv) >= 1) && (BitVecData(&bv) != NULL);
    
        // Test very large but reasonable reservation
        // Test very large but reasonable reservation
        BitVecReserve(&bv, 10000);
        result = result && (BitVecCapacity(&bv) >= 10000);
    
        // Test reserving same u64 repeatedly (should be no-op)
    
        // Test reserving same u64 repeatedly (should be no-op)
        u64 cap_before = BitVecCapacity(&bv);
        BitVecReserve(&bv, BitVecCapacity(&bv));
        BitVecReserve(&bv, BitVecCapacity(&bv));
        // Test reserving same u64 repeatedly (should be no-op)
        u64 cap_before = BitVecCapacity(&bv);
        BitVecReserve(&bv, BitVecCapacity(&bv));
        BitVecReserve(&bv, BitVecCapacity(&bv));
        result = result && (BitVecCapacity(&bv) == cap_before);
        u64 cap_before = BitVecCapacity(&bv);
        BitVecReserve(&bv, BitVecCapacity(&bv));
        BitVecReserve(&bv, BitVecCapacity(&bv));
        result = result && (BitVecCapacity(&bv) == cap_before);
        BitVecReserve(&bv, BitVecCapacity(&bv));
        BitVecReserve(&bv, BitVecCapacity(&bv));
        result = result && (BitVecCapacity(&bv) == cap_before);
    
        BitVecDeinit(&bv);
    
        // Initially should be empty
        bool result = (BitVecLen(&bv) == 0) && (BitVecCapacity(&bv) == 0);
    
        // Add some bits
    
        result = result && (BitVecLen(&bv) == 3);
        result = result && (BitVecCapacity(&bv) >= 3);
    
        // Test empty check
        // Test all macros on empty bitvector
        result = result && (BitVecLen(&bv) == 0);
        result = result && (BitVecCapacity(&bv) == 0);
        result = result && BitVecEmpty(&bv);
        result = result && (BitVecByteSize(&bv) == 0);
    
        result = result && (BitVecLen(&bv) == 65);
        result = result && (BitVecCapacity(&bv) >= 65);
        result = result && !BitVecEmpty(&bv);
        result = result && (BitVecByteSize(&bv) >= 9); // At least 9 bytes for 65 bits
Last updated on