TuringSim
C++ framework to simulate abstract computing models
Public Member Functions | Static Public Attributes | Friends | List of all members
TuringSim::Memory::Stack::Stack< T > Class Template Reference

Class to represent a stack memory. More...

#include <memory/stack.h>

Inheritance diagram for TuringSim::Memory::Stack::Stack< T >:
Inheritance graph
[legend]
Collaboration diagram for TuringSim::Memory::Stack::Stack< T >:
Collaboration graph
[legend]

Public Member Functions

constexpr Stack ()
 Construct an empty stack.
 
constexpr Stack (const Stack< T > &other)
 Copy a stack. More...
 
constexpr Stack (Stack< T > &&other)
 Move a stack. More...
 
constexpr Stack< T > & operator= (const Stack< T > &other)
 Copy a stack. More...
 
constexpr Stack< T > & operator= (Stack< T > &&other)
 Move a stack. More...
 
 ~Stack ()=default
 Destruct the stack.
 
constexpr void push (const T &symbol)
 Push an element on the stack. More...
 
constexpr void push (T &&symbol)
 Push an element on the stack. More...
 
constexpr void pushs (const std::vector< T > &symbol)
 Push multiple elements on the stack. More...
 
constexpr void pushs (std::vector< T > &&symbol)
 Push multiple elements on the stack. More...
 
const T & top () const
 Get the top element of the stack. More...
 
std::optional< T > top_opt () const
 Get the top element of the stack. More...
 
std::vector< T > tops (size_t nb) const
 Get the top elements of the stack. More...
 
std::optional< std::vector< T > > tops_opt (size_t nb) const
 Get the top elements of the stack. More...
 
void pop ()
 Pop the top element of the stack. More...
 
void pops (size_t nb)
 Pop multiple top elements of the stack. More...
 
constexpr bool empty () const noexcept
 Test if the stack is empty. More...
 
void assertNonEmpty () const
 Test if the stack is empty. More...
 
- Public Member Functions inherited from TuringSim::Memory::MemoryStructure< T, Stack< T >, StackObserver< T >, StackModifier< T > >
 ~MemoryStructure ()
 Release current observers and modifier.
 
const std::set< Observer * > & getObservers () const noexcept
 Gets the set of observers. More...
 
const std::optional< Modifier * > & getModifier () const noexcept
 Gets the optional modifier. More...
 

Static Public Attributes

static constexpr size_t MinAllocatedStackSize = 10
 The size allocated when constructing a stack. More...
 

Friends

class StackObserver< T >
 Observer for the Stack class.
 
class StackModifier< T >
 Modifier for the Stack class.
 

Additional Inherited Members

- Public Types inherited from TuringSim::Memory::MemoryStructure< T, Stack< T >, StackObserver< T >, StackModifier< T > >
typedef StackObserver< T > Observer
 The type of observers.
 
typedef StackModifier< T > Modifier
 The type of modifiers.
 
typedef T SymbolType
 The alphabet type. More...
 
- Public Attributes inherited from TuringSim::Memory::MemoryStructure< T, Stack< T >, StackObserver< T >, StackModifier< T > >
friend Observer
 Observer class.
 
friend Modifier
 Modifier class.
 

Detailed Description

template<typename T>
class TuringSim::Memory::Stack::Stack< T >

Class to represent a stack memory.

Template Parameters
TType representing the working alphabet

Unlike TuringSim::Memory::Tape, Stack can automatically free memory when the used segment is small enough compared to the allocated size.

Definition at line 76 of file stack.h.

Constructor & Destructor Documentation

◆ Stack() [1/2]

template<typename T >
constexpr TuringSim::Memory::Stack::Stack< T >::Stack ( const Stack< T > &  other)
constexpr

Copy a stack.

Parameters
[in]otherthe stack to copy.

Definition at line 218 of file stack.h.

◆ Stack() [2/2]

template<typename T >
constexpr TuringSim::Memory::Stack::Stack< T >::Stack ( Stack< T > &&  other)
constexpr

Move a stack.

Parameters
[in,out]otherthe stack to move.

Definition at line 227 of file stack.h.

Member Function Documentation

◆ assertNonEmpty()

template<typename T >
void TuringSim::Memory::Stack::Stack< T >::assertNonEmpty

Test if the stack is empty.

Exceptions
EmptyStackExceptionif the stack is empty.

Definition at line 386 of file stack.h.

