GCSIM
Public Member Functions | Private Member Functions | Private Attributes | List of all members
indexed_stack< T > Class Template Reference

hybrid implementation of an array and stack. More...

#include <indexed-stack.hpp>

Public Member Functions

 indexed_stack ()
 creates the instance of the indexed_stack.
 
 ~indexed_stack () noexcept
 destroys the indexed_stack object.
 
 indexed_stack (const indexed_stack &)=delete
 deleted copy constructor.
 
indexed_stackoperator= (const indexed_stack &)=delete
 deleted assignment operator.
 
 indexed_stack (indexed_stack &&other) noexcept
 constructs new indexed_stack from an existing one.
 
indexed_stackoperator= (indexed_stack &&other) noexcept
 constructs new indexed_stack by assigning it an existing one.
 
template<typename TT >
requires std::is_constructible_v<T, TT&&>
void push (TT &&value)
 pushes new value to the top of the stack.
 
void pop ()
 removes the element from the top of the stack.
 
T & peek ()
 getter for the element on the top of the stack.
 
const T & peek () const
 getter for the element on the top of the stack.
 
bool empty () const noexcept
 checks if the stack is empty.
 
T & operator[] (size_t i)
 operator for direct indexing.
 
const T & operator[] (size_t i) const
 operator for direct indexing.
 
size_t get_size () const noexcept
 getter for the size of the stack.
 
size_t get_capacity () const noexcept
 getter for the capacity of the stack.
 
T * begin ()
 getter for the bottom element of the stack.
 
T * end ()
 getter for the end of the stack.
 
const T * begin () const
 getter for the bottom element of the stack.
 
const T * end () const
 getter for the end of the stack.
 

Private Member Functions

void delete_range (size_t start, size_t end) noexcept
 frees elements on the stack that are in certain range.
 
void resize (size_t new_capacity)
 updates the capacity of the stack based on a size.
 

Private Attributes

T * data
 pointer to the bottom element of the stack.
 
size_t size
 number of the elements on the stack.
 
size_t capacity
 allocated space for the stack.
 

Detailed Description

template<typename T>
class indexed_stack< T >

hybrid implementation of an array and stack.

Template Parameters
T- type of the data stored on the stack.

Constructor & Destructor Documentation

◆ indexed_stack() [1/3]

template<typename T >
indexed_stack< T >::indexed_stack ( )
inline

creates the instance of the indexed_stack.

preallocates the memory for DEFAULT_STACK_CAPACITY elements; default size 0.

◆ ~indexed_stack()

template<typename T >
indexed_stack< T >::~indexed_stack ( )
inlinenoexcept

destroys the indexed_stack object.

frees all elements on the stack, frees the memory allocated for the stack.

◆ indexed_stack() [2/3]

template<typename T >
indexed_stack< T >::indexed_stack ( const indexed_stack< T > &  )
delete

deleted copy constructor.

◆ indexed_stack() [3/3]

template<typename T >
indexed_stack< T >::indexed_stack ( indexed_stack< T > &&  other)
inlinenoexcept

constructs new indexed_stack from an existing one.

Parameters
other- rvalue of the existing indexed_stack

moves ownership of the data, size and capacity from other to this.

Member Function Documentation

◆ begin() [1/2]

template<typename T >
T * indexed_stack< T >::begin ( )
inline

getter for the bottom element of the stack.

Returns
the pointer to the bottom element of the stack.

◆ begin() [2/2]

template<typename T >
const T * indexed_stack< T >::begin ( ) const
inline

getter for the bottom element of the stack.

Returns
the const pointer to the bottom element of the stack.

◆ delete_range()

template<typename T >
void indexed_stack< T >::delete_range ( size_t  start,
size_t  end 
)
inlineprivatenoexcept

frees elements on the stack that are in certain range.

Parameters
start- starting index.
end- ending index (excluded).

◆ empty()

template<typename T >
bool indexed_stack< T >::empty ( ) const
inlinenoexcept

