diff --git a/alib2algo/src/stringology/properties/NyldonFactoring.cpp b/alib2algo/src/stringology/properties/NyldonFactoring.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2cff51a625738604077b1af00ef4aa9dd470bd5 --- /dev/null +++ b/alib2algo/src/stringology/properties/NyldonFactoring.cpp @@ -0,0 +1,19 @@ +/* + * 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 */ diff --git a/alib2algo/src/stringology/properties/NyldonFactoring.h b/alib2algo/src/stringology/properties/NyldonFactoring.h new file mode 100644 index 0000000000000000000000000000000000000000..1ab2fe8c929d34f102c289c4b59823893f94a438 --- /dev/null +++ b/alib2algo/src/stringology/properties/NyldonFactoring.h @@ -0,0 +1,63 @@ +/* + * 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_ */