From a0b71e5757767fd57c402082e78fd4557a29428f Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 30 Aug 2018 16:33:30 +0200 Subject: [PATCH] implementation of Lyndon factorization --- .../properties/LyndonFactoring.cpp | 19 ++++++ .../stringology/properties/LyndonFactoring.h | 59 +++++++++++++++++++ alib2common/src/PrimitiveRegistrator.cpp | 2 + 3 files changed, 80 insertions(+) create mode 100644 alib2algo/src/stringology/properties/LyndonFactoring.cpp create mode 100644 alib2algo/src/stringology/properties/LyndonFactoring.h diff --git a/alib2algo/src/stringology/properties/LyndonFactoring.cpp b/alib2algo/src/stringology/properties/LyndonFactoring.cpp new file mode 100644 index 0000000000..5bc16d18d5 --- /dev/null +++ b/alib2algo/src/stringology/properties/LyndonFactoring.cpp @@ -0,0 +1,19 @@ +/* + * LyndonFactoring.cpp + * + * Created on: 30. 8. 2018 + * Author: Jan Travnicek + */ + +#include "LyndonFactoring.h" +#include <registration/AlgoRegistration.hpp> + +namespace stringology { + +namespace properties { + +auto lyndonFactoringString = registration::AbstractRegister < LyndonFactoring, ext::vector < unsigned >, const string::LinearString < > & > ( LyndonFactoring::factorize ); + +} /* namespace properties */ + +} /* namespace stringology */ diff --git a/alib2algo/src/stringology/properties/LyndonFactoring.h b/alib2algo/src/stringology/properties/LyndonFactoring.h new file mode 100644 index 0000000000..4bedf407d3 --- /dev/null +++ b/alib2algo/src/stringology/properties/LyndonFactoring.h @@ -0,0 +1,59 @@ +/* + * LyndonFactoring.h + * + * Created on: 30. 8. 2018 + * Author: Jan Travnicek + * + * Based on code from https://cp-algorithms.com/string/lyndon_factorization.html + */ + +#ifndef LYNDON_FACTORING_H_ +#define LYNDON_FACTORING_H_ + +#include <string/LinearString.h> + +namespace stringology { + +namespace properties { + +class LyndonFactoring { +public: + /** + * Computes length of backbone for a suffix/factor/oracle automaton + * @param automaton the suffix/factor/oracle automaton + * @return backbone length + */ + template < class SymbolType > + static ext::vector < unsigned > factorize ( const string::LinearString < SymbolType > & string ); + +}; + +template < class SymbolType > +ext::vector < unsigned > LyndonFactoring::factorize ( const string::LinearString < SymbolType > & string ) { + int n = string.getContent ( ).size ( ); + int i = 0; + ext::vector < unsigned > factorization; + + while ( i < n ) { + int j = i + 1, k = i; + while ( j < n && string.getContent ( ) [ k ] <= string.getContent ( ) [ j ] ) { + if ( string.getContent ( ) [ k ] < string.getContent ( ) [ j ] ) + k = i; + else + k ++; + j ++; + } + while ( i <= k ) { + factorization.push_back ( i ); + i += j - k; + } + } + + return factorization; +} + +} /* namespace properties */ + +} /* namespace stringology */ + +#endif /* LYNDON_FACTORING_H_ */ diff --git a/alib2common/src/PrimitiveRegistrator.cpp b/alib2common/src/PrimitiveRegistrator.cpp index 5cf0e2545a..fb4ed48fd4 100644 --- a/alib2common/src/PrimitiveRegistrator.cpp +++ b/alib2common/src/PrimitiveRegistrator.cpp @@ -62,7 +62,9 @@ public: abstraction::ValuePrinterRegistry::registerValuePrinter < ext::set < unsigned > > ( ); abstraction::ValuePrinterRegistry::registerValuePrinter < ext::vector < object::Object > > ( ); + abstraction::ValuePrinterRegistry::registerValuePrinter < ext::vector < unsigned > > ( ); abstraction::ValuePrinterRegistry::registerValuePrinter < ext::vector < ext::pair < object::Object, object::Object > > > ( ); + abstraction::ValuePrinterRegistry::registerValuePrinter < ext::pair < ext::vector < object::Object >, double > > ( ); abstraction::ValuePrinterRegistry::registerValuePrinter < ext::pair < ext::vector < object::Object >, int > > ( ); abstraction::ValuePrinterRegistry::registerValuePrinter < ext::pair < ext::vector < object::Object >, long > > ( ); -- GitLab