aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/StringRef.cpp
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2010-10-19 22:13:48 +0000
committerDouglas Gregor <dgregor@apple.com>2010-10-19 22:13:48 +0000
commit21afc3b012220a295828c8fbb8124cb3dc203bec (patch)
tree09efcb64210a8aa1e3b7d09578f0afbe7d17e3c7 /llvm/lib/Support/StringRef.cpp
parentba266eec79a9293910a5875d31cc0813bbc8a233 (diff)
downloadllvm-21afc3b012220a295828c8fbb8124cb3dc203bec.zip
llvm-21afc3b012220a295828c8fbb8124cb3dc203bec.tar.gz
llvm-21afc3b012220a295828c8fbb8124cb3dc203bec.tar.bz2
Extend StringRef's edit-distance algorithm to permit an upper bound on the allowed edit distance
llvm-svn: 116867
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r--llvm/lib/Support/StringRef.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index 46f26b2..5ad8628 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -68,7 +68,8 @@ int StringRef::compare_numeric(StringRef RHS) const {
// Compute the edit distance between the two given strings.
unsigned StringRef::edit_distance(llvm::StringRef Other,
- bool AllowReplacements) {
+ bool AllowReplacements,
+ unsigned MaxEditDistance) {
// The algorithm implemented below is the "classic"
// dynamic-programming algorithm for computing the Levenshtein
// distance, which is described here:
@@ -94,6 +95,8 @@ unsigned StringRef::edit_distance(llvm::StringRef Other,
for (size_type y = 1; y <= m; ++y) {
current[0] = y;
+ unsigned BestThisRow = current[0];
+
for (size_type x = 1; x <= n; ++x) {
if (AllowReplacements) {
current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
@@ -103,8 +106,12 @@ unsigned StringRef::edit_distance(llvm::StringRef Other,
if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
else current[x] = min(current[x-1], previous[x]) + 1;
}
+ BestThisRow = min(BestThisRow, current[x]);
}
+ if (MaxEditDistance && BestThisRow > MaxEditDistance)
+ return MaxEditDistance + 1;
+
unsigned *tmp = current;
current = previous;
previous = tmp;