52#define APLHABET_SIZE CHAR_MAX
90 arg.resize(str.size() + 1, 0);
94 std::vector<size_t> border_pos(str.size() + 1, 0);
96 size_t current_char = str.length();
98 size_t border_index = str.length() + 1;
100 border_pos[current_char] = border_index;
102 while (current_char > 0) {
103 while (border_index <= str.length() &&
104 str[current_char - 1] != str[border_index - 1]) {
105 if (arg[border_index] == 0) {
106 arg[border_index] = border_index - current_char;
109 border_index = border_pos[border_index];
114 border_pos[current_char] = border_index;
117 size_t largest_border_index = border_pos[0];
119 for (
size_t i = 0; i < str.size(); i++) {
121 arg[i] = largest_border_index;
125 if (i == largest_border_index) {
126 largest_border_index = border_pos[largest_border_index];
141 for (
size_t i = 0; i < str.length(); i++) {
142 arg[str[i]] = str.length() - i - 1;
166 size_t index_position = arg.pat.size() - 1;
167 std::vector<size_t> index_storage;
169 while (index_position < str.length()) {
170 size_t index_string = index_position;
171 int index_pattern =
static_cast<int>(arg.pat.size()) - 1;
173 while (index_pattern >= 0 &&
174 str[index_string] == arg.pat[index_pattern]) {
179 if (index_pattern < 0) {
180 index_storage.push_back(index_position - arg.pat.length() + 1);
183 index_position += std::max(arg.
bad_char[str[index_string]],
188 return index_storage;
200bool is_prefix(
const char* str,
const char* pat,
size_t len) {
201 if (strlen(str) < len) {
205 for (
size_t i = 0; i < len; i++) {
206 if (str[i] != pat[i]) {
225 assert(indexes.size() == 2);
240 assert(indexes.size() == 6);
242 for (
const auto& currentIndex : indexes) {
252 "When pat Mr. and Mrs. pat Dursley woke up on the dull, gray \
253 Tuesday our story starts, \
254 there was nothing about pat the cloudy sky outside to pat suggest that\
256 mysterious things would pat soon be happening all pat over the \
262 std::cout <<
"All tests have successfully passed!\n";
static void tests()
Self-test implementations.
#define APLHABET_SIZE
number of symbols in the alphabet we use
void pat_test(const char *text)
A test case in which we search for every appearance of the word 'pat'.
void and_test(const char *text)
A test case in which we search for every appearance of the word 'and'.
Functions for the Boyer Moore algorithm implementation.
bool is_prefix(const char *str, const char *pat, size_t len)
Check if pat is prefix of str.
void init_pattern(const std::string &str, pattern &arg)
A function that initializes pattern.
std::vector< size_t > search(const std::string &str, const pattern &arg)
A function that implements Boyer-Moore's algorithm.
void init_bad_char(const std::string &str, std::vector< size_t > &arg)
A function that preprocess the bad char table.
void init_good_suffix(const std::string &str, std::vector< size_t > &arg)
A function that preprocess the good suffix thable.
A structure representing all the data we need to search the preprocessed pattern in text.
std::vector< size_t > good_suffix
std::vector< size_t > bad_char