-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
272 lines (238 loc) · 8.13 KB
/
main.cpp
File metadata and controls
272 lines (238 loc) · 8.13 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <iostream>
using std::cout;
#include "max_block_decomposition.cpp"
#include "rand_utils.cpp"
#include "G4G_LCS.cpp"
#include "LCP.cpp"
void test_replace() {
int RUNS_PER_STRING = 1000;
for (int i = 0; i < 100; i++) {
int length = randint(4, 10);
string s = "";
string t = "";
for (int j = 0; j < length; j++) {
s += (char) randint('a', 'c');
t += (char) randint('a', 'c');
}
MaxBlockDecomposition decomp(s, t);
for (int j = 0; j < RUNS_PER_STRING; j++) {
if (i == 14 && j == 239) {
cout << "MERP";
}
long pos = randint(0, length-1);
char c = randint('a', 'h');
cout << t << '\n';
decomp.print();
decomp.replace(pos, c);
bool result = decomp.validate();
if (!result) {
cout << "FAIL i: " << i << ", j: " << j << '\n';
decomp.print();
return;
}
}
}
cout << "PASS\n";
}
void test_initial_blocks_different_lengths() {
int RUNS_PER_STRING = 1000;
for (int i = 0; i < 100; i++) {
if (i == 6) {
cout << "MERP";
}
int length_s = randint(4, 10);
int length_t = randint(4, 10);
string s = "";
string t = "";
for (int j = 0; j < length_s; j++) {
s += (char) randint('a', 'c');
}
for (int j = 0; j < length_t; j++) {
t += (char) randint('a', 'c');
}
MaxBlockDecomposition decomp(s, t);
if (!decomp.validate()) {
cout << t << '\n';
cout << "FAIL " << i << '\n';
decomp.print();
decomp.print_candidates();
return;
}
}
cout << "PASS\n";
}
//max_op 1 = replace only, 2 = append and replace, 3 = pop and 2
void test_LCS_different_lengths(int strings, int length_average, int runs_per_string, int validate_frequency, int max_op) {
for (int i = 0; i < strings; i++) {
int length_s = randint(length_average/2, length_average+length_average/2);
int length_t = randint(length_average/2, length_average+length_average/2);
string s = "";
string t = "";
for (int j = 0; j < length_s; j++) {
s += (char) randint('a', 'd');
}
for (int j = 0; j < length_t; j++) {
t += (char) randint('a', 'd');
}
MaxBlockDecomposition decomp(s, t);
cout << decomp.blocks.size() << '\n';
for (int j = 0; j < runs_per_string; j++) {
long pos = randint(0, s.size()-1);
int op = randint(1, max_op);
char c = randint('a', 'd');
if (op == 1) {
if (s == "") {
s += c;
decomp.append(c);
} else {
s[pos] = c;
decomp.replace(pos, c);
}
} else if (op == 2) {
s += c;
decomp.append(c);
} else if (op == 3) {
if (s == "") {
decomp.append(c);
s += c;
} else {
decomp.unappend();
s.erase(--s.end());
}
}
if (decomp.s != s) {
cout << "BADDDD\n";
}
//cout << t << '\n';
//decomp.print();
//decomp.print_candidates();
if (validate_frequency != 0 && (j+1) % validate_frequency == 0) {
bool result = decomp.validate();
if (!result) {
cout << "VALIDATE FAIL i: " << i << ", j: " << j << '\n';
decomp.print();
return;
}
string LCS_reference = G4G::LCS(s, t, 'd');
Slice LCS = decomp.get_lcs();
if (LCS.size() != LCS_reference.size()) {
cout << "FAIL: reference is " << LCS_reference.size() << " calculated is " << LCS.size() << '\n';
decomp.print();
decomp.print_candidates();
return;
}
}
}
cout << "PASS " << i << '\n';
}
cout << "PASS ALL\n";
}
//return n pairs of an index and a character
//the index will be in the range [0, str_length)
vector<pair<int, char>> generate_replacement_sequence(int str_length, int n, char min_char, char max_char) {
vector<pair<int, char>> replacement_sequence;
for (int i = 0; i < n; i++) {
replacement_sequence.push_back({randint(0, str_length-1), randint(min_char, max_char)});
}
return replacement_sequence;
}
enum Algorithm {
GEEKS_FOR_GEEKS,
MAX_BLOCK_DECOMPOSITION
};
//returns number of miliseconds per run the algorithm took
double run_trial(Algorithm algorithm, int string_len, int num_runs) {
const char minchar = 1;
const char maxchar = 4;
string s = randstring(string_len, minchar, maxchar);
string t = randstring(string_len, minchar, maxchar);
auto replacement_sequence = generate_replacement_sequence(string_len, num_runs, minchar, maxchar);
MaxBlockDecomposition mbd;
if (algorithm == MAX_BLOCK_DECOMPOSITION) {
auto start = std::chrono::steady_clock::now();
mbd = MaxBlockDecomposition(s, t);
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
double init_time = std::chrono::duration<double, std::milli>(diff).count();
cout << "initialized in " << init_time << "ms" << std::endl;
}
auto start = std::chrono::steady_clock::now();
for (auto [i, c] : replacement_sequence) {
if (algorithm == GEEKS_FOR_GEEKS) {
s[i] = c;
string lcs = G4G::LCS(s, t, 4);
} else if (algorithm == MAX_BLOCK_DECOMPOSITION) {
mbd.replace(i, c);
Slice lcs = mbd.get_lcs();
}
}
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
return std::chrono::duration<double, std::milli>(diff).count() / num_runs;
}
void performance_comparison() {
vector<pair<int, int>> trials = {
{100, 100000},
{500, 20000},
{2000, 5000},
{10000, 1000},
{50000, 200},
{200000, 100}
};
for (auto [len, runs] : trials) {
cout << "running len: " << len << ", runs: " << runs << std::endl;
double g4g_milli = run_trial(GEEKS_FOR_GEEKS, len, runs/5);
cout << "geeksforgeeks " << g4g_milli << "ms" << std::endl;
double normal_milli = run_trial(MAX_BLOCK_DECOMPOSITION, len, runs);
cout << "block decomposition " << normal_milli << "ms" << std::endl;
cout << std::endl;
}
}
int main() {
//auto mbd = MaxBlockDecomposition("aabababc", "aacbbcabc");
//mbd.print();
//test_fuse_substrings_auto();
//test_initial_blocks_different_lengths();
//test_LCS_different_lengths(3, 200000, 2000, 500, 3);
//test_LCS_different_lengths(3, 50, 2000, 1, 3);
//performance_comparison();
/*sdsl::csa_bitcompressed csa, csa_r;
string s = "abacadabra";
construct_im(csa, s, 1);
std::reverse(s.begin(), s.end());
construct_im(csa_r, s, 1);
std::reverse(s.begin(), s.end());
cout << "sa: ";
for (int i = 0; i <= s.size(); i++) {
cout << csa[i] << ' ';
}
cout << '\n';
cout << "isa: ";
for (int i = 0; i <= s.size(); i++) {
cout << csa.isa[i] << ' ';
}
cout << '\n';
cout << "isa_r: ";
for (int i = 0; i <= s.size(); i++) {
cout << csa_r.isa[i] << ' ';
}
cout << '\n';
MaxBlockDecomposition mbd("", "abacadabra");*/
//performance_comparison();
/*string s = "";
for (int i = 0; i < 1000000; i++) { //1MB
s += randint('a', 'z');
}
MaxBlockDecomposition mbd("", s, true);*/
//test_fuse_substrings();
//test_range_search_2d();
//test_leaf_index();
//performance_comparison();
/*CST T;
string s = "abacadabra";
construct_im(T, s, 1);
print_suffixes(T);
Node node = T.child(T.root(), 'a');
print_suffixes(T, node);*/
test_fuse_prefix_LCP();
}