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

fix MultiInitialState(Epsilon)NFA

parent e6b77f56
No related branches found
No related tags found
1 merge request!90Merge jt
......@@ -182,17 +182,6 @@ public:
this->template accessComponent < InitialStates > ( ).remove ( state );
}
 
/**
* Setter of the initial state.
*
* \param state new initial state of the automaton
*
* \returns true if the initial state was indeed changed
*/
bool setInitialState ( StateType state ) {
return this->template accessComponent < InitialStates > ( ).set ( std::move ( state ) );
}
/**
* Getter of states.
*
......@@ -393,6 +382,21 @@ public:
*/
bool addTransition ( StateType from, StateType to );
 
/**
* \brief Removes a transition to the automaton.
*
* \details The transition is in a form A \times a -> B, where A, B \in Q and a \in T \cup \{\epsilon\}
*
* \param current the source state (A)
* \param input the input symbol or epsilon (a)
* \param next the target state (B)
*
* \throws AutomatonException when transition contains state or symbol not present in the automaton components
*
* \returns true if the transition was indeed added
*/
bool removeTransition ( StateType from, ext::variant < EpsilonType, SymbolType > input, StateType to );
/**
* \brief Removes a transition from the automaton.
*
......@@ -664,21 +668,30 @@ bool MultiInitialStateEpsilonNFA < SymbolType, EpsilonType, StateType >::addTran
return addTransition ( std::move ( from ), std::move ( inputVariant ), std::move ( to ) );
}
 
template<class SymbolType, class EpsilonType, class StateType >
bool MultiInitialStateEpsilonNFA < SymbolType, EpsilonType, StateType >::removeTransition ( StateType from, ext::variant < EpsilonType, SymbolType > input, StateType to ) {
auto upper_bound = transitions.upper_bound ( ext::tie ( from, input ) );
auto lower_bound = transitions.lower_bound ( ext::tie ( from, input ) );
auto iter = std::find_if ( lower_bound, upper_bound, [ & ] ( const auto & transition ) { return transition.second == to; } );
if ( iter == upper_bound )
return false;
transitions.erase ( iter );
return true;
}
template<class SymbolType, class EpsilonType, class StateType >
bool MultiInitialStateEpsilonNFA < SymbolType, EpsilonType, StateType >::removeTransition ( const StateType & from, const SymbolType & input, const StateType & to ) {
ext::variant < EpsilonType, SymbolType > inputVariant ( input );
ext::pair < StateType, ext::variant < EpsilonType, SymbolType > > key = ext::make_pair ( from, inputVariant );
ext::variant < EpsilonType, SymbolType > inputVariant ( std::move ( input ) );
 
return transitions[key].erase ( to );
return addTransition ( std::move ( from ), std::move ( inputVariant ), std::move ( to ) );
}
 
template<class SymbolType, class EpsilonType, class StateType >
bool MultiInitialStateEpsilonNFA < SymbolType, EpsilonType, StateType >::removeTransition ( const StateType & from, const StateType & to ) {
auto inputVariant = ext::variant < EpsilonType, SymbolType >::template from < EpsilonType > ( );
 
ext::pair < StateType, ext::variant < EpsilonType, SymbolType > > key = ext::make_pair ( from, inputVariant );
return transitions[key].erase ( to );
return addTransition ( std::move ( from ), std::move ( inputVariant ), std::move ( to ) );
}
 
template<class SymbolType, class EpsilonType, class StateType >
......@@ -808,11 +821,14 @@ bool MultiInitialStateEpsilonNFA < SymbolType, EpsilonType, StateType >::isEpsil
 
template<class SymbolType, class EpsilonType, class StateType >
bool MultiInitialStateEpsilonNFA < SymbolType, EpsilonType, StateType >::isDeterministic ( ) const {
for ( const std::pair < const ext::pair < StateType, ext::variant < EpsilonType, SymbolType > >, StateType > & transition : transitions )
if ( transition.second.size ( ) > 1 )
if ( transitions.empty ( ) )
return true;
for ( auto iter = transitions.begin ( ); std::next ( iter ) != transitions.end ( ); ++ iter )
if ( iter->first == std::next ( iter )->first )
return false;
 
return isEpsilonFree ( );
return isEpsilonFree ( ) && getInitialStates ( ).size ( ) == 1;
}
 
template<class SymbolType, class EpsilonType, class StateType >
......
......@@ -399,13 +399,6 @@ public:
*/
bool isTotal ( ) const;
 
/**
* \brief Computes number of transitions in the automaton
*
* \return number of transitions in the automaton
*/
unsigned transitionsSize ( ) const;
/**
* The actual compare method
*
......@@ -572,17 +565,7 @@ bool MultiInitialStateNFA < SymbolType, StateType >::isDeterministic ( ) const {
 
template < class SymbolType, class StateType >
bool MultiInitialStateNFA < SymbolType, StateType >::isTotal ( ) const {
return isDeterministic ( ) && transitionsSize ( ) == getInputAlphabet ( ).size ( ) * getStates ( ).size ( );
}
template < class SymbolType, class StateType >
unsigned MultiInitialStateNFA < SymbolType, StateType >::transitionsSize ( ) const {
int res = 0;
for ( const auto & transition : transitions )
res += transition.second.size ( );
return res;
return isDeterministic ( ) && transitions.size ( ) == getInputAlphabet ( ).size ( ) * getStates ( ).size ( );
}
 
template < class SymbolType, class StateType >
......
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