Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PeriodicPrefix.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/vector>
9#include <string/LinearString.h>
10
11namespace string {
12
13namespace properties {
14
16public:
23 template < class SymbolType >
25};
26
27namespace {
28 // return period if pattern is periodic, else -1
29 template <class SymbolType>
30 ssize_t hasShortPeriod ( const ext::vector<SymbolType>& x , size_t prefixLen ) {
31 for ( size_t per = 1 ; per <= prefixLen/2 ; ++ per ){
32 bool hasPer = true ;
33 for ( size_t i = per ; i < prefixLen ; ++ i ) {
34 if ( x[i] != x[i - per] ){ hasPer = false ; break ; }
35 }
36 if ( hasPer ) return per ;
37 }
38 return -1 ;
39 }
40}
41
42template < class SymbolType >
44 const auto& x = string.getContent();
45
46 ssize_t maxlen = -1 , maxper = -1 ;
47 for ( size_t i = 1 ; i <= x.size() ; ++ i ) {
48 auto per = hasShortPeriod(x , i) ;
49 if ( per != -1 ) {
50 maxlen = i ;
51 maxper = per ;
52 }
53 }
54
55
56
57 return ext::make_pair( maxlen, maxper) ;
58}
59
60
61} /* namespace properties */
62
63} /* namespace string */
64
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Linear string.
Definition: LinearString.h:57
Definition: PeriodicPrefix.h:15
static ext::pair< ssize_t, ssize_t > construct(const string::LinearString< SymbolType > &string)
Definition: PeriodicPrefix.h:43
int i
Definition: AllEpsilonClosure.h:118
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: RandomStringFactory.cpp:12