ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::MultiStack< T > Class Template Reference

\( k \) stacks sharing a vector of linked cells More...

#include <ADS/stack/MultiStack.h>

Public types

using Item = T
 the type of the items
using value_type = T
 the type of the items
using const_iterator
 a constant iterator from a top down along the cells

Public member functions

 MultiStack (int k)
 \( k \) empty stacks
int stacks (void) const
 the number \( k \) of stacks
bool empty (int i) const
 true if the stack \( i \) has no item
int size (int i) const
 the number of items of the stack \( i \)
const T & top (int i) const
 the item on top of the stack \( i \)
void push (int i, const T &v)
 \( v \) on the stack \( i \); \( v \) may be an item of the stacks, e.g., top( j )
T pop (int i)
 removes the item on top of the stack \( i \) and returns it
std::ranges::subrange< const_iterator > stack (int i) const
 the items of the stack \( i \), the top first

Detailed description

template<class T>
class ADS::MultiStack< T >

\( k \) stacks sharing a vector of linked cells

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::MultiStack< T >::const_iterator
Initial value:
IndexIterator< Cell , T , &Cell::below ,
&Cell::value >
a forward iterator along the cells C of a vector linked by the index in the member Next,...
Definition Iterators.h:195

a constant iterator from a top down along the cells

◆ Item

template<class T>
using ADS::MultiStack< T >::Item = T

the type of the items

◆ value_type

template<class T>
using ADS::MultiStack< T >::value_type = T

the type of the items

Constructor and destructor documentation

◆ MultiStack()

template<class T>
ADS::MultiStack< T >::MultiStack ( int k)
explicit

\( k \) empty stacks

Precondition
\( k > 0 \)

Member function documentation

◆ empty()

template<class T>
bool ADS::MultiStack< T >::empty ( int i) const
nodiscard

true if the stack \( i \) has no item

Precondition
\( 0 \le i < k \)

◆ pop()

template<class T>
T ADS::MultiStack< T >::pop ( int i)

removes the item on top of the stack \( i \) and returns it

In pseudocode (as push()), the cell given back to the free list:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}( i ) \cr 2 & \quad c \gets \mathit{top}[ i ];\ v \gets C[ c ].\mathit{value}; \ \mathit{top}[ i ] \gets C[ c ].\mathit{below} \cr 3 & \quad C[ c ].\mathit{below} \gets \mathit{free};\ \mathit{free} \gets c\quad \triangleright\ c \text{ back on the free list} \cr 4 & \quad \textbf{return}\ v \end{array}} \]

Precondition
\( 0 \le i < k \), the stack \( i \) is not empty
Postcondition
one item less in the stack \( i \)

◆ push()

template<class T>
void ADS::MultiStack< T >::push ( int i,
const T & v )

\( v \) on the stack \( i \); \( v \) may be an item of the stacks, e.g., top( j )

In pseudocode (exercise 4.7, with a free list as in Knuth, section 2.2.3), \( C \) the cells, \( \mathit{top}[ i ] \) the top cell of the stack \( i \), -1 for none:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( i , v ) \cr 2 & \quad \textbf{if}\ \mathit{free} = -1\ \textbf{then}\ c \gets \text{a new cell at the end of } C\quad \triangleright \ \text{enlarging } C \text{ if full} \cr 3 & \quad \textbf{else}\ c \gets \mathit{free};\ \mathit{free} \gets C[ c ].\mathit{below} \cr 4 & \quad C[ c ] \gets ( v , \mathit{top}[ i ] );\ \mathit{top}[ i ] \gets c \end{array}} \]

Precondition
\( 0 \le i < k \)
Postcondition
one item more in the stack \( i \)

◆ size()

template<class T>
int ADS::MultiStack< T >::size ( int i) const
nodiscard

the number of items of the stack \( i \)

Precondition
\( 0 \le i < k \)

◆ stack()

template<class T>
std::ranges::subrange< const_iterator > ADS::MultiStack< T >::stack ( int i) const
inlinenodiscard

the items of the stack \( i \), the top first

Precondition
\( 0 \le i < k \)

◆ stacks()

template<class T>
int ADS::MultiStack< T >::stacks ( void ) const
nodiscard

the number \( k \) of stacks

◆ top()

template<class T>
const T & ADS::MultiStack< T >::top ( int i) const
nodiscard

the item on top of the stack \( i \)

Precondition
\( 0 \le i < k \), the stack \( i \) is not empty

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