checks if the stack is empty.

Returns
true if stack is empty; false otherwise.

◆ end() [1/2]

template<typename T >
T * indexed_stack< T >::end ( )
inline

getter for the end of the stack.

Returns
the pointer to the end of the stack.

◆ end() [2/2]

template<typename T >
const T * indexed_stack< T >::end ( ) const
inline

getter for the end of the stack.

Returns
the const pointer to the end of the stack.

◆ get_capacity()

template<typename T >
size_t indexed_stack< T >::get_capacity ( ) const
inlinenoexcept

getter for the capacity of the stack.

Returns
capacity of the stack.

◆ get_size()

template<typename T >
size_t indexed_stack< T >::get_size ( ) const
inlinenoexcept

getter for the size of the stack.

Returns
number of elements on the stack.

◆ operator=() [1/2]

template<typename T >
indexed_stack & indexed_stack< T >::operator= ( const indexed_stack< T > &  )
delete

deleted assignment operator.

◆ operator=() [2/2]

template<typename T >
indexed_stack & indexed_stack< T >::operator= ( indexed_stack< T > &&  other)
inlinenoexcept

constructs new indexed_stack by assigning it an existing one.

Parameters
other- rvalue of the existing indexed_stack

moves ownership of the data, size and capacity from other to this.

◆ operator[]() [1/2]

template<typename T >
T & indexed_stack< T >::operator[] ( size_t  i)
inline

operator for direct indexing.

Parameters
i- position of the element on the stack; calculated from the bottom.
Returns
reference to an element on the stack.
Exceptions
std::out_of_rangewhen index is bigger than or equal to size.

◆ operator[]() [2/2]

template<typename T >
const T & indexed_stack< T >::operator[] ( size_t  i) const
inline

operator for direct indexing.

Parameters
i- position of the element on the stack; calculated from the bottom.
Returns
const reference to an element on the stack.
Exceptions
std::out_of_rangewhen index is bigger than or equal to size.

◆ peek() [1/2]

template<typename T >
T & indexed_stack< T >::peek ( )
inline

getter for the element on the top of the stack.

Returns
the reference to the top element on the stack.
Exceptions
std::out_of_rangewhen stack is empty.

◆ peek() [2/2]

template<typename T >
const T & indexed_stack< T >::peek ( ) const
inline

getter for the element on the top of the stack.

Returns
the const reference to the top element on the stack.
Exceptions
std::out_of_rangewhen stack is empty.

◆ pop()

template<typename T >
void indexed_stack< T >::pop ( )
inline

removes the element from the top of the stack.

if 75% of capacity is free, stack is reallocated and capacity is half the previous capacity; doesn't go below DEFAULT_STACK_CAPACITY.

Exceptions
std::out_of_rangewhen stack is empty.

◆ push()

template<typename T >
template<typename TT >
requires std::is_constructible_v<T, TT&&>
void indexed_stack< T >::push ( TT &&  value)
inline

pushes new value to the top of the stack.

Template Parameters
TT- Value forwarding type.
Parameters
value- const reference object being added to the stack.

if size reaches capacity, capacity is doubled.

◆ resize()

template<typename T >
void indexed_stack< T >::resize ( size_t  new_capacity)
inlineprivate

updates the capacity of the stack based on a size.

Parameters
new_capacity- new capacity of the stack, can't go below DEFAULT_STACK_CAPACITY.
Exceptions
std::bad_allocwhen ::operator new fails to allocate memory.
std::invalid_argumentif new_capacity is below DEFAULT_STACK_CAPACITY

Member Data Documentation

◆ capacity

template<typename T >
size_t indexed_stack< T >::capacity
private

allocated space for the stack.

◆ data

template<typename T >
T* indexed_stack< T >::data
private

pointer to the bottom element of the stack.

◆ size

template<typename T >
size_t indexed_stack< T >::size
private

number of the elements on the stack.


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