diff --git a/alib2algo/src/stringology/cover/ApproximateEnhancedCoversCommon.h b/alib2algo/src/stringology/cover/ApproximateEnhancedCoversCommon.h index 67ba764d74c516d0fda260eb4e87cd8b495b6643..33f3128406ffd0243ff27027fa1688f0b3181b4b 100644 --- a/alib2algo/src/stringology/cover/ApproximateEnhancedCoversCommon.h +++ b/alib2algo/src/stringology/cover/ApproximateEnhancedCoversCommon.h @@ -35,6 +35,15 @@ protected: // corresponding factor ext::pair < unsigned, unsigned > lfactor; ext::vector < Element > elements; + + State ( ) = default; + ~State ( ) = default; + + // default move constructor + State ( State && other ) = default; + + // default move assignment operator + State & operator =( State && other ) = default; }; /** @@ -43,13 +52,13 @@ protected: * @param q state of the deterministic k-approximate suffix automaton * @return the number of covered positions */ - static unsigned distEnhCov ( State const * q ) { + static unsigned distEnhCov ( const State & q ) { unsigned r = 0; - unsigned m = q->elements[0].depth; + unsigned m = q.elements[0].depth; - for ( size_t i = 1; i < q->elements.size ( ); ++i ) { - if ( q->elements[i].depth - q->elements[i - 1].depth < m ) - r += q->elements[i].depth - q->elements[i - 1].depth; + for ( size_t i = 1; i < q.elements.size ( ); ++i ) { + if ( q.elements[i].depth - q.elements[i - 1].depth < m ) + r += q.elements[i].depth - q.elements[i - 1].depth; else r += m; } @@ -62,11 +71,11 @@ protected: * set of found (relaxed) k-approximate enhanced covers if necessary. * * @param state - * @param set of lfactors representing all found (relaxed) k-approximate - * enhanced covers + * @param enhCovers set of lfactors representing all found (relaxed) + * k-approximate enhanced covers * @param h the maximum number of covered position */ - static void updateEnhCov ( State const * state, ext::set < ext::pair < unsigned, unsigned > > & enhCovers, unsigned & h ) { + static void updateEnhCov ( const State & state, ext::set < ext::pair < unsigned, unsigned > > & enhCovers, unsigned & h ) { unsigned hNext = distEnhCov ( state ); if ( hNext > h ) { @@ -75,7 +84,7 @@ protected: } if ( hNext == h ) - enhCovers.insert ( state->lfactor ); + enhCovers.insert ( state.lfactor ); } /** @@ -88,17 +97,17 @@ protected: * @return the first state of deterministic k-approximate suffix automaton */ template < class SymbolType > - static State * constrFirstState ( const string::LinearString < SymbolType > & x, unsigned k, const SymbolType & symbol ) { - auto * firstState = new State ( ); + static State constrFirstState ( const string::LinearString < SymbolType > & x, unsigned k, const SymbolType & symbol ) { + State firstState; - firstState->depth = 1; - firstState->lfactor = ext::pair < unsigned, unsigned > ( 1, 1 ); + firstState.depth = 1; + firstState.lfactor = ext::pair < unsigned, unsigned > ( 1, 1 ); for ( size_t i = 0; i < x.getContent ( ).size ( ); ++i ) { if ( symbol == x.getContent ( )[i] ) - firstState->elements.push_back ( Element ( i + 1, 0 ) ); + firstState.elements.push_back ( Element ( i + 1, 0 ) ); else if ( k > 0 ) - firstState->elements.push_back ( Element ( i + 1, 1 ) ); + firstState.elements.push_back ( Element ( i + 1, 1 ) ); } return firstState; @@ -115,19 +124,19 @@ protected: * @return the next state of deterministic k-approximate suffix automaton */ template < class SymbolType > - static State * constrNextState ( const string::LinearString < SymbolType > & x, State const * previousState, unsigned k, const SymbolType & symbol ) { - auto * nextState = new State ( ); + static State constrNextState ( const string::LinearString < SymbolType > & x, const State & previousState, unsigned k, const SymbolType & symbol ) { + State nextState; - nextState->depth = previousState->depth + 1; - nextState->lfactor = previousState->lfactor; + nextState.depth = previousState.depth + 1; + nextState.lfactor = previousState.lfactor; - for ( const auto & element : previousState->elements ) + for ( const auto & element : previousState.elements ) if ( element.depth < x.getContent ( ).size ( ) ) { if ( symbol == x.getContent ( )[element.depth] ) { - nextState->elements.push_back ( Element ( element.depth + 1, element.level ) ); - nextState->lfactor = ext::pair < unsigned, unsigned > ( element.depth + 1, nextState->depth ); + nextState.elements.push_back ( Element ( element.depth + 1, element.level ) ); + nextState.lfactor = ext::pair < unsigned, unsigned > ( element.depth + 1, nextState.depth ); } else if ( element.level < k ) { - nextState->elements.push_back ( Element ( element.depth + 1, element.level + 1 ) ); + nextState.elements.push_back ( Element ( element.depth + 1, element.level + 1 ) ); } } diff --git a/alib2algo/src/stringology/cover/ApproximateEnhancedCoversComputation.h b/alib2algo/src/stringology/cover/ApproximateEnhancedCoversComputation.h index 43e9a54bbdefd523c2eecabf4dd26d667b0d26bd..d51a97d478dedd99cd46fd880de38ce663e31fff 100644 --- a/alib2algo/src/stringology/cover/ApproximateEnhancedCoversComputation.h +++ b/alib2algo/src/stringology/cover/ApproximateEnhancedCoversComputation.h @@ -31,7 +31,7 @@ private: * @return true if state represents border, false otherwise */ template < class SymbolType > - static bool isBorder ( State const * state, const string::LinearString < SymbolType > & x, unsigned k ); + static bool isBorder ( const State & state, const string::LinearString < SymbolType > & x, unsigned k ); }; template < class SymbolType > @@ -48,40 +48,33 @@ ext::set < string::LinearString < SymbolType > > ApproximateEnhancedCoversComput if ( x.getContent ( ).size ( ) < 2 ) return ext::set < string::LinearString < SymbolType > >( ); - State * previousState = constrFirstState ( x, k, x.getContent ( )[0] ); - State * nextState = nullptr; + State previousState = constrFirstState ( x, k, x.getContent ( )[0] ); // check if the first character is a border (makes sense only for k = 0) if ( isBorder ( previousState, x, k ) ) updateEnhCov ( previousState, result, h ); for ( size_t i = 1; i < x.getContent ( ).size ( ); ++i ) { - nextState = constrNextState ( x, previousState, k, x.getContent ( )[previousState->depth] ); + State nextState = constrNextState ( x, previousState, k, x.getContent ( )[previousState.depth] ); - if ( nextState->elements.size ( ) < 2 ) { - delete nextState; + if ( nextState.elements.size ( ) < 2 ) break; - } - - delete previousState; if ( isBorder ( nextState, x, k ) ) updateEnhCov ( nextState, result, h ); - previousState = nextState; + previousState = std::move ( nextState ); } - delete previousState; - // in the end the set of actual strings is computed from the set of lfactors return getFactors ( x, result ); } template < class SymbolType > -bool ApproximateEnhancedCoversComputation::isBorder ( State const * state, const string::LinearString < SymbolType > & x, unsigned k ) { - Element lastElement = state->elements[state->elements.size ( ) - 1]; +bool ApproximateEnhancedCoversComputation::isBorder ( const State & state, const string::LinearString < SymbolType > & x, unsigned k ) { + Element lastElement = state.elements[state.elements.size ( ) - 1]; - return ( lastElement.depth == x.getContent ( ).size ( ) ) && ( lastElement.level == 0 ) && ( state->depth > k ); + return ( lastElement.depth == x.getContent ( ).size ( ) ) && ( lastElement.level == 0 ) && ( state.depth > k ); } } /* namespace stringology::cover */ diff --git a/alib2algo/src/stringology/cover/RelaxedApproximateEnhancedCoversComputation.h b/alib2algo/src/stringology/cover/RelaxedApproximateEnhancedCoversComputation.h index 5a8f86668e750463c62a00d4806e1ce889151100..914ab2b99b5f9b0577214223a76ebcf6d75c4f69 100644 --- a/alib2algo/src/stringology/cover/RelaxedApproximateEnhancedCoversComputation.h +++ b/alib2algo/src/stringology/cover/RelaxedApproximateEnhancedCoversComputation.h @@ -43,7 +43,7 @@ private: * @param h the maximum number of covered position */ template < class SymbolType > - static void processState ( const string::LinearString < SymbolType > & x, unsigned k, State const * previousState, ext::set < ext::pair < unsigned, unsigned > > & covers, unsigned & h ); + static void processState ( const string::LinearString < SymbolType > & x, unsigned k, const State & previousState, ext::set < ext::pair < unsigned, unsigned > > & covers, unsigned & h ); }; template < class SymbolType > @@ -57,14 +57,13 @@ ext::set < string::LinearString < SymbolType > > RelaxedApproximateEnhancedCover return ext::set < string::LinearString < SymbolType > >( ); for ( const auto & symbol : x.getAlphabet ( ) ) { - auto * firstState = constrFirstState ( x, k, symbol ); + State firstState = constrFirstState ( x, k, symbol ); // check if the first character is a border (makes sense only for k = 0) - if ( ( firstState->elements[0].depth == 1 ) && ( firstState->elements[firstState->elements.size ( ) - 1].depth == x.getContent ( ).size ( ) ) && ( k == 0 ) ) + if ( ( firstState.elements[0].depth == 1 ) && ( firstState.elements[firstState.elements.size ( ) - 1].depth == x.getContent ( ).size ( ) ) && ( k == 0 ) ) updateEnhCov ( firstState, result, h ); processState ( x, k, firstState, result, h ); - delete firstState; } // in the end the set of actual strings is computed from the set of lfactors @@ -72,25 +71,23 @@ ext::set < string::LinearString < SymbolType > > RelaxedApproximateEnhancedCover } template < class SymbolType > -void RelaxedApproximateEnhancedCoversComputation::processState ( const string::LinearString < SymbolType > & x, unsigned k, State const * previousState, ext::set < ext::pair < unsigned, unsigned > > & covers, unsigned & h ) { +void RelaxedApproximateEnhancedCoversComputation::processState ( const string::LinearString < SymbolType > & x, unsigned k, const State & previousState, ext::set < ext::pair < unsigned, unsigned > > & covers, unsigned & h ) { for ( const auto & symbol : x.getAlphabet ( ) ) { - auto * currState = constrNextState ( x, previousState, k, symbol ); + State currState = constrNextState ( x, previousState, k, symbol ); bool isFactor = false; - for ( const Element & element : currState->elements ) + for ( const Element & element : currState.elements ) if ( element.level == 0 ) { - currState->lfactor = ext::pair < unsigned, unsigned > ( element.depth, currState->depth ); + currState.lfactor = ext::pair < unsigned, unsigned > ( element.depth, currState.depth ); isFactor = true; } - if ( ( currState->elements.size ( ) > 1 ) && ( ( currState->elements[0].depth == currState->depth ) && isFactor ) ) { - if ( ( currState->elements[currState->elements.size ( ) - 1].depth == x.getContent ( ).size ( ) ) && ( currState->depth > k ) ) + if ( ( currState.elements.size ( ) > 1 ) && ( ( currState.elements[0].depth == currState.depth ) && isFactor ) ) { + if ( ( currState.elements[currState.elements.size ( ) - 1].depth == x.getContent ( ).size ( ) ) && ( currState.depth > k ) ) updateEnhCov ( currState, covers, h ); processState ( x, k, currState, covers, h ); } - - delete currState; } }