18 if(source_length == 0)
22 if(target_length == 0)
27 using Tmatrix = std::vector<std::vector<int>>;
29 Tmatrix matrix(source_length + 1);
31 for(
int source_index = 0; source_index <= source_length; source_index++)
33 matrix[source_index].resize(target_length + 1);
36 for(
int source_index = 0; source_index <= source_length; source_index++)
38 matrix[source_index][0] = source_index;
41 for(
int target_index = 0; target_index <= target_length; target_index++)
43 matrix[0][target_index] = target_index;
46 for(
int source_index = 1; source_index <= source_length; source_index++)
48 const char source_char = source[source_index - 1];
50 for(
int target_index = 1; target_index <= target_length; target_index++)
52 const char target_char = target[target_index - 1];
54 const int cost = (source_char == target_char) ? 0 : 1;
56 const int above = matrix[source_index - 1][target_index];
57 const int left = matrix[source_index][target_index - 1];
58 const int diag = matrix[source_index - 1][target_index - 1];
61 if(source_index > 2 && target_index > 2)
63 int trans = matrix[source_index - 2][target_index - 2] + 1;
64 if(source[source_index - 2] != target_char)
68 if(source_char != target[target_index - 2])
78 matrix[source_index][target_index] = cell;
82 return matrix[source_length][target_length];
101 std::unique_ptr<int[]> v0 {
new int[row_length + 1]};
102 std::unique_ptr<int[]> v1 {
new int[row_length + 1]};
104 for(
int row_index = 0; row_index <= row_length; row_index++)
106 v0[row_index] = row_index;
110 for(
int col_index = 1; col_index <= col_length; col_index++)
114 for(
int row_index = 1; row_index <= row_length; row_index++)
117 = the_row[row_index - 1] == the_col[col_index - 1] ? 0 : 1;
119 const int m_min = v0[row_index] + 1;
120 const int b = v1[row_index - 1] + 1;
121 const int c = v0[row_index - 1] + cost;
129 return v0[row_length];
int c_sizet_to_int(size_t t)
size2f min(const size2f lhs, const size2f rhs)
int calc_edit_distance_fast(const std::string &the_row, const std::string &the_col)
int calc_edit_distance(const std::string &source, const std::string &target)