Bachelors_Thesis_Code/V1/simple_string_matching.cpp

66 lines
2.2 KiB
C++
Raw Normal View History

2021-11-14 14:35:05 +01:00
/* #define NDEBUG */
#include "Rabin_fingerprint.hpp"
#include "general_library.hpp"
#include <iostream>
#include <stdint.h>
#include <math.h>
#include <string>
#include <fstream>
void print_match (size_t index, size_t length, std::string &T) {
std::cout << "Match found at index " << index << " with the text \"";
for (size_t i = 0; i < length; i++)
std::cout << T[index + i];
std::cout << "\"" << std::endl;
}
int main() {
/* std::ifstream ifs("../books/the_complete_works_of_william_shakespeare.txt"); */
std::ifstream ifs("../books/genji_monogatari_english.txt");
std::string T( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
/* std::string T = "Hello, this is my test string averylongword is a necessary word to exceed the 32 bit window."; */
// Test without the modulo polynomial - and two matches
std::string P = "word";
// Test with the modulo polynomial
/* std::string P = "averylongword"; */
std::cout << "Searching for pattern:" << std::endl;
std::cout << " " << P << std::endl;
/* std::cout << "in text:" << std::endl; */
/* std::cout << " " << T << std::endl; */
std::cout << std::endl;
/* uint32_t polynomial = pow(2, 30) + pow(2, 2) + 1; // x^31 + x^3 + 1 */
uint32_t polynomial = get_random_irreducible_polynomial_in_Z2(31);
/* uint32_t polynomial = 0b11010011100100000111101011110111; */
// Test without the modulo polynomial
size_t window_size_in_bits = P.length()*8;
// Hash the pattern
Rabin_fingerprint fP(polynomial, window_size_in_bits);
for (char c : P)
fP.push_char(c);
// Hash the text
Rabin_fingerprint fT(polynomial, window_size_in_bits);
for (size_t i = 0; i < P.length(); i++)
fT.push_char(T[i]);
if (fT.get_fingerprint() == fP.get_fingerprint())
print_match(0, P.length(), T);
for (size_t i = P.length(); i < T.length(); i++) {
fT.slide_char(T[i], T[i-P.length()]);
if (fT.get_fingerprint() == fP.get_fingerprint())
print_match(i-P.length()+1, P.length(), T);
}
std::cout << std::endl;
std::cout << "Done!" << std::endl;
return EXIT_SUCCESS;
}