| 45 | // |
| 46 | template <typename T, typename Cmp> |
| 47 | inline int64 LevenshteinDistance(const gtl::ArraySlice<T>& s, |
| 48 | const gtl::ArraySlice<T>& t, const Cmp& cmp) { |
| 49 | const int64 s_size = s.size(); |
| 50 | const int64 t_size = t.size(); |
| 51 | |
| 52 | if (t_size > s_size) return LevenshteinDistance(t, s, cmp); |
| 53 | |
| 54 | const T* s_data = s.data(); |
| 55 | const T* t_data = t.data(); |
| 56 | |
| 57 | if (t_size == 0) return s_size; |
| 58 | if (s == t) return 0; |
| 59 | |
| 60 | // Create work vector |
| 61 | gtl::InlinedVector<int64, 32> scratch_holder(t_size); |
| 62 | |
| 63 | int64* scratch = scratch_holder.data(); |
| 64 | |
| 65 | // Special case for i = 0: Distance between empty string and string |
| 66 | // of length j is just j. |
| 67 | for (size_t j = 1; j < t_size; ++j) scratch[j - 1] = j; |
| 68 | |
| 69 | for (size_t i = 1; i <= s_size; ++i) { |
| 70 | // Invariant: scratch[j - 1] equals cost(i - 1, j). |
| 71 | int substitution_base_cost = i - 1; |
| 72 | int insertion_cost = i + 1; |
| 73 | for (size_t j = 1; j <= t_size; ++j) { |
| 74 | // Invariants: |
| 75 | // scratch[k - 1] = cost(i, k) for 0 < k < j. |
| 76 | // scratch[k - 1] = cost(i - 1, k) for j <= k <= t_size. |
| 77 | // substitution_base_cost = cost(i - 1, j - 1) |
| 78 | // insertion_cost = cost(i, j - 1) |
| 79 | const int replacement_cost = cmp(s_data[i - 1], t_data[j - 1]) ? 0 : 1; |
| 80 | const int substitution_cost = substitution_base_cost + replacement_cost; |
| 81 | const int deletion_cost = scratch[j - 1] + 1; |
| 82 | |
| 83 | // Select the cheapest edit. |
| 84 | const int cheapest = // = cost(i, j) |
| 85 | std::min(deletion_cost, std::min(insertion_cost, substitution_cost)); |
| 86 | |
| 87 | // Restore invariant for the next iteration of the loop. |
| 88 | substitution_base_cost = scratch[j - 1]; // = cost(i - 1, j) |
| 89 | scratch[j - 1] = cheapest; // = cost(i, j) |
| 90 | insertion_cost = cheapest + 1; // = cost(i, j) + 1 |
| 91 | } |
| 92 | } |
| 93 | return scratch[t_size - 1]; |
| 94 | } |
| 95 | |
| 96 | template <typename Container1, typename Container2, typename Cmp> |
| 97 | inline int64 LevenshteinDistance(const Container1& s, const Container2& t, |