ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MultiStack.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File MultiStack.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
29/*--------------------------------------------------------------------------*/
30/*----------------------------- DEFINITIONS --------------------------------*/
31/*--------------------------------------------------------------------------*/
32
33#ifndef __ADS_MultiStack
34 #define __ADS_MultiStack
35/* self-identification: #endif constant */
36
37/*--------------------------------------------------------------------------*/
38/*------------------------------ INCLUDES ----------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#include "ADS/Contract.h"
42#include "ADS/Iterators.h"
43
44#include <ranges>
45#include <utility>
46#include <vector>
47
48/*--------------------------------------------------------------------------*/
49/*--------------------------- NAMESPACE ------------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52namespace ADS
53{
54
55/*--------------------------------------------------------------------------*/
56/*---------------------------- CLASS MultiStack ----------------------------*/
57/*--------------------------------------------------------------------------*/
58
60template< class T >
62{
63
64/*--------------------------------------------------------------------------*/
65 public:
66 using Item = T;
67
70 explicit MultiStack( int k );
71
73 [[nodiscard]] int stacks( void ) const;
74
77 [[nodiscard]] bool empty( int i ) const;
78
81 [[nodiscard]] int size( int i ) const;
82
85 [[nodiscard]] const T & top( int i ) const;
86
107 void push( int i , const T & v );
108
124 T pop( int i );
125
126/*--------------------------------------------------------------------------*/
127 private:
129 struct Cell
130 {
131 T value;
132 int below;
133 };
134
135 public:
136/*------------------------- THE STANDARD INTERFACE -------------------------*/
137
138 // a constant view of each stack, from the top to the bottom, forward
139
140 using value_type = T;
142 using const_iterator = IndexIterator< Cell , T , &Cell::below ,
143 &Cell::value >;
144
147 [[nodiscard]] std::ranges::subrange< const_iterator > stack( int i ) const {
148 ADS_PRE( ( i >= 0 ) && ( i < stacks() ) );
149 return( std::ranges::subrange( const_iterator( &f_C , f_top[ i ] ) ,
150 const_iterator( &f_C , -1 ) ) );
151 }
152
153 private:
155 [[nodiscard]] bool partitioned( void ) const;
156
157 std::vector< Cell > f_C;
158 std::vector< int > f_top;
159 std::vector< int > f_size;
160 int f_free = -1;
161
162 }; // end( class MultiStack )
163
164/*--------------------------------------------------------------------------*/
165
166} // end( namespace ADS )
167
168/*--------------------------------------------------------------------------*/
169/*-------------------------- IMPLEMENTATION --------------------------------*/
170/*--------------------------------------------------------------------------*/
171
172#include "ADS/stack/impl/MultiStack.tpp"
173
174/*--------------------------------------------------------------------------*/
175
176#endif /* MultiStack.h included */
177
178/*--------------------------------------------------------------------------*/
179/*------------------------- End File MultiStack.h --------------------------*/
180/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
a forward iterator along the cells C of a vector linked by the index in the member Next,...
Definition Iterators.h:195
T Item
the type of the items
Definition MultiStack.h:66
void push(int i, const T &v)
on the stack ; may be an item of the stacks, e.g., top( j )
bool empty(int i) const
true if the stack has no item
MultiStack(int k)
empty stacks
T value_type
the type of the items
Definition MultiStack.h:140
T pop(int i)
removes the item on top of the stack and returns it
std::ranges::subrange< const_iterator > stack(int i) const
the items of the stack , the top first
Definition MultiStack.h:147
IndexIterator< Cell, T, &Cell::below, &Cell::value > const_iterator
a constant iterator from a top down along the cells
Definition MultiStack.h:142
int size(int i) const
the number of items of the stack
int stacks(void) const
the number of stacks
const T & top(int i) const
the item on top of the stack
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96