Skip to content

VecCapacity

Description

Capacity in elements: the most the vector can hold before the next reallocation. Always >= VecLen(v).

Parameters

Name Direction Description
v in Vector to query.

Usage example (Cross-references)

Usage examples (Cross-references)
    /// TAGS: Str, Access, Capacity
    ///
    #define StrCapacity(str) VecCapacity(str)
    
    ///
    
        ValidateGraph(graph);
        old_capacity = VecCapacity(&graph->slots);
    
        success = reserve_vec(GENERIC_VEC(&graph->free_indices), sizeof(u32), n) &&
        }
    
        if (VecCapacity(&graph->slots) != old_capacity) {
            graph_bump_mutation_epoch(graph);
        }
        bool ok = false;
        VecInitStack(u8, buf, 28) {
            if (!stream_read(self, 1, 0, VecBegin(&buf), VecCapacity(&buf)))
                break;
            BufIter bi = BufIterFromMemory(VecBegin(&buf), VecCapacity(&buf));
            if (!stream_read(self, 1, 0, VecBegin(&buf), VecCapacity(&buf)))
                break;
            BufIter bi = BufIterFromMemory(VecBegin(&buf), VecCapacity(&buf));
            if (!BufReadFmt(&bi, FMT_PDB_INFO_LE, self->info.version, self->info.signature, self->info.age)) {
                LOG_ERROR("PDB: info stream prefix truncated");
        u16 global_idx = 0, build_num = 0, public_idx = 0, pdb_dll_ver = 0, pdb_dll_rbld = 0, flags = 0, machine = 0;
        VecInitStack(u8, hdr, DBI_HEADER_SIZE) {
            if (!stream_read(self, DBI_STREAM_INDEX, 0, VecBegin(&hdr), VecCapacity(&hdr)))
                break;
                break;
    
            BufIter bi = BufIterFromMemory(VecBegin(&hdr), VecCapacity(&hdr));
            if (!BufReadFmt(
                    &bi,
        ValidateGraph(&graph);
    
        bool        result            = VecCapacity(&graph.slots) >= 8;
        GraphNodeId first_id          = GraphAddNodeR(&graph, 10);
        GraphNodeId second_id         = GraphAddNodeR(&graph, 20);
        GraphNodeId third_id          = GraphAddNodeR(&graph, 30);
        u64         slot_count        = VecLen(&graph.slots);
        size        slot_capacity     = VecCapacity(&graph.slots);
        u32         first_generation  = GraphNodeIdGeneration(first_id);
        u32         second_generation = GraphNodeIdGeneration(second_id);
        // surface does not expose.
        result = result && VecLen(&graph.slots) == slot_count && VecLen(&graph.free_indices) == slot_count;
        result = result && VecCapacity(&graph.slots) == slot_capacity && VecCapacity(&graph.free_indices) >= slot_count;
        result = result && graph.pending_delete_count == 0 && VecLen(&graph.pending_edge_removals) == 0;
    
        bool result = GraphReserve(&graph, 64);
        result      = result && (VecCapacity(&graph.slots) >= 64);
    
        GraphDeinit(&graph);
        (void)GraphAddNodeR(&graph, 1);
    
        size old_capacity = VecCapacity(&graph.slots);
        u64  old_epoch    = GraphMutationEpoch(&graph);
    
        bool result = grew;
        result      = result && (VecCapacity(&graph.slots) > old_capacity);
        result      = result && (GraphMutationEpoch(&graph) > old_epoch);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL &&
             VecCopyInit(&vec) == (GenericCopyInit)ComplexItemCopyInit &&
             VecCopyDeinit(&vec) == (GenericCopyDeinit)ComplexItemDeinit);
        // Reserve zero capacity
        VecReserve(&vec, 0);
        result = result && (VecCapacity(&vec) == 0);
    
        // Push an element (should auto-resize)
    
        // Original capacity should be at least 100
        bool result = (VecCapacity(&vec) >= 100);
    
        // Try to reduce space
    
        // Capacity should now be closer to the actual length
        result = result && (VecCapacity(&vec) < 100) && (VecCapacity(&vec) >= VecLen(&vec));
    
        // Check that the data is still intact
    
        // Initial capacity should be 0
        bool result = (VecCapacity(&vec) == 0);
    
        // Reserve space for 50 elements
    
        // Capacity should now be at least 50
        result = result && (VecCapacity(&vec) >= 50);
    
        // Length should still be 0
    
        // Capacity should still be at least 50
        result = result && (VecCapacity(&vec) >= 50);
    
        // Length should now be 5
    
        // Capacity should still be at least 50
        result = result && (VecCapacity(&vec) >= 50);
    
        // Clean up
    
        // Remember the capacity
        size original_capacity = VecCapacity(&vec);
    
        // Clear the vector
    
        // Capacity should remain the same
        result = result && (VecCapacity(&vec) == original_capacity);
    
        // Data pointer should still be valid
        bool ok = VecResize(&vec, 100);
    
        bool result = ok && (VecLen(&vec) == 100) && (VecCapacity(&vec) >= 100);
    
        VecDeinit(&vec);
        // Whichever way the constant forces the branch, a real grow must leave
        // capacity large enough to hold the new length.
        bool result = (VecCapacity(&vec) >= 64);
    
        VecDeinit(&vec);
        VecResize(&vec, 10);
    
        bool result = (VecLen(&vec) == 10) && (VecCapacity(&vec) == 16);
    
        VecDeinit(&vec);
    
        // Real: capacity 0.
        bool result = (VecCapacity(&vec) == 0);
    
        VecDeinit(&vec);
    
        // Real: capacity == length == 5.
        bool result = (VecCapacity(&vec) == 5);
    
        VecDeinit(&vec);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 1 &&
             VecCopyInit(&vec) == NULL && VecCopyDeinit(&vec) == NULL);
    
        // Check initial state
        result = result && (VecLen(&test_vec) == 0 && VecCapacity(&test_vec) == 0 && VecBegin(&test_vec) == NULL &&
                            VecAllocator(&test_vec)->alignment == 1 && VecCopyInit(&test_vec) == NULL &&
                            VecCopyDeinit(&test_vec) == NULL);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 4 &&
             VecCopyInit(&vec) == NULL && VecCopyDeinit(&vec) == NULL);
    
        // Check initial state
        result = result && (VecLen(&test_vec) == 0 && VecCapacity(&test_vec) == 0 && VecBegin(&test_vec) == NULL &&
                            VecAllocator(&test_vec)->alignment == 16 && VecCopyInit(&test_vec) == NULL &&
                            VecCopyDeinit(&test_vec) == NULL);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 1 &&
             VecCopyInit(&vec) == (GenericCopyInit)TestItemCopyInit &&
             VecCopyDeinit(&vec) == (GenericCopyDeinit)TestItemDeinit);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 8 &&
             VecCopyInit(&vec) == (GenericCopyInit)TestItemCopyInit &&
             VecCopyDeinit(&vec) == (GenericCopyDeinit)TestItemDeinit);
        VecInitStack(int, vec, 10) {
            // Stack-init: NULL allocator distinguishes from heap-init.
            if (VecLen(&vec) != 0 || VecCapacity(&vec) != 10 || VecBegin(&vec) == NULL || VecAllocator(&vec) != NULL) {
                result = false;
            }
        // Test with struct type
        VecInitStack(TestItem, test_vec, 5) {
            if (VecLen(&test_vec) != 0 || VecCapacity(&test_vec) != 5 || VecBegin(&test_vec) == NULL ||
                VecAllocator(&test_vec) != NULL) {
                result = false;
        // Check that the clone has the same data but different memory
        bool result =
            (VecLen(&clone) == VecLen(&src) && VecCapacity(&clone) >= VecLen(&src) && VecBegin(&clone) != VecBegin(&src) &&
             VecAllocator(&clone)->alignment == VecAllocator(&src)->alignment);
        // run, so capacity must have expanded past the old value of 8. The mutant
        // skips the grow and leaves capacity == 8.
        bool result = ok && (VecLen(&vec) == 16) && (VecCapacity(&vec) >= 16);
        for (int i = 0; i < 8; i++) {
            result = result && (VecAt(&vec, (size)i) == i);
        // Real code reserves past the old capacity (10 -> 16). The `>` mutant skips
        // the grow and leaves capacity at 10.
        bool result = ok && (VecLen(&vec) == 10) && (VecCapacity(&vec) > 10);
        for (int i = 0; i < 5; i++) {
            result = result && (VecAt(&vec, (size)i) == i);
        // Holding 8 elements requires growth from capacity 0; the mutant's
        // inverted predicate skips it.
        bool result = ok && (VecLen(&vec) == 8) && (VecCapacity(&vec) >= 8);
        for (size i = 0; i < 8; i++) {
            result = result && (VecAt(&vec, i) == add[i]);
        // replacement makes the call return false. Either way the real growth
        // contract (true return, all 10 elements, capacity >= 10) breaks.
        bool result = ok && (VecLen(&vec) == 10) && (VecCapacity(&vec) >= 10);
        for (size i = 0; i < 10; i++) {
            result = result && (VecAt(&vec, i) == add[i]);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 1 &&
             VecCopyInit(&vec) == NULL && VecCopyDeinit(&vec) == NULL);
    
        // Check initial state
        result = result && (VecLen(&test_vec) == 0 && VecCapacity(&test_vec) == 0 && VecBegin(&test_vec) == NULL &&
                            VecAllocator(&test_vec)->alignment == 1 && VecCopyInit(&test_vec) == NULL &&
                            VecCopyDeinit(&test_vec) == NULL);
Last updated on