Skip to content
Snippets Groups Projects
Commit e386c54e authored by Jan Jirák's avatar Jan Jirák Committed by Jan Trávníček
Browse files

Not-so-naive implemented

parent 8ab1615b
No related branches found
No related tags found
1 merge request!153Jirakjan bp rebase
This commit is part of merge request !153. Comments created here will be created in the context of that merge request.
/*
* NotSoNaive.cpp
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#include "NotSoNaive.h"
#include <registration/AlgoRegistration.hpp>
namespace {
auto NotSoNaive = registration::AbstractRegister < stringology::exact::NotSoNaive, ext::set < unsigned >, const string::LinearString < > &, const string::LinearString < > & > ( stringology::exact::NotSoNaive::match );
} /* namespace */
/*
* NotSoNaive.h
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#ifndef _STRINGOLOGY_NOT_SO_NAIVE_H_
#define _STRINGOLOGY_NOT_SO_NAIVE_H_
#include <alib/measure>
#include <alib/set>
#include <alib/vector>
#include <string/LinearString.h>
namespace stringology {
namespace exact {
/**
* Implementation of Not-So-Naive algorithm
*/
class NotSoNaive{
public:
/**
* Search for pattern in linear string.
* @return set set of occurences
*/
template < class SymbolType >
static ext::set < unsigned > match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern );
};
template < class SymbolType >
ext::set < unsigned > NotSoNaive::match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) {
ext::set<unsigned> occ;
// if pattern is one char, skip the rest and use naive approach
if (pattern.getContent().size() == 1) {
for (unsigned i = 0; i < subject.getContent().size(); ++i) {
if (subject.getContent()[i] == pattern.getContent()[0]) occ.insert(i);
}
return occ;
}
if (pattern.getContent()[0] != pattern.getContent()[1]) {
for (unsigned i = 0; i + pattern.getContent().size() <= subject.getContent().size(); ++i) {
bool match = true ;
// try to match the whole string, in case of mathing fist two characters you can schift by 2
for (unsigned j = 0; j < pattern.getContent().size(); ++j) {
if (subject.getContent()[i + j] != pattern.getContent()[j]) {
match = false ;
if (j > 1) {
++i;
break;
} else break;
}
}
if ( match ) occ.insert(i);
}
} else {
for (unsigned i = 0; i + pattern.getContent().size() <= subject.getContent().size(); ++i) {
bool match = true ;
// try to match the whole string, if pattern[0] matches and pattern[1] not, then advance by 2
for (unsigned j = 0; j < pattern.getContent().size(); ++j) {
if (subject.getContent()[i + j] != pattern.getContent()[j]) {
match = false ;
if (j == 1) {
++i;
break;
} else break;
}
}
if ( match ) occ.insert(i);
}
}
return occ;
}
} /* namespace exact */
} /* namespace stringology */
#endif /* _STRINGOLOGY_NOT_SO_NAIVE_H_ */
......@@ -25,6 +25,7 @@ static std::string qGenString ( size_t min_len, size_t max_len, size_t alph_len,
 
TEST_CASE ( "ExactMatching", "[integration]" ) {
auto definition = GENERATE ( as < std::tuple < std::string, std::string, bool > > ( ),
std::make_tuple ( "Exact Not So Naive", "stringology::exact::NotSoNaive $subject $pattern", false ),
std::make_tuple ( "Exact Boyer Moore", "stringology::exact::BoyerMoore $subject $pattern", true ),
std::make_tuple ( "Exact Knuth Morris Pratt", "stringology::exact::KnuthMorrisPratt $subject $pattern", false ),
std::make_tuple ( "Exact Boyer Moore Horspool", " stringology::exact::BoyerMooreHorspool $subject $pattern", true ),
......
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