CppCommon  1.0.4.1
C++ Common Library
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 
16 namespace CppCommon {
17 
18 template <typename T>
19 class StackIterator;
20 template <typename T>
21 class StackConstIterator;
22 
24 
104 template <typename T>
105 class Stack
106 {
107 public:
108  // Standard container type definitions
109  typedef T value_type;
111  typedef const value_type& const_reference;
112  typedef value_type* pointer;
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 
182 private:
183  size_t _size; // Stack size
184  T* _top; // Stack top node
185 };
186 
188 
191 template <typename T>
193 {
194  friend StackConstIterator<T>;
195 
196 public:
197  // Standard iterator type definitions
198  typedef T value_type;
200  typedef const value_type& const_reference;
201  typedef value_type* pointer;
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 
235 private:
236  T* _node;
237 };
238 
240 
243 template <typename T>
245 {
246 public:
247  // Standard iterator type definitions
248  typedef T value_type;
250  typedef const value_type& const_reference;
251  typedef value_type* pointer;
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 
288 private:
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
StackConstIterator & operator=(const StackConstIterator &it) noexcept=default
StackConstIterator & operator=(StackConstIterator &&it) noexcept=default
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
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
std::forward_iterator_tag iterator_category
Definition: stack.h:255
value_type & reference
Definition: stack.h:249
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 * top() noexcept
Get the top stack item.
Definition: stack.h:147
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
size_t size() const noexcept
Get the stack size.
Definition: stack.h:144
const T * top() const noexcept
Definition: stack.h:148
size_t size_type
Definition: stack.h:115
StackConstIterator< T > const_iterator
Definition: stack.h:117
value_type * pointer
Definition: stack.h:112
void swap(Stack &stack) noexcept
Swap two instances.
Definition: stack.inl:101
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
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.
Definition: token_bucket.h:15
Intrusive stack container inline implementation.
Stack node.
Definition: stack.h:121
T * next
Pointer to the next stack node.
Definition: stack.h:122