Skip to content

BitVecSwap

Description

Efficiently swap the contents of two bitvectors. This is much faster than copying both bitvectors.

Parameters

Name Direction Description
bv1 in,out First bitvector.
bv2 in,out Second bitvector.

Usage example (from documentation)

  BitVecSwap(&flags1, &flags2);  // Swap contents

Success

Returns to the caller. The data pointer, length, capacity, byte_size, and allocator binding of bv1 and bv2 have been exchanged. No allocations are performed and the stored bits are not copied.

Failure

Function cannot fail. NULL pointers or invalid magic on either bitvector are caller bugs and abort via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    void BitVecSwap(BitVec *bv1, BitVec *bv2) {
        ValidateBitVec(bv1);
        ValidateBitVec(bv2);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecSwap\n");
    
        BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Swap the bitvectors
        BitVecSwap(&bv1, &bv2);
    
        // Check that they swapped
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecSwap edge cases\n");
    
        BitVec bv1    = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Test swap with both empty
        BitVecSwap(&bv1, &bv2);
        result = result && (BitVecLen(&bv1) == 0) && (BitVecLen(&bv2) == 0);
        BitVecPush(&bv1, true);
        BitVecPush(&bv1, false);
        BitVecSwap(&bv1, &bv2);
    
        result = result && (BitVecLen(&bv1) == 0);
        }
    
        BitVecSwap(&bv1, &bv2);
        result = result && (BitVecLen(&bv1) == 2) && (BitVecLen(&bv2) == 1000);
        result = result && (BitVecGet(&bv2, 0) == true); // 0 % 3 == 0
    
        // Test swapping with itself (should be safe)
        BitVecSwap(&bv1, &bv1);
        result = result && (BitVecLen(&bv1) == 2);
            // Clone and swap
            BitVec clone = BitVecClone(&bv1);
            BitVecSwap(&bv1, &bv2);
    
            BitVecReserve(&bv1, cycle * 20);
    
        // Test NULL pointer - should abort
        BitVecSwap(NULL, &bv);
    
        BitVecDeinit(&bv);
Last updated on