2021-11-14 14:35:05 +01:00
|
|
|
/* #define NDEBUG */
|
2021-11-14 16:19:11 +01:00
|
|
|
/* #include "Rabin_fingerprint.hpp" */
|
2021-11-14 18:13:51 +01:00
|
|
|
#include "general_library.hpp"
|
2021-11-14 16:19:11 +01:00
|
|
|
#include "processes.hpp"
|
2021-11-14 14:35:05 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
int main() {
|
2021-11-14 18:13:51 +01:00
|
|
|
/* std::ifstream T("books/the_complete_works_of_william_shakespeare.txt"); */
|
|
|
|
std::ifstream T("books/genji_monogatari_english.txt");
|
|
|
|
std::ifstream P("books/pattern.txt");
|
2021-11-14 14:35:05 +01:00
|
|
|
|
2021-11-14 18:13:51 +01:00
|
|
|
char c;
|
|
|
|
size_t P_length = 0;
|
2021-11-14 14:35:05 +01:00
|
|
|
std::cout << "Searching for pattern:" << std::endl;
|
2021-11-14 18:13:51 +01:00
|
|
|
std::cout << " ";
|
|
|
|
while(P.get(c)) {
|
|
|
|
std::cout << c;
|
|
|
|
P_length++;
|
|
|
|
|
|
|
|
}
|
|
|
|
std::cout << std::endl << std::endl;
|
|
|
|
|
|
|
|
P.clear(); // clear fail and eof bits
|
|
|
|
P.seekg(0, std::ios::beg); // back to the start!
|
2021-11-14 14:35:05 +01:00
|
|
|
|
2021-11-14 16:19:11 +01:00
|
|
|
uint32_t irreducible_polynomial = get_random_irreducible_polynomial_in_Z2(31);
|
2021-11-14 18:13:51 +01:00
|
|
|
size_t window_size_in_bits = P_length*8;
|
2021-11-14 14:35:05 +01:00
|
|
|
|
|
|
|
// Hash the pattern
|
2021-11-14 16:19:11 +01:00
|
|
|
Rabin_fingerprint_process phiP(irreducible_polynomial, (size_t)window_size_in_bits);
|
2021-11-14 18:13:51 +01:00
|
|
|
while(P.get(c)) {
|
2021-11-14 16:19:11 +01:00
|
|
|
phiP.stream_char(c);
|
2021-11-14 18:13:51 +01:00
|
|
|
}
|
2021-11-14 14:35:05 +01:00
|
|
|
|
|
|
|
// Hash the text
|
2021-11-14 16:19:11 +01:00
|
|
|
Rabin_fingerprint_process phiT(irreducible_polynomial, window_size_in_bits);
|
2021-11-14 18:13:51 +01:00
|
|
|
size_t index = 0;
|
|
|
|
while(T.get(c)) {
|
|
|
|
phiT.stream_char(c);
|
|
|
|
index++;
|
2021-11-14 16:19:11 +01:00
|
|
|
if (phiT.get_fingerprint() == phiP.get_fingerprint())
|
2021-11-14 18:13:51 +01:00
|
|
|
std::cout << "Match found at index " << index-P_length << " with the text \"" << phiT.get_string_in_window() << "\"" << std::endl;
|
2021-11-14 14:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "Done!" << std::endl;
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|