| 1 | #cxx . . $ `<file>/misc/levenshtein.cpp`/include "./levenshtein.h"cxx . . $ `<file>/misc/levenshtein.h`/ |
| 2 | |
| 3 | #include "./math.h"cxx . . $ `<file>/misc/math.h`/ |
| 4 | #include "./multiarray.h"cxx . . $ `<file>/misc/multiarray.h`/ |
| 5 | |
| 6 | #include <limits> |
| 7 | |
| 8 | using namespace std; |
| 9 | |
| 10 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 11 | |
| 12 | /// \cond |
| 13 | |
| 14 | /// \brief The DistanceArray is a 2D array which is allocated either on the stack or the heap. |
| 15 | using DistanceArraycxx . . $ CppUtilities/DistanceArray# = MultiArraycxx . . $ CppUtilities/MultiArray#<size_t, NoneOwningMultiArraycxx . . $ CppUtilities/NoneOwningMultiArray#, size_t, size_t>; |
| 16 | |
| 17 | /// \brief Initializes the distance array. |
| 18 | /// |
| 19 | /// For size1=5 and size2=4 it would look like this: |
| 20 | /// ``` |
| 21 | /// 9 9 9 9 9 9 |
| 22 | /// 9 0 1 2 3 4 |
| 23 | /// 9 1 0 0 0 0 |
| 24 | /// 9 2 0 0 0 0 |
| 25 | /// 9 3 0 0 0 0 |
| 26 | /// 9 4 0 0 0 0 |
| 27 | /// 9 5 0 0 0 0 |
| 28 | /// ``` |
| 29 | void initDistanceArraycxx . . $ CppUtilities/initDistanceArray(a16de7843403191e).(DistanceArraycxx . . $ CppUtilities/DistanceArray# &distanceArray, const size_t size1, const size_t size2) |
| 30 | { |
| 31 | const auto maxDistance(size1 + size2); |
| 32 | // ignore warning about null pointer dereference for now (which is *likely* not correct) |
| 33 | #ifdef __GNUC__cxx . . $ `<built-in>:8:9`! |
| 34 | #pragma GCC diagnostic push |
| 35 | #pragma GCC diagnostic ignored "-Wnull-dereference" |
| 36 | #endif |
| 37 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(0, 0) = maxDistance; |
| 38 | #ifdef __GNUC__cxx . . $ `<built-in>:8:9`! |
| 39 | #pragma GCC diagnostic pop |
| 40 | #endif |
| 41 | for (size_t i = 0; i <= size1; ++i) { |
| 42 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(i + 1, 1) = i; |
| 43 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(i + 1, 0) = maxDistance; |
| 44 | } |
| 45 | for (size_t i = 0; i <= size2; ++i) { |
| 46 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(1, i + 1) = i; |
| 47 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(0, i + 1) = maxDistance; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// \brief Performs the actual Damerau–Levenshtein algorithm. |
| 52 | /// \sa computeDamerauLevenshteinDistance() for more details on the algorithm. |
| 53 | size_t performDamerauLevenshteinAlgorithmcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithm(c243ff434cc08bbb).( |
| 54 | DistanceArraycxx . . $ CppUtilities/DistanceArray# &distanceArray, const char *const str1, const size_t size1, const char *const str2, const size_t size2) |
| 55 | { |
| 56 | size_t dist1[stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<unsigned char>::maxcxx . . $ std/numeric_limits#max(ef2994725c9008f7).() + 1] = { 0 }; |
| 57 | for (size_t index1 = 1; index1 <= size1; ++index1) { |
| 58 | size_t dist2 = 0; |
| 59 | for (size_t index2 = 1; index2 <= size2; ++index2) { |
| 60 | const size_t substitution((str1[index1 - 1] == str2[index2 - 1]) ? 0 : 1); |
| 61 | const size_t transposition1(dist1[static_cast<unsigned char>(str2[index2 - 1])]); |
| 62 | const size_t transposition2(dist2); |
| 63 | if (!substitution) { |
| 64 | dist2 = index2; |
| 65 | } |
| 66 | // clang-format off |
| 67 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(index1 + 1, index2 + 1) = CppUtilitiescxx . . $ CppUtilities/::mincxx . . $ CppUtilities/min(da21754e2b185302).( |
| 68 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(index1, index2) + substitution, // substitution |
| 69 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(index1 + 1, index2) + 1, // insertion |
| 70 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(index1, index2 + 1) + 1, // deletion |
| 71 | distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(transposition1, transposition2) + (index1 - transposition1 - 1) + 1 + (index2 - transposition2 - 1) // transposition |
| 72 | ); |
| 73 | // clang-format on |
| 74 | } |
| 75 | dist1[static_cast<int>(str1[index1 - 1])] = index1; |
| 76 | } |
| 77 | return distanceArray.atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(size1 + 1, size2 + 1); |
| 78 | } |
| 79 | |
| 80 | /// \brief Allocates the distance array on the heap and performs the Damerau–Levenshtein algorithm. |
| 81 | template <typename DistanceArray> |
| 82 | size_t performDamerauLevenshteinAlgorithmAllocatingOnHeapcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithmAllocatingOnHeap(4d8917a35f9dce88).( |
| 83 | DistanceArray &distanceArray, const char *const str1, const size_t size1, const char *const str2, const size_t size2) |
| 84 | { |
| 85 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<size_t> buffer(distanceArray.totalSize()); |
| 86 | distanceArray.buffer() = buffer.datacxx . . $ std/vector#data(258ee1c9e0a18a5c).(); |
| 87 | initDistanceArraycxx . . $ CppUtilities/initDistanceArray(a16de7843403191e).(distanceArray, size1, size2); |
| 88 | return performDamerauLevenshteinAlgorithmcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithm(c243ff434cc08bbb).(distanceArray, str1, size1, str2, size2); |
| 89 | } |
| 90 | |
| 91 | /// \brief Allocates the distance array on the stack and performs the Damerau–Levenshtein algorithm. |
| 92 | /// \remarks The totalSize() of \a distanceArray mustn't exceed 128 byte. |
| 93 | template <typename DistanceArray> |
| 94 | size_t performDamerauLevenshteinAlgorithmAllocatingOnStackcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithmAllocatingOnStack(4d8917a35f9dce88).( |
| 95 | DistanceArray &distanceArray, const char *const str1, const size_t size1, const char *const str2, const size_t size2) |
| 96 | { |
| 97 | size_t buffer[128] = { 0 }; |
| 98 | distanceArray.buffer() = buffer; |
| 99 | initDistanceArraycxx . . $ CppUtilities/initDistanceArray(a16de7843403191e).(distanceArray, size1, size2); |
| 100 | return performDamerauLevenshteinAlgorithmcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithm(c243ff434cc08bbb).(distanceArray, str1, size1, str2, size2); |
| 101 | } |
| 102 | |
| 103 | /// \endcond |
| 104 | |
| 105 | /*! |
| 106 | * \brief Computes Damerau–Levenshtein distance with adjacent transpositions. |
| 107 | * |
| 108 | * \returns Returns the number of editing steps required to turn \a str1 into \a str2. |
| 109 | * The following operations are considered as editing steps: |
| 110 | * - substitution: replace one character with another character |
| 111 | * - insertion: insert one character at any position |
| 112 | * - deletion: delete one character at any position |
| 113 | * - transposition: swap any pair of adjacent characters |
| 114 | * |
| 115 | * \remarks |
| 116 | * - Computing "Optimal string alignment distance" is a *different* thing. |
| 117 | * - The algorithm operates on byte-level. So characters requiring more than one byte in |
| 118 | * the used character encoding (e.g. UTF-8 encoded German umlauts) are counted as multiple |
| 119 | * characters (e.g. substitution of those umlauts with non-umlauts requires 2 editing steps). |
| 120 | * - The memory consumption of this algorithm is considerably. The required memory increases |
| 121 | * with the product of \a size1 and \a size2. Pass only short words to this function! |
| 122 | */ |
| 123 | stdcxx . . $ std/::size_tcxx . . $ std/size_t# computeDamerauLevenshteinDistancecxx . . $ CppUtilities/computeDamerauLevenshteinDistance(de56e73a02f050dc).(const char *const str1, const size_t size1, const char *const str2, const size_t size2) |
| 124 | { |
| 125 | // allocate distance array |
| 126 | auto distanceArray(makeNoneOwningMultiArraycxx . . $ CppUtilities/makeNoneOwningMultiArray(1fd8e6172299e45f).<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(size1 + 2, size2 + 2)); |
| 127 | if (distanceArray.totalSizecxx . . $ CppUtilities/MultiArray#totalSize(fe4d2d262d4164a8).() <= 128) { |
| 128 | return performDamerauLevenshteinAlgorithmAllocatingOnStackcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithmAllocatingOnStack(4d8917a35f9dce88).(distanceArray, str1, size1, str2, size2); |
| 129 | } else { |
| 130 | return performDamerauLevenshteinAlgorithmAllocatingOnHeapcxx . . $ CppUtilities/performDamerauLevenshteinAlgorithmAllocatingOnHeap(4d8917a35f9dce88).(distanceArray, str1, size1, str2, size2); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace CppUtilities |