26#include <alib/multimap>
65template <
class SymbolTypeT = DefaultSymbolType,
class StateTypeT = DefaultStateType >
66class NFA final :
public core::Components < NFA < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates >, StateTypeT, component::Value, InitialState > {
108 return this->
template accessComponent < InitialState > ( ).get ( );
117 return std::move ( this->
template accessComponent < InitialState > ( ).
get ( ) );
128 return this->
template accessComponent < InitialState > ( ).set ( std::move ( state ) );
137 return this->
template accessComponent < States > ( ).get ( );
146 return std::move ( this->
template accessComponent < States > ( ).
get ( ) );
157 return this->
template accessComponent < States > ( ).add ( std::move ( state ) );
166 this->
template accessComponent < States > ( ).set ( std::move ( states ) );
177 this->
template accessComponent < States > ( ).remove ( state );
186 return this->
template accessComponent < FinalStates > ( ).get ( );
195 return std::move ( this->
template accessComponent < FinalStates > ( ).
get ( ) );
206 return this->
template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
215 this->
template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
226 this->
template accessComponent < FinalStates > ( ).remove ( state );
235 return this->
template accessComponent < InputAlphabet > ( ).get ( );
244 return std::move ( this->
template accessComponent < InputAlphabet > ( ).
get ( ) );
255 return this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
264 this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
273 this->
template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
284 this->
template accessComponent < InputAlphabet > ( ).remove ( symbol );
375 auto operator <=> ( const
NFA & other )
const {
400 <<
" states = " << instance.
getStates ( )
425template <
class SymbolType,
class StateType >
436template<
class SymbolType,
class StateType >
437NFA < SymbolType, StateType >::NFA (
ext::set < StateType > states,
ext::set < SymbolType > inputAlphabet,
StateType initialState,
ext::set < StateType > finalStates ) :
core::Components <
NFA,
ext::set < SymbolType >, component::Set, InputAlphabet,
ext::set < StateType >, component::Set, std::tuple < States, FinalStates >,
StateType, component::Value, InitialState > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ), std::move ( initialState ) ) {
440template<
class SymbolType,
class StateType >
444template<
class SymbolType,
class StateType >
446 transitions.insert ( other.getTransitions ( ).begin ( ), other.getTransitions ( ).end ( ) );
449template<
class SymbolType,
class StateType >
451 if ( ! getStates ( ).contains ( from ) )
454 if ( ! getInputAlphabet ( ).contains ( input ) )
457 if ( ! getStates ( ).contains ( to ) )
460 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input ) );
461 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input ) );
462 auto iter = std::lower_bound ( lower_bound, upper_bound, to, [ ] (
const auto & transition,
const auto & target ) {
return transition.second < target; } );
463 if ( iter != upper_bound && to >= iter->second )
467 transitions.insert ( iter,
std::make_pair ( std::move ( key ), std::move ( to ) ) );
471template<
class SymbolType,
class StateType >
473 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input ) );
474 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input ) );
475 auto iter = std::find_if ( lower_bound, upper_bound, [ & ] (
const auto & transition ) {
return transition.second == to; } );
476 if ( iter == upper_bound )
479 transitions.erase ( iter );
483template<
class SymbolType,
class StateType >
488template<
class SymbolType,
class StateType >
490 return std::move ( transitions );
493template<
class SymbolType,
class StateType >
495 if ( ! getStates ( ).contains ( from ) )
501template<
class SymbolType,
class StateType >
503 if ( ! getStates ( ).contains ( to ) )
509 if ( transition.second == to )
510 transitionsToState.
insert ( transition );
512 return transitionsToState;
515template<
class SymbolType,
class StateType >
517 if ( transitions.empty ( ) )
520 for (
auto iter = transitions.begin ( ); std::next ( iter ) != transitions.end ( ); ++ iter )
521 if ( iter->first == std::next ( iter )->first )
527template<
class SymbolType,
class StateType >
529 for (
const StateType & state : getStates ( ) ) {
530 for (
const SymbolType & symbol : getInputAlphabet ( ) ) {
531 if ( ! getTransitions ( ).contains (
ext::make_pair ( state, symbol ) ) )
548template<
class SymbolType,
class StateType >
561 if ( transition.first.second == symbol )
595template<
class SymbolType,
class StateType >
607 if (
automaton.getInitialState ( ) == state )
610 if (
automaton.getFinalStates ( ).contains ( state ) )
614 if ( ( transition.first.first == state ) || transition.second == state )
648template<
class SymbolType,
class StateType >
672 return automaton.template accessComponent < automaton::States > ( ).get ( ).contains ( state );
691template<
class SymbolType,
class StateType >
703 return automaton.template accessComponent < automaton::States > ( ).get ( ).contains ( state );
721template <
class SymbolType,
class StateType >
736 res.addTransition ( std::move ( from ), std::move ( input ), std::move ( target ) );
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Definition: AutomatonException.h:15
static DefaultStateType normalizeState(StateType &&state)
Definition: AutomatonNormalize.h:76
static ext::multiset< DefaultStateType > normalizeStates(ext::multiset< StateType > &&states)
Definition: AutomatonNormalize.h:49
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
StateType && getInitialState() &&
Definition: NFA.h:116
void removeInputSymbol(const SymbolType &symbol)
Definition: NFA.h:283
ext::set< StateType > && getFinalStates() &&
Definition: NFA.h:194
const ext::set< StateType > & getStates() const &
Definition: NFA.h:136
bool isDeterministic() const
Determines whether the automaton is deterministic.
Definition: NFA.h:516
friend ext::ostream & operator<<(ext::ostream &out, const NFA &instance)
Definition: NFA.h:398
bool addState(StateType state)
Definition: NFA.h:156
NFA(StateType initialState)
Creates a new instance of the automaton with a concrete initial state.
Definition: NFA.h:441
ext::set< StateType > && getStates() &&
Definition: NFA.h:145
void addInputSymbols(ext::set< SymbolType > symbols)
Definition: NFA.h:263
const StateType & getInitialState() const &
Definition: NFA.h:107
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: NFA.h:234
const ext::multimap< ext::pair< StateType, SymbolType >, StateType > & getTransitions() const &
Definition: NFA.h:484
const ext::set< StateType > & getFinalStates() const &
Definition: NFA.h:185
ext::multimap< ext::pair< StateType, SymbolType >, StateType > getTransitionsToState(const StateType &to) const
Definition: NFA.h:502
void setFinalStates(ext::set< StateType > states)
Definition: NFA.h:214
bool addInputSymbol(SymbolType symbol)
Definition: NFA.h:254
void removeFinalState(const StateType &state)
Definition: NFA.h:225
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: NFA.h:272
ext::set< SymbolType > && getInputAlphabet() &&
Definition: NFA.h:243
bool operator==(const NFA &other) const
Definition: NFA.h:386
auto getTransitionsFromState(const StateType &from) const
Definition: NFA.h:494
bool setInitialState(StateType state)
Definition: NFA.h:127
bool removeTransition(const StateType &from, const SymbolType &input, const StateType &to)
Removes a transition from the automaton.
Definition: NFA.h:472
bool addTransition(StateType from, SymbolType input, StateType to)
Add a transition to the automaton.
Definition: NFA.h:450
void removeState(const StateType &state)
Definition: NFA.h:176
bool isTotal() const
Determines whether the automaton is total deterministic.
Definition: NFA.h:528
void setStates(ext::set< StateType > states)
Definition: NFA.h:165
bool addFinalState(StateType state)
Definition: NFA.h:205
StateTypeT StateType
Definition: NFA.h:69
SymbolTypeT SymbolType
Definition: NFA.h:68
Definition: components.hpp:181
static void valid(const automaton::NFA< SymbolType, StateType > &, const StateType &)
Definition: NFA.h:712
static bool available(const automaton::NFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: NFA.h:702
Definition: components.hpp:25
static bool available(const automaton::NFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: NFA.h:671
static void valid(const automaton::NFA< SymbolType, StateType > &, const StateType &)
Definition: NFA.h:681
static bool used(const automaton::NFA< SymbolType, StateType > &, const StateType &)
Definition: NFA.h:659
static bool used(const automaton::NFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: NFA.h:606
static void valid(const automaton::NFA< SymbolType, StateType > &, const StateType &)
Definition: NFA.h:638
static bool available(const automaton::NFA< SymbolType, StateType > &, const StateType &)
Definition: NFA.h:628
static bool available(const automaton::NFA< SymbolType, StateType > &, const SymbolType &)
Definition: NFA.h:575
static void valid(const automaton::NFA< SymbolType, StateType > &, const SymbolType &)
Definition: NFA.h:585
static bool used(const automaton::NFA< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: NFA.h:559
Definition: setComponents.hpp:26
Class extending the multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.hpp:48
iterator insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:118
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: BarSymbol.cpp:12
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
constexpr bool isNFA
Definition: NFA.h:434
Definition: normalize.hpp:10
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
reference_mover< T > make_mover(T ¶m)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
SliceComp< Ts ... > slice_comp(const Ts &... inst)
Definition: functional.hpp:95
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::NFA< > eval(automaton::NFA< SymbolType, StateType > &&value)
Definition: NFA.h:723
Definition: normalize.hpp:13