edit_distance.h 4.7 KB

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