pytorch/torch/csrc/jit/frontend/edit_distance.cpp
Meghan Lele 6384c2d81b [JIT] clang-format JIT code (#35115)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/35115

This commit runs the newly added tools/clang_format.py on the JIT
codebase and includes all of the formatting changes thus produced.

Testing:
Ran the script, CI.

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D20568523

Pulled By: SplitInfinity

fbshipit-source-id: e09bdb982ccf090eecfb7c7b461b8d0681eef82b
2020-03-26 11:24:51 -07:00

55 lines
1.4 KiB
C++

#include <torch/csrc/jit/frontend/edit_distance.h>
#include <algorithm>
#include <cstring>
#include <memory>
namespace torch {
namespace jit {
// computes levenshtein edit distance between two words
// returns maxEditDistance + 1 if the edit distance exceeds MaxEditDistance
// reference: http://llvm.org/doxygen/edit__distance_8h_source.html
size_t ComputeEditDistance(
const char* word1,
const char* word2,
size_t maxEditDistance) {
size_t m = strlen(word1);
size_t n = strlen(word2);
const unsigned small_buffer_size = 64;
unsigned small_buffer[small_buffer_size];
std::unique_ptr<unsigned[]> allocated;
unsigned* row = small_buffer;
if (n + 1 > small_buffer_size) {
row = new unsigned[n + 1];
allocated.reset(row);
}
for (unsigned i = 1; i <= n; ++i)
row[i] = i;
for (size_t y = 1; y <= m; ++y) {
row[0] = y;
unsigned best_this_row = row[0];
unsigned previous = y - 1;
for (size_t x = 1; x <= n; ++x) {
int old_row = row[x];
row[x] = std::min(
previous + (word1[y - 1] == word2[x - 1] ? 0u : 1u),
std::min(row[x - 1], row[x]) + 1);
previous = old_row;
best_this_row = std::min(best_this_row, row[x]);
}
if (maxEditDistance && best_this_row > maxEditDistance)
return maxEditDistance + 1;
}
unsigned result = row[n];
return result;
}
} // namespace jit
} // namespace torch