semf
linkedqueue.h
Go to the documentation of this file.
1
10#ifndef SEMF_UTILS_CORE_QUEUES_LINKEDQUEUE_H_
11#define SEMF_UTILS_CORE_QUEUES_LINKEDQUEUE_H_
12
13#include <iterator>
14#include <cstdint>
15#include <cstddef>
16#include <algorithm>
17
18namespace semf
19{
36template <class T>
38{
39public:
45 class Node
46 {
47 public:
48 virtual ~Node() = default;
49
55 T* next() const
56 {
57 return m_next;
58 }
64 void setNext(T* next)
65 {
66 m_next = next;
67 }
76 {
77 Node* me = this;
78 return queue.end() != std::find_if(queue.begin(), queue.end(), [&me](T& node) { return &node == me; });
79 }
80
81 private:
83 T* m_next = nullptr;
84 };
85
90 {
91 public:
92 using iterator_category = std::forward_iterator_tag;
93 using value_type = T;
94 using difference_type = std::ptrdiff_t;
95 using pointer = T*;
96 using refernce = T&;
97
98 Iterator() = default;
103 explicit Iterator(T* element)
104 : m_node(element)
105 {
106 }
107 virtual ~Iterator() = default;
108
115 T& operator*() const
116 {
117 return *m_node;
118 }
125 T* operator->() const
126 {
127 return m_node;
128 }
136 {
137 m_node = m_node->LinkedQueue::Node::next();
138 return *this;
139 }
147 {
148 Iterator temp = *this;
149 m_node = m_node->LinkedQueue::Node::next();
150 return temp;
151 }
158 bool operator==(const Iterator& other) const
159 {
160 return m_node == other.m_node;
161 }
168 bool operator!=(const Iterator& other) const
169 {
170 return m_node != other.m_node;
171 }
172
173 private:
175 T* m_node = nullptr;
176 };
177
182 {
183 public:
184 using iterator_category = std::forward_iterator_tag;
185 using value_type = T;
186 using difference_type = std::ptrdiff_t;
187 using pointer = T*;
188 using refernce = T&;
189
190 ConstIterator() = default;
195 explicit ConstIterator(const T* element)
196 : m_node(element)
197 {
198 }
203 explicit ConstIterator(const Iterator& iterator)
204 : m_node(iterator.m_node)
205 {
206 }
207 virtual ~ConstIterator() = default;
208
215 const T& operator*() const
216 {
217 return *m_node;
218 }
225 const T* operator->() const
226 {
227 return m_node;
228 }
236 {
237 m_node = m_node->LinkedQueue::Node::next();
238 return *this;
239 }
247 {
248 ConstIterator temp = *this;
249 m_node = m_node->LinkedQueue::Node::next();
250 return temp;
251 }
258 bool operator==(const ConstIterator& other) const
259 {
260 return m_node == other.m_node;
261 }
268 bool operator!=(const ConstIterator& other) const
269 {
270 return m_node != other.m_node;
271 }
272
273 private:
275 const T* m_node = nullptr;
276 };
277
285 T& front();
293 const T& front() const;
301 T& back();
309 const T& back() const;
317 Iterator begin();
325 ConstIterator begin() const;
333 ConstIterator cbegin() const;
344 Iterator end();
355 ConstIterator end() const;
366 ConstIterator cend() const;
373 bool empty() const;
378 size_t size() const;
386 void push(T& element);
391 void pop();
400
401private:
403 T* m_front = nullptr;
405 T* m_back = nullptr;
407 size_t m_size = 0;
408};
409
410template <class T>
412{
413 return *m_front;
414}
415
416template <class T>
417const T& LinkedQueue<T>::front() const
418{
419 return *m_front;
420}
421
422template <class T>
424{
425 return *m_back;
426}
427
428template <class T>
429const T& LinkedQueue<T>::back() const
430{
431 return *m_back;
432}
433
434template <class T>
436{
437 return Iterator(m_front);
438}
439
440template <class T>
442{
443 return ConstIterator(m_front);
444}
445
446template <class T>
448{
449 return ConstIterator(m_front);
450}
451
452template <class T>
454{
455 return Iterator(nullptr);
456}
457
458template <class T>
460{
461 return ConstIterator(nullptr);
462}
463
464template <class T>
466{
467 return ConstIterator(nullptr);
468}
469
470template <class T>
472{
473 return m_size == 0;
474}
475
476template <class T>
478{
479 return m_size;
480}
481
482template <class T>
483void LinkedQueue<T>::push(T& element)
484{
485 element.LinkedQueue::Node::setNext(nullptr);
486 if (m_size > 1)
487 {
488 m_back->LinkedQueue::Node::setNext(&element);
489 m_back = &element;
490 }
491 else if (m_size == 1)
492 {
493 m_front->LinkedQueue::Node::setNext(&element);
494 m_back = &element;
495 }
496 else // m_size == 0
497 {
498 m_front = &element;
499 m_back = &element;
500 }
501 m_size++;
502}
503
504template <class T>
506{
507 if (m_size == 1)
508 {
509 m_front = nullptr;
510 m_back = nullptr;
511 m_size--;
512 }
513 else
514 {
515 m_front = m_front->LinkedQueue::Node::next();
516 m_size--;
517 }
518}
519
520template <class T>
522{
523 this->m_front = queue.m_front;
524 this->m_back = queue.m_back;
525 this->m_size = queue.m_size;
526 return *this;
527}
528} /* namespace semf */
529#endif /* SEMF_UTILS_CORE_QUEUES_LINKEDQUEUE_H_ */
Implementation of a forward constant iterator for LinkedQueue.
Definition: linkedqueue.h:182
std::forward_iterator_tag iterator_category
Definition: linkedqueue.h:184
const T & operator*() const
Returns the reference of the element the iterator's position.
Definition: linkedqueue.h:215
ConstIterator operator++(int)
Iterates to the next element in the queue.
Definition: linkedqueue.h:246
bool operator!=(const ConstIterator &other) const
Compares this element with that element.
Definition: linkedqueue.h:268
ConstIterator(const T *element)
Constructor with member variable initialization.
Definition: linkedqueue.h:195
bool operator==(const ConstIterator &other) const
Compares this element with that element.
Definition: linkedqueue.h:258
const T * operator->() const
Returns the pointer into the element the iterator's position.
Definition: linkedqueue.h:225
ConstIterator(const Iterator &iterator)
Copy constructor.
Definition: linkedqueue.h:203
virtual ~ConstIterator()=default
ConstIterator & operator++()
Iterates to the next element in the queue.
Definition: linkedqueue.h:235
Implementation of a forward iterator for LinkedQueue.
Definition: linkedqueue.h:90
bool operator!=(const Iterator &other) const
Compares this element with that element.
Definition: linkedqueue.h:168
std::forward_iterator_tag iterator_category
Definition: linkedqueue.h:92
bool operator==(const Iterator &other) const
Compares this element with that element.
Definition: linkedqueue.h:158
Iterator & operator++()
Iterates to the next element in the list.
Definition: linkedqueue.h:135
std::ptrdiff_t difference_type
Definition: linkedqueue.h:94
Iterator(T *element)
Constructor with member variable initialization.
Definition: linkedqueue.h:103
Iterator operator++(int)
Iterates to the next element in the queue.
Definition: linkedqueue.h:146
virtual ~Iterator()=default
T & operator*() const
Returns the reference of the element the iterator's position.
Definition: linkedqueue.h:115
T * operator->() const
Returns the pointer into the element the iterator's position.
Definition: linkedqueue.h:125
Implements the next() functionality for every element in a queue.
Definition: linkedqueue.h:46
T * next() const
Returns a pointer to the next element in a queue.
Definition: linkedqueue.h:55
virtual ~Node()=default
void setNext(T *next)
Sets a pointer to the next element in a list.
Definition: linkedqueue.h:64
bool isInQueue(LinkedQueue &queue)
Returns if a node is part of this LinkedQueue.
Definition: linkedqueue.h:75
LinkedQueue is an managed single linked queue implementation.
Definition: linkedqueue.h:38
void push(T &element)
Adds a new element to the end of the queue, after its current last element.
Definition: linkedqueue.h:483
T & front()
Returns a reference to the first element in the queue.
Definition: linkedqueue.h:411
const T & front() const
Returns a reference to the first element in the queue.
Definition: linkedqueue.h:417
const T & back() const
Returns a reference to the last element in the queue.
Definition: linkedqueue.h:429
bool empty() const
Returns whether the queue is empty (i.e. whether its size is 0).
Definition: linkedqueue.h:471
ConstIterator cbegin() const
Returns an iterator pointing to the first element in the queue.
Definition: linkedqueue.h:447
LinkedQueue< T > & operator=(const LinkedQueue< T > &queue)
Assigns new contents to the queue, replacing its current contents, and modifying its size accordingly...
Definition: linkedqueue.h:521
ConstIterator cend() const
Returns an iterator referring to the past-the-end element in the queue.
Definition: linkedqueue.h:465
Iterator end()
Returns an iterator referring to the past-the-end element in the queue.
Definition: linkedqueue.h:453
size_t size() const
Returns the number of elements in the queue.
Definition: linkedqueue.h:477
void pop()
Removes the first element in the queue, effectively reducing the queue size by one.
Definition: linkedqueue.h:505
Iterator begin()
Returns an iterator pointing to the first element in the queue.
Definition: linkedqueue.h:435
ConstIterator begin() const
Returns an iterator pointing to the first element in the queue.
Definition: linkedqueue.h:441
T & back()
Returns a reference to the last element in the queue.
Definition: linkedqueue.h:423
ConstIterator end() const
Returns an iterator referring to the past-the-end element in the queue.
Definition: linkedqueue.h:459