55std::vector<std::vector<double >> compute_dp_table (
66 const std::string& a,
77 const std::string& b,
8- const std::map<EditopName , double >& cost_map
8+ const std::map<CppEditopName , double >& cost_map
99) {
1010 int len_a = a.length ();
1111 int len_b = b.length ();
@@ -30,7 +30,7 @@ std::vector<std::vector<double>> compute_dp_table(
3030 if (i > 1 && j > 1 &&
3131 a[i-1 ] == b[j-2 ] && a[i-2 ] == b[j-1 ]) {
3232 dp[i][j] = std::min (dp[i][j],
33- dp[i-2 ][j-2 ] + cost_map.at (TRANSPOSE ));
33+ dp[i-2 ][j-2 ] + cost_map.at (SWAP ));
3434 }
3535 }
3636 }
@@ -42,42 +42,42 @@ std::vector<std::vector<double>> compute_dp_table(
4242double cpp_compute_distance (
4343 const std::string& a,
4444 const std::string& b,
45- const std::map<EditopName , double >& cost_map
45+ const std::map<CppEditopName , double >& cost_map
4646) {
4747 auto dp = compute_dp_table (a, b, cost_map);
4848 return dp[a.length ()][b.length ()];
4949}
5050
51- std::vector<std::vector<Editop >> backtrack_all_paths (
51+ std::vector<std::vector<CppEditop >> backtrack_all_paths (
5252 const std::string& a,
5353 const std::string& b,
54- const std::map<EditopName , double >& cost_map,
54+ const std::map<CppEditopName , double >& cost_map,
5555 const std::vector<std::vector<double >>& dp,
5656 int i,
5757 int j,
58- std::vector<Editop >& current_path
58+ std::vector<CppEditop >& current_path
5959) {
6060 if (i == 0 && j == 0 ) {
61- std::vector<Editop > reversed_path = current_path;
61+ std::vector<CppEditop > reversed_path = current_path;
6262 std::reverse (reversed_path.begin (), reversed_path.end ());
6363 return {reversed_path};
6464 }
6565
66- std::vector<std::vector<Editop >> all_paths;
66+ std::vector<std::vector<CppEditop >> all_paths;
6767 double current_cost = dp[i][j];
6868 const double tol = 1e-6 ;
6969
7070
7171 if (i > 0 && std::abs ((dp[i-1 ][j] + cost_map.at (DELETE)) - current_cost) < tol) {
72- Editop op (DELETE, i-1 , i-1 , cost_map.at (DELETE), std::string (1 , a[i-1 ]));
72+ CppEditop op (DELETE, i-1 , i-1 , cost_map.at (DELETE), std::string (1 , a[i-1 ]));
7373 current_path.push_back (op);
7474 auto paths = backtrack_all_paths (a, b, cost_map, dp, i-1 , j, current_path);
7575 all_paths.insert (all_paths.end (), paths.begin (), paths.end ());
7676 current_path.pop_back ();
7777 }
7878
7979 if (j > 0 && std::abs ((dp[i][j-1 ] + cost_map.at (INSERT)) - current_cost) < tol) {
80- Editop op (INSERT, i, i, cost_map.at (INSERT), std::string (1 , b[j-1 ]));
80+ CppEditop op (INSERT, i, i, cost_map.at (INSERT), std::string (1 , b[j-1 ]));
8181 current_path.push_back (op);
8282 auto paths = backtrack_all_paths (a, b, cost_map, dp, i, j-1 , current_path);
8383 all_paths.insert (all_paths.end (), paths.begin (), paths.end ());
@@ -89,7 +89,7 @@ std::vector<std::vector<Editop>> backtrack_all_paths(
8989 double sub_cost = (a[i-1 ] == b[j-1 ]) ? 0.0 : cost_map.at (REPLACE);
9090 if (std::abs ((dp[i-1 ][j-1 ] + sub_cost) - current_cost) < tol) {
9191 std::string out_char = (sub_cost == 0.0 ) ? std::string (1 , a[i-1 ]) : std::string (1 , b[j-1 ]);
92- Editop op (REPLACE, i-1 , j-1 , sub_cost, out_char);
92+ CppEditop op (REPLACE, i-1 , j-1 , sub_cost, out_char);
9393 current_path.push_back (op);
9494 auto paths = backtrack_all_paths (a, b, cost_map, dp, i-1 , j-1 , current_path);
9595 all_paths.insert (all_paths.end (), paths.begin (), paths.end ());
@@ -100,9 +100,9 @@ std::vector<std::vector<Editop>> backtrack_all_paths(
100100
101101 if (i > 1 && j > 1 &&
102102 a[i-1 ] == b[j-2 ] && a[i-2 ] == b[j-1 ] &&
103- std::abs ((dp[i-2 ][j-2 ] + cost_map.at (TRANSPOSE )) - current_cost) < tol) {
104- std::string transpose_str = std::string (1 , b[j-2 ]) + std::string (1 , b[j-1 ]);
105- Editop op (TRANSPOSE , i-2 , j-2 , cost_map.at (TRANSPOSE ), transpose_str );
103+ std::abs ((dp[i-2 ][j-2 ] + cost_map.at (SWAP )) - current_cost) < tol) {
104+ std::string swap_str = std::string (1 , b[j-2 ]) + std::string (1 , b[j-1 ]);
105+ CppEditop op (SWAP , i-2 , j-2 , cost_map.at (SWAP ), swap_str );
106106 current_path.push_back (op);
107107 auto paths = backtrack_all_paths (a, b, cost_map, dp, i-2 , j-2 , current_path);
108108 all_paths.insert (all_paths.end (), paths.begin (), paths.end ());
@@ -113,21 +113,21 @@ std::vector<std::vector<Editop>> backtrack_all_paths(
113113}
114114
115115
116- std::vector<std::vector<Editop >> cpp_compute_all_paths (
116+ std::vector<std::vector<CppEditop >> cpp_compute_all_paths (
117117 const std::string& a,
118118 const std::string& b,
119- const std::map<EditopName , double >& cost_map
119+ const std::map<CppEditopName , double >& cost_map
120120) {
121121 auto dp = compute_dp_table (a, b, cost_map);
122- std::vector<Editop > current_path;
122+ std::vector<CppEditop > current_path;
123123 return backtrack_all_paths (a, b, cost_map, dp, a.length (), b.length (), current_path);
124124}
125125
126126
127127void cpp_print_all_paths (
128128 const std::string& a,
129129 const std::string& b,
130- const std::map<EditopName , double >& cost_map
130+ const std::map<CppEditopName , double >& cost_map
131131) {
132132 auto paths = cpp_compute_all_paths (a, b, cost_map);
133133 double distance = cpp_compute_distance (a, b, cost_map);
@@ -145,18 +145,18 @@ void cpp_print_all_paths(
145145 }
146146}
147147
148- std::string editop_name_to_string (EditopName name) {
148+ std::string editop_name_to_string (CppEditopName name) {
149149 switch (name) {
150150 case INSERT: return " INSERT" ;
151151 case DELETE: return " DELETE" ;
152152 case REPLACE: return " REPLACE" ;
153- case TRANSPOSE : return " TRANSPOSE " ;
153+ case SWAP : return " SWAP " ;
154154 default : return " UNKNOWN" ;
155155 }
156156}
157157
158- std::ostream& operator <<(std::ostream& os, const Editop & op) {
159- os << " Editop (name=" << editop_name_to_string (op.name )
158+ std::ostream& operator <<(std::ostream& os, const CppEditop & op) {
159+ os << " CppEditop (name=" << editop_name_to_string (op.name )
160160 << " , src_idx=" << op.src_idx
161161 << " , dst_idx=" << op.dst_idx
162162 << " , cost=" << op.cost
0 commit comments