BitVecClone
Description
Create a deep copy of a bitvector. The returned bitvector must be deinitialized when no longer needed.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in | Bitvector to clone. |
Usage example (from documentation)
BitVec copy = BitVecClone(&flags);
// ... use copy ...
BitVecDeinit(©);Success
Returns a fresh BitVec holding a deep copy of bv: same length, same bits, capacity sized for the length, inheriting the source’s allocator binding. The source is untouched.
Failure
Returns an empty bitvector (length 0, capacity 0, data NULL, allocator still bound) when allocation fails. Use BitVecTryClone if you need explicit failure propagation.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:247:
}
BitVec BitVecClone(BitVec *bv) {
BitVec clone;- In
BitVec.c:1119:
}
BitVec temp = BitVecClone(bv);
if (temp.length != bv->length) {
BitVecDeinit(&temp);- In
BitVec.c:1145:
}
BitVec temp = BitVecClone(bv);
if (temp.length != bv->length) {
BitVecDeinit(&temp); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecClone\n");
BitVec original = BitVecInit(ALLOCATOR_OF(&alloc));
// Clone the bitvector
BitVec clone = BitVecClone(&original);
// Check that clone has same content as original
bool test_bitvec_clone_inherits_allocator_config(void) {
WriteFmt("Testing BitVecClone allocator inheritance\n");
DefaultAllocator alloc = DefaultAllocatorInit(); BitVecPush(&original, true);
BitVec clone = BitVecClone(&original);
// Clone should share the same Allocator* and therefore see identical
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecClone edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Test clone empty bitvec
BitVec clone1 = BitVecClone(&bv);
result = result && (BitVecLen(&clone1) == 0);
BitVecDeinit(&clone1); // Test clone single element
BitVecPush(&bv, true);
BitVec clone2 = BitVecClone(&bv);
result = result && (BitVecLen(&clone2) == 1);
result = result && (BitVecGet(&clone2, 0) == true); }
BitVec clone3 = BitVecClone(&bv);
result = result && (BitVecLen(&clone3) == 1000);
// Clone and swap
BitVec clone = BitVecClone(&bv1);
BitVecSwap(&bv1, &bv2);
// Test NULL pointer - should abort
BitVecClone(NULL);
return false;
// Test various shift amounts
BitVec original = BitVecClone(&bv);
// Shift left by 1, then right by 1 - should NOT restore original (data loss)
}
BitVec original = BitVecClone(&bv);
// Rotate left by 3, then right by 3
// Test various shift amounts
BitVec original = BitVecClone(&bv);
// Shift left by 1, then right by 1 - should NOT restore original (data loss)
}
BitVec original = BitVecClone(&bv);
// Rotate left by 3, then right by 3
// Test equal signed values
BitVec bv3 = BitVecClone(&bv1);
result = result && (BitVecSignedCompare(&bv1, &bv3) == 0);
// Test equal cases
BitVec bv3 = BitVecClone(&bv1);
result = result && !(BitVecCompare(&bv1, &bv3) < 0);
result = result && !(BitVecNumericalCompare(&bv1, &bv3) < 0);
Last updated on