CppCommon  1.0.4.1
C++ Common Library
queue.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_QUEUE_H
10 #define CPPCOMMON_CONTAINERS_QUEUE_H
11 
12 #include <cassert>
13 #include <cstddef>
14 #include <iterator>
15 
16 namespace CppCommon {
17 
18 template <typename T>
19 class QueueIterator;
20 template <typename T>
21 class QueueConstIterator;
22 
24 
102 template <typename T>
103 class Queue
104 {
105 public:
106  // Standard container type definitions
107  typedef T value_type;
109  typedef const value_type& const_reference;
110  typedef value_type* pointer;
111  typedef const value_type* const_pointer;
112  typedef ptrdiff_t difference_type;
113  typedef size_t size_type;
116 
118  struct Node
119  {
120  T* next;
121 
122  Node() : next(nullptr) {}
123  };
124 
125  Queue() noexcept : _size(0), _front(nullptr), _back(nullptr) {}
126  template <class InputIterator>
127  Queue(InputIterator first, InputIterator last) noexcept;
128  Queue(const Queue&) noexcept = default;
129  Queue(Queue&&) noexcept = default;
130  ~Queue() noexcept = default;
131 
132  Queue& operator=(const Queue&) noexcept = default;
133  Queue& operator=(Queue&&) noexcept = default;
134 
136  explicit operator bool() const noexcept { return !empty(); }
137 
139  bool empty() const noexcept { return _front == nullptr; }
140 
142  size_t size() const noexcept { return _size; }
143 
145  T* front() noexcept { return _front; }
146  const T* front() const noexcept { return _front; }
148  T* back() noexcept { return _back; }
149  const T* back() const noexcept { return _back; }
150 
152  iterator begin() noexcept;
153  const_iterator begin() const noexcept;
154  const_iterator cbegin() const noexcept;
156  iterator end() noexcept;
157  const_iterator end() const noexcept;
158  const_iterator cend() const noexcept;
159 
161 
164  void push(T& item) noexcept;
165 
167 
170  T* pop() noexcept;
171 
173  void reverse() noexcept;
174 
176  void clear() noexcept;
177 
179  void swap(Queue& queue) noexcept;
180  template <typename U>
181  friend void swap(Queue<U>& queue1, Queue<U>& queue2) noexcept;
182 
183 private:
184  size_t _size; // Queue size
185  T* _front; // Queue front node
186  T* _back; // Queue back node
187 };
188 
190 
193 template <typename T>
195 {
196  friend QueueConstIterator<T>;
197 
198 public:
199  // Standard iterator type definitions
200  typedef T value_type;
202  typedef const value_type& const_reference;
203  typedef value_type* pointer;
204  typedef const value_type* const_pointer;
205  typedef ptrdiff_t difference_type;
206  typedef size_t size_type;
207  typedef std::forward_iterator_tag iterator_category;
208 
209  QueueIterator() noexcept : _node(nullptr) {}
210  explicit QueueIterator(T* node) noexcept : _node(node) {}
211  QueueIterator(const QueueIterator& it) noexcept = default;
212  QueueIterator(QueueIterator&& it) noexcept = default;
213  ~QueueIterator() noexcept = default;
214 
215  QueueIterator& operator=(const QueueIterator& it) noexcept = default;
216  QueueIterator& operator=(QueueIterator&& it) noexcept = default;
217 
218  friend bool operator==(const QueueIterator& it1, const QueueIterator& it2) noexcept
219  { return it1._node == it2._node; }
220  friend bool operator!=(const QueueIterator& it1, const QueueIterator& it2) noexcept
221  { return it1._node != it2._node; }
222 
223  QueueIterator& operator++() noexcept;
224  QueueIterator operator++(int) noexcept;
225 
226  reference operator*() noexcept;
227  pointer operator->() noexcept;
228 
230  explicit operator bool() const noexcept { return _node != nullptr; }
231 
233  void swap(QueueIterator& it) noexcept;
234  template <typename U>
235  friend void swap(QueueIterator<U>& it1, QueueIterator<U>& it2) noexcept;
236 
237 private:
238  T* _node;
239 };
240 
242 
245 template <typename T>
247 {
248 public:
249  // Standard iterator type definitions
250  typedef T value_type;
252  typedef const value_type& const_reference;
253  typedef value_type* pointer;
254  typedef const value_type* const_pointer;
255  typedef ptrdiff_t difference_type;
256  typedef size_t size_type;
257  typedef std::forward_iterator_tag iterator_category;
258 
259  QueueConstIterator() noexcept : _node(nullptr) {}
260  explicit QueueConstIterator(const T* node) noexcept : _node(node) {}
261  QueueConstIterator(const QueueIterator<T>& it) noexcept : _node(it._node) {}
262  QueueConstIterator(const QueueConstIterator& it) noexcept = default;
263  QueueConstIterator(QueueConstIterator&& it) noexcept = default;
264  ~QueueConstIterator() noexcept = default;
265 
266  QueueConstIterator& operator=(const QueueIterator<T>& it) noexcept
267  { _node = it._node; return *this; }
268  QueueConstIterator& operator=(const QueueConstIterator& it) noexcept = default;
270 
271  friend bool operator==(const QueueConstIterator& it1, const QueueConstIterator& it2) noexcept
272  { return it1._node == it2._node; }
273  friend bool operator!=(const QueueConstIterator& it1, const QueueConstIterator& it2) noexcept
274  { return it1._node != it2._node; }
275 
276  QueueConstIterator& operator++() noexcept;
277  QueueConstIterator operator++(int) noexcept;
278 
279  const_reference operator*() const noexcept;
280  const_pointer operator->() const noexcept;
281 
283  explicit operator bool() const noexcept { return _node != nullptr; }
284 
286  void swap(QueueConstIterator& it) noexcept;
287  template <typename U>
288  friend void swap(QueueConstIterator<U>& it1, QueueConstIterator<U>& it2) noexcept;
289 
290 private:
291  const T* _node;
292 };
293 
296 } // namespace CppCommon
297 
298 #include "queue.inl"
299 
300 #endif // CPPCOMMON_CONTAINERS_QUEUE_H
Intrusive queue constant iterator.
Definition: queue.h:247
QueueConstIterator & operator=(QueueConstIterator &&it) noexcept=default
QueueConstIterator(QueueConstIterator &&it) noexcept=default
QueueConstIterator(const QueueConstIterator &it) noexcept=default
std::forward_iterator_tag iterator_category
Definition: queue.h:257
const value_type * const_pointer
Definition: queue.h:254
QueueConstIterator & operator=(const QueueConstIterator &it) noexcept=default
friend void swap(QueueConstIterator< U > &it1, QueueConstIterator< U > &it2) noexcept
friend bool operator==(const QueueConstIterator &it1, const QueueConstIterator &it2) noexcept
Definition: queue.h:271
friend bool operator!=(const QueueConstIterator &it1, const QueueConstIterator &it2) noexcept
Definition: queue.h:273
QueueConstIterator() noexcept
Definition: queue.h:259
value_type & reference
Definition: queue.h:251
QueueConstIterator(const QueueIterator< T > &it) noexcept
Definition: queue.h:261
~QueueConstIterator() noexcept=default
const value_type & const_reference
Definition: queue.h:252
QueueConstIterator(const T *node) noexcept
Definition: queue.h:260
Intrusive queue container.
Definition: queue.h:104
const_iterator cbegin() const noexcept
Definition: queue.inl:32
T * pop() noexcept
Pop the item from the front of the queue.
Definition: queue.inl:68
size_t size_type
Definition: queue.h:113
void reverse() noexcept
Reverse the queue.
Definition: queue.inl:83
Queue(Queue &&) noexcept=default
const_iterator cend() const noexcept
Definition: queue.inl:50
T * front() noexcept
Get the front queue item.
Definition: queue.h:145
bool empty() const noexcept
Is the queue empty?
Definition: queue.h:139
iterator end() noexcept
Get the end queue iterator.
Definition: queue.inl:38
Queue() noexcept
Definition: queue.h:125
QueueIterator< T > iterator
Definition: queue.h:114
value_type * pointer
Definition: queue.h:110
void push(T &item) noexcept
Push a new item into the back of the queue.
Definition: queue.inl:56
QueueConstIterator< T > const_iterator
Definition: queue.h:115
const value_type * const_pointer
Definition: queue.h:111
void swap(Queue &queue) noexcept
Swap two instances.
Definition: queue.inl:109
ptrdiff_t difference_type
Definition: queue.h:112
void clear() noexcept
Clear the queue.
Definition: queue.inl:101
T * back() noexcept
Get the back queue item.
Definition: queue.h:148
value_type & reference
Definition: queue.h:108
size_t size() const noexcept
Get the queue size.
Definition: queue.h:142
const T * front() const noexcept
Definition: queue.h:146
iterator begin() noexcept
Get the begin queue iterator.
Definition: queue.inl:20
const value_type & const_reference
Definition: queue.h:109
Queue(const Queue &) noexcept=default
const T * back() const noexcept
Definition: queue.h:149
Intrusive queue iterator.
Definition: queue.h:195
value_type * pointer
Definition: queue.h:203
const value_type & const_reference
Definition: queue.h:202
QueueIterator() noexcept
Definition: queue.h:209
QueueIterator(QueueIterator &&it) noexcept=default
const value_type * const_pointer
Definition: queue.h:204
~QueueIterator() noexcept=default
QueueIterator(T *node) noexcept
Definition: queue.h:210
friend void swap(QueueIterator< U > &it1, QueueIterator< U > &it2) noexcept
value_type & reference
Definition: queue.h:201
QueueIterator(const QueueIterator &it) noexcept=default
ptrdiff_t difference_type
Definition: queue.h:205
std::forward_iterator_tag iterator_category
Definition: queue.h:207
friend bool operator!=(const QueueIterator &it1, const QueueIterator &it2) noexcept
Definition: queue.h:220
C++ Common project definitions.
Definition: token_bucket.h:15
Intrusive queue container inline implementation.
Queue node.
Definition: queue.h:119
T * next
Pointer to the next queue node.
Definition: queue.h:120