Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.5 KiB

  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 "llvm/ADT/OwningPtr.h"
  19. #include <algorithm>
  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 two rows are used at a time, so this implemenation
  50. // just keeps two separate vectors for those two rows.
  51. typename ArrayRef<T>::size_type m = FromArray.size();
  52. typename ArrayRef<T>::size_type n = ToArray.size();
  53. const unsigned SmallBufferSize = 64;
  54. unsigned SmallBuffer[SmallBufferSize];
  55. llvm::OwningArrayPtr<unsigned> Allocated;
  56. unsigned *Previous = SmallBuffer;
  57. if (2*(n + 1) > SmallBufferSize) {
  58. Previous = new unsigned [2*(n+1)];
  59. Allocated.reset(Previous);
  60. }
  61. unsigned *Current = Previous + (n + 1);
  62. for (unsigned i = 0; i <= n; ++i)
  63. Previous[i] = i;
  64. for (typename ArrayRef<T>::size_type y = 1; y <= m; ++y) {
  65. Current[0] = y;
  66. unsigned BestThisRow = Current[0];
  67. for (typename ArrayRef<T>::size_type x = 1; x <= n; ++x) {
  68. if (AllowReplacements) {
  69. Current[x] = std::min(
  70. Previous[x-1] + (FromArray[y-1] == ToArray[x-1] ? 0u : 1u),
  71. std::min(Current[x-1], Previous[x])+1);
  72. }
  73. else {
  74. if (FromArray[y-1] == ToArray[x-1]) Current[x] = Previous[x-1];
  75. else Current[x] = std::min(Current[x-1], Previous[x]) + 1;
  76. }
  77. BestThisRow = std::min(BestThisRow, Current[x]);
  78. }
  79. if (MaxEditDistance && BestThisRow > MaxEditDistance)
  80. return MaxEditDistance + 1;
  81. unsigned *tmp = Current;
  82. Current = Previous;
  83. Previous = tmp;
  84. }
  85. unsigned Result = Previous[n];
  86. return Result;
  87. }
  88. } // End llvm namespace
  89. #endif