diff --git a/alib2algo/src/string/properties/BadCharacterShiftTable.cpp b/alib2algo/src/string/properties/BadCharacterShiftTable.cpp index 1a6b0a780a2765cb4392d7bf9c0259574a928ff7..7d1b613b11ef18a3ac68a7110d767a81b0468b7a 100644 --- a/alib2algo/src/string/properties/BadCharacterShiftTable.cpp +++ b/alib2algo/src/string/properties/BadCharacterShiftTable.cpp @@ -7,9 +7,6 @@ #include "BadCharacterShiftTable.h" -#include <string/LinearString.h> -#include <alphabet/Symbol.h> - namespace string { namespace properties { @@ -18,28 +15,6 @@ std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::Str return dispatch(pattern.getData()); } -std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::LinearString < >& pattern) { - const std::set<alphabet::Symbol>& alphabet = pattern.getAlphabet(); - std::map<alphabet::Symbol, size_t> bcs; - - /* Initialization of BCS to the length of the needle. */ - for(const auto& symbol : alphabet) - bcs.insert(std::make_pair(symbol, pattern.getContent().size())); - - /* Filling out BCS, ignoring last character. */ - for(size_t i = 0; i < pattern.getContent().size() - 1; i++) - bcs[pattern.getContent().at(i)] = pattern.getContent().size() - i - 1; - - /* - for(const auto& kv: bcs) - std::cout << std::string(kv.first) << " " << kv.second << std::endl; - for(const auto& s: string.getContent()) - std::cout << std::string(s);std::cout << std::endl; - */ - - return bcs; -} - auto BadCharacterShiftTableLinearString = BadCharacterShiftTable::RegistratorWrapper<std::map<alphabet::Symbol, size_t>, string::LinearString < >>(BadCharacterShiftTable::bcs); } /* namespace properties */ diff --git a/alib2algo/src/string/properties/BadCharacterShiftTable.h b/alib2algo/src/string/properties/BadCharacterShiftTable.h index c98eca6caf0842b3685deed2d40779a806c4042c..a46bf6556af5e5c5f1c8e50f79973fdf97dce2a1 100644 --- a/alib2algo/src/string/properties/BadCharacterShiftTable.h +++ b/alib2algo/src/string/properties/BadCharacterShiftTable.h @@ -15,6 +15,9 @@ #include <set> #include <map> +#include <string/LinearString.h> +#include <alphabet/Symbol.h> + namespace string { namespace properties { @@ -31,10 +34,34 @@ public: */ static std::map < alphabet::Symbol, size_t > bcs ( const string::String & pattern ); - static std::map < alphabet::Symbol, size_t > bcs ( const string::LinearString < > & pattern ); + template < class SymbolType > + static std::map < SymbolType, size_t > bcs ( const string::LinearString < SymbolType > & pattern ); }; +template < class SymbolType > +std::map<SymbolType, size_t> BadCharacterShiftTable::bcs(const string::LinearString < SymbolType >& pattern) { + const std::set<SymbolType>& alphabet = pattern.getAlphabet(); + std::map<SymbolType, size_t> bcs; + + /* Initialization of BCS to the length of the needle. */ + for(const auto& symbol : alphabet) + bcs.insert(std::make_pair(symbol, pattern.getContent().size())); + + /* Filling out BCS, ignoring last character. */ + for(size_t i = 0; i < pattern.getContent().size() - 1; i++) + bcs[pattern.getContent().at(i)] = pattern.getContent().size() - i - 1; + + /* + for(const auto& kv: bcs) + std::cout << std::string(kv.first) << " " << kv.second << std::endl; + for(const auto& s: string.getContent()) + std::cout << std::string(s);std::cout << std::endl; + */ + + return bcs; +} + } /* namespace properties */ } /* namespace string */ diff --git a/alib2algo/src/string/properties/BorderArray.cpp b/alib2algo/src/string/properties/BorderArray.cpp index bed42f90fc9d65430da46d2861feecf14cfd4216..da63c9319f5a1fe8734d567543ecaf4f316b5015 100644 --- a/alib2algo/src/string/properties/BorderArray.cpp +++ b/alib2algo/src/string/properties/BorderArray.cpp @@ -7,12 +7,6 @@ #include "BorderArray.h" -#include <container/ObjectsVector.h> -#include <container/ObjectsPair.h> -#include <string/String.h> -#include <primitive/Integer.h> -#include <string/LinearString.h> - namespace string { namespace properties { @@ -21,26 +15,6 @@ std::vector<unsigned> BorderArray::construct(const string::String& string) { return dispatch(string.getData()); } -std::vector<unsigned> BorderArray::construct(const string::LinearString < >& string) { - const auto& w = string.getContent(); - std::vector<unsigned> res(w.size() + 1); - - res[0] = 0; - res[1] = 0; - for(size_t i = 1; i < w.size(); i++) { - unsigned b = res[i]; - while (b > 0 && w[i + 1 - 1] != w[b + 1 - 1]) - b = res[b]; - - if(w[i + 1 - 1] == w[b + 1 - 1]) - res[i + 1] = b + 1; - else - res[i + 1] = 0; - } - - return res; -} - auto BorderArrayLinearString = BorderArray::RegistratorWrapper<std::vector<unsigned>, string::LinearString < >>(BorderArray::construct); } /* namespace properties */ diff --git a/alib2algo/src/string/properties/BorderArray.h b/alib2algo/src/string/properties/BorderArray.h index c3d1e6dadc54deaacfa368d3ba6976f009036478..151d01f21da9bba6d68dfe68d985cc6a4c3d2679 100644 --- a/alib2algo/src/string/properties/BorderArray.h +++ b/alib2algo/src/string/properties/BorderArray.h @@ -12,6 +12,9 @@ #include <core/multipleDispatch.hpp> #include <string/StringFeatures.h> +#include <string/String.h> +#include <string/LinearString.h> + namespace string { namespace properties { @@ -25,9 +28,31 @@ public: */ static std::vector<unsigned> construct(const string::String& string); - static std::vector<unsigned> construct(const string::LinearString < >& string); + template < class SymbolType > + static std::vector<unsigned> construct(const string::LinearString < SymbolType >& string); }; +template < class SymbolType > +std::vector<unsigned> BorderArray::construct(const string::LinearString < SymbolType >& string) { + const auto& w = string.getContent(); + std::vector<unsigned> res(w.size() + 1); + + res[0] = 0; + res[1] = 0; + for(size_t i = 1; i < w.size(); i++) { + unsigned b = res[i]; + while (b > 0 && w[i + 1 - 1] != w[b + 1 - 1]) + b = res[b]; + + if(w[i + 1 - 1] == w[b + 1 - 1]) + res[i + 1] = b + 1; + else + res[i + 1] = 0; + } + + return res; +} + } /* namespace properties */ } /* namespace string */ diff --git a/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.cpp b/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.cpp index b31067dc1116dda1bcb0cf1eaafcaeb2771796f4..93323282aafd487dea713e465b2e156fb34f8dd2 100644 --- a/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.cpp +++ b/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.cpp @@ -7,9 +7,6 @@ #include "ReversedBadCharacterShiftTable.h" -#include <string/LinearString.h> -#include <alphabet/Symbol.h> - namespace string { namespace properties { @@ -18,21 +15,6 @@ std::map < alphabet::Symbol, size_t > ReversedBadCharacterShiftTable::bcs ( cons return dispatch ( pattern.getData ( ) ); } -std::map < alphabet::Symbol, size_t > ReversedBadCharacterShiftTable::bcs ( const string::LinearString < > & pattern ) { - const std::set < alphabet::Symbol > & alphabet = pattern.getAlphabet ( ); - std::map < alphabet::Symbol, size_t > bcs; - - /* Initialization of BCS to the length of the needle. */ - for ( const auto & symbol : alphabet ) - bcs.insert ( std::make_pair ( symbol, pattern.getContent ( ).size ( ) ) ); - - /* Filling out BCS, ignoring first character. */ - for ( size_t i = pattern.getContent ( ).size ( ) - 1; i > 0; i-- ) - bcs[pattern.getContent ( ).at ( i )] = i; - - return bcs; -} - auto ReversedBadCharacterShiftTableLinearString = ReversedBadCharacterShiftTable::RegistratorWrapper < std::map < alphabet::Symbol, size_t >, string::LinearString < > > ( ReversedBadCharacterShiftTable::bcs ); } /* namespace properties */ diff --git a/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.h b/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.h index 2609f6e265089d33bf2bb7d1af68c16b1c3f3763..04c22221992d139c518fe562127436019e7c5d87 100644 --- a/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.h +++ b/alib2algo/src/string/properties/ReversedBadCharacterShiftTable.h @@ -15,6 +15,9 @@ #include <set> #include <map> +#include <string/LinearString.h> +#include <alphabet/Symbol.h> + namespace string { namespace properties { @@ -31,10 +34,27 @@ public: */ static std::map < alphabet::Symbol, size_t > bcs ( const string::String & pattern ); - static std::map < alphabet::Symbol, size_t > bcs ( const string::LinearString < > & pattern ); + template < class SymbolType > + static std::map < SymbolType, size_t > bcs ( const string::LinearString < SymbolType > & pattern ); }; +template < class SymbolType > +std::map < SymbolType, size_t > ReversedBadCharacterShiftTable::bcs ( const string::LinearString < SymbolType > & pattern ) { + const std::set < SymbolType > & alphabet = pattern.getAlphabet ( ); + std::map < SymbolType, size_t > bcs; + + /* Initialization of BCS to the length of the needle. */ + for ( const auto & symbol : alphabet ) + bcs.insert ( std::make_pair ( symbol, pattern.getContent ( ).size ( ) ) ); + + /* Filling out BCS, ignoring first character. */ + for ( size_t i = pattern.getContent ( ).size ( ) - 1; i > 0; i-- ) + bcs[pattern.getContent ( ).at ( i )] = i; + + return bcs; +} + } /* namespace properties */ } /* namespace string */