1 #include <torch/csrc/jit/script/edit_distance.h> 13 size_t ComputeEditDistance(
16 size_t maxEditDistance) {
18 size_t m = strlen(word1);
19 size_t n = strlen(word2);
21 const unsigned small_buffer_size = 64;
22 unsigned small_buffer[small_buffer_size];
23 std::unique_ptr<unsigned[]> allocated;
24 unsigned* row = small_buffer;
25 if (n + 1 > small_buffer_size) {
26 row =
new unsigned[n + 1];
30 for (
unsigned i = 1; i <= n; ++i)
33 for (
size_t y = 1; y <= m; ++y) {
35 unsigned best_this_row = row[0];
37 unsigned previous = y - 1;
38 for (
size_t x = 1; x <= n; ++x) {
41 previous + (word1[y - 1] == word2[x - 1] ? 0u : 1u),
42 std::min(row[x - 1], row[x]) + 1);
44 best_this_row = std::min(best_this_row, row[x]);
47 if (maxEditDistance && best_this_row > maxEditDistance)
48 return maxEditDistance + 1;
51 unsigned result = row[n];