From 477ab52d155a263dabb5ec3b1fb6cf32bcefc143 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 27 Oct 2014 22:30:50 +0100 Subject: [PATCH] naive exact matching --- alib2algo/src/string/naive/ExactMatch.cpp | 56 +++++++++++++++++++++++ alib2algo/src/string/naive/ExactMatch.h | 39 ++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 alib2algo/src/string/naive/ExactMatch.cpp create mode 100644 alib2algo/src/string/naive/ExactMatch.h diff --git a/alib2algo/src/string/naive/ExactMatch.cpp b/alib2algo/src/string/naive/ExactMatch.cpp new file mode 100644 index 0000000000..d3f28bb6c5 --- /dev/null +++ b/alib2algo/src/string/naive/ExactMatch.cpp @@ -0,0 +1,56 @@ +/* + * ExactMatch.cpp + * + * Created on: 9. 2. 2014 + * Author: Jan Travnicek + */ + +#include "ExactMatch.h" +#include <exception/AlibException.h> +#include <string/LinearString.h> +#include <string/Epsilon.h> + +#include <deque> + +namespace string { + +namespace match { + +std::set<unsigned> ExactMatch::match(const string::String& subject, const string::String& pattern) { + std::set<unsigned> data; + Accept((void*) &data, subject.getData(), pattern.getData(), ExactMatch::EXACT_MATCH); + return data; +} + +std::set<unsigned> ExactMatch::match(const string::LinearString& subject, const string::LinearString& pattern) { + std::set<unsigned> occ; + for(unsigned i = 0; i < subject.getContent().size() - pattern.getContent().size(); i++) { + unsigned j = 0; + for(; j < pattern.getContent().size(); j++) { + if(pattern.getContent()[j] != subject.getContent()[i+j]) break; + } + + if( j == pattern.getContent().size() ) + occ.insert(i); + } + return occ; +} + +void ExactMatch::Visit(void*, const string::Epsilon&, const string::Epsilon&) const { + throw exception::AlibException("Unsupported string type Epsilon"); +} + +void ExactMatch::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 ExactMatch::Visit(void*, const string::CyclicString&, const string::CyclicString&) const { + throw exception::AlibException("Unsupported string type CyclicString"); +} + +const ExactMatch ExactMatch::EXACT_MATCH; + +} /* namespace match */ + +} /* namespace string */ diff --git a/alib2algo/src/string/naive/ExactMatch.h b/alib2algo/src/string/naive/ExactMatch.h new file mode 100644 index 0000000000..f51ffca71a --- /dev/null +++ b/alib2algo/src/string/naive/ExactMatch.h @@ -0,0 +1,39 @@ +/* + * ExactMatch.h + * + * Created on: 9. 2. 2014 + * Author: Jan Travnicek + */ + +#ifndef _EXACT_MATCH_H__ +#define _EXACT_MATCH_H__ + +#include <string/LinearString.h> +#include <string/String.h> + +namespace string { + +namespace match { + +class ExactMatch : public string::VisitableStringBase::const_same_visitor_type { +public: + /** + * Performs conversion. + * @return left regular grammar equivalent to source automaton. + */ + 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 Epsilon& subject, const Epsilon& pattern) const; + void Visit(void*, const LinearString& subject, const LinearString& pattern) const; + void Visit(void*, const CyclicString& subject, const CyclicString& pattern) const; + + static const ExactMatch EXACT_MATCH; +}; + +} /* namespace match */ + +} /* namespace string */ + +#endif /* _EXACT_MATCH_H__ */ -- GitLab