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)
- In
Math.c:379:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance basic functionality\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:394:
bool error = true;
u64 distance = BitVecEditDistance(&bv1, &bv2, &error);
result = result && !error && (distance == 0);- In
Math.c:403:
BitVecPush(&bv2, true);
distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 1);- In
Math.c:413:
BitVecPush(&bv2, false); // Extra bit
distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 1);- In
Math.c:426:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance edge cases\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:433:
// Test empty to empty
u64 distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 0);- In
Math.c:440:
BitVecPush(&bv2, false);
distance = BitVecEditDistance(&bv1, &bv2);
result = result && (distance == 2);- In
Math.c:444:
// Test non-empty to empty
distance = BitVecEditDistance(&bv2, &bv1);
result = result && (distance == 2);- In
Math.c:738:
BitVecPush(&small2, i % 3 == 0);
}
u64 edit_dist = BitVecEditDistance(&small1, &small2);
result = result && (hamming < 1000);- In
Math.c:863:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance(NULL, bv2) - should fatal\n");
BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true);- In
Math.c:866:
BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true);
BitVecEditDistance(NULL, &bv2);
BitVecDeinit(&bv2);
DefaultAllocatorDeinit(&alloc);- In
Math.c:875:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance(bv1, NULL) - should fatal\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true);- In
Math.c:878:
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true);
BitVecEditDistance(&bv1, NULL);
BitVecDeinit(&bv1);
DefaultAllocatorDeinit(&alloc);- In
Math.c:1079:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance column-0 base case\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1088:
BitVecPush(&bv2, true);
u64 dist = BitVecEditDistance(&bv1, &bv2);
bool result = (dist == 1);- In
Math.c:1104:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance deletion term\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1113:
BitVecPush(&bv2, false);
u64 dist = BitVecEditDistance(&bv1, &bv2);
bool result = (dist == 1);- In
Math.c:1133:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance empty->len base row fill\n");
// Stamp large values into the scratch arena: a 6-vs-6 edit distance fills
- In
Math.c:1146:
BitVecPush(&warm_b, (i % 3) == 0);
}
(void)BitVecEditDistance(&warm_a, &warm_b);
BitVecDeinit(&warm_a);
BitVecDeinit(&warm_b);- In
Math.c:1156:
}
u64 dist = BitVecEditDistance(&bv1, &bv2);
bool result = (dist == 5);- In
Math.c:1173:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance base row counter advances forward\n");
// warm_b length == target bv2 length (4): freed scratch rows match the size
- In
Math.c:1184:
BitVecPush(&warm_b, (i % 2) == 0);
}
(void)BitVecEditDistance(&warm_a, &warm_b);
BitVecDeinit(&warm_a);
BitVecDeinit(&warm_b);- In
Math.c:1194:
}
u64 dist = BitVecEditDistance(&bv1, &bv2);
bool result = (dist == 4);- In
Math.c:1212:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecEditDistance insertion term\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
Math.c:1222:
BitVecPush(&bv2, true);
u64 dist = BitVecEditDistance(&bv1, &bv2);
bool result = (dist == 2);
Last updated on