BitVecEditDistance
Description
Calculate edit distance between two bitvectors. Edit distance is the minimum number of single-bit operations required to transform one into the other.
This public macro supports both forms:
BitVecEditDistance(bv1, bv2)- returns the result, no error channel.BitVecEditDistance(bv1, bv2, error)- writes the error flag througherror.
Parameters
| Name | Direction | Description |
|---|---|---|
bv1 |
in | First bitvector. |
bv2 |
in | Second bitvector. |
error |
out | Optional pointer set to true on failure and false on success. |
Usage example (from documentation)
u64 distance = BitVecEditDistance(&bv1, &bv2);Success
Returns the edit distance as a u64. Neither operand is modified.
Failure
Returns 0 on scratch-buffer allocation failure. With the three-argument form *error is set to true; with the two-argument form the caller cannot distinguish failure from a true zero result.
Usage example (Cross-references)
Usage examples (Cross-references)
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance basic functionality\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
bool error = true;
u64 distance = BitVecEditDistance(&bv1, &bv2, &error);
result = result && !error && (distance == 0); BitVecPush(&bv2, true);
distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 1); BitVecPush(&bv2, false); // Extra bit
distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 1); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance edge cases\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
// Test empty to empty
u64 distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 0); BitVecPush(&bv2, false);
distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 2);
// Test non-empty to empty
distance = BitVecEditDistance(&bv2, &bv1);
result = result && (distance == 2); BitVecPush(&small2, i % 3 == 0);
}
u64 edit_dist = BitVecEditDistance(&small1, &small2);
result = result && (hamming < 1000); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance(NULL, bv2) - should fatal\n");
BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true); BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true);
BitVecEditDistance(NULL, &bv2);
BitVecDeinit(&bv2);
DefaultAllocatorDeinit(&alloc); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance(bv1, NULL) - should fatal\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true); BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true);
BitVecEditDistance(&bv1, NULL);
BitVecDeinit(&bv1);
DefaultAllocatorDeinit(&alloc);
Last updated on