[llvm-commits] [llvm] r92340 - in /llvm/trunk: lib/Support/StringRef.cpp unittests/ADT/StringRefTest.cpp

Douglas Gregor dgregor at apple.com
Wed Dec 30 20:24:37 PST 2009


Author: dgregor
Date: Wed Dec 30 22:24:34 2009
New Revision: 92340

URL: http://llvm.org/viewvc/llvm-project?rev=92340&view=rev
Log:
Document the edit-distance algorithm used in StringRef, switch it over
to SmallVector, and add a unit test.

Modified:
    llvm/trunk/lib/Support/StringRef.cpp
    llvm/trunk/unittests/ADT/StringRefTest.cpp

Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=92340&r1=92339&r2=92340&view=diff

==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Wed Dec 30 22:24:34 2009
@@ -8,7 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/StringRef.h"
-#include <vector>
+#include "llvm/ADT/SmallVector.h"
 using namespace llvm;
 
 // MSVC emits references to this into the translation units which reference it.
@@ -36,17 +36,26 @@
   return Length < RHS.Length ? -1 : 1;
 }
 
-/// \brief Compute the edit distance between the two given strings.
+// Compute the edit distance between the two given strings.
 unsigned StringRef::edit_distance(llvm::StringRef Other, 
                                   bool AllowReplacements) {
+  // The algorithm implemented below is the "classic"
+  // dynamic-programming algorithm for computing the Levenshtein
+  // distance, which is described here:
+  //
+  //   http://en.wikipedia.org/wiki/Levenshtein_distance
+  //
+  // Although the algorithm is typically described using an m x n
+  // array, only two rows are used at a time, so this implemenation
+  // just keeps two separate vectors for those two rows.
   size_type m = size();
   size_type n = Other.size();
 
-  std::vector<unsigned> previous(n+1, 0);
-  for (std::vector<unsigned>::size_type i = 0; i <= n; ++i) 
+  SmallVector<unsigned, 32> previous(n+1, 0);
+  for (SmallVector<unsigned, 32>::size_type i = 0; i <= n; ++i) 
     previous[i] = i;
 
-  std::vector<unsigned> current(n+1, 0);
+  SmallVector<unsigned, 32> current(n+1, 0);
   for (size_type y = 1; y <= m; ++y) {
     current.assign(n+1, 0);
     current[0] = y;

Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=92340&r1=92339&r2=92340&view=diff

==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Wed Dec 30 22:24:34 2009
@@ -247,6 +247,11 @@
   EXPECT_EQ(0U, Str.count("zz"));
 }
 
+TEST(StringRefTest, EditDistance) {
+  StringRef Str("hello");
+  EXPECT_EQ(2, Str.edit_distance("hill"));
+}
+
 TEST(StringRefTest, Misc) {
   std::string Storage;
   raw_string_ostream OS(Storage);





More information about the llvm-commits mailing list