MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
tm.hpp
Go to the documentation of this file.
1
11#ifndef TURING_MACHINE_HPP
12#define TURING_MACHINE_HPP
13
14#include "alphabet.hpp"
15#include "state.hpp"
16#include "tape.hpp"
17#include "transition.hpp"
18
19#include <cstddef>
20#include <set>
21#include <string_view>
22#include <unordered_map>
23#include <vector>
24
33{
34 public:
38 TuringMachine() noexcept : tape_count_(1) {}
39
48 explicit TuringMachine(
49 Alphabet input_alpha,
50 Alphabet tape_alpha,
51 std::set<State> states,
52 State initial_state,
53 std::size_t tape_count
54 ) noexcept(false);
55
64 [[nodiscard]] bool load_input(std::string_view input) noexcept(false);
65
74 bool step() noexcept(false);
75
80 void run() noexcept(false);
81
86 [[nodiscard]] const Alphabet &get_input_alphabet() const noexcept
87 {
88 return this->input_alphabet_;
89 }
90
95 [[nodiscard]] const Alphabet &get_tape_alphabet() const noexcept
96 {
97 return this->tape_alphabet_;
98 }
99
104 [[nodiscard]] const std::set<State> &get_states() const noexcept { return this->states_; }
105
110 [[nodiscard]] const State &get_start_state() const noexcept { return this->start_state_; }
111
116 [[nodiscard]] const State &get_current_state() const noexcept { return this->current_state_; }
117
122 [[nodiscard]] const std::vector<Tape> &get_tapes() const noexcept { return this->tapes_; }
123
129 [[nodiscard]] const std::unordered_map<std::size_t, Transition> &
130 get_transitions() const noexcept
131 {
132 return this->transitions_;
133 }
134
139 [[nodiscard]] std::size_t get_tape_count() const noexcept { return this->tape_count_; }
140
159 std::size_t add_transition(
160 State current,
161 State next,
162 std::vector<Symbol> read,
163 std::vector<Symbol> write,
164 std::vector<Direction> dir
165 ) noexcept(false)
166 {
167 auto [it, success] = this->transitions_.emplace(
168 this->id_counter_,
170 this->id_counter_,
171 std::move(current),
172 std::move(next),
173 std::move(read),
174 std::move(write),
175 std::move(dir)
176 )
177 );
178 if (success)
179 {
180 return this->id_counter_++;
181 }
182 return it->first; // Return existing ID if transition already exists
183 }
184
190 [[nodiscard]] Transition *get_transition(std::size_t id) noexcept
191 {
192 auto it = this->transitions_.find(id);
193 if (it != this->transitions_.end())
194 {
195 return &(it->second);
196 }
197 return nullptr;
198 }
199
206 bool remove_transition(std::size_t id) noexcept { return this->transitions_.erase(id) > 0; }
207
214 [[nodiscard]] bool contains_transition(std::size_t id) const noexcept
215 {
216 return this->transitions_.contains(id);
217 }
218
219 private:
220 std::size_t id_counter_{0};
223 std::set<State> states_;
226 std::vector<Tape> tapes_;
227 std::unordered_map<std::size_t, Transition>
229 std::size_t tape_count_;
230};
231
232#endif // TURING_MACHINE_HPP
Alphabet class to manage the collection of valid symbols for the Turing Machine.
Represents the formal alphabet ( or ) of a Turing Machine.
Definition alphabet.hpp:27
Represents a single state in the finite set of states (Q) of the Turing Machine.
Definition state.hpp:26
Represents a multi-tape transition rule (δ: Q × Γ^k → Q × Γ^k × {L, R}^k).
The central execution engine representing a deterministic Multi-Tape Turing Machine.
Definition tm.hpp:33
Alphabet tape_alphabet_
Structural execution workspace vocabulary ( ).
Definition tm.hpp:222
const std::set< State > & get_states() const noexcept
Retrieves the complete formal set of legal operational state conditions.
Definition tm.hpp:104
std::size_t id_counter_
Unique runtime identifier counter for transitions.
Definition tm.hpp:220
bool step() noexcept(false)
Executes a single computational transition step across all parallel tapes.
Definition tm.cpp:61
bool load_input(std::string_view input) noexcept(false)
Validates an input word against the input alphabet and loads it onto Tape 0.
Definition tm.cpp:41
const Alphabet & get_input_alphabet() const noexcept
Retrieves the formal input validation alphabet.
Definition tm.hpp:86
const std::vector< Tape > & get_tapes() const noexcept
Retrieves the entire collection layout of parallel tape tracks.
Definition tm.hpp:122
const Alphabet & get_tape_alphabet() const noexcept
Retrieves the wider operational tape workspace alphabet.
Definition tm.hpp:95
void run() noexcept(false)
Loops execution triggers calling step() continuously until a halting condition occurs or the safety s...
Definition tm.cpp:111
std::vector< Tape > tapes_
Collection of k dynamic memory layouts.
Definition tm.hpp:226
State current_state_
Active contextual processing marker.
Definition tm.hpp:225
std::set< State > states_
Valid configurations collection.
Definition tm.hpp:223
Alphabet input_alphabet_
Input validation vocabulary ( ).
Definition tm.hpp:221
const std::unordered_map< std::size_t, Transition > & get_transitions() const noexcept
Retrieves the entire lookup table of transitions.
Definition tm.hpp:130
TuringMachine() noexcept
Default constructor.
Definition tm.hpp:38
State start_state_
Factory default backup tracking state.
Definition tm.hpp:224
std::size_t tape_count_
Cardinality multiplier tracking active tapes (k).
Definition tm.hpp:229
const State & get_start_state() const noexcept
Retrieves the factory default initial state configuration.
Definition tm.hpp:110
bool contains_transition(std::size_t id) const noexcept
Checks whether the machine contains a specific transition.
Definition tm.hpp:214
bool remove_transition(std::size_t id) noexcept
Removes a transition from the machine.
Definition tm.hpp:206
std::size_t get_tape_count() const noexcept
Retrieves the total cardinality of parallel tapes operating inside the machine.
Definition tm.hpp:139
const State & get_current_state() const noexcept
Retrieves the active operational state context marker.
Definition tm.hpp:116
Transition * get_transition(std::size_t id) noexcept
Retrieves a pointer to a transition by its unique runtime identifier.
Definition tm.hpp:190
std::size_t add_transition(State current, State next, std::vector< Symbol > read, std::vector< Symbol > write, std::vector< Direction > dir) noexcept(false)
Adds a transition to the machine.
Definition tm.hpp:159
std::unordered_map< std::size_t, Transition > transitions_
Lookup table for transitions by ID.
Definition tm.hpp:228
State class to represent a state within the Turing Machine's finite control.
Tape class representing the infinite memory structure of the Turing Machine.
Transition class representing a multi-tape rule in the TM's transition function.