MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
tape.hpp
Go to the documentation of this file.
1
11#ifndef TAPE_HPP
12#define TAPE_HPP
13
14#include "direction.hpp"
15#include "string.hpp"
16#include "symbol.hpp"
17
18#include <iostream>
19#include <map>
20#include <ostream>
21
29class Tape
30{
31 public:
35 Tape() noexcept : head_pos_(0) {}
36
46 explicit Tape(String str) noexcept(false);
47
56 [[nodiscard]] Symbol read() const noexcept;
57
65 void write(Symbol symbol) noexcept(false) { this->cells_[this->head_pos_] = symbol; }
66
74 void move(Direction dir) noexcept
75 {
76 switch (dir)
77 {
78 case Direction::LEFT:
79 --this->head_pos_;
80 break;
82 ++this->head_pos_;
83 break;
84 case Direction::STAY:
85 default:
86 break;
87 }
88 }
89
94 void print(std::ostream &os = std::cout) const noexcept;
95
102 friend std::ostream &operator<<(std::ostream &os, const Tape &tape) noexcept;
103
104 private:
106 std::map<int, Symbol> cells_;
107};
108
109#endif // TAPE_HPP
Represents a formal string (w) consisting of a finite sequence of Symbols.
Definition string.hpp:29
Represents a single symbol on the Turing Machine's tape.
Definition symbol.hpp:30
Simulates the bilateral infinite tape layout of a Turing Machine.
Definition tape.hpp:30
std::map< int, Symbol > cells_
Memory cells mapping coordinates to written Symbols.
Definition tape.hpp:106
void print(std::ostream &os=std::cout) const noexcept
Renders the active layout of the tape and the head tracking to an output stream.
Definition tape.cpp:63
friend std::ostream & operator<<(std::ostream &os, const Tape &tape) noexcept
Overloaded stream insertion operator to allow direct streaming of Tape instances.
Definition tape.cpp:111
Tape() noexcept
Default constructor.
Definition tape.hpp:35
Symbol read() const noexcept
Reads the symbol currently located under the tape head.
Definition tape.cpp:48
void write(Symbol symbol) noexcept(false)
Writes a symbol into the cell currently under the tape head.
Definition tape.hpp:65
void move(Direction dir) noexcept
Shifts the tape head one cell to the left, right or make it stay.
Definition tape.hpp:74
int head_pos_
Current mathematical index coordinate of the head.
Definition tape.hpp:105
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
@ RIGHT
Move the tape head one cell to the right.
@ LEFT
Move the tape head one cell to the left.
@ STAY
Keep the tape head on the current cell.
String class representing a formal string (sequence of symbols) over an alphabet.
Symbol class to represent the symbols on the Turing Machine's tape.