string_search Namespace Reference

String search algorithms. More...

## Functions

int | brute_force (const std::string &text, const std::string &pattern)
std::vector< int > | getFailureArray (const std::string &pattern)
bool | kmp (const std::string &pattern, const std::string &text)
int64_t | create_hash (const std::string &s, int n)
int64_t | recalculate_hash (const std::string &s, int old_index, int new_index, int64_t old_hash, int patLength)
bool | check_if_equal (const std::string &str1, const std::string &str2, int start1, int end1, int start2, int end2)
int | rabin_karp (const std::string &str, const std::string &pat)

## Detailed Description

String search algorithms.

## Function Documentation

## brute_force()

int string_search::brute_force | ( | const std::string & | text,
|  | const std::string & | pattern
| ) |  |

Find a pattern in a string by comparing the pattern to every substring.

Parameters

  text | Any string that might contain the pattern.
  pattern | String that we are searching for.

Returns
  Index where the pattern starts in the text
  -1 if the pattern was not found.

{
size_t pat_l = pattern.length();
size_t txt_l = text.length();
int index = -1;
if (pat_l <= txt_l) {
for (size_t i = 0; i < txt_l - pat_l + 1; i++) {
std::string s = text.substr(i, pat_l);
if (s == pattern) {
index = i;
break;
}
}
}
return index;
}
std::string
std::string::length
T length(T... args)
std::string::substr
T substr(T... args)
Here is the call graph for this function:

## check_if_equal()

bool string_search::check_if_equal | ( | const std::string & | str1,
|  | const std::string & | str2,
|  | int | start1,
|  | int | end1,
|  | int | start2,
|  | int | end2
| ) |  |

compare if two sub-strings are equal

Parameters

  [in] | str1 | string pattern to search
  [in] | str2 | text in which to search
  [in] | start1,end1 | start and end indices for substring in str1
  [in] | start2,end2 | start and end indices for substring in str2

Returns
  `true` if pattern was found
  `false` if pattern was not found

Note
  can this be replaced by std::string::compare?

{
if (end1 - start1 != end2 - start2) {
return false;
}
while (start1 <= end1 && start2 <= end2) {
if (str1[start1] != str2[start2]) {
return false;
}
start1++;
start2++;
}
return true;
}

## create_hash()

int64_t string_search::create_hash | ( | const std::string & | s,
|  | int | n
| ) |  |

convert a string to an intger - called as hashing function

Parameters

  [in] | s | source of string to hash
  [in] | n | length of substring to hash

Returns
  hash integer

{
int64_t result = 0;
for (int i = 0; i < n; ++i) {
result += (int64_t)(s[i] * (int64_t)pow(PRIME, i));
}
return result;
}
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::pow
T pow(T... args)
PRIME
#define PRIME
Prime modulus for hash functions.
Definition: rabin_karp.cpp:16

## getFailureArray()

std::vector<int> string_search::getFailureArray | ( | const std::string & | pattern | ) |

Generate the partial match table aka failure function for a pattern to search.

Parameters

  [in] | pattern | text for which to create the partial match table

Returns
  the partial match table as a vector array

{
int pattern_length = pattern.size();
std::vector<int> failure(pattern_length + 1);
failure[0] = -1;
int j = -1;
for (int i = 0; i < pattern_length; i++) {
while (j != -1 && pattern[j] != pattern[i]) {
j = failure[j];
}
j++;
failure[i + 1] = j;
}
return failure;
}
std::vector< int >
Here is the call graph for this function:

## kmp()

bool string_search::kmp | ( | const std::string & | pattern,
|  | const std::string & | text
| ) |  |

KMP algorithm to find a pattern in a text

Parameters

  [in] | pattern | string pattern to search
  [in] | text | text in which to search

Returns
  `true` if pattern was found
  `false` if pattern was not found

{
int text_length = text.size(), pattern_length = pattern.size();
std::vector<int> failure = getFailureArray(pattern);
int k = 0;
for (int j = 0; j < text_length; j++) {
while (k != -1 && pattern[k] != text[j]) {
k = failure[k];
}
k++;
if (k == pattern_length)
return true;
}
return false;
}
string_search::getFailureArray
std::vector< int > getFailureArray(const std::string &pattern)
Definition: knuth_morris_pratt.cpp:33
Here is the call graph for this function:

## rabin_karp()

int string_search::rabin_karp | ( | const std::string & | str,
|  | const std::string & | pat
| ) |  |

Perform string pattern search using Rabin-Karp algorithm

Parameters

  [in] | str | string to search in
  [in] | pat | pattern to search for

Returns
  index of first occurrence of pattern
  -1 if pattern not found

{
int64_t pat_hash = create_hash(pat, pat.size());
int64_t str_hash = create_hash(str, pat.size());
for (int i = 0; i <= str.size() - pat.size(); ++i) {
if (pat_hash == str_hash &&
check_if_equal(str, pat, i, i + pat.size() - 1, 0,
pat.size() - 1)) {
return i;
}
if (i < str.size() - pat.size()) {
str_hash =
recalculate_hash(str, i, i + pat.size(), str_hash, pat.size());
}
}
return -1; // return -1 if given pattern not found
}
string_search::create_hash
int64_t create_hash(const std::string &s, int n)
Definition: rabin_karp.cpp:25
string_search::check_if_equal
bool check_if_equal(const std::string &str1, const std::string &str2, int start1, int end1, int start2, int end2)
Definition: rabin_karp.cpp:60
string_search::recalculate_hash
int64_t recalculate_hash(const std::string &s, int old_index, int new_index, int64_t old_hash, int patLength)
Definition: rabin_karp.cpp:42
Here is the call graph for this function:

## recalculate_hash()

int64_t string_search::recalculate_hash | ( | const std::string & | s,
|  | int | old_index,
|  | int | new_index,
|  | int64_t | old_hash,
|  | int | patLength
| ) |  |

re-hash a string using known existing hash

Parameters

  [in] | s | source of string to hash
  [in] | old_index | previous index of string
  [in] | new_index | new index of string
  [in] | old_hash | previous hash of substring
  [in] | patLength | length of substring to hash

Returns
  new hash integer

{
int64_t new_hash = old_hash - s[old_index];
new_hash /= PRIME;
new_hash += (int64_t)(s[new_index] * (int64_t)pow(PRIME, patLength - 1));
return new_hash;
}