[llvm-commits] [llvm] r116867 - in /llvm/trunk: include/llvm/ADT/StringRef.h lib/Support/StringRef.cpp
Douglas Gregor
dgregor at apple.com
Tue Oct 19 15:13:48 PDT 2010
Author: dgregor
Date: Tue Oct 19 17:13:48 2010
New Revision: 116867
URL: http://llvm.org/viewvc/llvm-project?rev=116867&view=rev
Log:
Extend StringRef's edit-distance algorithm to permit an upper bound on the allowed edit distance
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/lib/Support/StringRef.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=116867&r1=116866&r2=116867&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Tue Oct 19 17:13:48 2010
@@ -142,11 +142,16 @@
/// operation, rather than as two operations (an insertion and a
/// removal).
///
+ /// \param MaxEditDistance If non-zero, the maximum edit distance that
+ /// this routine is allowed to compute. If the edit distance will exceed
+ /// that maximum, returns \c MaxEditDistance+1.
+ ///
/// \returns the minimum number of character insertions, removals,
/// or (if \p AllowReplacements is \c true) replacements needed to
/// transform one of the given strings into the other. If zero,
/// the strings are identical.
- unsigned edit_distance(StringRef Other, bool AllowReplacements = true);
+ unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
+ unsigned MaxEditDistance = 0);
/// str - Get the contents as an std::string.
std::string str() const {
Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=116867&r1=116866&r2=116867&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Tue Oct 19 17:13:48 2010
@@ -68,7 +68,8 @@
// 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 @@
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 @@
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;
More information about the llvm-commits
mailing list