[llvm-commits] [llvm] r92309 - in /llvm/trunk: include/llvm/ADT/StringRef.h lib/Support/StringRef.cpp
Daniel Dunbar
daniel at zuster.org
Wed Dec 30 11:44:09 PST 2009
On Wed, Dec 30, 2009 at 9:23 AM, Douglas Gregor <dgregor at apple.com> 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
Nice! Can this use SmallVector instead of vector, and get a unittest?
- Daniel
> 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=92309&r1=92308&r2=92309&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/StringRef.h (original)
> +++ llvm/trunk/include/llvm/ADT/StringRef.h Wed Dec 30 11:23:44 2009
> @@ -133,6 +133,22 @@
> /// compare_lower - Compare two strings, ignoring case.
> int compare_lower(StringRef RHS) const;
>
> + /// \brief Determine the edit distance between this string and another
> + /// string.
> + ///
> + /// \param Other the string to compare this string against.
> + ///
> + /// \param AllowReplacements whether to allow character
> + /// replacements (change one character into another) as a single
> + /// operation, rather than as two operations (an insertion and a
> + /// removal).
> + ///
> + /// \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);
> +
> /// str - Get the contents as an std::string.
> std::string str() const { return std::string(Data, Length); }
>
>
> Modified: llvm/trunk/lib/Support/StringRef.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=92309&r1=92308&r2=92309&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Support/StringRef.cpp (original)
> +++ llvm/trunk/lib/Support/StringRef.cpp Wed Dec 30 11:23:44 2009
> @@ -8,6 +8,7 @@
> //===----------------------------------------------------------------------===//
>
> #include "llvm/ADT/StringRef.h"
> +#include <vector>
> using namespace llvm;
>
> // MSVC emits references to this into the translation units which reference it.
> @@ -35,6 +36,36 @@
> return Length < RHS.Length ? -1 : 1;
> }
>
> +/// \brief Compute the edit distance between the two given strings.
> +unsigned StringRef::edit_distance(llvm::StringRef Other,
> + bool AllowReplacements) {
> + 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