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

TailedSustring algorithm

parent 4d513d73
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.
/*
* TailedSubstring.cpp
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#include "TailedSubstring.h"
#include <registration/AlgoRegistration.hpp>
namespace {
auto TailedSubstring = registration::AbstractRegister < stringology::exact::TailedSubstring, ext::set < unsigned >, const string::LinearString < > &, const string::LinearString < > & > ( stringology::exact::TailedSubstring::match );
} /* namespace */
/*
* TailedSubstring.h
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#ifndef _STRINGOLOGY_TAILED_SUBSTRING_H_
#define _STRINGOLOGY_TAILED_SUBSTRING_H_
#include <alib/measure>
#include <alib/set>
#include <alib/vector>
#include <string/LinearString.h>
namespace stringology {
namespace exact {
/**
* Implementation of the TailedSubstring algorithm from article “ IT’S ECONOMY, STUPID! ” : SEARCHING FOR A SUBSTRING
* WITH CONSTANT EXTRA SPACE COMPLEXITY
* Domenico Cantone and Simone Faro
*/
class TailedSubstring{
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 > TailedSubstring::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();
long int n = text.size(), m = pat.size();
long int s = 0;
long int delta = 1;
long int i , k;
i = k = m - 1;
// Phase 1
while ( s <= n - m && i - delta >= 0 ) {
if ( pat[i] != text[s + i] ) s = s + 1;
else {
long int j = 0;
while ( j < m && pat[j] == text[s + j] ) ++j;
if ( j == m ) occ.insert(s);
long int h = i - 1;
while ( h >= 0 && pat[h] != pat[i] ) --h;
if ( delta < i - h ){
delta = i - h;
k = i;
}
s = s + i - h;
i = i - 1;
}
}
// Phase 2
while ( s <= n - m ){
if ( pat[k] != text[s+k] ) ++ s;
else {
long int j = 0 ;
while ( j < m && pat[j] == text[s+j]) ++j;
if ( j == m ) occ.insert(s);
s += delta;
}
}
return occ;
}
} /* namespace exact */
} /* namespace stringology */
#endif /* _STRINGOLOGY_TAILED_SUBSTRING_H_ */
......@@ -26,6 +26,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 Tailed Substring", "stringology::exact::TailedSubstring $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