a STACK with its least item in \( O( 1 ) \) time
More...
#include <ADS/stack/MinStack.h>
|
| using | Item = T |
| | the type of the items
|
|
| | 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
|
template<class T, class C = std::less<>>
class ADS::MinStack< T, C >
a STACK with its least item in \( O( 1 ) \) time
◆ Item
template<class T, class C = std::less<>>
◆ MinStack()
template<class T, class C = std::less<>>
the empty stack, ordered by less
◆ clear()
template<class T, class C = std::less<>>
◆ empty()
template<class T, class C = std::less<>>
◆ 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<>>
the least item
- Precondition
- the stack is not empty
◆ minima()
template<class T, class C = std::less<>>
the number of minima kept, \( n \) at most
◆ pop()
template<class T, class C = std::less<>>
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<>>
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<>>
the number \( n \) of items
◆ top()
template<class T, class C = std::less<>>
the item on top
- Precondition
- the stack is not empty
The documentation for this class was generated from the following file: