diff --git a/Makefile b/Makefile index cc3cf4b..1ca0280 100644 --- a/Makefile +++ b/Makefile @@ -24,8 +24,8 @@ LDLIBS = # library flags # -llapacke (LAPACK) NOTE: The order of "-llapacke -llapack -lblas" is very important #LINK.o = $(CXX) $(LDFLAGS) # use CXX for linking -simple_string_matching: simple_string_matching.o Rabin_fingerprint.o general_library.o - $(CXX) $(CXXFLAGS) simple_string_matching.o Rabin_fingerprint.o general_library.o -o simple_string_matching +simple_string_matching: simple_string_matching.o Rabin_fingerprint.o general_library.o processes.o + $(CXX) $(CXXFLAGS) simple_string_matching.o Rabin_fingerprint.o general_library.o processes.o -o simple_string_matching simple_string_matching.o: simple_string_matching.cpp $(CXX) $(CXXFLAGS) -c simple_string_matching.cpp @@ -36,6 +36,9 @@ Rabin_fingerprint.o: Rabin_fingerprint.cpp Rabin_fingerprint.hpp general_library.o: general_library.cpp general_library.hpp $(CXX) $(CXXFLAGS) -c general_library.cpp +processes.o: processes.cpp processes.hpp + $(CXX) $(CXXFLAGS) -c processes.cpp + porat-porat: porat-porat.cpp # Tell the compiler that 'clean' isn't referring to a file diff --git a/Rabin_fingerprint.hpp b/Rabin_fingerprint.hpp index 01ff03c..f9b74dd 100644 --- a/Rabin_fingerprint.hpp +++ b/Rabin_fingerprint.hpp @@ -14,7 +14,7 @@ class Rabin_fingerprint { void push_bit (bool b); void shift_bit (bool b); void slide_char (char c_in, char c_out); - void slide_bit (bool b1, bool b2); + void slide_bit (bool b_in, bool b_out); uint32_t get_fingerprint(); diff --git a/V1/simple_string_matching b/V1/simple_string_matching index 36894f0..54fb419 100755 Binary files a/V1/simple_string_matching and b/V1/simple_string_matching differ diff --git a/processes.cpp b/processes.cpp new file mode 100644 index 0000000..3f53510 --- /dev/null +++ b/processes.cpp @@ -0,0 +1,29 @@ +#include "processes.hpp" + +Rabin_fingerprint_process::Rabin_fingerprint_process(uint32_t irr_poly, size_t window_size_in_bits) + : window_size_in_bits(window_size_in_bits), + phi(irr_poly, window_size_in_bits) +{} + +void Rabin_fingerprint_process::stream_char (char c) { + std::bitset<8> b(c); + for (char i = 7; i >= 0; i--) { + stream_bit((bool)b[i]); + } +} + +void Rabin_fingerprint_process::stream_bit (bool b) { + if (window.size() == window_size_in_bits) { + window.push(b); + bool b_out = window.front(); + window.pop(); + phi.slide_bit(b, b_out); + } else { + window.push(b); + phi.push_bit(b); + } +} + +uint32_t Rabin_fingerprint_process::get_fingerprint () { + return phi.get_fingerprint(); +} diff --git a/processes.hpp b/processes.hpp new file mode 100644 index 0000000..05561b1 --- /dev/null +++ b/processes.hpp @@ -0,0 +1,23 @@ +#ifndef PROCESSES_H +#define PROCESSES_H + +#include "Rabin_fingerprint.hpp" +#include "general_library.hpp" + +#include +#include + +class Rabin_fingerprint_process { + public: + Rabin_fingerprint_process(uint32_t irr_poly, size_t window_size_in_bits); + void stream_char(char c); + void stream_bit(bool b); + uint32_t get_fingerprint(); + + private: + std::queue window; + size_t window_size_in_bits; + Rabin_fingerprint phi; +}; + +#endif diff --git a/simple_string_matching b/simple_string_matching new file mode 100755 index 0000000..4dd6519 Binary files /dev/null and b/simple_string_matching differ diff --git a/simple_string_matching.cpp b/simple_string_matching.cpp index 84520a0..735ceac 100644 --- a/simple_string_matching.cpp +++ b/simple_string_matching.cpp @@ -1,6 +1,7 @@ /* #define NDEBUG */ -#include "Rabin_fingerprint.hpp" -#include "general_library.hpp" +/* #include "Rabin_fingerprint.hpp" */ +/* #include "general_library.hpp" */ +#include "processes.hpp" #include #include @@ -21,11 +22,7 @@ int main() { std::string T( (std::istreambuf_iterator(ifs) ), (std::istreambuf_iterator() ) ); - /* 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; @@ -33,27 +30,24 @@ int main() { /* 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 + 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 fP(polynomial, window_size_in_bits); + Rabin_fingerprint_process phiP(irreducible_polynomial, (size_t)window_size_in_bits); for (char c : P) - fP.push_char(c); + phiP.stream_char(c); // Hash the text - Rabin_fingerprint fT(polynomial, window_size_in_bits); + Rabin_fingerprint_process phiT(irreducible_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()) + 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++) { - fT.slide_char(T[i], T[i-P.length()]); - if (fT.get_fingerprint() == fP.get_fingerprint()) + phiT.stream_char(T[i]); + if (phiT.get_fingerprint() == phiP.get_fingerprint()) print_match(i-P.length()+1, P.length(), T); }