[llvm-commits] [llvm] r92309 - in /llvm/trunk: include/llvm/ADT/StringRef.h lib/Support/StringRef.cpp

Chris Lattner clattner at apple.com
Wed Dec 30 11:42:23 PST 2009


On Dec 30, 2009, at 9:23 AM, Douglas Gregor wrote:

> Author: dgregor
> Date: Wed Dec 30 11:23:44 2009
> New Revision: 92309
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=92309&view=rev
> Log:
> Implement edit distance for StringRef

Very cool.
> 
> +/// \brief Compute the edit distance between the two given strings.
> +unsigned StringRef::edit_distance(llvm::StringRef Other, 
> +                                  bool AllowReplacements) {

This function is a big blob of magic :).  Is there a citation or some description of the algorithm you can add?

-Chris

> +  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) 
> +    previous[i] = i;
> +
> +  std::vector<unsigned> current(n+1, 0);
> +  for (size_type y = 1; y <= m; ++y) {
> +    current.assign(n+1, 0);
> +    current[0] = y;
> +    for (size_type x = 1; x <= n; ++x) {
> +      if (AllowReplacements) {
> +        current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
> +                         min(current[x-1], previous[x])+1);
> +      }
> +      else {
> +        if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
> +        else current[x] = min(current[x-1], previous[x]) + 1;
> +      }
> +    }
> +    current.swap(previous);
> +  }
> +
> +  return previous[n];
> +}
> +
> //===----------------------------------------------------------------------===//
> // String Searching
> //===----------------------------------------------------------------------===//
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list