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

a STACK with its least item in \( O( 1 ) \) time More...

#include <ADS/stack/MinStack.h>

Public types

using Item = T
 the type of the items

Public member functions

 MinStack (C less=C())
 the empty stack, ordered by less
bool empty (void) const
 true if there is no item
std::size_t size (void) const
 the number \( n \) of items
const T & top (void) const
 the item on top
const T & min (void) const
 the least item
void push (const T &x)
 puts \( x \) on top, and among the minima if it is not larger than the least item
T pop (void)
 removes the item on top and returns it, from the minima too if it is their top
void clear (void)
 removes all the items
const std::vector< T > & items (void) const
 the items from the bottom to the top
std::size_t minima (void) const
 the number of minima kept, \( n \) at most

Detailed description

template<class T, class C = std::less<>>
class ADS::MinStack< T, C >

a STACK with its least item in \( O( 1 ) \) time

Member typedef documentation

◆ Item

template<class T, class C = std::less<>>
using ADS::MinStack< T, C >::Item = T

the type of the items

Constructor and destructor documentation

◆ MinStack()

template<class T, class C = std::less<>>
ADS::MinStack< T, C >::MinStack ( C less = C())
inlineexplicit

the empty stack, ordered by less

Member function documentation

◆ clear()

template<class T, class C = std::less<>>
void ADS::MinStack< T, C >::clear ( void )
inline

removes all the items

◆ empty()

template<class T, class C = std::less<>>
bool ADS::MinStack< T, C >::empty ( void ) const
inlinenodiscard

true if there is no item

◆ items()

template<class T, class C = std::less<>>
const std::vector< T > & ADS::MinStack< T, C >::items ( void ) const
inlinenodiscard

the items from the bottom to the top

◆ min()

template<class T, class C = std::less<>>
const T & ADS::MinStack< T, C >::min ( void ) const
inlinenodiscard

the least item

Precondition
the stack is not empty

◆ minima()

template<class T, class C = std::less<>>
std::size_t ADS::MinStack< T, C >::minima ( void ) const
inlinenodiscard

the number of minima kept, \( n \) at most

◆ pop()

template<class T, class C = std::less<>>
T ADS::MinStack< T, C >::pop ( void )

removes the item on top and returns it, from the minima too if it is their top

In pseudocode (Morin, exercise 3.14):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad x \gets \mathrm{pop}( A ) \cr 3 & \quad \textbf{if not}\ \mathrm{top}( M ) < x\ \textbf{then} \ \mathrm{pop}( M ) \cr 4 & \quad \textbf{return}\ x \end{array}} \]

Precondition
the stack is not empty
Postcondition
one item less

◆ push()

template<class T, class C = std::less<>>
void ADS::MinStack< T, C >::push ( const T & x)

puts \( x \) on top, and among the minima if it is not larger than the least item

In pseudocode (Morin, exercise 3.14), \( A \) the items and \( M \) the minima:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( x ) \cr 2 & \quad \mathrm{push}( A , x ) \cr 3 & \quad \textbf{if}\ M = \emptyset\ \textbf{or not}\ \mathrm{top}( M ) < x\ \textbf{then}\ \mathrm{push}( M , x ) \end{array}} \]

Postcondition
one item more, \( x \) on top

◆ size()

template<class T, class C = std::less<>>
std::size_t ADS::MinStack< T, C >::size ( void ) const
inlinenodiscard

the number \( n \) of items

◆ top()

template<class T, class C = std::less<>>
const T & ADS::MinStack< T, C >::top ( void ) const
inlinenodiscard

the item on top

Precondition
the stack is not empty

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