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

add Nyldon factoring algorithm

parent d4cd6143
No related branches found
No related tags found
1 merge request!102Merge jt
/*
* NyldonFactoring.cpp
*
* Created on: 30. 8. 2018
* Author: Jan Travnicek
*/
#include "NyldonFactoring.h"
#include <registration/AlgoRegistration.hpp>
namespace {
auto nyldonFactoringString = registration::AbstractRegister < stringology::properties::NyldonFactoring, ext::vector < unsigned >, const string::LinearString < > & > ( stringology::properties::NyldonFactoring::factorize ).setDocumentation (
"Computes the nyldon factoring of a given nonempty string\n\
\n\
@param string the nonempty string to factorize\n\
@return positions where the string is split to nyldon factors" );
} /* namespace */
/*
* NyldonFactoring.h
*
* Created on: 29. 8. 2019
* Author: Jan Travnicek
*
* Based on code from arXiv:1804.09735
*/
#ifndef NYLDON_FACTORING_H_
#define NYLDON_FACTORING_H_
#include <string/LinearString.h>
#include <alib/deque>
namespace stringology {
namespace properties {
class NyldonFactoring {
public:
/**
* Computes the nyldon factoring of a given nonempty string.
*
* @param string the nonempty string to factorize
* @return positions where the string is split to nyldon factors
*/
template < class SymbolType >
static ext::vector < unsigned > factorize ( const string::LinearString < SymbolType > & string );
};
template < class SymbolType >
ext::vector < unsigned > NyldonFactoring::factorize ( const string::LinearString < SymbolType > & string ) {
unsigned n = string.getContent ( ).size ( );
ext::deque < ext::vector < SymbolType > > NyIF { ext::vector < SymbolType > { string.getContent ( ) [ n - 1 ] } };
for ( unsigned i = 2; i <= n; ++ i ) {
NyIF.push_front ( ext::vector < SymbolType > { string.getContent ( ) [ n - i ] } );
while ( NyIF.size ( ) >= 2 && NyIF [ 0 ] > NyIF [ 1 ] ) {
ext::vector < SymbolType > tmp = std::move ( NyIF [ 0 ] );
tmp.insert ( tmp.end ( ), NyIF [ 1 ].begin ( ), NyIF [ 1 ].end ( ) );
NyIF.pop_front ( );
NyIF.pop_front ( );
NyIF.push_front ( std::move ( tmp ) );
}
}
ext::vector < unsigned > factorization;
unsigned i = 0;
for ( const ext::vector < SymbolType > factor : NyIF ) {
factorization.push_back ( i );
i += factor.size ( );
}
return factorization;
}
} /* namespace properties */
} /* namespace stringology */
#endif /* NYLDON_FACTORING_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