ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ArrayStack.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ArrayStack.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
31/*--------------------------------------------------------------------------*/
32/*----------------------------- DEFINITIONS --------------------------------*/
33/*--------------------------------------------------------------------------*/
34
35#ifndef __ADS_ArrayStack
36 #define __ADS_ArrayStack
37/* self-identification: #endif constant */
38
39/*--------------------------------------------------------------------------*/
40/*------------------------------ INCLUDES ----------------------------------*/
41/*--------------------------------------------------------------------------*/
42
43#include "ADS/Contract.h"
44
45#include <cstddef>
46#include <memory>
47#include <utility>
48
49/*--------------------------------------------------------------------------*/
50/*--------------------------- NAMESPACE ------------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53namespace ADS
54{
55
56/*--------------------------------------------------------------------------*/
57/*---------------------------- CLASS ArrayStack ----------------------------*/
58/*--------------------------------------------------------------------------*/
59
61template< class T >
63{
64
65/*--------------------------------------------------------------------------*/
66 public:
67 using Item = T;
68
70 explicit ArrayStack( std::size_t dim );
71
73 ArrayStack( const ArrayStack & other );
74
76 ArrayStack( ArrayStack && other ) noexcept;
77
79 ArrayStack & operator=( ArrayStack other ) noexcept;
80
82 void swap( ArrayStack & other ) noexcept;
83
85 [[nodiscard]] bool empty( void ) const;
86
89 [[nodiscard]] const T & top( void ) const;
90
103 T pop( void );
104
116 void push( const T & v );
117
119 [[nodiscard]] std::size_t size( void ) const;
120
122 [[nodiscard]] std::size_t capacity( void ) const;
123
125 [[nodiscard]] bool operator==( const ArrayStack & o ) const;
126
127/*------------------------- THE STANDARD INTERFACE -------------------------*/
128
129 // a constant view of the items, from the bottom to the top, contiguous;
130 // the operations of the stack stay the only way to change it
131
132 using value_type = T;
133 using size_type = std::size_t;
134 using difference_type = std::ptrdiff_t;
135 using reference = const T &;
136 using const_reference = const T &;
137 using iterator = const T *;
138 using const_iterator = const T *;
139
141 [[nodiscard]] const_iterator begin( void ) const { return( f_A.get() ); }
142
144 [[nodiscard]] const_iterator end( void ) const {
145 return( f_A.get() + f_size );
146 }
147
149 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
150
152 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
153
154/*--------------------------------------------------------------------------*/
155 private:
156 std::unique_ptr< T[] > f_A;
157 std::size_t f_size;
158 std::size_t f_capacity;
159
160 }; // end( class ArrayStack )
161
162/*--------------------------------------------------------------------------*/
163
164} // end( namespace ADS )
165
166/*--------------------------------------------------------------------------*/
167/*-------------------------- IMPLEMENTATION --------------------------------*/
168/*--------------------------------------------------------------------------*/
169
170#include "ADS/stack/impl/ArrayStack.tpp"
171
172/*--------------------------------------------------------------------------*/
173
174#endif /* ArrayStack.h included */
175
176/*--------------------------------------------------------------------------*/
177/*------------------------- End File ArrayStack.h --------------------------*/
178/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
std::size_t size_type
the type of the sizes
Definition ArrayStack.h:133
const T & reference
a constant reference
Definition ArrayStack.h:135
const_iterator cend(void) const
as end()
Definition ArrayStack.h:152
std::size_t capacity(void) const
the maximum number of items,
ArrayStack & operator=(ArrayStack other) noexcept
the items of other, by copy and swap
const_iterator end(void) const
the iterator past the item on top
Definition ArrayStack.h:144
bool operator==(const ArrayStack &o) const
the same items in the same order, whatever the capacities
T value_type
the type of the items
Definition ArrayStack.h:132
void push(const T &v)
puts on top
const T * const_iterator
a constant iterator
Definition ArrayStack.h:138
T pop(void)
removes the item on top and returns it
ArrayStack(std::size_t dim)
the empty stack of at most items
const T * iterator
a constant iterator
Definition ArrayStack.h:137
ArrayStack(ArrayStack &&other) noexcept
the items of other, which is left empty with no capacity
bool empty(void) const
true if there is no item, i.e.,
const T & top(void) const
the item on top,
ArrayStack(const ArrayStack &other)
a copy of other, item by item
void swap(ArrayStack &other) noexcept
exchanges the items and the capacities with other
std::ptrdiff_t difference_type
the type of the distances
Definition ArrayStack.h:134
T Item
the type of the items
Definition ArrayStack.h:67
const_iterator begin(void) const
the iterator on the item at the bottom
Definition ArrayStack.h:141
std::size_t size(void) const
the number of items,
const T & const_reference
a constant reference
Definition ArrayStack.h:136
const_iterator cbegin(void) const
as begin()
Definition ArrayStack.h:149
Definition ADT.h:96