Skip to content
Snippets Groups Projects
Commit 477ab52d authored by Jan Trávníček's avatar Jan Trávníček
Browse files

naive exact matching

parent 25145593
No related branches found
No related tags found
No related merge requests found
/*
* 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 */
/*
* 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__ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment