CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_CONTAINERS_STACK_H
10#define CPPCOMMON_CONTAINERS_STACK_H
11
12#include <cassert>
13#include <cstddef>
14#include <iterator>
15
16namespace CppCommon {
17
18template <typename T>
19class StackIterator;
20template <typename T>
21class StackConstIterator;
22
24
104template <typename T>
105class Stack
106{
107public:
108 // Standard container type definitions
109 typedef T value_type;
113 typedef const value_type* const_pointer;
114 typedef ptrdiff_t difference_type;
115 typedef size_t size_type;
118
120 struct Node
121 {
122 T* next;
123
124 Node() : next(nullptr) {}
125 };
126
127 Stack() noexcept : _size(0), _top(nullptr) {}
128 template <class InputIterator>
129 Stack(InputIterator first, InputIterator last) noexcept;
130 Stack(const Stack&) noexcept = default;
131 Stack(Stack&&) noexcept = default;
132 ~Stack() noexcept = default;
133
134 Stack& operator=(const Stack&) noexcept = default;
135 Stack& operator=(Stack&&) noexcept = default;
136
138 explicit operator bool() const noexcept { return !empty(); }
139
141 bool empty() const noexcept { return _top == nullptr; }
142
144 size_t size() const noexcept { return _size; }
145
147 T* top() noexcept { return _top; }
148 const T* top() const noexcept { return _top; }
149
151 iterator begin() noexcept;
152 const_iterator begin() const noexcept;
153 const_iterator cbegin() const noexcept;
155 iterator end() noexcept;
156 const_iterator end() const noexcept;
157 const_iterator cend() const noexcept;
158
160
163 void push(T& item) noexcept;
164
166
169 T* pop() noexcept;
170
172 void reverse() noexcept;
173
175 void clear() noexcept;
176
178 void swap(Stack& stack) noexcept;
179 template <typename U>
180 friend void swap(Stack<U>& stack1, Stack<U>& stack2) noexcept;
181
182private:
183 size_t _size; // Stack size
184 T* _top; // Stack top node
185};
186
188
191template <typename T>
193{
195
196public:
197 // Standard iterator type definitions
198 typedef T value_type;
202 typedef const value_type* const_pointer;
203 typedef ptrdiff_t difference_type;
204 typedef size_t size_type;
205 typedef std::forward_iterator_tag iterator_category;
206
207 StackIterator() noexcept : _node(nullptr) {}
208 explicit StackIterator(T* node) noexcept : _node(node) {}
209 StackIterator(const StackIterator& it) noexcept = default;
210 StackIterator(StackIterator&& it) noexcept = default;
211 ~StackIterator() noexcept = default;
212
213 StackIterator& operator=(const StackIterator& it) noexcept = default;
214 StackIterator& operator=(StackIterator&& it) noexcept = default;
215
216 friend bool operator==(const StackIterator& it1, const StackIterator& it2) noexcept
217 { return it1._node == it2._node; }
218 friend bool operator!=(const StackIterator& it1, const StackIterator& it2) noexcept
219 { return it1._node != it2._node; }
220
221 StackIterator& operator++() noexcept;
222 StackIterator operator++(int) noexcept;
223
224 reference operator*() noexcept;
225 pointer operator->() noexcept;
226
228 explicit operator bool() const noexcept { return _node != nullptr; }
229
231 void swap(StackIterator& it) noexcept;
232 template <typename U>
233 friend void swap(StackIterator<U>& it1, StackIterator<U>& it2) noexcept;
234
235private:
236 T* _node;
237};
238
240
243template <typename T>
245{
246public:
247 // Standard iterator type definitions
248 typedef T value_type;
252 typedef const value_type* const_pointer;
253 typedef ptrdiff_t difference_type;
254 typedef size_t size_type;
255 typedef std::forward_iterator_tag iterator_category;
256
257 StackConstIterator() noexcept : _node(nullptr) {}
258 explicit StackConstIterator(const T* node) noexcept : _node(node) {}
259 StackConstIterator(const StackIterator<T>& it) noexcept : _node(it._node) {}
260 StackConstIterator(const StackConstIterator& it) noexcept = default;
261 StackConstIterator(StackConstIterator&& it) noexcept = default;
262 ~StackConstIterator() noexcept = default;
263
264 StackConstIterator& operator=(const StackIterator<T>& it) noexcept
265 { _node = it._node; return *this; }
266 StackConstIterator& operator=(const StackConstIterator& it) noexcept = default;
268
269 friend bool operator==(const StackConstIterator& it1, const StackConstIterator& it2) noexcept
270 { return it1._node == it2._node; }
271 friend bool operator!=(const StackConstIterator& it1, const StackConstIterator& it2) noexcept
272 { return it1._node != it2._node; }
273
274 StackConstIterator& operator++() noexcept;
275 StackConstIterator operator++(int) noexcept;
276
277 const_reference operator*() const noexcept;
278 const_pointer operator->() const noexcept;
279
281 explicit operator bool() const noexcept { return _node != nullptr; }
282
284 void swap(StackConstIterator& it) noexcept;
285 template <typename U>
286 friend void swap(StackConstIterator<U>& it1, StackConstIterator<U>& it2) noexcept;
287
288private:
289 const T* _node;
290};
291
294} // namespace CppCommon
295
296#include "stack.inl"
297
298#endif // CPPCOMMON_CONTAINERS_STACK_H
Intrusive stack constant iterator.
Definition stack.h:245
const value_type & const_reference
Definition stack.h:250
friend bool operator==(const StackConstIterator &it1, const StackConstIterator &it2) noexcept
Definition stack.h:269
StackConstIterator(const StackConstIterator &it) noexcept=default
StackConstIterator & operator=(StackConstIterator &&it) noexcept=default
const value_type * const_pointer
Definition stack.h:252
StackConstIterator(const StackIterator< T > &it) noexcept
Definition stack.h:259
StackConstIterator(const T *node) noexcept
Definition stack.h:258
StackConstIterator() noexcept
Definition stack.h:257
StackConstIterator(StackConstIterator &&it) noexcept=default
friend void swap(StackConstIterator< U > &it1, StackConstIterator< U > &it2) noexcept
~StackConstIterator() noexcept=default
friend bool operator!=(const StackConstIterator &it1, const StackConstIterator &it2) noexcept
Definition stack.h:271
StackConstIterator & operator=(const StackConstIterator &it) noexcept=default
std::forward_iterator_tag iterator_category
Definition stack.h:255
Intrusive stack container.
Definition stack.h:106
value_type & reference
Definition stack.h:110
void push(T &item) noexcept
Push a new item into the top of the stack.
Definition stack.inl:56
Stack(const Stack &) noexcept=default
void reverse() noexcept
Reverse the stack.
Definition stack.inl:77
ptrdiff_t difference_type
Definition stack.h:114
const_iterator cend() const noexcept
Definition stack.inl:50
Stack(Stack &&) noexcept=default
T * pop() noexcept
Pop the item from the top of the stack.
Definition stack.inl:64
StackIterator< T > iterator
Definition stack.h:116
iterator end() noexcept
Get the end stack iterator.
Definition stack.inl:38
const T * top() const noexcept
Definition stack.h:148
friend void swap(Stack< U > &stack1, Stack< U > &stack2) noexcept
size_t size() const noexcept
Get the stack size.
Definition stack.h:144
size_t size_type
Definition stack.h:115
StackConstIterator< T > const_iterator
Definition stack.h:117
value_type * pointer
Definition stack.h:112
const_iterator cbegin() const noexcept
Definition stack.inl:32
iterator begin() noexcept
Get the begin stack iterator.
Definition stack.inl:20
Stack() noexcept
Definition stack.h:127
void clear() noexcept
Clear the stack.
Definition stack.inl:94
bool empty() const noexcept
Is the stack empty?
Definition stack.h:141
const value_type & const_reference
Definition stack.h:111
T * top() noexcept
Get the top stack item.
Definition stack.h:147
const value_type * const_pointer
Definition stack.h:113
Intrusive stack iterator.
Definition stack.h:193
StackIterator(T *node) noexcept
Definition stack.h:208
StackIterator(StackIterator &&it) noexcept=default
value_type & reference
Definition stack.h:199
friend void swap(StackIterator< U > &it1, StackIterator< U > &it2) noexcept
const value_type * const_pointer
Definition stack.h:202
ptrdiff_t difference_type
Definition stack.h:203
const value_type & const_reference
Definition stack.h:200
~StackIterator() noexcept=default
StackIterator() noexcept
Definition stack.h:207
StackIterator(const StackIterator &it) noexcept=default
value_type * pointer
Definition stack.h:201
friend bool operator!=(const StackIterator &it1, const StackIterator &it2) noexcept
Definition stack.h:218
std::forward_iterator_tag iterator_category
Definition stack.h:205
C++ Common project definitions.
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition filecache.inl:23
Intrusive stack container inline implementation.
Stack node.
Definition stack.h:121
T * next
Pointer to the next stack node.
Definition stack.h:122