◆ empty()

template<typename T >
constexpr bool TuringSim::Memory::Stack::Stack< T >::empty
constexprnoexcept

Test if the stack is empty.

Returns
true if and only if the stack is empty.

Definition at line 381 of file stack.h.

◆ operator=() [1/2]

template<typename T >
constexpr Stack< T > & TuringSim::Memory::Stack::Stack< T >::operator= ( const Stack< T > &  other)
constexpr

Copy a stack.

Parameters
[in]otherthe stack to copy.
Returns
a reference to *this.

Definition at line 236 of file stack.h.

◆ operator=() [2/2]

template<typename T >
constexpr Stack< T > & TuringSim::Memory::Stack::Stack< T >::operator= ( Stack< T > &&  other)
constexpr

Move a stack.

Parameters
[in,out]otherthe stack to move.
Returns
a reference to *this.

Definition at line 248 of file stack.h.

◆ pop()

template<typename T >
void TuringSim::Memory::Stack::Stack< T >::pop

Pop the top element of the stack.

Exceptions
EmptyStackExceptionif the stack is empty.

Definition at line 355 of file stack.h.

◆ pops()

template<typename T >
void TuringSim::Memory::Stack::Stack< T >::pops ( size_t  nb)

Pop multiple top elements of the stack.

Parameters
[in]nbnumber of elements to pop.
Exceptions
EmptyStackExceptionif the stack has not at least nb elements.

Definition at line 368 of file stack.h.

◆ push() [1/2]

template<typename T >
constexpr void TuringSim::Memory::Stack::Stack< T >::push ( const T &  symbol)
constexpr

Push an element on the stack.

Parameters
[in]symbolthe symbol to copy on the stack

Definition at line 260 of file stack.h.

◆ push() [2/2]

template<typename T >
constexpr void TuringSim::Memory::Stack::Stack< T >::push ( T &&  symbol)
constexpr

Push an element on the stack.

Parameters
[in,out]symbolthe symbol to move on the stack.

Definition at line 271 of file stack.h.

◆ pushs() [1/2]

template<typename T >
constexpr void TuringSim::Memory::Stack::Stack< T >::pushs ( const std::vector< T > &  symbol)
constexpr

Push multiple elements on the stack.

Parameters
[in]symbolthe symbol to copy on the stack

Definition at line 282 of file stack.h.

◆ pushs() [2/2]

template<typename T >
constexpr void TuringSim::Memory::Stack::Stack< T >::pushs ( std::vector< T > &&  symbol)
constexpr

Push multiple elements on the stack.

Parameters
[in,out]symbolthe symbol to move on the stack.

Definition at line 296 of file stack.h.

◆ top()

template<typename T >
const T & TuringSim::Memory::Stack::Stack< T >::top

Get the top element of the stack.

Returns
get a const reference to the top of the stack.
Exceptions
EmptyStackExceptionif the stack is empty.

The reference is valid as long as it is not popped from the stack, and the stack is not reallocated: either free is disabled or the stack is not modified.

Definition at line 323 of file stack.h.

◆ top_opt()

template<typename T >
std::optional< T > TuringSim::Memory::Stack::Stack< T >::top_opt

Get the top element of the stack.

Returns
the optional top element.

The option is empty iff the stack is empty.

Definition at line 331 of file stack.h.

◆ tops()

template<typename T >
std::vector< T > TuringSim::Memory::Stack::Stack< T >::tops ( size_t  nb) const

Get the top elements of the stack.

Parameters
[in]nbnumber of elements to return.
Returns
a vector of nb elements.
Exceptions
EmptyStackExceptionif the stack has not at least nb elements.

Definition at line 339 of file stack.h.

Here is the caller graph for this function:

◆ tops_opt()

template<typename T >
std::optional< std::vector< T > > TuringSim::Memory::Stack::Stack< T >::tops_opt ( size_t  nb) const

Get the top elements of the stack.

Parameters
[in]nbnumber of elements to return.
Returns
a vector of nb elements if the stack is big enough. An empty option otherwise.

Definition at line 347 of file stack.h.

Member Data Documentation

◆ MinAllocatedStackSize

template<typename T >
constexpr size_t TuringSim::Memory::Stack::Stack< T >::MinAllocatedStackSize = 10
staticconstexpr

The size allocated when constructing a stack.

This value is totally arbitrary but seems to suit.

Definition at line 93 of file stack.h.


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