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

Repetition

parent a8d41420
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.
/*
* Repetition.cpp
*
* Created on: 27.3.2020
* Author: Jan Jirak
*/
#include "Repetition.h"
#include <registration/AlgoRegistration.hpp>
namespace {
// todo Note is it necessary to register
auto Repetition = registration::AbstractRegister < string::properties::Repetition, ext::tuple < size_t, size_t, size_t >, const string::LinearString < > & > ( string::properties::Repetition::construct );
} /* namespace */
/*
* Repetition.h
*
* Created on: 27.3.2020
* Author: Jan Jirak
*/
#ifndef _REPETITION_H_
#define _REPETITION_H_
#include <alib/vector>
#include <string/LinearString.h>
namespace string {
namespace properties {
class Repetition {
public:
/**
* Computes maximal repetition in a string
* @param string to compute repetition for
* @return Tuple of three values: repetitions first position in the string, repetitions second position in the string, length of the repetition,
* in case when no repetition is present return tuple (0, 0, 0)
*/
template < class SymbolType >
static ext::tuple<size_t, size_t, size_t> construct(const string::LinearString < SymbolType >& string);
};
template < class SymbolType >
ext::tuple<size_t, size_t, size_t> Repetition::construct(const string::LinearString < SymbolType >& string) {
const auto& x = string.getContent();
size_t maxlen = 0 , first = 0 , second = 0 ;
for ( size_t i = 0 ; i < x.size() ; ++ i ) {
for ( size_t j = i + 1 ; j < x.size() ; ++ j ) {
size_t k = 0 ;
while ( i + k < j && j + k < x.size() && x[i + k] == x[j + k]) ++ k ;
if ( k > maxlen ) {
maxlen = k ;
first = i ;
second = j ;
}
}
}
return ext::make_tuple(maxlen, first, second) ;
}
} /* namespace properties */
} /* namespace string */
#endif /* _REPETITION_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