diff --git a/alib2algo/src/stringology/simulations/HammingBitParalelism.h b/alib2algo/src/stringology/simulations/HammingBitParalelism.h new file mode 100644 index 0000000000000000000000000000000000000000..b6716bc8789357c8fa241589a8ded27bc56a5e0f --- /dev/null +++ b/alib2algo/src/stringology/simulations/HammingBitParalelism.h @@ -0,0 +1,67 @@ +/* + * HammingBitParalelism.h + * + * Created on: 29.4.2018 + * Author: Tomas Capek + */ + +#ifndef _HAMMING_BIT_PARALELISM_H__ +#define _HAMMING_BIT_PARALELISM_H__ + +#include <exception> +#include <string/LinearString.h> + +#include "BitParalelism.h" + +namespace stringology { + +namespace simulations { + +class HammingBitParalelism { +public: + template <class SymbolType> + static ext::vector<unsigned int> search(const string::LinearString<SymbolType> & text, const string::LinearString<SymbolType> & pattern, unsigned int errors); +}; + + +template <class SymbolType> +ext::vector<unsigned int> HammingBitParalelism::search(const string::LinearString<SymbolType> & text, const string::LinearString<SymbolType> & pattern, unsigned int errors) { + // preparation stage + ext::set<SymbolType> common_alphabet = text.getAlphabet(); + common_alphabet.insert(pattern.getAlphabet().begin(), pattern.getAlphabet().end()); + + ext::map<SymbolType, ext::vector<bool> > D_vectors = BitParalelism::constructDVectors(common_alphabet, pattern); + + // computation part + ext::vector<unsigned int> result; + + ext::vector<ext::vector<bool> > B_vectors; + for(unsigned int i=0; i<=errors; i++) { + B_vectors.push_back(ext::vector<bool>(pattern.getContent().size(), 1)); + } + + for(unsigned int i=0; i<text.getContent().size(); i++) { + ext::vector< ext::vector<bool> > previous_B_vectors = B_vectors; + + B_vectors[0] = (B_vectors[0] << 1) | D_vectors[text.getContent()[i]]; + + for(unsigned int j=1; j<=errors; j++) { + B_vectors[j] = ( (B_vectors[j] << 1) | D_vectors[text.getContent()[i]] ) & (previous_B_vectors[j-1] << 1); + } + + for(const auto & B_vector : B_vectors) { + if (B_vector[pattern.getContent().size()-1] == false) { + result.push_back(i - pattern.getContent().size() + 1); + break; + } + } + } + + return result; +} + +} // namespace simulations + +} // namespace stringology + +#endif /* _HAMMING_BIT_PARALELISM_H__ */ diff --git a/alib2algo/test-src/stringology/simulations/HammingBitParalelismTest.cpp b/alib2algo/test-src/stringology/simulations/HammingBitParalelismTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..139b9fdebf5ce7ed71ce5763cd1db28cf8e62b91 --- /dev/null +++ b/alib2algo/test-src/stringology/simulations/HammingBitParalelismTest.cpp @@ -0,0 +1,20 @@ +#include "HammingBitParalelismTest.h" + +#include <string/LinearString.h> +#include <stringology/simulations/HammingBitParalelism.h> + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( HammingBitParalelismTest, "bit paralelism" ); +CPPUNIT_TEST_SUITE_REGISTRATION ( HammingBitParalelismTest ); + +void HammingBitParalelismTest::testSimple() { + auto text = string::LinearString<>("adcabcaabadbbca"); + auto pattern = string::LinearString<>("adbbca"); + + ext::vector<unsigned int> expected_result = {1,9}; + auto result = stringology::simulations::HammingBitParalelism::search(text, pattern, 3); + CPPUNIT_ASSERT(expected_result == result); +} + +void HammingBitParalelismTest::setUp() { } + +void HammingBitParalelismTest::tearDown() { } diff --git a/alib2algo/test-src/stringology/simulations/HammingBitParalelismTest.h b/alib2algo/test-src/stringology/simulations/HammingBitParalelismTest.h new file mode 100644 index 0000000000000000000000000000000000000000..540acf16b46a0888bb47b296cb858ff24858ccc2 --- /dev/null +++ b/alib2algo/test-src/stringology/simulations/HammingBitParalelismTest.h @@ -0,0 +1,17 @@ +#ifndef HAMMING_BIT_PARALELISM_TEST_H_ +#define HAMMING_BIT_PARALELISM_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class HammingBitParalelismTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(HammingBitParalelismTest); + CPPUNIT_TEST(testSimple); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp ( ); + void tearDown ( ); + + void testSimple(); +}; +#endif // HAMMING_BIT_PARALELISM_TEST_H_