diff --git a/alib2algo/src/stringology/exact/BadCharacterShiftTable.cpp b/alib2algo/src/stringology/exact/BadCharacterShiftTable.cpp index 4f0de6a4dbc18ce8f429726c6ed3362a1ba1ff8c..e3a9c6ad28defe7cf66269451fef42cf1c9b5671 100644 --- a/alib2algo/src/stringology/exact/BadCharacterShiftTable.cpp +++ b/alib2algo/src/stringology/exact/BadCharacterShiftTable.cpp @@ -15,18 +15,15 @@ namespace stringology { namespace exact { -std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::String& pattern, const std::set<alphabet::Symbol>& alphabet) { - std::pair<std::map<alphabet::Symbol, size_t>, const std::set<alphabet::Symbol>*> data; - data.second = &alphabet; - pattern.getData().Accept((void*) &data, BadCharacterShiftTable::BAD_CHARACTER_SHIFT); - return data.first; +std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const std::set<alphabet::Symbol>& alphabet, const string::String& pattern) { + return getInstance().dispatch(alphabet, pattern.getData()); } std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::String& pattern) { - return bcs(pattern, pattern.getAlphabet()); + return bcs(pattern.getAlphabet(), pattern); } -std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::LinearString& pattern, const std::set<alphabet::Symbol>& alphabet) { +std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const std::set<alphabet::Symbol>& alphabet, const string::LinearString& pattern) { std::map<alphabet::Symbol, size_t> bcs; /* Initialization of BCS to the length of the needle. */ @@ -47,25 +44,12 @@ std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::Lin return bcs; } -std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::LinearString& pattern) { - return bcs(pattern, pattern.getAlphabet()); -} - -void BadCharacterShiftTable::Visit(void*, const string::Epsilon&) const { - throw exception::AlibException("Unsupported string type Epsilon"); -} +auto BadCharacterShiftTableLinearString = BadCharacterShiftTable::RegistratorWrapper<std::map<alphabet::Symbol, size_t>, string::LinearString>(BadCharacterShiftTable::getInstance(), BadCharacterShiftTable::bcs); -void BadCharacterShiftTable::Visit(void* data, const string::LinearString& pattern) const { - std::pair<std::map<alphabet::Symbol, size_t>, const std::set<alphabet::Symbol>*> & res = *((std::pair<std::map<alphabet::Symbol, size_t>, const std::set<alphabet::Symbol>*>*) data); - res.first = this->bcs(pattern, *res.second); -} - -void BadCharacterShiftTable::Visit(void*, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); +std::map<alphabet::Symbol, size_t> BadCharacterShiftTable::bcs(const string::LinearString& pattern) { + return bcs(pattern.getAlphabet(), pattern); } -const BadCharacterShiftTable BadCharacterShiftTable::BAD_CHARACTER_SHIFT; - } /* namespace exact */ } /* namespace stringology */ diff --git a/alib2algo/src/stringology/exact/BadCharacterShiftTable.h b/alib2algo/src/stringology/exact/BadCharacterShiftTable.h index e7a4d02457205c0d51a82080862f2a9d85cc6237..0fcae07f1e8697ed4f1c04b2a72745a355c81574 100644 --- a/alib2algo/src/stringology/exact/BadCharacterShiftTable.h +++ b/alib2algo/src/stringology/exact/BadCharacterShiftTable.h @@ -9,6 +9,8 @@ #define _BAD_CHARACTER_SHIFT_TABLE_H_ #include <string/String.h> +#include <string/StringFeatures.h> +#include <common/multipleDispatch.hpp> #include <set> #include <map> @@ -21,10 +23,8 @@ namespace exact { * Computation of BCS table for BMH from MI(E+\eps)-EVY course 2014 * To get rid of zeros in BCS table we ignore last haystack character */ -class BadCharacterShiftTable : public string::VisitableStringBase::const_visitor_type { +class BadCharacterShiftTable : public std::SingleDispatchFirstStaticParam<std::map<alphabet::Symbol, size_t>, const std::set<alphabet::Symbol>, string::StringBase> { public: - BadCharacterShiftTable() {} - /** * Search for pattern in linear string. * @return set set of occurences @@ -35,17 +35,16 @@ public: * Search for pattern in linear string. * @return set set of occurences */ - static std::map<alphabet::Symbol, size_t> bcs(const string::String& pattern, const std::set<alphabet::Symbol>& alphabet); + static std::map<alphabet::Symbol, size_t> bcs(const std::set<alphabet::Symbol>& alphabet, const string::String& pattern); static std::map<alphabet::Symbol, size_t> bcs(const string::LinearString& pattern); - static std::map<alphabet::Symbol, size_t> bcs(const string::LinearString& pattern, const std::set<alphabet::Symbol>& alphabet); -private: - void Visit(void*, const string::Epsilon& subject) const; - void Visit(void*, const string::LinearString& subject) const; - void Visit(void*, const string::CyclicString& subject) const; + static std::map<alphabet::Symbol, size_t> bcs(const std::set<alphabet::Symbol>& alphabet, const string::LinearString& pattern); - static const BadCharacterShiftTable BAD_CHARACTER_SHIFT; + static BadCharacterShiftTable& getInstance() { + static BadCharacterShiftTable res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/BorderArray.cpp b/alib2algo/src/stringology/exact/BorderArray.cpp index 6d8ad7274b3705322d8544e9f399ca53ab7b98be..8feec7d64f927695ca71c32c1ed244a8ec8c5c87 100644 --- a/alib2algo/src/stringology/exact/BorderArray.cpp +++ b/alib2algo/src/stringology/exact/BorderArray.cpp @@ -10,7 +10,7 @@ #include <container/ObjectsVector.h> #include <container/ObjectsPair.h> #include <exception/AlibException.h> -#include <object/Object.h> +#include <string/String.h> #include <primitive/Integer.h> #include <string/LinearString.h> @@ -19,9 +19,7 @@ namespace stringology { namespace exact { std::vector<unsigned> BorderArray::construct(const string::String& string) { - std::vector<unsigned> out; - string.getData().Accept((void*) &out, BorderArray::BORDER_ARRAY); - return out; + return getInstance().dispatch(string.getData()); } std::vector<unsigned> BorderArray::construct(const string::LinearString& string) { @@ -44,20 +42,7 @@ std::vector<unsigned> BorderArray::construct(const string::LinearString& string) return res; } -void BorderArray::Visit(void* data, const string::LinearString& string) const { - std::vector<unsigned> & out = *(std::vector<unsigned>*) data; - out = this->construct(string); -} - -void BorderArray::Visit(void*, const string::Epsilon&) const { - throw exception::AlibException("Unsupported string type Epsilon"); -} - -void BorderArray::Visit(void*, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const BorderArray BorderArray::BORDER_ARRAY; +auto BorderArrayLinearString = BorderArray::RegistratorWrapper<std::vector<unsigned>, string::LinearString>(BorderArray::getInstance(), BorderArray::construct); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/BorderArray.h b/alib2algo/src/stringology/exact/BorderArray.h index 348a7599711493aed9150ad17e66720abb4845f8..f939bf3578f8e7ef19d8a08c36cce74299c29a5a 100644 --- a/alib2algo/src/stringology/exact/BorderArray.h +++ b/alib2algo/src/stringology/exact/BorderArray.h @@ -9,16 +9,15 @@ #define _BORDER_ARRAY_H_ #include <vector> -#include <string/String.h> +#include <common/multipleDispatch.hpp> +#include <string/StringFeatures.h> namespace stringology { namespace exact { -class BorderArray : public string::VisitableStringBase::const_visitor_type { +class BorderArray : public std::SingleDispatch<std::vector<unsigned>, string::StringBase> { public: - BorderArray() {} - /** * Computes border array of string * @param string string to compute border array for @@ -28,12 +27,10 @@ public: static std::vector<unsigned> construct(const string::LinearString& string); -private: - void Visit(void*, const string::Epsilon& pattern) const; - void Visit(void*, const string::LinearString& pattern) const; - void Visit(void*, const string::CyclicString& pattern) const; - - static const BorderArray BORDER_ARRAY; + static BorderArray& getInstance() { + static BorderArray res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/BoyerMooreHorspool.cpp b/alib2algo/src/stringology/exact/BoyerMooreHorspool.cpp index c23e905648518ec880e5463fcd77002a043de2b5..836c27c7fe403adcfe19edd725fa421869d21a65 100644 --- a/alib2algo/src/stringology/exact/BoyerMooreHorspool.cpp +++ b/alib2algo/src/stringology/exact/BoyerMooreHorspool.cpp @@ -19,15 +19,13 @@ namespace stringology { namespace exact { std::set<unsigned> BoyerMooreHorspool::match(const string::String& subject, const string::String& pattern) { - std::set<unsigned> data; - Accept((void*) &data, subject.getData(), pattern.getData(), BoyerMooreHorspool::BOYER_MOORE_HORSPOOL); - return data; + return getInstance().dispatch(subject.getData(), pattern.getData()); } std::set<unsigned> BoyerMooreHorspool::match(const string::LinearString& string, const string::LinearString& pattern) { std::set<unsigned> occ; - std::map<alphabet::Symbol, size_t> bcs = BadCharacterShiftTable::bcs(pattern, string.getAlphabet()); + std::map<alphabet::Symbol, size_t> bcs = BadCharacterShiftTable::bcs(string.getAlphabet(), pattern); size_t haystack_offset = 0; while(haystack_offset + pattern.getContent().size() <= string.getContent().size()) { @@ -44,20 +42,7 @@ std::set<unsigned> BoyerMooreHorspool::match(const string::LinearString& string, return occ; } -void BoyerMooreHorspool::Visit(void*, const string::Epsilon&, const string::Epsilon&) const { - throw exception::AlibException("Unsupported string type Epsilon"); -} - -void BoyerMooreHorspool::Visit(void* data, const string::LinearString& subject, const string::LinearString& pattern) const { - std::set<unsigned> & res = *((std::set<unsigned>*) data); - res = this->match(subject, pattern); -} - -void BoyerMooreHorspool::Visit(void*, const string::CyclicString&, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const BoyerMooreHorspool BoyerMooreHorspool::BOYER_MOORE_HORSPOOL; +auto BoyerMooreHorpoolLinearStringLinearString = BoyerMooreHorspool::RegistratorWrapper<std::set<unsigned>, string::LinearString, string::LinearString>(BoyerMooreHorspool::getInstance(), BoyerMooreHorspool::match); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/BoyerMooreHorspool.h b/alib2algo/src/stringology/exact/BoyerMooreHorspool.h index db9276d3137c61959ee3772ebf5a34b4076925e9..f3970faa2cca657030c6dee32801e5bb87dcb26a 100644 --- a/alib2algo/src/stringology/exact/BoyerMooreHorspool.h +++ b/alib2algo/src/stringology/exact/BoyerMooreHorspool.h @@ -8,8 +8,10 @@ #ifndef _BOYER_MOORE_HORSPOOL_H #define _BOYER_MOORE_HORSPOOL_H -#include <string/String.h> #include <set> +#include <common/multipleDispatch.hpp> +#include <string/String.h> +#include <string/StringFeatures.h> namespace stringology { @@ -19,10 +21,8 @@ namespace exact { * Implementation of BMH for MI(E+\eps)-EVY course 2014 * To get rid of zeros in BCS table we ignore last haystack character */ -class BoyerMooreHorspool : public string::VisitableStringBase::const_same_visitor_type { +class BoyerMooreHorspool : public std::DoubleDispatch<std::set<unsigned>, string::StringBase, string::StringBase> { public: - BoyerMooreHorspool() {} - /** * Search for pattern in linear string. * @return set set of occurences @@ -30,12 +30,11 @@ public: static std::set<unsigned> match(const string::String& subject, const string::String& pattern); static std::set<unsigned> match(const string::LinearString& subject, const string::LinearString& pattern); -private: - void Visit(void*, const string::Epsilon& subject, const string::Epsilon& pattern) const; - void Visit(void*, const string::LinearString& subject, const string::LinearString& pattern) const; - void Visit(void*, const string::CyclicString& subject, const string::CyclicString& pattern) const; - static const BoyerMooreHorspool BOYER_MOORE_HORSPOOL; + static BoyerMooreHorspool& getInstance() { + static BoyerMooreHorspool res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactFactorAutomaton.cpp b/alib2algo/src/stringology/exact/ExactFactorAutomaton.cpp index 8d20f302fc3f5254e1270796a4b0f3de8e343169..1f5c4fdadb7c50da842618e06dda956dc436c827 100644 --- a/alib2algo/src/stringology/exact/ExactFactorAutomaton.cpp +++ b/alib2algo/src/stringology/exact/ExactFactorAutomaton.cpp @@ -17,11 +17,7 @@ namespace stringology { namespace exact { automaton::Automaton ExactFactorAutomaton::construct(const string::String& text) { - automaton::Automaton* out = NULL; - text.getData().Accept((void*) &out, ExactFactorAutomaton::EXACT_FACTOR_AUTOMATON); - automaton::Automaton res = std::move(*out); - delete out; - return res; + return getInstance().dispatch(text.getData()); } automaton::EpsilonNFA ExactFactorAutomaton::construct(const string::LinearString& text) { @@ -39,21 +35,7 @@ automaton::EpsilonNFA ExactFactorAutomaton::construct(const string::LinearString return res; } -void ExactFactorAutomaton::Visit(void* data, const string::Epsilon& text) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(string::LinearString(text))); -} - -void ExactFactorAutomaton::Visit(void* data, const string::LinearString& text) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(text)); -} - -void ExactFactorAutomaton::Visit(void*, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const ExactFactorAutomaton ExactFactorAutomaton::EXACT_FACTOR_AUTOMATON; +auto ExactFactorAutomatonLinearString = ExactFactorAutomaton::RegistratorWrapper<automaton::EpsilonNFA, string::LinearString>(ExactFactorAutomaton::getInstance(), ExactFactorAutomaton::construct); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactFactorAutomaton.h b/alib2algo/src/stringology/exact/ExactFactorAutomaton.h index d5549a32e8486610e8481e7085b71af1678fe0cb..6eba9e96d653d29eab2ac786bdd3eff190467d08 100644 --- a/alib2algo/src/stringology/exact/ExactFactorAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactFactorAutomaton.h @@ -12,15 +12,14 @@ #include <automaton/FSM/EpsilonNFA.h> #include <string/LinearString.h> #include <string/String.h> +#include <common/multipleDispatch.hpp> namespace stringology { namespace exact { -class ExactFactorAutomaton : public string::VisitableStringBase::const_visitor_type { +class ExactFactorAutomaton : public std::SingleDispatch<automaton::Automaton, string::StringBase> { public: - ExactFactorAutomaton() {} - /** * Performs conversion. * @return left regular grammar equivalent to source automaton. @@ -28,12 +27,11 @@ public: static automaton::Automaton construct(const string::String& text); static automaton::EpsilonNFA construct(const string::LinearString& text); -private: - void Visit(void*, const string::Epsilon& text) const; - void Visit(void*, const string::LinearString& text) const; - void Visit(void*, const string::CyclicString& text) const; - static const ExactFactorAutomaton EXACT_FACTOR_AUTOMATON; + static ExactFactorAutomaton& getInstance() { + static ExactFactorAutomaton res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactFactorMatch.cpp b/alib2algo/src/stringology/exact/ExactFactorMatch.cpp index fe388a234ba7f2c4f6243972497ceedec7ccf5d6..2c79217bdfb8196218fcf261b6090d6884617a8e 100644 --- a/alib2algo/src/stringology/exact/ExactFactorMatch.cpp +++ b/alib2algo/src/stringology/exact/ExactFactorMatch.cpp @@ -16,9 +16,7 @@ namespace stringology { namespace exact { std::set<unsigned> ExactFactorMatch::match(const string::String& subject, const string::String& pattern) { - std::set<unsigned> data; - Accept((void*) &data, subject.getData(), pattern.getData(), ExactFactorMatch::EXACT_FACTOR_MATCH); - return data; + return getInstance().dispatch(subject.getData(), pattern.getData()); } std::set<unsigned> ExactFactorMatch::match(const string::LinearString& subject, const string::LinearString& pattern) { @@ -35,20 +33,7 @@ std::set<unsigned> ExactFactorMatch::match(const string::LinearString& subject, return occ; } -void ExactFactorMatch::Visit(void*, const string::Epsilon&, const string::Epsilon&) const { - throw exception::AlibException("Unsupported string type Epsilon"); -} - -void ExactFactorMatch::Visit(void* data, const string::LinearString& subject, const string::LinearString& pattern) const { - std::set<unsigned> & res = *((std::set<unsigned>*) data); - res = this->match(subject, pattern); -} - -void ExactFactorMatch::Visit(void*, const string::CyclicString&, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const ExactFactorMatch ExactFactorMatch::EXACT_FACTOR_MATCH; +auto ExactFactorMatchLinearStringLinearString = ExactFactorMatch::RegistratorWrapper<std::set<unsigned>, string::LinearString, string::LinearString>(ExactFactorMatch::getInstance(), ExactFactorMatch::match); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactFactorMatch.h b/alib2algo/src/stringology/exact/ExactFactorMatch.h index 99ff150395b46e6ec9a3b5ea42c5df798531ea66..451ac76e3d277d617290a491054724d3161aac02 100644 --- a/alib2algo/src/stringology/exact/ExactFactorMatch.h +++ b/alib2algo/src/stringology/exact/ExactFactorMatch.h @@ -10,15 +10,15 @@ #include <string/String.h> #include <set> +#include <common/multipleDispatch.hpp> +#include <string/StringFeatures.h> namespace stringology { namespace exact { -class ExactFactorMatch : public string::VisitableStringBase::const_promoting_visitor_type { +class ExactFactorMatch : public std::DoubleDispatch<std::set<unsigned>, string::StringBase, string::StringBase> { public: - ExactFactorMatch() {} - /** * Performs conversion. * @return left regular grammar equivalent to source automaton. @@ -26,12 +26,11 @@ public: static std::set<unsigned> match(const string::String& subject, const string::String& pattern); static std::set<unsigned> match(const string::LinearString& subject, const string::LinearString& pattern); -private: - void Visit(void*, const string::Epsilon& subject, const string::Epsilon& pattern) const; - void Visit(void*, const string::LinearString& subject, const string::LinearString& pattern) const; - void Visit(void*, const string::CyclicString& subject, const string::CyclicString& pattern) const; - static const ExactFactorMatch EXACT_FACTOR_MATCH; + static ExactFactorMatch& getInstance() { + static ExactFactorMatch res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp index d78995f66f3da7da6b978bcd330caed2186398e7..24f8cd2d0a572569068b4408ceb1b20a94e1a0da 100644 --- a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp +++ b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp @@ -17,11 +17,7 @@ namespace stringology { namespace exact { automaton::Automaton ExactMatchingAutomaton::construct(const string::String& pattern) { - automaton::Automaton* out = NULL; - pattern.getData().Accept((void*) &out, ExactMatchingAutomaton::EXACT_MATCHING_AUTOMATON); - automaton::Automaton res = std::move(*out); - delete out; - return res; + return getInstance().dispatch(pattern.getData()); } automaton::NFA ExactMatchingAutomaton::construct(const string::LinearString& pattern) { @@ -40,20 +36,7 @@ automaton::NFA ExactMatchingAutomaton::construct(const string::LinearString& pat return res; } -void ExactMatchingAutomaton::Visit(void*, const string::Epsilon&) const { - throw exception::AlibException("Unsupported string type Epsilon"); -} - -void ExactMatchingAutomaton::Visit(void* data, const string::LinearString& pattern) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(pattern)); -} - -void ExactMatchingAutomaton::Visit(void*, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const ExactMatchingAutomaton ExactMatchingAutomaton::EXACT_MATCHING_AUTOMATON; +auto ExactMatchingAutomatonLinearString = ExactMatchingAutomaton::RegistratorWrapper<automaton::NFA, string::LinearString>(ExactMatchingAutomaton::getInstance(), ExactMatchingAutomaton::construct); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h index d2fc0ef8c1a81c77484bac60a802960d370c2b40..27c13ab70c0815189c239a1f6e6cb5756fa17149 100644 --- a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h @@ -12,15 +12,14 @@ #include <automaton/FSM/NFA.h> #include <string/LinearString.h> #include <string/String.h> +#include <common/multipleDispatch.hpp> namespace stringology { namespace exact { -class ExactMatchingAutomaton : public string::VisitableStringBase::const_visitor_type { +class ExactMatchingAutomaton : public std::SingleDispatch<automaton::Automaton, string::StringBase> { public: - ExactMatchingAutomaton() {} - /** * Performs conversion. * @return left regular grammar equivalent to source automaton. @@ -28,12 +27,11 @@ public: static automaton::Automaton construct(const string::String& pattern); static automaton::NFA construct(const string::LinearString& pattern); -private: - void Visit(void*, const string::Epsilon& pattern) const; - void Visit(void*, const string::LinearString& pattern) const; - void Visit(void*, const string::CyclicString& pattern) const; - static const ExactMatchingAutomaton EXACT_MATCHING_AUTOMATON; + static ExactMatchingAutomaton& getInstance() { + static ExactMatchingAutomaton res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h b/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h index 17b021b8daac486be65cfb31261682d6d030d4f0..eec9e5c0d47edfe22ec4ffd35ad1655b02b958b6 100644 --- a/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h @@ -19,8 +19,6 @@ namespace exact { class ExactMultiNondeterministicSubsequenceAutomaton { public: - ExactMultiNondeterministicSubsequenceAutomaton() {} - /** * Performs conversion. * @return left regular grammar equivalent to source automaton. diff --git a/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.cpp b/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.cpp index 0298e68b400538f3f3f77aef27aef28e085e6dcd..5cf4becc78a3a94a715959d7de74232fe3770c00 100644 --- a/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.cpp +++ b/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.cpp @@ -17,11 +17,7 @@ namespace stringology { namespace exact { automaton::Automaton ExactNondeterministicSubsequenceAutomaton::construct(const string::String& text) { - automaton::Automaton* out = NULL; - text.getData().Accept((void*) &out, ExactNondeterministicSubsequenceAutomaton::EXACT_NONDETERMINISTIC_SUBSEQUENCE_AUTOMATON); - automaton::Automaton res = std::move(*out); - delete out; - return res; + return getInstance().dispatch(text.getData()); } automaton::EpsilonNFA ExactNondeterministicSubsequenceAutomaton::construct(const string::LinearString& text) { @@ -41,21 +37,7 @@ automaton::EpsilonNFA ExactNondeterministicSubsequenceAutomaton::construct(const return res; } -void ExactNondeterministicSubsequenceAutomaton::Visit(void* data, const string::Epsilon& text) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(string::LinearString(text))); -} - -void ExactNondeterministicSubsequenceAutomaton::Visit(void* data, const string::LinearString& text) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(text)); -} - -void ExactNondeterministicSubsequenceAutomaton::Visit(void*, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const ExactNondeterministicSubsequenceAutomaton ExactNondeterministicSubsequenceAutomaton::EXACT_NONDETERMINISTIC_SUBSEQUENCE_AUTOMATON; +auto ExactNondeterministicSubsequenceAutomatonLinearString = ExactNondeterministicSubsequenceAutomaton::RegistratorWrapper<automaton::EpsilonNFA, string::LinearString>(ExactNondeterministicSubsequenceAutomaton::getInstance(), ExactNondeterministicSubsequenceAutomaton::construct); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.h b/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.h index 40fccb82eb7a50cdfe5b50d7ee9b379d980b36f1..6a360c4b7cb79746bd8d7600eeab61b9b788c9e3 100644 --- a/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactNondeterministicSubsequenceAutomaton.h @@ -12,15 +12,14 @@ #include <automaton/FSM/EpsilonNFA.h> #include <string/LinearString.h> #include <string/String.h> +#include <common/multipleDispatch.hpp> namespace stringology { namespace exact { -class ExactNondeterministicSubsequenceAutomaton : public string::VisitableStringBase::const_visitor_type { +class ExactNondeterministicSubsequenceAutomaton : public std::SingleDispatch<automaton::Automaton, string::StringBase> { public: - ExactNondeterministicSubsequenceAutomaton() {} - /** * Performs conversion. * @return left regular grammar equivalent to source automaton. @@ -28,12 +27,11 @@ public: static automaton::Automaton construct(const string::String& text); static automaton::EpsilonNFA construct(const string::LinearString& text); -private: - void Visit(void*, const string::Epsilon& text) const; - void Visit(void*, const string::LinearString& text) const; - void Visit(void*, const string::CyclicString& text) const; - static const ExactNondeterministicSubsequenceAutomaton EXACT_NONDETERMINISTIC_SUBSEQUENCE_AUTOMATON; + static ExactNondeterministicSubsequenceAutomaton& getInstance() { + static ExactNondeterministicSubsequenceAutomaton res; + return res; + } }; } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.cpp b/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.cpp index 977c1320faf944613ddee85b188206e28fae84c4..e744d799218216f75fe13974cd325f2ee6981d95 100644 --- a/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.cpp +++ b/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.cpp @@ -17,11 +17,7 @@ namespace stringology { namespace exact { automaton::Automaton ExactSubsequenceAutomaton::construct(const string::String& text) { - automaton::Automaton* out = NULL; - text.getData().Accept((void*) &out, ExactSubsequenceAutomaton::EXACT_SUBSEQUENCE_AUTOMATON); - automaton::Automaton res = std::move(*out); - delete out; - return res; + return getInstance().dispatch(text.getData()); } automaton::DFA ExactSubsequenceAutomaton::construct(const string::LinearString& text) { @@ -48,21 +44,7 @@ automaton::DFA ExactSubsequenceAutomaton::construct(const string::LinearString& return res; } -void ExactSubsequenceAutomaton::Visit(void* data, const string::Epsilon& text) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(string::LinearString(text))); -} - -void ExactSubsequenceAutomaton::Visit(void* data, const string::LinearString& text) const { - automaton::Automaton* & out = *((automaton::Automaton**) data); - out = new automaton::Automaton(this->construct(text)); -} - -void ExactSubsequenceAutomaton::Visit(void*, const string::CyclicString&) const { - throw exception::AlibException("Unsupported string type CyclicString"); -} - -const ExactSubsequenceAutomaton ExactSubsequenceAutomaton::EXACT_SUBSEQUENCE_AUTOMATON; +auto ExactSubsequenceAutomatonLinearString = ExactSubsequenceAutomaton::RegistratorWrapper<automaton::DFA, string::LinearString>(ExactSubsequenceAutomaton::getInstance(), ExactSubsequenceAutomaton::construct); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.h b/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.h index 0da0b1d86be452109d551536f32465b2f2971466..b9155279fdd56b59751f9fded93caac1f54b800f 100644 --- a/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactSubsequenceAutomaton.h @@ -12,15 +12,14 @@ #include <automaton/FSM/DFA.h> #include <string/LinearString.h> #include <string/String.h> +#include <common/multipleDispatch.hpp> namespace stringology { namespace exact { -class ExactSubsequenceAutomaton : public string::VisitableStringBase::const_visitor_type { +class ExactSubsequenceAutomaton : public std::SingleDispatch<automaton::Automaton, string::StringBase> { public: - ExactSubsequenceAutomaton() {} - /** * Performs conversion. * @return left regular grammar equivalent to source automaton. @@ -28,12 +27,11 @@ public: static automaton::Automaton construct(const string::String& text); static automaton::DFA construct(const string::LinearString& text); -private: - void Visit(void*, const string::Epsilon& text) const; - void Visit(void*, const string::LinearString& text) const; - void Visit(void*, const string::CyclicString& text) const; - static const ExactSubsequenceAutomaton EXACT_SUBSEQUENCE_AUTOMATON; + static ExactSubsequenceAutomaton& getInstance() { + static ExactSubsequenceAutomaton res; + return res; + } }; } /* namespace exact */