CppCommon  1.0.4.1
C++ Common Library
queue.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 template <typename T>
12 template <class InputIterator>
13 inline Queue<T>::Queue(InputIterator first, InputIterator last) noexcept
14 {
15  for (auto it = first; it != last; ++it)
16  push(*it);
17 }
18 
19 template <typename T>
20 inline typename Queue<T>::iterator Queue<T>::begin() noexcept
21 {
22  return iterator(front());
23 }
24 
25 template <typename T>
26 inline typename Queue<T>::const_iterator Queue<T>::begin() const noexcept
27 {
28  return const_iterator(front());
29 }
30 
31 template <typename T>
32 inline typename Queue<T>::const_iterator Queue<T>::cbegin() const noexcept
33 {
34  return const_iterator(front());
35 }
36 
37 template <typename T>
38 inline typename Queue<T>::iterator Queue<T>::end() noexcept
39 {
40  return iterator(nullptr);
41 }
42 
43 template <typename T>
44 inline typename Queue<T>::const_iterator Queue<T>::end() const noexcept
45 {
46  return const_iterator(nullptr);
47 }
48 
49 template <typename T>
50 inline typename Queue<T>::const_iterator Queue<T>::cend() const noexcept
51 {
52  return const_iterator(nullptr);
53 }
54 
55 template <typename T>
56 inline void Queue<T>::push(T& item) noexcept
57 {
58  if (_back != nullptr)
59  _back->next = &item;
60  item.next = nullptr;
61  _back = &item;
62  if (_front == nullptr)
63  _front = _back;
64  ++_size;
65 }
66 
67 template <typename T>
68 inline T* Queue<T>::pop() noexcept
69 {
70  if (_front == nullptr)
71  return nullptr;
72 
73  T* result = _front;
74  _front = result->next;
75  result->next = nullptr;
76  if (_front == nullptr)
77  _back = nullptr;
78  --_size;
79  return result;
80 }
81 
82 template <typename T>
83 inline void Queue<T>::reverse() noexcept
84 {
85  T* current = _front;
86  T* prev = nullptr;
87  T* next = nullptr;
88 
89  _back = current;
90  while (current != nullptr)
91  {
92  next = current->next;
93  current->next = prev;
94  prev = current;
95  current = next;
96  }
97  _front = prev;
98 }
99 
100 template <typename T>
101 inline void Queue<T>::clear() noexcept
102 {
103  _size = 0;
104  _front = nullptr;
105  _back = nullptr;
106 }
107 
108 template <typename T>
109 inline void Queue<T>::swap(Queue& queue) noexcept
110 {
111  using std::swap;
112  swap(_size, queue._size);
113  swap(_front, queue._front);
114  swap(_back, queue._back);
115 }
116 
117 template <typename T>
118 inline void swap(Queue<T>& queue1, Queue<T>& queue2) noexcept
119 {
120  queue1.swap(queue2);
121 }
122 
123 template <typename T>
125 {
126  if (_node != nullptr)
127  _node = _node->next;
128  return *this;
129 }
130 
131 template <typename T>
133 {
134  QueueIterator<T> result(*this);
135  operator++();
136  return result;
137 }
138 
139 template <typename T>
141 {
142  assert((_node != nullptr) && "Iterator must be valid!");
143 
144  return *_node;
145 }
146 
147 template <typename T>
149 {
150  return _node;
151 }
152 
153 template <typename T>
155 {
156  using std::swap;
157  swap(_node, it._node);
158 }
159 
160 template <typename T>
161 void swap(QueueIterator<T>& it1, QueueIterator<T>& it2) noexcept
162 {
163  it1.swap(it2);
164 }
165 
166 template <typename T>
168 {
169  if (_node != nullptr)
170  _node = _node->next;
171  return *this;
172 }
173 
174 template <typename T>
176 {
177  QueueConstIterator<T> result(*this);
178  operator++();
179  return result;
180 }
181 
182 template <typename T>
184 {
185  assert((_node != nullptr) && "Iterator must be valid!");
186 
187  return *_node;
188 }
189 
190 template <typename T>
192 {
193  return _node;
194 }
195 
196 template <typename T>
198 {
199  using std::swap;
200  swap(_node, it._node);
201 }
202 
203 template <typename T>
205 {
206  it1.swap(it2);
207 }
208 
209 } // namespace CppCommon
Intrusive queue constant iterator.
Definition: queue.h:247
QueueConstIterator & operator++() noexcept
Definition: queue.inl:167
const value_type * const_pointer
Definition: queue.h:254
void swap(QueueConstIterator &it) noexcept
Swap two instances.
Definition: queue.inl:197
const value_type & const_reference
Definition: queue.h:252
const_pointer operator->() const noexcept
Definition: queue.inl:191
const_reference operator*() const noexcept
Definition: queue.inl:183
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
void reverse() noexcept
Reverse the queue.
Definition: queue.inl:83
const_iterator cend() const noexcept
Definition: queue.inl:50
iterator end() noexcept
Get the end queue iterator.
Definition: queue.inl:38
Queue() noexcept
Definition: queue.h:125
void push(T &item) noexcept
Push a new item into the back of the queue.
Definition: queue.inl:56
void swap(Queue &queue) noexcept
Swap two instances.
Definition: queue.inl:109
void clear() noexcept
Clear the queue.
Definition: queue.inl:101
iterator begin() noexcept
Get the begin queue iterator.
Definition: queue.inl:20
Intrusive queue iterator.
Definition: queue.h:195
reference operator*() noexcept
Definition: queue.inl:140
value_type * pointer
Definition: queue.h:203
void swap(QueueIterator &it) noexcept
Swap two instances.
Definition: queue.inl:154
pointer operator->() noexcept
Definition: queue.inl:148
value_type & reference
Definition: queue.h:201
QueueIterator & operator++() noexcept
Definition: queue.inl:124
C++ Common project definitions.
Definition: token_bucket.h:15
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23
void swap(QueueConstIterator< T > &it1, QueueConstIterator< T > &it2) noexcept
Definition: queue.inl:204