Skip to content
VecInitWithDeepCopy

VecInitWithDeepCopy

Description

Initialize a Vec with deep-copy callbacks. The allocator argument is optional in the same way as VecInit - inside a Scope block you may omit it and MisraScope is used automatically.

Usage example (Cross-references)

Usage examples (Cross-references)
        StrIter si2   = StrIterFromStr(json2);
    
        Vec(Str) data = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        JR_OBJ(si2, {
            true,
            StrInitFromZstr("Success", &alloc),
            VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)
        };
        Str  json    = StrInit(&alloc);
    
        Vec(FunctionInfo) functions = VecInitWithDeepCopy(NULL, FunctionInfoDeinit, &alloc);
    
        FunctionInfo func1 = {12345, StrInitFromZstr("test_func", &alloc), 1024, 4096};
        Str  json    = StrInit(&alloc);
    
        Vec(AnnSymbol) symbols = VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc);
    
        AnnSymbol sym1             = {0};
        VecPushBack(&numbers, num3);
    
        Vec(Str) strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        // Create strings and push them properly
        Str  json    = StrInit(&alloc);
    
        Vec(Str) languages = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        // Create strings and push them properly
    
        Vec(i32) empty_numbers = VecInit(&alloc);
        Vec(Str) empty_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        JW_OBJ(json, {
    
        typedef Vec(AnnSymbol) Symbols;
        Symbols symbols = VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc);
    
        JR_OBJ(si, {
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
    
        JR_OBJ(si, {
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
    
        WriteFmt("[DEBUG] About to parse JSON...\n");
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
    
        WriteFmt("[DEBUG] About to parse JSON...\n");
        StrIter si      = StrIterFromStr(json);
    
        Vec(Str) languages = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        JR_OBJ(si, {
        // Original data
        Vec(i32) original_numbers = VecInit(&alloc);
        Vec(Str) original_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        // Populate arrays
        // Read back from JSON
        Vec(i32) parsed_numbers = VecInit(&alloc);
        Vec(Str) parsed_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        StrIter si = StrIterFromStr(json);
        // Original empty data
        Vec(i32) empty_numbers = VecInit(&alloc);
        Vec(Str) empty_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
        Str empty_str          = StrInit(&alloc);
        // Read back from JSON
        Vec(i32) parsed_numbers = VecInit(&alloc);
        Vec(Str) parsed_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
        Str  parsed_str         = StrInit(&alloc);
        bool found_empty_object = false;
    
        BitVec  source   = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecs patterns = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        bool    result   = true;
    
        BitVec  source   = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecs patterns = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        bool    result   = true;
    // Test vector initialization with deep copy functions
    bool test_vec_init_with_deep_copy(void) {
        WriteFmt("Testing VecInitWithDeepCopy\n");
    
        // Test with struct type and custom copy/deinit functions
        // Test with struct type and custom copy/deinit functions
        typedef Vec(TestItem) TestVec;
        TestVec vec = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, &alloc);
    
        // Check initial state
        // Test with struct type, custom copy/deinit functions, and 8-byte alignment
        typedef Vec(TestItem) TestVec;
        TestVec vec = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, &aligned8);
    
        // Check initial state
        TestVec vec_a = VecInit(&h_default);
        TestVec vec_b = VecInitT(vec_b, &h_default);
        TestVec vec_c = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, &h_default);
        TestVec vec_d = VecInitWithDeepCopyT(vec_d, TestItemCopyInit, TestItemDeinit, &h_default);
        TestVec vec_e = VecInit(&h8);
        TestVec vec_e = VecInit(&h8);
        TestVec vec_f = VecInitT(vec_f, &h16);
        TestVec vec_g = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, &h32);
        TestVec vec_h = VecInitWithDeepCopyT(vec_h, TestItemCopyInit, TestItemDeinit, &h64);
    
        // Create a Strs object (vector of strings)
        Strs sv = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        // Add some strings
        // Create a vector of ComplexItem with deep copy functions
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
    
        // Check initial state
        // Create a vector of ComplexItem with deep copy functions
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
    
        // Create test items
        // Create a vector of ComplexItem with deep copy functions
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
    
        // Create test items
        // Create two vectors of ComplexItem with deep copy functions
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec1 = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
        ComplexVec vec2 = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec1 = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
        ComplexVec vec2 = VecInitWithDeepCopy(ComplexItemCopyInit, ComplexItemDeinit, &alloc);
    
        // Create test items
    
        // Now test VecMergeL which transfers ownership
        ComplexVec vec3 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
        ComplexVec vec4 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
        // Now test VecMergeL which transfers ownership
        ComplexVec vec3 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
        ComplexVec vec4 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // Create more test items
        // Create a temporary vector with no copy_init but with copy_deinit for proper cleanup
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec temp_vec = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // Insert with L-value semantics (vector takes ownership)
        // Create a vector with no copy_init but with copy_deinit for proper cleanup
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // First, create a dummy item and add it to the vector
        // Create a vector with no copy_init but with copy_deinit for proper cleanup
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // Make sure we have enough capacity to avoid reallocation issues
        // Create a vector with no copy_init but with copy_deinit for proper cleanup
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // Add a dummy item first
        // Create a vector with no copy_init but with copy_deinit for proper cleanup
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec1 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
        ComplexVec vec2 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec1 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
        ComplexVec vec2 = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // Create test items for vec2
        // Create a vector with no copy_init but with copy_deinit for proper cleanup
        typedef Vec(ComplexItem) ComplexVec;
        ComplexVec vec = VecInitWithDeepCopy(NULL, ComplexItemDeinit, &alloc);
    
        // Create an array of complex items
        DefaultAllocator alloc = DefaultAllocatorInit();
        WriteFmt("Testing BitVecPrefixMatch(NULL, patterns, 1) - should fatal\n");
        BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        BitVecPush(VecPtrAt(&vp, 0), true);
        BitVecPrefixMatch(NULL, &vp);
        DefaultAllocator alloc = DefaultAllocatorInit();
        WriteFmt("Testing BitVecSuffixMatch(NULL, patterns, 1) - should fatal\n");
        BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        BitVecPush(VecPtrAt(&vp, 0), true);
        BitVecSuffixMatch(NULL, &vp);
Last updated on