27#include <ext/algorithm>
30#include <alib/multimap>
49class PushdownStoreAlphabet;
73template <
class InputSymbolTypeT = DefaultSymbolType,
class PushdownStoreSymbolTypeT = DefaultSymbolType,
class StateTypeT = DefaultStateType >
74class NPDA final :
public core::Components < NPDA < InputSymbolTypeT, PushdownStoreSymbolTypeT, StateTypeT >, ext::set < InputSymbolTypeT >, component::Set, InputAlphabet, ext::set < PushdownStoreSymbolTypeT >, component::Set, PushdownStoreAlphabet, PushdownStoreSymbolTypeT, component::Value, InitialSymbol, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates >, StateTypeT, component::Value, InitialState > {
120 return this->
template accessComponent < InitialState > ( ).get ( );
129 return std::move ( this->
template accessComponent < InitialState > ( ).
get ( ) );
140 return this->
template accessComponent < InitialState > ( ).set ( std::move ( state ) );
149 return this->
template accessComponent < States > ( ).get ( );
158 return std::move ( this->
template accessComponent < States > ( ).
get ( ) );
169 return this->
template accessComponent < States > ( ).add ( std::move ( state ) );
178 this->
template accessComponent < States > ( ).set ( std::move ( states ) );
189 this->
template accessComponent < States > ( ).remove ( state );
198 return this->
template accessComponent < FinalStates > ( ).get ( );
207 return std::move ( this->
template accessComponent < FinalStates > ( ).
get ( ) );
218 return this->
template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
227 this->
template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
238 this->
template accessComponent < FinalStates > ( ).remove ( state );
247 return this->
template accessComponent < PushdownStoreAlphabet > ( ).get ( );
256 return std::move ( this->
template accessComponent < PushdownStoreAlphabet > ( ).
get ( ) );
267 return this->
template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) );
276 this->
template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) );
285 this->
template accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) );
296 this->
template accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol );
305 return this->
template accessComponent < InitialSymbol > ( ).get ( );
314 return std::move ( this->
template accessComponent < InitialSymbol > ( ).
get ( ) );
325 return this->
template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) );
334 return this->
template accessComponent < InputAlphabet > ( ).get ( );
343 return std::move ( this->
template accessComponent < InputAlphabet > ( ).
get ( ) );
354 return this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
363 this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
372 this->
template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
383 this->
template accessComponent < InputAlphabet > ( ).remove ( symbol );
525 auto operator <=> ( const
NPDA & other )
const {
526 return std::tie(
getStates(),
getInputAlphabet(),
getInitialState(),
getFinalStates(),
getPushdownStoreAlphabet(),
getInitialSymbol(), transitions) <=>
std::tie(other.getStates(), other.getInputAlphabet(), other.getInitialState(), other.getFinalStates(), other.getPushdownStoreAlphabet(), other.getInitialSymbol(), other.transitions);
537 return std::tie(
getStates(),
getInputAlphabet(),
getInitialState(),
getFinalStates(),
getPushdownStoreAlphabet(),
getInitialSymbol(), transitions) ==
std::tie(other.
getStates(), other.
getInputAlphabet(), other.
getInitialState(), other.
getFinalStates(), other.
getPushdownStoreAlphabet(), other.
getInitialSymbol(), other.transitions);
549 return out <<
"(NPDA"
550 <<
" states = " << instance.
getStates ( )
561template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
562NPDA < InputSymbolType, PushdownStoreSymbolType, StateType >::NPDA (
ext::set < StateType > states,
ext::set < InputSymbolType > inputAlphabet,
ext::set < PushdownStoreSymbolType > pushdownStoreAlphabet,
StateType initialState,
PushdownStoreSymbolType initialSymbol,
ext::set < StateType > finalStates ) :
core::Components <
NPDA,
ext::set < InputSymbolType >, component::Set, InputAlphabet,
ext::set < PushdownStoreSymbolType >, component::Set, PushdownStoreAlphabet,
PushdownStoreSymbolType, component::Value, InitialSymbol,
ext::set < StateType >, component::Set, std::tuple < States, FinalStates >,
StateType, component::Value, InitialState > ( std::move ( inputAlphabet ), std::move ( pushdownStoreAlphabet ), std::move ( initialSymbol ), std::move ( states ), std::move ( finalStates ), std::move ( initialState ) ) {
565template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
569template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
571 transitions.insert ( other.getTransitions ( ).begin ( ), other.getTransitions ( ).end ( ) );
574template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
576 if (! getStates().count(from))
579 if ( ! input.is_epsilon ( ) && ! getInputAlphabet().count(input.getSymbol ( ) ) )
582 if (! getStates().count(to))
586 if (! getPushdownStoreAlphabet().count(popSymbol))
590 if (! getPushdownStoreAlphabet().count(pushSymbol))
593 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input, pop ) );
594 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input, pop ) );
598 auto iter = std::lower_bound ( lower_bound, upper_bound, value, [ ] (
const auto & transition,
const auto & target ) {
return transition.second < target; } );
599 if ( iter != upper_bound && value >= iter->second )
603 transitions.insert ( iter,
std::make_pair ( std::move ( key ), std::move ( value ) ) );
607template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
610 return addTransition(std::move(from), std::move(inputVariant), std::move(pop), std::move(to), std::move(push));
613template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
616 return addTransition(std::move(from), std::move(inputVariant), std::move(pop), std::move(to), std::move(push));
619template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
621 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input, pop ) );
622 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input, pop ) );
623 auto iter = std::find_if ( lower_bound, upper_bound, [ & ] (
const auto & transition ) {
return transition.second.first == to && transition.second.second == push; } );
624 if ( iter == upper_bound )
627 transitions.erase ( iter );
631template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
634 return removeTransition(from, inputVariant, pop, to, push);
637template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
640 return removeTransition(from, inputVariant, pop, to, push);
643template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
648template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
650 return std::move ( transitions );
653template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
655 if( ! getStates().count(from) )
660 if (std::get<0>(transition.first) == from) {
661 transitionsFromState.
insert ( transition.first, transition.second );
665 return transitionsFromState;
668template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
670 if( ! getStates().count(to))
675 if (transition.second.first == to) {
676 transitionsToState.
insert ( transition.first, transition.second );
680 return transitionsToState;
694template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
695class SetConstraint<
automaton::NPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::InputAlphabet > {
707 if ( ! std::get < 1 > ( transition.first ).is_epsilon ( ) && symbol == std::get<1>(transition.first).getSymbol ( ) )
742template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
743class SetConstraint<
automaton::NPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::PushdownStoreAlphabet > {
754 if(
automaton.getInitialSymbol() == symbol)
758 if (std::find(std::get<2>(transition.first).begin(), std::get<2>(transition.first).end(), symbol) != std::get<2>(transition.first).end())
761 if (std::find(transition.second.second.begin(), transition.second.second.end(), symbol) != transition.second.second.end())
797template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
798class ElementConstraint<
automaton::NPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::InitialSymbol > {
809 return automaton.template accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol );
829template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
841 if (
automaton.getInitialState ( ) == state )
844 if (
automaton.getFinalStates ( ).count ( state ) )
848 if (state == std::get<0>(transition.first))
850 if(transition.second.first == state)
886template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
910 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
930template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
942 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
960template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
970 automaton::NPDA < > res ( std::move ( states ), std::move (
alphabet ), std::move ( pushdownAlphabet ), std::move ( initialState ), std::move ( initialSymbol ), std::move ( finalStates ) );
980 res.addTransition ( std::move ( from ), std::move ( input ), std::move ( pop ), std::move ( to ), std::move ( push ) );
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
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Definition: AutomatonException.h:15
static common::symbol_or_epsilon< DefaultSymbolType > normalizeSymbolEpsilon(common::symbol_or_epsilon< SymbolType > &&symbol)
Definition: AutomatonNormalize.h:81
static DefaultStateType normalizeState(StateType &&state)
Definition: AutomatonNormalize.h:76
static ext::multiset< DefaultStateType > normalizeStates(ext::multiset< StateType > &&states)
Definition: AutomatonNormalize.h:49
Deterministic pushdown automaton. Accepts subset of context free languages.
Definition: DPDA.h:78
bool setInitialSymbol(PushdownStoreSymbolType symbol)
Definition: NPDA.h:324
const PushdownStoreSymbolType & getInitialSymbol() const &
Definition: NPDA.h:304
ext::set< StateType > && getFinalStates() &&
Definition: NPDA.h:206
const ext::set< StateType > & getFinalStates() const &
Definition: NPDA.h:197
bool addFinalState(StateType state)
Definition: NPDA.h:217
PushdownStoreSymbolType && getInitialSymbol() &&
Definition: NPDA.h:313
void removeFinalState(const StateType &state)
Definition: NPDA.h:237
friend ext::ostream & operator<<(ext::ostream &out, const NPDA &instance)
Definition: NPDA.h:548
void setFinalStates(ext::set< StateType > states)
Definition: NPDA.h:226
bool addTransition(StateType from, common::symbol_or_epsilon< InputSymbolType > input, ext::vector< PushdownStoreSymbolType > pop, StateType to, ext::vector< PushdownStoreSymbolType > push)
Adds a transition to the automaton.
Definition: NPDA.h:575
void addInputSymbols(ext::set< InputSymbolType > symbols)
Definition: NPDA.h:362
ext::multimap< ext::tuple< StateType, common::symbol_or_epsilon< InputSymbolType >, ext::vector< PushdownStoreSymbolType > >, ext::pair< StateType, ext::vector< PushdownStoreSymbolType > > > getTransitionsToState(const StateType &to) const
Definition: NPDA.h:669
InputSymbolTypeT InputSymbolType
Definition: NPDA.h:76
bool setInitialState(StateType state)
Definition: NPDA.h:139
void setStates(ext::set< StateType > states)
Definition: NPDA.h:177
StateType && getInitialState() &&
Definition: NPDA.h:128
ext::set< InputSymbolType > && getInputAlphabet() &&
Definition: NPDA.h:342
void setInputAlphabet(ext::set< InputSymbolType > symbols)
Definition: NPDA.h:371
const StateType & getInitialState() const &
Definition: NPDA.h:119
ext::set< StateType > && getStates() &&
Definition: NPDA.h:157
bool addInputSymbol(InputSymbolType symbol)
Definition: NPDA.h:353
void removePushdownStoreSymbol(const PushdownStoreSymbolType &symbol)
Definition: NPDA.h:295
NPDA(ext::set< StateType > states, ext::set< InputSymbolType > inputAlphabet, ext::set< PushdownStoreSymbolType > pushdownStoreAlphabet, StateType initialState, PushdownStoreSymbolType initialSymbol, ext::set< StateType > finalStates)
Creates a new instance of the automaton with a concrete set of states, input alphabet,...
Definition: NPDA.h:562
ext::set< PushdownStoreSymbolType > && getPushdownStoreAlphabet() &&
Definition: NPDA.h:255
bool removeTransition(const StateType &from, const common::symbol_or_epsilon< InputSymbolType > &input, const ext::vector< PushdownStoreSymbolType > &pop, const StateType &to, const ext::vector< PushdownStoreSymbolType > &push)
Removes a transition from the automaton.
Definition: NPDA.h:620
const ext::set< StateType > & getStates() const &
Definition: NPDA.h:148
bool addState(StateType state)
Definition: NPDA.h:168
void removeInputSymbol(const InputSymbolType &symbol)
Definition: NPDA.h:382
ext::multimap< ext::tuple< StateType, common::symbol_or_epsilon< InputSymbolType >, ext::vector< PushdownStoreSymbolType > >, ext::pair< StateType, ext::vector< PushdownStoreSymbolType > > > getTransitionsFromState(const StateType &from) const
Definition: NPDA.h:654
PushdownStoreSymbolTypeT PushdownStoreSymbolType
Definition: NPDA.h:77
void addPushdownStoreSymbols(ext::set< PushdownStoreSymbolType > symbols)
Definition: NPDA.h:275
const ext::multimap< ext::tuple< StateType, common::symbol_or_epsilon< InputSymbolType >, ext::vector< PushdownStoreSymbolType > >, ext::pair< StateType, ext::vector< PushdownStoreSymbolType > > > & getTransitions() const &
Definition: NPDA.h:644
bool addPushdownStoreSymbol(PushdownStoreSymbolType symbol)
Definition: NPDA.h:266
void removeState(const StateType &state)
Definition: NPDA.h:188
void setPushdownStoreAlphabet(ext::set< PushdownStoreSymbolType > symbols)
Definition: NPDA.h:284
const ext::set< InputSymbolType > & getInputAlphabet() const &
Definition: NPDA.h:333
bool operator==(const NPDA &other) const
Definition: NPDA.h:536
const ext::set< PushdownStoreSymbolType > & getPushdownStoreAlphabet() const &
Definition: NPDA.h:246
StateTypeT StateType
Definition: NPDA.h:78
Definition: symbol_or_epsilon.hpp:24
Definition: components.hpp:181
Definition: components.hpp:25
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
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
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
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
Definition: Permutation.hpp:18
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
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
Definition: normalize.hpp:13