edit_distance.h 3.6 KB

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