TuringSim
C++ framework to simulate abstract computing models
generalPushdownMachineTransition.h
1 #pragma once
2 
3 #include <memory/word.h>
4 #include <memory/stack.h>
5 #include <transition/PDM/pushdownMachineTransition.h>
6 
15  template <
16  typename StateType_,
17  typename LetterType_,
18  typename StackSymbolType_
19  >
21  public PushdownMachineTransition<StateType_, LetterType_, StackSymbolType_> {
23  public:
31 
32  static_assert(std::is_same_v<StateType, StateType_>);
33  static_assert(std::is_same_v<ApplyHelperType, std::monostate>);
34 
36 
45  StateType preState,
46  std::optional<LetterType> preLetter,
47  std::vector<StackSymbolType> preSymbols,
48  StateType postState,
49  std::vector<StackSymbolType> postSymbols) :
50  preState(preState),
51  preLetter(preLetter),
52  preSymbols(preSymbols),
53  postState(postState),
54  postSymbols(postSymbols)
55  {}
56 
59 
62 
67 
72 
74  virtual ~GeneralPushdownMachineTransition() = default;
75 
83  virtual std::optional<ApplyHelperType> match(const StateType& state, const StorageType& storage) const override final {
84  return this->matchFromCurrentLetter(state, std::get<WordType>(storage).front_opt(), std::get<StackType>(storage));
85  }
86 
95  virtual std::optional<ApplyHelperType> matchFromCurrentLetter(const StateType& state, const std::optional<LetterType>& letter, const StackType& stack) const {
96  if(state != preState || (!letter && preLetter) || (letter && preLetter && *letter != *preLetter)) {
97  return {};
98  }
99 
100  size_t preSymbolsSize = preSymbols.size();
101  try {
102  std::vector<StackSymbolType> tops = stack.tops(preSymbolsSize);
103 
104  if(tops != preSymbols) {
105  return {};
106  }
107  }
109  return {};
110  }
111  return {std::monostate{}};
112  }
113 
114 #pragma clang diagnostic push
115 #pragma clang diagnostic ignored "-Wunused-parameter"
116 
128  virtual StateType apply(const StateType& state, StorageType& storage, ApplyHelperType&& helper, bool& running) const override final {
129  size_t preSymbolsSize = preSymbols.size();
130  std::get<StackType>(storage).pops(preSymbolsSize);
131  std::get<StackType>(storage).pushs(postSymbols);
132  if(preLetter) {
133  std::get<WordType>(storage).read();
134  }
135  return postState;
136  }
137 #pragma clang diagnostic pop
138 
142  const StateType& getPreState() const noexcept {
143  return preState;
144  }
145 
149  const std::optional<LetterType>& getPreLetter() const noexcept {
150  return preLetter;
151  }
152 
156  const std::vector<StackSymbolType>& getPreSymbols() const noexcept {
157  return preSymbols;
158  }
159 
163  size_t getPreSymbolsSize() const noexcept {
164  return preSymbols.size();
165  }
166 
167  private:
168  StateType preState;
169  std::optional<LetterType> preLetter;
170  std::vector<StackSymbolType> preSymbols;
171  StateType postState;
172  std::vector<StackSymbolType> postSymbols;
173  };
174 }
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::getPreState
const StateType & getPreState() const noexcept
Get the pre-state.
Definition: generalPushdownMachineTransition.h:142
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::apply
virtual StateType apply(const StateType &state, StorageType &storage, ApplyHelperType &&helper, bool &running) const override final
Apply a previously matching transition and return the new state.
Definition: generalPushdownMachineTransition.h:128
TuringSim::Transition::PDM::PushdownMachineTransition::StorageType
StorageType_ StorageType
The type of storage.
Definition: transition.h:77
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::matchFromCurrentLetter
virtual std::optional< ApplyHelperType > matchFromCurrentLetter(const StateType &state, const std::optional< LetterType > &letter, const StackType &stack) const
Test whether the transition matches a configuration. Returned optional is empty iff the transition do...
Definition: generalPushdownMachineTransition.h:95
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::operator=
GeneralPushdownMachineTransition & operator=(GeneralPushdownMachineTransition &&)=default
The default move assignment operator.
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::getPreSymbols
const std::vector< StackSymbolType > & getPreSymbols() const noexcept
Get the pre-symbols.
Definition: generalPushdownMachineTransition.h:156
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::getPreSymbolsSize
size_t getPreSymbolsSize() const noexcept
Get the size of stack needed by this transition.
Definition: generalPushdownMachineTransition.h:163
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::GeneralPushdownMachineTransition
GeneralPushdownMachineTransition(StateType preState, std::optional< LetterType > preLetter, std::vector< StackSymbolType > preSymbols, StateType postState, std::vector< StackSymbolType > postSymbols)
Builds a GeneralPushdownMachineTransition from its components.
Definition: generalPushdownMachineTransition.h:44
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::GeneralPushdownMachineTransition
GeneralPushdownMachineTransition(const GeneralPushdownMachineTransition &)=default
The default copy constructor.
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::match
virtual std::optional< ApplyHelperType > match(const StateType &state, const StorageType &storage) const override final
Test whether the transition matches a configuration. Returned optional is empty iff the transition do...
Definition: generalPushdownMachineTransition.h:83
TuringSim::Memory::Stack::Stack
Class to represent a stack memory.
Definition: stack.h:77
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::GeneralPushdownMachineTransition
GeneralPushdownMachineTransition(GeneralPushdownMachineTransition &&)=default
The default move constructor.
TuringSim::Transition::PDM::PushdownMachineTransition::LetterType
LetterType_ LetterType
The type of letter of the word.
Definition: pushdownMachineTransition.h:51
TuringSim::Transition::PDM::GeneralPushdownMachineTransition
Transition of pushdown machines, i.e. transition on a word and a stack.
Definition: generalPushdownMachineTransition.h:21
TuringSim::Memory::Stack::Stack::tops
std::vector< T > tops(size_t nb) const
Get the top elements of the stack.
Definition: stack.h:339
TuringSim::Transition::PDM::PushdownMachineTransition::ApplyHelperType
ApplyHelperType_ ApplyHelperType
The type of apply helpers.
Definition: transition.h:82
TuringSim::Transition::PDM::PushdownMachineTransition::StackSymbolType
StackSymbolType_ StackSymbolType
The type of symbols in the stack.
Definition: pushdownMachineTransition.h:56
TuringSim::Transition::PDM::PushdownMachineTransition::StackType
Memory::Stack::Stack< StackSymbolType_ > StackType
The type of the stack of a machine with such transition.
Definition: pushdownMachineTransition.h:39
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::operator=
GeneralPushdownMachineTransition & operator=(const GeneralPushdownMachineTransition &)=default
The default copy assignment operator.
TuringSim::Transition::PDM::PushdownMachineTransition
Transition of pushdown machines, i.e. transition on a word and a stack.
Definition: pushdownMachineTransition.h:30
TuringSim::Transition::PDM::PushdownMachineTransition::StateType
StateType_ StateType
The type of states.
Definition: transition.h:72
TuringSim::Memory::Stack::StackTooSmallException
Exception thrown when we try to pop or top an empty stack, or pops or tops a too small stack.
Definition: stack.h:18
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::~GeneralPushdownMachineTransition
virtual ~GeneralPushdownMachineTransition()=default
The default virtual destructor.
TuringSim::Transition::PDM::GeneralPushdownMachineTransition::getPreLetter
const std::optional< LetterType > & getPreLetter() const noexcept
Get the pre-letter.
Definition: generalPushdownMachineTransition.h:149
TuringSim::Transition::PDM::PushdownMachineTransition::WordType
Memory::Word::Word< LetterType_ > WordType
The type of words read by a machine with such transition.
Definition: pushdownMachineTransition.h:35
TuringSim::Transition::PDM
Transitions for pushdown machines.
Definition: generalPushdownMachineTransition.h:7