LLVM API Documentation

edit_distance.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/edit_distance.h - Array edit distance function --- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a Levenshtein distance function that works for any two
00011 // sequences, with each element of each sequence being analogous to a character
00012 // in a string.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_ADT_EDIT_DISTANCE_H
00017 #define LLVM_ADT_EDIT_DISTANCE_H
00018 
00019 #include "llvm/ADT/ArrayRef.h"
00020 #include <algorithm>
00021 #include <memory>
00022 
00023 namespace llvm {
00024 
00025 /// \brief Determine the edit distance between two sequences.
00026 ///
00027 /// \param FromArray the first sequence to compare.
00028 ///
00029 /// \param ToArray the second sequence to compare.
00030 ///
00031 /// \param AllowReplacements whether to allow element replacements (change one
00032 /// element into another) as a single operation, rather than as two operations
00033 /// (an insertion and a removal).
00034 ///
00035 /// \param MaxEditDistance If non-zero, the maximum edit distance that this
00036 /// routine is allowed to compute. If the edit distance will exceed that
00037 /// maximum, returns \c MaxEditDistance+1.
00038 ///
00039 /// \returns the minimum number of element insertions, removals, or (if
00040 /// \p AllowReplacements is \c true) replacements needed to transform one of
00041 /// the given sequences into the other. If zero, the sequences are identical.
00042 template<typename T>
00043 unsigned ComputeEditDistance(ArrayRef<T> FromArray, ArrayRef<T> ToArray,
00044                              bool AllowReplacements = true,
00045                              unsigned MaxEditDistance = 0) {
00046   // The algorithm implemented below is the "classic"
00047   // dynamic-programming algorithm for computing the Levenshtein
00048   // distance, which is described here:
00049   //
00050   //   http://en.wikipedia.org/wiki/Levenshtein_distance
00051   //
00052   // Although the algorithm is typically described using an m x n
00053   // array, only two rows are used at a time, so this implemenation
00054   // just keeps two separate vectors for those two rows.
00055   typename ArrayRef<T>::size_type m = FromArray.size();
00056   typename ArrayRef<T>::size_type n = ToArray.size();
00057 
00058   const unsigned SmallBufferSize = 64;
00059   unsigned SmallBuffer[SmallBufferSize];
00060   std::unique_ptr<unsigned[]> Allocated;
00061   unsigned *Previous = SmallBuffer;
00062   if (2*(n + 1) > SmallBufferSize) {
00063     Previous = new unsigned [2*(n+1)];
00064     Allocated.reset(Previous);
00065   }
00066   unsigned *Current = Previous + (n + 1);
00067 
00068   for (unsigned i = 0; i <= n; ++i)
00069     Previous[i] = i;
00070 
00071   for (typename ArrayRef<T>::size_type y = 1; y <= m; ++y) {
00072     Current[0] = y;
00073     unsigned BestThisRow = Current[0];
00074 
00075     for (typename ArrayRef<T>::size_type x = 1; x <= n; ++x) {
00076       if (AllowReplacements) {
00077         Current[x] = std::min(
00078             Previous[x-1] + (FromArray[y-1] == ToArray[x-1] ? 0u : 1u),
00079             std::min(Current[x-1], Previous[x])+1);
00080       }
00081       else {
00082         if (FromArray[y-1] == ToArray[x-1]) Current[x] = Previous[x-1];
00083         else Current[x] = std::min(Current[x-1], Previous[x]) + 1;
00084       }
00085       BestThisRow = std::min(BestThisRow, Current[x]);
00086     }
00087 
00088     if (MaxEditDistance && BestThisRow > MaxEditDistance)
00089       return MaxEditDistance + 1;
00090 
00091     unsigned *tmp = Current;
00092     Current = Previous;
00093     Previous = tmp;
00094   }
00095 
00096   unsigned Result = Previous[n];
00097   return Result;
00098 }
00099 
00100 } // End llvm namespace
00101 
00102 #endif