-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_search.cpp
66 lines (58 loc) · 2.33 KB
/
string_search.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
#include "test_utils.hpp"
#include "strings/boyer_moore.hpp"
#include "strings/kmp.hpp"
#include "strings/z_search.hpp"
#include "lib/strings.hpp"
auto naive_search_all(const string& haystack, const string& needle) {
int P = needle.size(), T = haystack.size();
vector<int> indices;
const char* np = needle.data();
const char* hp = haystack.data();
for (int i = 0; i + P <= T; i++) {
if (equal(np, np + P, hp + i))
indices.push_back(i);
}
return indices;
}
void stress_test_string_searchers() {
vector<string> needles, haystacks;
auto add = [&](vector<string>& strs, vector<string>&& more) {
strs.insert(end(strs), begin(more), end(more));
};
add(needles, generate_all_strings(1, 6, 'a', 'c'));
add(needles, {"abab", "ababa", "ababab", "abababab", "abba", "abbaab", "abbaabba"});
add(needles, {"cabab", "abcabababc", "ababcabcab", "ababcab", "abcabab"});
add(needles, {"aabaa", "aaaab", "baaaa", "ababa", "bcabcaabc"});
add(needles, {"accbcc", "accbccacbc", "accbccaccbcc", "acbcacbc"});
add(needles, {".", ",!?#:", "aa.bb", "a,", ",a"});
add(needles, rand_strings(37, 7, 15, 'a', 'c'));
add(needles, rand_strings(20, 16, 40, 'a', 'c'));
add(haystacks, {"...", "#?!", "?!", "!?", " ", "abcdabc", ",bab,", ",aba,"});
add(haystacks, generate_all_strings(0, 7, 'a', 'c'));
add(haystacks, rand_strings(3'000, 8, 15, 'a', 'c'));
add(haystacks, rand_strings(1'000, 16, 70, 'a', 'c'));
add(haystacks, rand_strings(100, 71, 1000, 'a', 'c'));
add(haystacks, rand_strings(20, 1001, 10000, 'a', 'c'));
int N = needles.size(), H = haystacks.size();
print(" no. needles: {}\n", N);
print(" no. haystacks: {}\n", H);
for (int i = 0; i < N; i++) {
print_progress(i, N, "stress test string searchers");
auto needle = needles[i];
KMP kmp(needle);
BoyerMoore bm(needle);
for (auto haystack : haystacks) {
auto i1 = naive_search_all(haystack, needle);
auto i2 = kmp_search_all(haystack, kmp);
auto i3 = boyer_moore_search_all(haystack, bm);
auto i4 = z_search_all(haystack, needle);
assert(i1 == i2);
assert(i1 == i3);
assert(i1 == i4);
}
}
}
int main() {
RUN_BLOCK(stress_test_string_searchers());
return 0;
}