28#include <alib/multimap>
31#include <ext/algorithm>
77template <
class SymbolTypeT = DefaultSymbolType,
class StateTypeT = DefaultStateType >
78class CompactNFA final :
public core::Components < CompactNFA < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates >, StateTypeT, component::Value, InitialState > {
155 return this->
template accessComponent < InitialState > ( ).get ( );
164 return std::move ( this->
template accessComponent < InitialState > ( ).
get ( ) );
175 return this->
template accessComponent < InitialState > ( ).set ( std::move ( state ) );
184 return this->
template accessComponent < States > ( ).get ( );
193 return std::move ( this->
template accessComponent < States > ( ).
get ( ) );
204 return this->
template accessComponent < States > ( ).add ( std::move ( state ) );
213 this->
template accessComponent < States > ( ).set ( std::move ( states ) );
224 this->
template accessComponent < States > ( ).remove ( state );
233 return this->
template accessComponent < FinalStates > ( ).get ( );
242 return std::move ( this->
template accessComponent < FinalStates > ( ).
get ( ) );
253 return this->
template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
262 this->
template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
273 this->
template accessComponent < FinalStates > ( ).remove ( state );
282 return this->
template accessComponent < InputAlphabet > ( ).get ( );
291 return std::move ( this->
template accessComponent < InputAlphabet > ( ).
get ( ) );
302 return this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
311 this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
320 this->
template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
331 this->
template accessComponent < InputAlphabet > ( ).remove ( symbol );
425 return out <<
"(CompactNFA"
426 <<
" states = " << instance.
getStates ( )
451template <
class SymbolType,
class StateType >
462template <
class SymbolType,
class StateType >
463CompactNFA < SymbolType, StateType >::CompactNFA (
ext::set < StateType > states,
ext::set < SymbolType > inputAlphabet,
StateType initialState,
ext::set < StateType > finalStates ) :
core::Components <
CompactNFA,
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 ) ) {
466template <
class SymbolType,
class StateType >
470template <
class SymbolType,
class StateType >
472 for (
const auto & transition : other.getTransitions ( ) )
473 if ( transition.first.second.is_epsilon ( ) )
478 for (
const auto & state : other.getInitialStates ( ) )
482template <
class SymbolType,
class StateType >
484 for (
const auto & transition : other.getTransitions ( ) )
485 if ( transition.first.second.is_epsilon ( ) )
491template <
class SymbolType,
class StateType >
493 for (
const auto & transition : other.getTransitions ( ) )
496 for (
const auto & state : other.getInitialStates ( ) )
500template <
class SymbolType,
class StateType >
502 for (
const auto & transition : other.getTransitions ( ) )
506template <
class SymbolType,
class StateType >
508 transitions.insert ( other.getTransitions ( ).begin ( ), other.getTransitions ( ).end ( ) );
511template <
class SymbolType,
class StateType >
513 for (
const auto & transition : other.getTransitions ( ) )
517template <
class SymbolType,
class StateType >
519 if ( ! getStates ( ).contains ( from ) )
525 if ( ! std::includes ( getInputAlphabet ( ).
begin ( ), getInputAlphabet ( ).
end ( ), inputStringAlphabet.
begin ( ), inputStringAlphabet.
end ( ) ) )
526 throw AutomatonException (
"Input string is over different alphabet than automaton" );
528 if ( ! getStates ( ).
contains ( to ) )
531 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input ) );
532 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input ) );
533 auto iter = std::lower_bound ( lower_bound, upper_bound, to, [ ] (
const auto & transition,
const auto & target ) {
return transition.second < target; } );
534 if ( iter != upper_bound && to >= iter->second )
538 transitions.insert ( iter,
std::make_pair ( std::move ( key ), std::move ( to ) ) );
542template <
class SymbolType,
class StateType >
544 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input ) );
545 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input ) );
546 auto iter = std::find_if ( lower_bound, upper_bound, [ & ] (
const auto & transition ) {
return transition.second == to; } );
547 if ( iter == upper_bound )
550 transitions.erase ( iter );
554template <
class SymbolType,
class StateType >
559template <
class SymbolType,
class StateType >
561 return std::move ( transitions );
564template <
class SymbolType,
class StateType >
566 if ( ! getStates ( ).contains ( from ) )
572 if ( transition.first.first == from )
573 transitionsFromState.
insert ( transition );
575 return transitionsFromState;
578template <
class SymbolType,
class StateType >
580 if ( ! getStates ( ).contains ( to ) )
586 if ( transition.second == to )
587 transitionsToState.
insert ( transition );
589 return transitionsToState;
602template <
class SymbolType,
class StateType >
651template <
class SymbolType,
class StateType >
663 if (
automaton.getInitialState ( ) == state )
666 if (
automaton.getFinalStates ( ).contains ( state ) )
670 if ( t.first.first == state || t.second == state )
704template <
class SymbolType,
class StateType >
728 return automaton.template accessComponent < automaton::States > ( ).get ( ).contains ( state );
747template <
class SymbolType,
class StateType >
759 return automaton.template accessComponent < automaton::States > ( ).get ( ).contains ( state );
777template <
class SymbolType,
class StateType >
792 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 ext::vector< DefaultSymbolType > normalizeSymbols(ext::vector< SymbolType > &&symbols)
Definition: SymbolNormalize.h:86
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
Compact deterministic finite automaton. Accepts regular languages. The automaton has a list of symbol...
Definition: CompactDFA.h:73
Compact nondeterministic finite automaton. Accepts regular languages. The automaton has a list of sym...
Definition: CompactNFA.h:78
SymbolTypeT SymbolType
Definition: CompactNFA.h:80
StateType && getInitialState() &&
Definition: CompactNFA.h:163
void removeFinalState(const StateType &state)
Definition: CompactNFA.h:272
const ext::set< StateType > & getFinalStates() const &
Definition: CompactNFA.h:232
void removeState(const StateType &state)
Definition: CompactNFA.h:223
CompactNFA(StateType initialState)
Creates a new instance of the automaton with a concrete initial state.
Definition: CompactNFA.h:467
const ext::multimap< ext::pair< StateType, ext::vector< SymbolType > >, StateType > & getTransitions() const &
Definition: CompactNFA.h:555
ext::multimap< ext::pair< StateType, ext::vector< SymbolType > >, StateType > getTransitionsToState(const StateType &to) const
Definition: CompactNFA.h:579
bool addFinalState(StateType state)
Definition: CompactNFA.h:252
bool setInitialState(StateType state)
Definition: CompactNFA.h:174
void setStates(ext::set< StateType > states)
Definition: CompactNFA.h:212
const ext::set< StateType > & getStates() const &
Definition: CompactNFA.h:183
const StateType & getInitialState() const &
Definition: CompactNFA.h:154
bool addState(StateType state)
Definition: CompactNFA.h:203
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: CompactNFA.h:281
void setFinalStates(ext::set< StateType > states)
Definition: CompactNFA.h:261
bool operator==(const CompactNFA &other) const
Definition: CompactNFA.h:412
StateTypeT StateType
Definition: CompactNFA.h:81
ext::set< SymbolType > && getInputAlphabet() &&
Definition: CompactNFA.h:290
ext::multimap< ext::pair< StateType, ext::vector< SymbolType > >, StateType > getTransitionsFromState(const StateType &from) const
Definition: CompactNFA.h:565
ext::set< StateType > && getStates() &&
Definition: CompactNFA.h:192
bool addInputSymbol(SymbolType symbol)
Definition: CompactNFA.h:301
void removeInputSymbol(const SymbolType &symbol)
Definition: CompactNFA.h:330
bool addTransition(StateType from, ext::vector< SymbolType > input, StateType to)
Add a transition to the automaton.
Definition: CompactNFA.h:518
bool removeTransition(const StateType &from, const ext::vector< SymbolType > &input, const StateType &to)
Removes a transition from the automaton.
Definition: CompactNFA.h:543
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: CompactNFA.h:319
ext::set< StateType > && getFinalStates() &&
Definition: CompactNFA.h:241
void addInputSymbols(ext::set< SymbolType > symbols)
Definition: CompactNFA.h:310
friend ext::ostream & operator<<(ext::ostream &out, const CompactNFA &instance)
Definition: CompactNFA.h:424
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: EpsilonNFA.h:74
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: MultiInitialStateEpsilonNFA.h:75
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
Definition: CompactNFA.h:441
Definition: components.hpp:181
static void valid(const automaton::CompactNFA< SymbolType, StateType > &, const StateType &)
Definition: CompactNFA.h:768
static bool available(const automaton::CompactNFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: CompactNFA.h:758
Definition: components.hpp:25
static void valid(const automaton::CompactNFA< SymbolType, StateType > &, const StateType &)
Definition: CompactNFA.h:737
static bool available(const automaton::CompactNFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: CompactNFA.h:727
static bool used(const automaton::CompactNFA< SymbolType, StateType > &, const StateType &)
Definition: CompactNFA.h:715
static void valid(const automaton::CompactNFA< SymbolType, StateType > &, const StateType &)
Definition: CompactNFA.h:694
static bool used(const automaton::CompactNFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: CompactNFA.h:662
static bool available(const automaton::CompactNFA< SymbolType, StateType > &, const StateType &)
Definition: CompactNFA.h:684
static void valid(const automaton::CompactNFA< SymbolType, StateType > &, const SymbolType &)
Definition: CompactNFA.h:641
static bool available(const automaton::CompactNFA< SymbolType, StateType > &, const SymbolType &)
Definition: CompactNFA.h:631
static bool used(const automaton::CompactNFA< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: CompactNFA.h:613
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
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
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 isCompactNFA
Definition: CompactNFA.h:460
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
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
bool contains(InputIt first, InputIt last, const Element &elem)
Linear version of search in a range of values.
Definition: algorithm.hpp:170
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 begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::CompactNFA< > eval(automaton::CompactNFA< SymbolType, StateType > &&value)
Definition: CompactNFA.h:779
Definition: normalize.hpp:13