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

Quite Naive algorithm

parent 70353513
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.
/*
* QuiteNaive.cpp
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#include "QuiteNaive.h"
#include <registration/AlgoRegistration.hpp>
namespace {
auto QuiteNaive = registration::AbstractRegister < stringology::exact::QuiteNaive, ext::set < unsigned >, const string::LinearString < > &, const string::LinearString < > & > ( stringology::exact::QuiteNaive::match );
} /* namespace */
/*
* QuiteNaive.h
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#ifndef _STRINGOLOGY_QUITE_NAIVE_H_
#define _STRINGOLOGY_QUITE_NAIVE_H_
#include <alib/measure>
#include <alib/set>
#include <alib/vector>
#include <string/LinearString.h>
namespace stringology {
namespace exact {
/**
* Implementation of the QuiteNaive algorithm from article “ IT’S ECONOMY, STUPID! ” : SEARCHING FOR A SUBSTRING
* WITH CONSTANT EXTRA SPACE COMPLEXITY
* Domenico Cantone and Simone Faro
*/
class QuiteNaive{
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 > QuiteNaive::match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) {
ext::set<unsigned> occ;
const auto & text = subject.getContent();
const auto & pat = pattern.getContent();
size_t n = text.size(), m = pat.size();
// Preprocessing
size_t gamma = 1 , delta = 1 ;
while ( delta < m && pat[m-1] != pat[m-1-delta]) ++ delta;
while ( gamma < m && pat[m-1] == pat[m-1-gamma]) ++ gamma;
// Searching
size_t s = 0;
while ( s <= n - m ){
if ( pat[m-1] != text[s+m-1] ) s += gamma;
else {
ssize_t j = m - 2;
while ( j >= 0 && pat[j] == text[s+j] ) -- j;
if ( j < 0 ) occ.insert(s);
s += delta;
}
}
return occ;
}
} /* namespace exact */
} /* namespace stringology */
#endif /* _STRINGOLOGY_QUITE_NAIVE_H_ */
......@@ -27,6 +27,7 @@ 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 Tailed Substring", "stringology::exact::TailedSubstring $subject $pattern", false ),
std::make_tuple ( "Exact Quite Naive", "stringology::exact::QuiteNaive $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