MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
Tape Class Reference

Simulates the bilateral infinite tape layout of a Turing Machine. More...

#include <tape.hpp>

Collaboration diagram for Tape:

Public Member Functions

void move (Direction dir) noexcept
 Shifts the tape head one cell to the left, right or make it stay.
 
void print (std::ostream &os=std::cout) const noexcept
 Renders the active layout of the tape and the head tracking to an output stream.
 
Symbol read () const noexcept
 Reads the symbol currently located under the tape head.
 
 Tape () noexcept
 Default constructor.
 
 Tape (String str) noexcept(false)
 Constructs a Tape populated with an initial formal String.
 
void write (Symbol symbol) noexcept(false)
 Writes a symbol into the cell currently under the tape head.
 

Private Attributes

std::map< int, Symbolcells_
 Memory cells mapping coordinates to written Symbols.
 
int head_pos_
 Current mathematical index coordinate of the head.
 

Friends

std::ostream & operator<< (std::ostream &os, const Tape &tape) noexcept
 Overloaded stream insertion operator to allow direct streaming of Tape instances.
 

Detailed Description

Simulates the bilateral infinite tape layout of a Turing Machine.

Uses a dynamic std::map indexed by signed integers to support infinite expansion toward both negative and positive coordinates with logarithmic lookup overhead.

Definition at line 29 of file tape.hpp.

Constructor & Destructor Documentation

◆ Tape() [1/2]

Tape::Tape ( )
inlinenoexcept

Default constructor.

Initializes an empty tape with the head at position 0.

Definition at line 35 of file tape.hpp.

◆ Tape() [2/2]

Tape::Tape ( String  str)
explicit

Constructs a Tape populated with an initial formal String.

Custom constructor to load a formal Word into the machine's memory.

Places the string's symbols sequentially starting at index 0 and sets the head to 0.

Parameters
strThe formal String sequence to load onto the tape.
Exceptions
std::out_of_rangeIf the input string length exceeds the maximum signed integer capacity of the tape.

Uses C++20 views::enumerate to extract both the structural index and the Symbol object, populating the map from coordinate 0 onward.

Parameters
strThe formal String sequence to load onto the tape.
Exceptions
std::out_of_rangeIf the input string length exceeds the maximum signed integer capacity of the tape.

Definition at line 27 of file tape.cpp.

Member Function Documentation

◆ move()

void Tape::move ( Direction  dir)
inlinenoexcept

Shifts the tape head one cell to the left, right or make it stay.

Passed by value since Direction is an enum class.

Parameters
dirThe movement direction (LEFT, RIGHT or STAY).

Definition at line 74 of file tape.hpp.

References head_pos_, LEFT, RIGHT, and STAY.

◆ print()

void Tape::print ( std::ostream &  os = std::cout) const
noexcept

Renders the active layout of the tape and the head tracking to an output stream.

Renders a structural blueprint of the active tape cells to any std::ostream.

Parameters
osThe target output stream (defaults to std::cout).

Automatically wraps current track margins based on the dynamic map bounds and the current hardware head displacement coordinates.

Definition at line 63 of file tape.cpp.

References kBlank.

◆ read()

Symbol Tape::read ( ) const
noexcept

Reads the symbol currently located under the tape head.

Scans the active cell under the simulation head.

If the current coordinate has not been modified yet, it assumes the cell belongs to the infinite blank space and safely returns a kBlank symbol.

Returns
Symbol The symbol scanned at the current head position.

Safeguards the infinite tape constraint by looking up coordinates via std::map::find. If the element is missing, it dynamically rolls a virtual kBlank context.

Definition at line 48 of file tape.cpp.

References cells_, head_pos_, and kBlank.

◆ write()

void Tape::write ( Symbol  symbol)
inline

Writes a symbol into the cell currently under the tape head.

Passed by value since Symbol is a lightweight, trivially-copyable type.

Parameters
symbolThe Symbol instance to write.
Exceptions
std::bad_allocIf the internal map fails to allocate memory for a new coordinate node.

Definition at line 65 of file tape.hpp.

References cells_, and head_pos_.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const Tape tape 
)
friend

Overloaded stream insertion operator to allow direct streaming of Tape instances.

Parameters
osThe target output stream.
tapeThe Tape instance to render.
Returns
std::ostream& A reference to the modified output stream.

Seamlessly pipes the custom Tape print layout directly into standard I/O pipelines.

Definition at line 111 of file tape.cpp.

Member Data Documentation

◆ cells_

std::map<int, Symbol> Tape::cells_
private

Memory cells mapping coordinates to written Symbols.

Definition at line 106 of file tape.hpp.

Referenced by read(), and write().

◆ head_pos_

int Tape::head_pos_
private

Current mathematical index coordinate of the head.

Definition at line 105 of file tape.hpp.

Referenced by move(), read(), and write().


The documentation for this class was generated from the following files: