Knyffen
c6b6329134
The process contains the fingerprinting function along with a queue representing the sliding window. This means that the user no longer needs to remember the outgoing character when streaming the text.
60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
/* #define NDEBUG */
|
|
/* #include "Rabin_fingerprint.hpp" */
|
|
/* #include "general_library.hpp" */
|
|
#include "processes.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 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);
|
|
size_t window_size_in_bits = P.length()*8;
|
|
|
|
// Hash the pattern
|
|
Rabin_fingerprint_process phiP(irreducible_polynomial, (size_t)window_size_in_bits);
|
|
for (char c : P)
|
|
phiP.stream_char(c);
|
|
|
|
// Hash the text
|
|
Rabin_fingerprint_process phiT(irreducible_polynomial, window_size_in_bits);
|
|
for (size_t i = 0; i < P.length(); i++)
|
|
phiT.stream_char(T[i]);
|
|
if (phiT.get_fingerprint() == phiP.get_fingerprint())
|
|
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())
|
|
print_match(i-P.length()+1, P.length(), T);
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
std::cout << "Done!" << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|