edit_distance.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===-- llvm/ADT/edit_distance.h - Array edit distance function --- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines a Levenshtein distance function that works for any two
  11. // sequences, with each element of each sequence being analogous to a character
  12. // in a string.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ADT_EDIT_DISTANCE_H
  16. #define LLVM_ADT_EDIT_DISTANCE_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include <algorithm>
  19. #include <memory>
  20. namespace llvm {
  21. /// \brief Determine the edit distance between two sequences.
  22. ///
  23. /// \param FromArray the first sequence to compare.
  24. ///
  25. /// \param ToArray the second sequence to compare.
  26. ///
  27. /// \param AllowReplacements whether to allow element replacements (change one
  28. /// element into another) as a single operation, rather than as two operations
  29. /// (an insertion and a removal).
  30. ///
  31. /// \param MaxEditDistance If non-zero, the maximum edit distance that this
  32. /// routine is allowed to compute. If the edit distance will exceed that
  33. /// maximum, returns \c MaxEditDistance+1.
  34. ///
  35. /// \returns the minimum number of element insertions, removals, or (if
  36. /// \p AllowReplacements is \c true) replacements needed to transform one of
  37. /// the given sequences into the other. If zero, the sequences are identical.
  38. template<typename T>
  39. unsigned ComputeEditDistance(ArrayRef<T> FromArray, ArrayRef<T> ToArray,
  40. bool AllowReplacements = true,
  41. unsigned MaxEditDistance = 0) {
  42. // The algorithm implemented below is the "classic"
  43. // dynamic-programming algorithm for computing the Levenshtein
  44. // distance, which is described here:
  45. //
  46. // http://en.wikipedia.org/wiki/Levenshtein_distance
  47. //
  48. // Although the algorithm is typically described using an m x n
  49. // array, only one row plus one element are used at a time, so this
  50. // implementation just keeps one vector for the row. To update one entry,
  51. // only the entries to the left, top, and top-left are needed. The left
  52. // entry is in Row[x-1], the top entry is what's in Row[x] from the last
  53. // iteration, and the top-left entry is stored in Previous.
  54. typename ArrayRef<T>::size_type m = FromArray.size();
  55. typename ArrayRef<T>::size_type n = ToArray.size();
  56. const unsigned SmallBufferSize = 64;
  57. unsigned SmallBuffer[SmallBufferSize];
  58. std::unique_ptr<unsigned[]> Allocated;
  59. unsigned *Row = SmallBuffer;
  60. if (n + 1 > SmallBufferSize) {
  61. Row = new unsigned[n + 1];
  62. Allocated.reset(Row);
  63. }
  64. for (unsigned i = 1; i <= n; ++i)
  65. Row[i] = i;
  66. for (typename ArrayRef<T>::size_type y = 1; y <= m; ++y) {
  67. Row[0] = y;
  68. unsigned BestThisRow = Row[0];
  69. unsigned Previous = y - 1;
  70. for (typename ArrayRef<T>::size_type x = 1; x <= n; ++x) {
  71. int OldRow = Row[x];
  72. if (AllowReplacements) {
  73. Row[x] = std::min(
  74. Previous + (FromArray[y-1] == ToArray[x-1] ? 0u : 1u),
  75. std::min(Row[x-1], Row[x])+1);
  76. }
  77. else {
  78. if (FromArray[y-1] == ToArray[x-1]) Row[x] = Previous;
  79. else Row[x] = std::min(Row[x-1], Row[x]) + 1;
  80. }
  81. Previous = OldRow;
  82. BestThisRow = std::min(BestThisRow, Row[x]);
  83. }
  84. if (MaxEditDistance && BestThisRow > MaxEditDistance)
  85. return MaxEditDistance + 1;
  86. }
  87. #pragma warning( push ) // HLSL Change - suppress this warning
  88. #pragma warning( disable : 28199 ) // 'Using possibly uninitialized memory '*Row': The variable has had its address taken but no assignment to it has been discovered.'
  89. // n is assigned early on and is never < 1 because it's an array size
  90. unsigned Result = Row[n];
  91. #pragma warning( pop )
  92. return Result;
  93. }
  94. } // End llvm namespace
  95. #endif