MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
transition.hpp
Go to the documentation of this file.
1
11#ifndef TRANSITION_HPP
12#define TRANSITION_HPP
13
14#include "direction.hpp"
15#include "state.hpp"
16#include "symbol.hpp"
17
18#include <utility>
19#include <vector>
20
29{
30 public:
34 Transition() = default;
35
45 explicit Transition(
46 std::size_t id,
47 State current,
48 State next,
49 std::vector<Symbol> read,
50 std::vector<Symbol> write,
51 std::vector<Direction> dir
52 ) noexcept
53 : id_(id), current_state_(std::move(current)), next_state_(std::move(next)),
54 read_symbols_(std::move(read)), write_symbols_(std::move(write)),
55 dir_vectors_(std::move(dir))
56 {}
57
62 [[nodiscard]] std::size_t get_id() const noexcept { return this->id_; }
63
68 [[nodiscard]] const State &get_current_state() const noexcept { return this->current_state_; }
69
74 [[nodiscard]] const State &get_next_state() const noexcept { return this->next_state_; }
75
80 [[nodiscard]] const std::vector<Symbol> &get_read_symbols() const noexcept
81 {
82 return this->read_symbols_;
83 }
84
89 [[nodiscard]] const std::vector<Symbol> &get_write_symbols() const noexcept
90 {
91 return this->write_symbols_;
92 }
93
98 [[nodiscard]] const std::vector<Direction> &get_directions() const noexcept
99 {
100 return this->dir_vectors_;
101 }
102
112 [[nodiscard]] bool operator==(const Transition &other) const noexcept
113 {
114 return this->current_state_ == other.current_state_ &&
115 this->read_symbols_ == other.read_symbols_;
116 }
117
124 [[nodiscard]] auto operator<=>(const Transition &other) const noexcept
125 {
126 if (auto cmp = this->current_state_ <=> other.current_state_; cmp != nullptr)
127 {
128 return cmp;
129 }
130 return this->read_symbols_ <=> other.read_symbols_;
131 }
132
136 void set_next_state(State next) noexcept { this->next_state_ = std::move(next); }
137
141 void set_read_symbols(std::vector<Symbol> read) noexcept
142 {
143 this->read_symbols_ = std::move(read);
144 }
145
149 void set_write_symbols(std::vector<Symbol> write) noexcept
150 {
151 this->write_symbols_ = std::move(write);
152 }
153
157 void set_directions(std::vector<Direction> dir) noexcept
158 {
159 this->dir_vectors_ = std::move(dir);
160 }
161
166 void set_read_symbol_at(std::size_t tape_idx, const Symbol &sym) noexcept(false)
167 {
168 if (tape_idx >= this->read_symbols_.size())
169 {
170 throw std::out_of_range("Tape index out of bounds for read_symbols_");
171 }
172 this->read_symbols_[tape_idx] = sym;
173 }
174
179 void set_write_symbol_at(std::size_t tape_idx, const Symbol &sym) noexcept(false)
180 {
181 if (tape_idx >= this->write_symbols_.size())
182 {
183 throw std::out_of_range("Tape index out of bounds for write_symbols_");
184 }
185 this->write_symbols_[tape_idx] = sym;
186 }
187
192 void set_direction_at(std::size_t tape_idx, const Direction &dir) noexcept(false)
193 {
194 if (tape_idx >= this->dir_vectors_.size())
195 {
196 throw std::out_of_range("Tape index out of bounds for dir_vectors_");
197 }
198 this->dir_vectors_[tape_idx] = dir;
199 }
200
201 private:
202 std::size_t id_{0};
205 std::vector<Symbol> read_symbols_;
206 std::vector<Symbol> write_symbols_;
207 std::vector<Direction> dir_vectors_;
208};
209
210#endif // TRANSITION_HPP
Represents a single state in the finite set of states (Q) of the Turing Machine.
Definition state.hpp:26
Represents a single symbol on the Turing Machine's tape.
Definition symbol.hpp:30
Represents a multi-tape transition rule (δ: Q × Γ^k → Q × Γ^k × {L, R}^k).
State next_state_
Destination state after execution.
void set_next_state(State next) noexcept
Modifies the transition's destination state.
void set_direction_at(std::size_t tape_idx, const Direction &dir) noexcept(false)
Allows granular modification of a specific tape cell.
const State & get_current_state() const noexcept
Retrieves the source state required to trigger this transition.
bool operator==(const Transition &other) const noexcept
Checks equality of transitions based on their semantic identity.
const std::vector< Direction > & get_directions() const noexcept
Retrieves the directions in which each parallel tape head must shift.
void set_directions(std::vector< Direction > dir) noexcept
Modifies the heads' movement direction.
void set_write_symbols(std::vector< Symbol > write) noexcept
Modifies the whole writing symbols block (k-tapes).
State current_state_
Source state for the transition rule.
Transition(std::size_t id, State current, State next, std::vector< Symbol > read, std::vector< Symbol > write, std::vector< Direction > dir) noexcept
Explicit constructor to define a complete multi-tape transition rule.
std::vector< Direction > dir_vectors_
Structural head shift directions vector.
void set_read_symbol_at(std::size_t tape_idx, const Symbol &sym) noexcept(false)
Allows granular modification of a specific tape cell.
std::vector< Symbol > read_symbols_
Expected vector array of symbols on the tracks.
std::size_t id_
Unique runtime identifier for UI and graph edge tracking.
const std::vector< Symbol > & get_read_symbols() const noexcept
Retrieves the collection of symbols that must be present under the tape heads.
auto operator<=>(const Transition &other) const noexcept
Lexicographical ordering of transitions for container storage.
void set_read_symbols(std::vector< Symbol > read) noexcept
Modifies the whole reading symbols block (k-tapes).
std::size_t get_id() const noexcept
Retrieves the unique identifier of this transition rule.
Transition()=default
Default constructor.
const std::vector< Symbol > & get_write_symbols() const noexcept
Retrieves the new symbols that will overwrite the current parallel tape cells.
const State & get_next_state() const noexcept
Retrieves the target state the machine will enter after executing this rule.
void set_write_symbol_at(std::size_t tape_idx, const Symbol &sym) noexcept(false)
Allows granular modification of a specific tape cell.
std::vector< Symbol > write_symbols_
New vector array of symbols to write.
Direction enum class representing the possible directions to move on a Turing Machine.
Direction
Specifies the movement direction of the tape head.
Definition direction.hpp:19
State class to represent a state within the Turing Machine's finite control.
Symbol class to represent the symbols on the Turing Machine's tape.