Bachelors_Thesis_Code/simple_string_matching.cpp

60 lines
1.8 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 "processes.hpp"
2021-11-14 14:35:05 +01:00
#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 P = "word";
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 irreducible_polynomial = get_random_irreducible_polynomial_in_Z2(31);
2021-11-14 14:35:05 +01:00
size_t window_size_in_bits = P.length()*8;
// Hash the pattern
Rabin_fingerprint_process phiP(irreducible_polynomial, (size_t)window_size_in_bits);
2021-11-14 14:35:05 +01:00
for (char c : P)
phiP.stream_char(c);
2021-11-14 14:35:05 +01:00
// Hash the text
Rabin_fingerprint_process phiT(irreducible_polynomial, window_size_in_bits);
2021-11-14 14:35:05 +01:00
for (size_t i = 0; i < P.length(); i++)
phiT.stream_char(T[i]);
if (phiT.get_fingerprint() == phiP.get_fingerprint())
2021-11-14 14:35:05 +01:00
print_match(0, P.length(), T);
for (size_t i = P.length(); i < T.length(); i++) {
phiT.stream_char(T[i]);
if (phiT.get_fingerprint() == phiP.get_fingerprint())
2021-11-14 14:35:05 +01:00
print_match(i-P.length()+1, P.length(), T);
}
std::cout << std::endl;
std::cout << "Done!" << std::endl;
return EXIT_SUCCESS;
}