16 #ifndef LLVM_ADT_EDIT_DISTANCE_H
17 #define LLVM_ADT_EDIT_DISTANCE_H
44 bool AllowReplacements =
true,
45 unsigned MaxEditDistance = 0) {
58 const unsigned SmallBufferSize = 64;
59 unsigned SmallBuffer[SmallBufferSize];
61 unsigned *Previous = SmallBuffer;
62 if (2*(n + 1) > SmallBufferSize) {
63 Previous =
new unsigned [2*(n+1)];
64 Allocated.
reset(Previous);
66 unsigned *Current = Previous + (n + 1);
68 for (
unsigned i = 0; i <= n; ++i)
73 unsigned BestThisRow = Current[0];
76 if (AllowReplacements) {
77 Current[x] = std::min(
78 Previous[x-1] + (FromArray[y-1] == ToArray[x-1] ? 0u : 1u),
79 std::min(Current[x-1], Previous[x])+1);
82 if (FromArray[y-1] == ToArray[x-1]) Current[x] = Previous[x-1];
83 else Current[x] = std::min(Current[x-1], Previous[x]) + 1;
85 BestThisRow = std::min(BestThisRow, Current[x]);
88 if (MaxEditDistance && BestThisRow > MaxEditDistance)
89 return MaxEditDistance + 1;
91 unsigned *tmp = Current;
96 unsigned Result = Previous[n];
unsigned ComputeEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, bool AllowReplacements=true, unsigned MaxEditDistance=0)
Determine the edit distance between two sequences.
size_t size() const
size - Get the array size.