CppCommon  1.0.4.1
C++ Common Library
list.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 template <typename T>
12 template <class InputIterator>
13 inline List<T>::List(InputIterator first, InputIterator last) noexcept
14 {
15  for (auto it = first; it != last; ++it)
16  push_back(*it);
17 }
18 
19 template <typename T>
20 inline typename List<T>::iterator List<T>::begin() noexcept
21 {
22  return iterator(front());
23 }
24 
25 template <typename T>
26 inline typename List<T>::const_iterator List<T>::begin() const noexcept
27 {
28  return const_iterator(front());
29 }
30 
31 template <typename T>
32 inline typename List<T>::const_iterator List<T>::cbegin() const noexcept
33 {
34  return const_iterator(front());
35 }
36 
37 template <typename T>
38 inline typename List<T>::iterator List<T>::end() noexcept
39 {
40  return iterator(nullptr);
41 }
42 
43 template <typename T>
44 inline typename List<T>::const_iterator List<T>::end() const noexcept
45 {
46  return const_iterator(nullptr);
47 }
48 
49 template <typename T>
50 inline typename List<T>::const_iterator List<T>::cend() const noexcept
51 {
52  return const_iterator(nullptr);
53 }
54 
55 template <typename T>
56 inline typename List<T>::reverse_iterator List<T>::rbegin() noexcept
57 {
58  return reverse_iterator(back());
59 }
60 
61 template <typename T>
62 inline typename List<T>::const_reverse_iterator List<T>::rbegin() const noexcept
63 {
64  return const_reverse_iterator(back());
65 }
66 
67 template <typename T>
68 inline typename List<T>::const_reverse_iterator List<T>::crbegin() const noexcept
69 {
70  return const_reverse_iterator(back());
71 }
72 
73 template <typename T>
74 inline typename List<T>::reverse_iterator List<T>::rend() noexcept
75 {
76  return reverse_iterator(nullptr);
77 }
78 
79 template <typename T>
80 inline typename List<T>::const_reverse_iterator List<T>::rend() const noexcept
81 {
82  return const_reverse_iterator(nullptr);
83 }
84 
85 template <typename T>
86 inline typename List<T>::const_reverse_iterator List<T>::crend() const noexcept
87 {
88  return const_reverse_iterator(nullptr);
89 }
90 
91 template <typename T>
92 inline void List<T>::push_front(T& item) noexcept
93 {
94  if (_front != nullptr)
95  _front->prev = &item;
96  item.next = _front;
97  item.prev = nullptr;
98  _front = &item;
99  if (_back == nullptr)
100  _back = _front;
101  ++_size;
102 }
103 
104 template <typename T>
105 inline void List<T>::push_back(T& item) noexcept
106 {
107  if (_back != nullptr)
108  _back->next = &item;
109  item.next = nullptr;
110  item.prev = _back;
111  _back = &item;
112  if (_front == nullptr)
113  _front = _back;
114  ++_size;
115 }
116 
117 template <typename T>
118 inline void List<T>::push_next(T& base, T& item) noexcept
119 {
120  item.next = base.next;
121  item.prev = &base;
122  if (_back == &base)
123  _back = &item;
124  if (base.next != nullptr)
125  base.next->prev = &item;
126  base.next = &item;
127  ++_size;
128 }
129 
130 template <typename T>
131 inline void List<T>::push_prev(T& base, T& item) noexcept
132 {
133  item.next = &base;
134  item.prev = base.prev;
135  if (_front == &base)
136  _front = &item;
137  if (base.prev != nullptr)
138  base.prev->next = &item;
139  base.prev = &item;
140  ++_size;
141 }
142 
143 template <typename T>
144 inline T* List<T>::pop_front() noexcept
145 {
146  if (_front == nullptr)
147  return nullptr;
148 
149  T* result = _front;
150  _front = result->next;
151  result->next = nullptr;
152  result->prev = nullptr;
153  if (_front == nullptr)
154  _back = nullptr;
155  else
156  _front->prev = nullptr;
157  --_size;
158  return result;
159 }
160 
161 template <typename T>
162 inline T* List<T>::pop_back() noexcept
163 {
164  if (_back == nullptr)
165  return nullptr;
166 
167  T* result = _back;
168  _back = result->prev;
169  result->next = nullptr;
170  result->prev = nullptr;
171  if (_back == nullptr)
172  _front = nullptr;
173  else
174  _back->next = nullptr;
175  --_size;
176  return result;
177 }
178 
179 template <typename T>
180 inline T* List<T>::pop_current(T& base) noexcept
181 {
182  T* result = &base;
183  if (result->next != nullptr)
184  result->next->prev = result->prev;
185  else
186  _back = result->prev;
187  if (result->prev != nullptr)
188  result->prev->next = result->next;
189  else
190  _front = result->next;
191  result->next = nullptr;
192  result->prev = nullptr;
193  --_size;
194  return result;
195 }
196 
197 template <typename T>
198 inline T* List<T>::pop_next(T& base) noexcept
199 {
200  if (base.next == nullptr)
201  return nullptr;
202 
203  T* result = base.next;
204  if (result->next != nullptr)
205  result->next->prev = &base;
206  else
207  _back = &base;
208  base.next = result->next;
209  result->next = nullptr;
210  result->prev = nullptr;
211  --_size;
212  return result;
213 }
214 
215 template <typename T>
216 inline T* List<T>::pop_prev(T& base) noexcept
217 {
218  if (base.prev == nullptr)
219  return nullptr;
220 
221  T* result = base.prev;
222  if (result->prev != nullptr)
223  result->prev->next = &base;
224  else
225  _front = &base;
226  base.prev = result->prev;
227  result->next = nullptr;
228  result->prev = nullptr;
229  --_size;
230  return result;
231 }
232 
233 template <typename T>
234 inline void List<T>::reverse() noexcept
235 {
236  T* current = _front;
237  T* prev = nullptr;
238  T* next = nullptr;
239 
240  _back = current;
241  while (current != nullptr)
242  {
243  next = current->next;
244  current->next = prev;
245  current->prev = next;
246  prev = current;
247  current = next;
248  }
249  _front = prev;
250 }
251 
252 template <typename T>
253 inline void List<T>::clear() noexcept
254 {
255  _size = 0;
256  _front = nullptr;
257  _back = nullptr;
258 }
259 
260 template <typename T>
261 inline void List<T>::swap(List& list) noexcept
262 {
263  using std::swap;
264  swap(_size, list._size);
265  swap(_front, list._front);
266  swap(_back, list._back);
267 }
268 
269 template <typename T>
270 inline void swap(List<T>& list1, List<T>& list2) noexcept
271 {
272  list1.swap(list2);
273 }
274 
275 template <typename T>
277 {
278  if (_node != nullptr)
279  _node = _node->next;
280  return *this;
281 }
282 
283 template <typename T>
285 {
286  ListIterator<T> result(*this);
287  operator++();
288  return result;
289 }
290 
291 template <typename T>
293 {
294  assert((_node != nullptr) && "Iterator must be valid!");
295 
296  return *_node;
297 }
298 
299 template <typename T>
301 {
302  return _node;
303 }
304 
305 template <typename T>
307 {
308  using std::swap;
309  swap(_node, it._node);
310 }
311 
312 template <typename T>
313 void swap(ListIterator<T>& it1, ListIterator<T>& it2) noexcept
314 {
315  it1.swap(it2);
316 }
317 
318 template <typename T>
320 {
321  if (_node != nullptr)
322  _node = _node->next;
323  return *this;
324 }
325 
326 template <typename T>
328 {
329  ListConstIterator<T> result(*this);
330  operator++();
331  return result;
332 }
333 
334 template <typename T>
336 {
337  assert((_node != nullptr) && "Iterator must be valid!");
338 
339  return *_node;
340 }
341 
342 template <typename T>
344 {
345  return _node;
346 }
347 
348 template <typename T>
350 {
351  using std::swap;
352  swap(_node, it._node);
353 }
354 
355 template <typename T>
357 {
358  it1.swap(it2);
359 }
360 
361 template <typename T>
363 {
364  if (_node != nullptr)
365  _node = _node->prev;
366  return *this;
367 }
368 
369 template <typename T>
371 {
372  ListReverseIterator<T> result(*this);
373  operator++();
374  return result;
375 }
376 
377 template <typename T>
379 {
380  assert((_node != nullptr) && "Iterator must be valid!");
381 
382  return *_node;
383 }
384 
385 template <typename T>
387 {
388  return _node;
389 }
390 
391 template <typename T>
393 {
394  using std::swap;
395  swap(_node, it._node);
396 }
397 
398 template <typename T>
400 {
401  it1.swap(it2);
402 }
403 
404 template <typename T>
406 {
407  if (_node != nullptr)
408  _node = _node->prev;
409  return *this;
410 }
411 
412 template <typename T>
414 {
415  ListConstReverseIterator<T> result(*this);
416  operator++();
417  return result;
418 }
419 
420 template <typename T>
422 {
423  assert((_node != nullptr) && "Iterator must be valid!");
424 
425  return *_node;
426 }
427 
428 template <typename T>
430 {
431  return _node;
432 }
433 
434 template <typename T>
436 {
437  using std::swap;
438  swap(_node, it._node);
439 }
440 
441 template <typename T>
443 {
444  it1.swap(it2);
445 }
446 
447 } // namespace CppCommon
Intrusive list constant iterator.
Definition: list.h:351
const value_type & const_reference
Definition: list.h:356
const_pointer operator->() const noexcept
Definition: list.inl:343
const_reference operator*() const noexcept
Definition: list.inl:335
const value_type * const_pointer
Definition: list.h:358
ListConstIterator & operator++() noexcept
Definition: list.inl:319
void swap(ListConstIterator &it) noexcept
Swap two instances.
Definition: list.inl:349
Intrusive list constant reverse iterator.
Definition: list.h:456
void swap(ListConstReverseIterator &it) noexcept
Swap two instances.
Definition: list.inl:435
const_reference operator*() const noexcept
Definition: list.inl:421
const value_type * const_pointer
Definition: list.h:463
const_pointer operator->() const noexcept
Definition: list.inl:429
ListConstReverseIterator & operator++() noexcept
Definition: list.inl:405
const value_type & const_reference
Definition: list.h:461
Intrusive list container.
Definition: list.h:156
const_reverse_iterator crbegin() const noexcept
Definition: list.inl:68
const_reverse_iterator crend() const noexcept
Definition: list.inl:86
void push_next(T &base, T &item) noexcept
Push a new item as a next to the given one.
Definition: list.inl:118
iterator end() noexcept
Get the end list iterator.
Definition: list.inl:38
List() noexcept
Definition: list.h:180
void push_back(T &item) noexcept
Push a new item into the back of the list.
Definition: list.inl:105
reverse_iterator rend() noexcept
Get the reverse end list iterator.
Definition: list.inl:74
reverse_iterator rbegin() noexcept
Get the reverse begin list iterator.
Definition: list.inl:56
const_iterator cend() const noexcept
Definition: list.inl:50
T * pop_back() noexcept
Pop the item from the back of the list.
Definition: list.inl:162
void push_prev(T &base, T &item) noexcept
Push a new item as a previous to the given one.
Definition: list.inl:131
T * pop_front() noexcept
Pop the item from the front of the list.
Definition: list.inl:144
void swap(List &list) noexcept
Swap two instances.
Definition: list.inl:261
iterator begin() noexcept
Get the begin list iterator.
Definition: list.inl:20
const_iterator cbegin() const noexcept
Definition: list.inl:32
T * pop_current(T &base) noexcept
Pop the given item from the list.
Definition: list.inl:180
T * pop_prev(T &base) noexcept
Pop the previous item of the given one from the list.
Definition: list.inl:216
void push_front(T &item) noexcept
Push a new item into the front of the list.
Definition: list.inl:92
void clear() noexcept
Clear the list.
Definition: list.inl:253
void reverse() noexcept
Reverse the list.
Definition: list.inl:234
T * pop_next(T &base) noexcept
Pop the next item of the given one from the list.
Definition: list.inl:198
Intrusive list iterator.
Definition: list.h:299
value_type & reference
Definition: list.h:305
pointer operator->() noexcept
Definition: list.inl:300
ListIterator & operator++() noexcept
Definition: list.inl:276
void swap(ListIterator &it) noexcept
Swap two instances.
Definition: list.inl:306
value_type * pointer
Definition: list.h:307
reference operator*() noexcept
Definition: list.inl:292
Intrusive list reverse iterator.
Definition: list.h:404
ListReverseIterator & operator++() noexcept
Definition: list.inl:362
value_type & reference
Definition: list.h:410
reference operator*() noexcept
Definition: list.inl:378
pointer operator->() noexcept
Definition: list.inl:386
void swap(ListReverseIterator &it) noexcept
Swap two instances.
Definition: list.inl:392
C++ Common project definitions.
Definition: token_bucket.h:15
void swap(ListConstReverseIterator< T > &it1, ListConstReverseIterator< T > &it2) noexcept
Definition: list.inl:442
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23