-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_comparator.hpp
182 lines (161 loc) · 6.16 KB
/
sequence_comparator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* ****************************************************************************
* File: Sequence comparator implementation by dynamic programming.
* Author: Devid Dokash.
* Date: 15/05/2022.
* ***************************************************************************/
#pragma once
#include <iostream>
#include <map>
#include <regex>
#include <string>
#include <vector>
enum Operation { INS, DEL, SUB, NON };
std::map<Operation,std::string> Names {
{INS, "+"}, {DEL, "-"}, {SUB, "="}, {NON, ""}
};
bool verbose = false;
class sequence_comparator {
private:
int line_offset;
bool compress = false;
struct action {
Operation op;
std::string seq;
int begin, end;
bool wiped;
action(): op(NON), begin(0), end(0) {}
action(Operation op, char c, int n) : op(op), begin(n), end(n) {
seq = (c != '\0' ? std::string(1, c) : "");
wiped = false;
}
std::string to_string() {
wiped = true;
std::string s = Names[op] + std::to_string(begin);
if (op == DEL) { if(begin != end) s += ":" + std::to_string(end); }
else s += ":" + parse(seq);
return s;
}
} a;
std::string pa_str = "";
std::string ch_str = "";
std::string A, B;
std::vector<int> c;
std::vector<std::vector<Operation>> t;
static std::string parse(std::string seq)
{
seq = std::regex_replace(seq, std::regex("\\+"), "<ADD>");
seq = std::regex_replace(seq, std::regex("\\-"), "<DEL>");
seq = std::regex_replace(seq, std::regex("\\="), "<SUB>");
return seq;
}
bool pre_analysis() {
if (!A.compare(B)) {
ch_str = "";
return true;
}
else if (!A.empty() && B.empty()) {
ch_str = "-1:" + std::to_string(A.length());
return true;
}
else if (!B.empty() && A.empty()) {
ch_str = "+1:" + parse(B);
return true;
}
return false;
}
void compare() {
int c_prev, tmp, aux;
for (int i = 1; i <= A.length(); i++) {
c_prev = c[0];
c[0] = i;
for (int j = 1; j < c.size(); j++) {
// -- X -------------------------------------------------------
tmp = c[j] + 1;
t[i-1][j-1] = DEL;
// ------------------------------------------------------------
// -- Y -------------------------------------------------------
aux = c[j-1] + 1;
if (tmp > aux) { tmp = aux; t[i-1][j-1] = INS; }
// ------------------------------------------------------------
// -- Z -------------------------------------------------------
aux = c_prev + (A[i-1] != B[j-1] ? 1 : 0);
if (tmp > aux) { tmp = aux; t[i-1][j-1] = (A[i-1] != B[j-1] ? SUB : NON); }
// ------------------------------------------------------------
// -- C[i][j] = min(x, y, z) ----------------------------------
c_prev = c[j];
c[j] = tmp;
// ------------------------------------------------------------
}
}
}
/*
void resolve(int i, int j) {
if(!i && !j) return;
else if(!i) { Resolve(0, j-1); std::cout << "+" << B[j-1] << i << " "; }
else if(!j) { Resolve(i-1, 0); std::cout << "-" << i << " "; }
else {
switch(t[i-1][j-1]) {
case DEL: Resolve(i-1, j); std::cout << "-" << i << " "; break;
case INS: Resolve(i, j-1); std::cout << "+" << B[j-1] << i << " "; break;
case SUB: Resolve(i-1, j-1); std::cout << "=" << B[j-1] << i << " "; break;
case NON: Resolve(i-1, j-1);
}
}
}
*/
void resolve(int i, int j) {
if(!i && !j) return;
else if(!i) { resolve(0, j-1); solution(INS, B[j-1], i+1); }
else if(!j) { resolve(i-1, 0); solution(DEL, '\0', i); }
else if(t[i-1][j-1] == DEL) { resolve(i-1, j); solution(DEL, '\0', i); }
else if(t[i-1][j-1] == INS) { resolve(i, j-1); solution(INS, B[j-1], i+1); }
else if(t[i-1][j-1] == SUB) { resolve(i-1, j-1); solution(SUB, B[j-1], i); }
else resolve(i-1, j-1);
}
void solution(Operation op, char c, int n) {
if (compress) {
if (a.op == NON) a = action(op, c, n);
else if (a.op != op) {
ch_str += a.to_string();
a = action(op, c, n);
}
else if (op == DEL) {
if (n - a.end != 1) {
ch_str += a.to_string();
a = action(DEL, c, n);
} else a.end++;
} else if (op == INS) {
if (n - a.begin) {
ch_str += a.to_string();
a = action(INS, c, n);
} else a.seq += std::string(1,c);
} else if (op == SUB) {
if (n - a.end != 1) {
ch_str += a.to_string();
a = action(SUB, c, n);
} else { a.end++; a.seq += std::string(1,c); }
}
} else ch_str += Names[op] + (c != '\0' ? std::string(1,c) : "")
+ std::to_string(n);
}
public:
sequence_comparator(std::string A, std::string B, int line_offset, bool compress)
: A(A), B(B), line_offset(line_offset), compress(compress)
{
// -- Initiating costs table ------------------------------------------
for (int j = 0; j <= B.length(); j++) { c.push_back(j); }
// --------------------------------------------------------------------
// -- Initiating actions table ----------------------------------------
t = std::vector<std::vector<Operation>>(A.length(),
std::vector<Operation>(B.length()));
// --------------------------------------------------------------------
}
std::string to_string() {
if (!pre_analysis()) {
compare();
resolve(A.length(), B.length());
ch_str += (!a.wiped ? a.to_string() : "");
}
return ch_str;
}
};