-
Notifications
You must be signed in to change notification settings - Fork 21
/
wm_string.cpp
455 lines (375 loc) · 13.6 KB
/
wm_string.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <bits/stdc++.h>
#include <random>
#include "dynamic/dynamic.hpp"
using namespace std;
double speed_access(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str wm(num_of_alphabet, data);
int dummy = 0;
auto start = std::chrono::system_clock::now();
for (int i = 0; i < data.size(); ++i) {
if (wm.at(i) == -1) {
dummy++;
}
}
auto end = std::chrono::system_clock::now();
if (dummy) {
cout << "dummy" << endl;
}
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
double speed_rank(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str wm(num_of_alphabet, data);
std::uniform_int_distribution<uint64_t> data_distrib(0, num-1);
int dummy = 0;
auto start = std::chrono::system_clock::now();
for (uint64_t rank = 1; rank < data.size(); ++rank) {
uint64_t val = data[data_distrib(gen)];
if (wm.rank(rank, val) == -1) {
dummy++;
}
}
auto end = std::chrono::system_clock::now();
if (dummy) {
cout << "dummy" << endl;
}
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
double speed_select(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num), count(num_of_alphabet, 0);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
count[data[i]]++;
}
dyn::wm_str wm(num_of_alphabet, data);
std::uniform_int_distribution<uint64_t> data_distrib(0, num-1);
int dummy = 0;
auto start = std::chrono::system_clock::now();
for (int i = 0; i < num; ++i) {
uint64_t val = data[data_distrib(gen)];
std::uniform_int_distribution<uint64_t> rank_distrib(1, count.at(val));
uint64_t rank = rank_distrib(gen);
//std::cerr << "select(" << rank << ", " << val << ")" << std::endl;
if (wm.select(rank, val) == -1) {
dummy++;
}
}
auto end = std::chrono::system_clock::now();
if (dummy) {
cout << "dummy" << endl;
}
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
double speed_remove(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data.emplace_back(alpha_distrib(gen));
}
dyn::wm_str dwm(num_of_alphabet, data);
auto start = std::chrono::system_clock::now();
for (int i = 0; i < num; ++i) {
std::uniform_int_distribution<uint64_t> pos_distrib(0, dwm.size()-1);
uint64_t pos = pos_distrib(gen);
dwm.remove(pos);
}
auto end = std::chrono::system_clock::now();
assert(dwm.size() == 0);
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
double speed_insert(uint64_t num, uint64_t num_of_alphabet) {
dyn::wm_str dwm(num_of_alphabet);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
auto start = std::chrono::system_clock::now();
for (int i = 0; i < num; ++i) {
std::uniform_int_distribution<uint64_t> pos_distrib(0, dwm.size());
uint64_t pos = pos_distrib(gen);
uint64_t c = alpha_distrib(gen);
dwm.insert(pos, c);
}
auto end = std::chrono::system_clock::now();
if (dwm.at(0) == -1) {
cout << "dummy" << endl;
}
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
double speed_update(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str dwm(num_of_alphabet, data);
std::uniform_int_distribution<uint64_t> pos_distrib(0, dwm.size()-1);
auto start = std::chrono::system_clock::now();
for (int i = 0; i < num - 1; ++i) {
uint64_t pos = pos_distrib(gen);
uint64_t c = alpha_distrib(gen);
dwm.update(pos, c);
}
auto end = std::chrono::system_clock::now();
if (dwm.at(0) == -1) {
cout << "dummy" << endl;
}
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
bool test_access(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str wm(num_of_alphabet, data);
for (int i = 0; i < data.size(); ++i) {
if (data[i] != wm.at(i)) {
return false;
}
}
return true;
}
bool test_rank(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str wm(num_of_alphabet, data);
set<uint64_t> s(data.begin(), data.end());
for (auto val : s) {
for (uint64_t i = 0; i < data.size(); ++i) {
int expected = 0;
for (int j = 0; j < i; ++j) {
expected += (data[j] == val);
}
if (wm.rank(i, val) != expected) {
return false;
}
}
}
return true;
}
bool test_select(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num), count(num_of_alphabet, 0);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
count[data[i]]++;
}
dyn::wm_str wm(num_of_alphabet, data);
set<uint64_t> s(data.begin(), data.end());
for (auto val : s) {
//cerr << "selecting " << val << endl;
for (uint64_t rank = 1; rank < count.at(val); ++rank) {
uint64_t expected = 0;
int n = 0;
for (uint64_t i = 0; i < data.size(); ++i) {
n += (data.at(i) == val);
if (n == rank) {
expected = i + 1; // selectは見つけたindex+1
break;
}
}
//cerr << "val/rank " << val << " " << rank << endl;
if (wm.select(rank, val) != expected) {
//cerr << "got " << wm.select(val, rank) << " but expected " << expected << endl;
return false;
} else {
//cerr << "got " << wm.select(val, rank) << endl;
}
}
}
return true;
}
// 2つの動的ビットベクトルが同じかチェック
bool same(dyn::wm_str expected, dyn::wm_str actual) {
if (expected.size() != actual.size()) {
cout << "Error at n:" << " expected:" << expected.size() << " actual: " << actual.size() << endl;
return false;
}
if (expected.sigma != actual.sigma) {
cout << "Error at sigma:" << " expected:" << expected.sigma << " actual: " << actual.sigma << endl;
return false;
}
if (expected.bit_width != actual.bit_width) {
cout << "Error at num_of_bit:" << " expected:" << expected.bit_width << " actual: " << actual.bit_width << endl;
return false;
}
if (expected.begin_one != actual.begin_one) {
cout << "Error at begin_one" << endl;
assert(expected.begin_one.size() == actual.begin_one.size());
for (int i = 0; i < expected.begin_one.size(); ++i) {
if (expected.begin_one.at(i) != actual.begin_one.at(i)) {
cout << "begin_one[" << i << "] expected:" << expected.begin_one.at(i) << " actual: " << actual.begin_one.at(i) << endl;
return false;
}
}
}
for (int i = 0; i < expected.bit_arrays.size(); ++i) {
vector<uint64_t> expected_bits, actual_bits;
for (int j = 0; j < expected.bit_arrays.at(i).size(); ++j) {
expected_bits.emplace_back(expected.bit_arrays.at(i).at(j));
}
for (int j = 0; j < actual.bit_arrays.at(i).size(); ++j) {
actual_bits.emplace_back(actual.bit_arrays.at(i).at(j));
}
if (expected_bits != actual_bits) {
return false;
}
}
return true;
}
bool test_remove(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str actual(num_of_alphabet, data);
for (int i = 0; i < num; ++i) {
std::uniform_int_distribution<uint64_t> pos_distrib(0, actual.size()-1);
uint64_t pos = pos_distrib(gen);
data.erase(data.begin() + pos);
dyn::wm_str expected(num_of_alphabet, data);
actual.remove(pos);
if (not same(expected, actual)) {
return false;
}
}
return true;
}
bool test_insert(uint64_t num, uint64_t num_of_alphabet) {
dyn::wm_str actual(num_of_alphabet);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
vector<uint64_t> data;
for (int i = 0; i < num; ++i) {
std::uniform_int_distribution<uint64_t> pos_distrib(0, actual.size()-1);
uint64_t pos = pos_distrib(gen);
uint64_t c = alpha_distrib(gen);
data.insert(data.begin() + pos, c);
dyn::wm_str expected(num_of_alphabet, data);
actual.insert(pos, c);
if (not same(expected, actual)) {
return false;
}
}
return true;
}
bool test_update(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str actual(num_of_alphabet, data);
std::uniform_int_distribution<uint64_t> pos_distrib(0, actual.size()-1);
for (int i = 0; i < num - 1; ++i) {
uint64_t pos = pos_distrib(gen);
uint64_t c = alpha_distrib(gen);
data[pos] = c;
dyn::wm_str expected(num_of_alphabet, data);
actual.update(pos, c);
if (not same(expected, actual)) {
return false;
}
}
return true;
}
bool test_serialize(uint64_t num, uint64_t num_of_alphabet) {
vector<uint64_t> data(num);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(0, num_of_alphabet-1);
for (int i = 0; i < num; ++i) {
data[i] = alpha_distrib(gen);
}
dyn::wm_str expected(num_of_alphabet, data);
string f = "wm_string.test.temp";
ofstream out(f.c_str());
expected.serialize(out);
out.close();
dyn::wm_str actual;
ifstream in(f.c_str());
actual.load(in);
in.close();
std::remove(f.c_str());
if (not same(expected, actual)) {
return false;
}
return true;
}
void speed_test(uint64_t num, uint64_t num_of_alphabet) {
cout << "access:" << speed_access(num, num_of_alphabet) << "ms" << endl;
cout << "rank:" << speed_rank(num, num_of_alphabet) << "ms" << endl;
cout << "select:" << speed_select(num, num_of_alphabet) << "ms" << endl;
cout << "insert:" << speed_insert(num, num_of_alphabet) << "ms" << endl;
cout << "erase:" << speed_remove(num, num_of_alphabet) << "ms" << endl;
cout << "update:" << speed_update(num, num_of_alphabet) << "ms" << endl;
}
bool test(uint64_t num, uint64_t num_of_alphabet) {
bool ok = true;
ok &= test_access(num, num_of_alphabet);
ok &= test_rank(num, num_of_alphabet);
ok &= test_select(num, num_of_alphabet);
ok &= test_remove(num, num_of_alphabet);
ok &= test_update(num, num_of_alphabet);
ok &= test_serialize(num, num_of_alphabet);
return ok;
}
int main() {
uint64_t num = 100000;
uint64_t num_of_alphabet = 10000000;
cout << "SPEED alpha=100" << endl;
speed_test(num, 100);
cout << "SPEED alpha=" << num_of_alphabet << endl;
speed_test(num, num_of_alphabet);
cout << "TEST" << endl;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> alpha_distrib(1, 1000);
std::uniform_int_distribution<uint64_t> len_distrib(1, 100);
for (int i = 0; i < 100; ++i) {
num = len_distrib(gen);
num_of_alphabet = alpha_distrib(gen);
cout << "TEST size=" << num << " alpha=" << num_of_alphabet << endl;
if (not test(num, num_of_alphabet)) {
cout << "ERROR!" << std::endl;
break;
}
}
cout << "OK" << endl;
return 0;
}