0.2.0
Loading...
Searching...
No Matches
TsDeque.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <deque>
17#include <mutex>
18
19namespace NAV
20{
21
25template<class T, class Alloc = std::allocator<T>>
27{
28 public:
29 // #######################################################################################################
30 // Member functions
31 // #######################################################################################################
32
34 TsDeque() = default;
35
37 ~TsDeque() = default;
38
41 explicit TsDeque(const Alloc& alloc)
42 : _queue(alloc) {}
43
48 TsDeque(typename std::deque<T, Alloc>::size_type count, const T& value, const Alloc& alloc = Alloc())
49 : _queue(count, value, alloc) {}
50
54 explicit TsDeque(typename std::deque<T, Alloc>::size_type count, const Alloc& alloc = Alloc())
55 : _queue(count, alloc) {}
56
61 template<class InputIt>
62 TsDeque(InputIt first, InputIt last, const Alloc& alloc = Alloc())
63 : _queue(first, last, alloc)
64 {}
65
68 TsDeque(const TsDeque& other)
69 {
70 std::scoped_lock lk(other._mutex);
71 _queue = std::deque<T, Alloc>(other._queue); // NOLINT(cppcoreguidelines-prefer-member-initializer)
72 }
76 TsDeque(const TsDeque& other, const Alloc& alloc)
77 {
78 std::scoped_lock lk(other._mutex);
79 _queue = std::deque<T, Alloc>(other._queue, alloc); // NOLINT(cppcoreguidelines-prefer-member-initializer)
80 }
84 TsDeque(TsDeque&& other) noexcept
85 {
86 std::scoped_lock lk(other._mutex);
87 _queue = std::deque<T, Alloc>(std::move(other._queue)); // NOLINT(cppcoreguidelines-prefer-member-initializer)
88 }
93 TsDeque(TsDeque&& other, const Alloc& alloc) noexcept
94 {
95 std::scoped_lock lk(other._mutex);
96 _queue = std::deque<T, Alloc>(std::move(other._queue), alloc); // NOLINT(cppcoreguidelines-prefer-member-initializer)
97 }
101 TsDeque(std::initializer_list<T> init, const Alloc& alloc = Alloc())
102 : _queue(init, alloc) {}
103
105 TsDeque& operator=(const TsDeque& other)
106 {
107 if (this != &other)
108 {
109 std::scoped_lock lk(_mutex);
110 std::scoped_lock lko(other._mutex);
111 _queue = other._queue;
112 }
113 return *this;
114 }
116 TsDeque& operator=(TsDeque&& other) noexcept
117 {
118 if (this != &other)
119 {
120 std::scoped_lock lk(_mutex);
121 std::scoped_lock lko(other._mutex);
122 _queue = std::move(other._queue);
123 }
124 return *this;
125 }
128 TsDeque& operator=(std::initializer_list<T> ilist)
129 {
130 _queue = ilist;
131 }
132
138 void assign(typename std::deque<T, Alloc>::size_type count, const T& value)
139 {
140 std::scoped_lock lk(_mutex);
141 _queue.assign(count, value);
142 }
143
149 template<class InputIt>
150 void assign(InputIt first, InputIt last)
151 {
152 std::scoped_lock lk(_mutex);
153 _queue.assign(first, last);
154 }
159 void assign(std::initializer_list<T> ilist)
160 {
161 std::scoped_lock lk(_mutex);
162 _queue.assign(ilist);
163 }
164
167 typename std::deque<T, Alloc>::allocator_type get_allocator() const noexcept
168 {
169 return _queue.get_allocator();
170 }
171
172 // #######################################################################################################
173 // Element access
174 // #######################################################################################################
175
180 auto& at(typename std::deque<T, Alloc>::size_type pos) { return _queue.at(pos); }
185 const auto& at(typename std::deque<T, Alloc>::size_type pos) const { return _queue.at(pos); }
186
190 auto& operator[](typename std::deque<T, Alloc>::size_type pos) { return _queue[pos]; }
194 const auto& operator[](typename std::deque<T, Alloc>::size_type pos) const { return _queue[pos]; }
195
198 auto& front() { return _queue.front(); }
201 const auto& front() const { return _queue.front(); }
202
205 auto& back() { return _queue.back(); }
208 const auto& back() const { return _queue.back(); }
209
210 // #######################################################################################################
211 // Iterators
212 // #######################################################################################################
213
217 typename std::deque<T>::iterator begin() noexcept { return _queue.begin(); }
221 typename std::deque<T>::const_iterator begin() const noexcept { return _queue.begin(); }
225 typename std::deque<T>::const_iterator cbegin() const noexcept { return _queue.cbegin(); }
226
230 typename std::deque<T>::iterator end() noexcept { return _queue.end(); }
234 typename std::deque<T>::const_iterator end() const noexcept { return _queue.end(); }
238 typename std::deque<T>::const_iterator cend() const noexcept { return _queue.cend(); }
239
243 typename std::deque<T>::reverse_iterator rbegin() noexcept { return _queue.rbegin(); }
247 typename std::deque<T>::const_reverse_iterator rbegin() const noexcept { return _queue.rbegin(); }
251 typename std::deque<T>::const_reverse_iterator crbegin() const noexcept { return _queue.crbegin(); }
252
257 typename std::deque<T>::reverse_iterator rend() noexcept { return _queue.rend(); }
262 typename std::deque<T>::const_reverse_iterator rend() const noexcept { return _queue.rend(); }
267 typename std::deque<T>::const_reverse_iterator crend() const noexcept { return _queue.crend(); }
268
269 // #######################################################################################################
270 // Capacity
271 // #######################################################################################################
272
275 [[nodiscard]] bool empty() const noexcept
276 {
277 std::scoped_lock lk(_mutex);
278 return _queue.empty();
279 }
280
283 typename std::deque<T, Alloc>::size_type size() const noexcept
284 {
285 std::scoped_lock lk(_mutex);
286 return _queue.size();
287 }
288
291 typename std::deque<T, Alloc>::size_type max_size() const noexcept
292 {
293 std::scoped_lock lk(_mutex);
294 return _queue.max_size();
295 }
296
299 {
300 std::scoped_lock lk(_mutex);
301 _queue.shrink_to_fit();
302 }
303
304 // #######################################################################################################
305 // Modifiers
306 // #######################################################################################################
307
310 void clear() noexcept
311 {
312 std::scoped_lock lk(_mutex);
313 _queue.clear();
314 }
315
320 typename std::deque<T>::iterator insert(typename std::deque<T>::const_iterator pos, const T& value)
321 {
322 std::scoped_lock lk(_mutex);
323 return _queue.insert(pos, value);
324 }
325
330 typename std::deque<T>::iterator insert(typename std::deque<T>::const_iterator pos, T&& value)
331 {
332 std::scoped_lock lk(_mutex);
333 return _queue.insert(pos, value);
334 }
335
341 typename std::deque<T>::iterator insert(typename std::deque<T>::const_iterator pos, typename std::deque<T, Alloc>::size_type count, const T& value)
342 {
343 std::scoped_lock lk(_mutex);
344 return _queue.insert(pos, count, value);
345 }
346
353 template<class InputIt>
354 typename std::deque<T>::iterator insert(typename std::deque<T>::const_iterator pos, InputIt first, InputIt last)
355 {
356 std::scoped_lock lk(_mutex);
357 return _queue.insert(pos, first, last);
358 }
359
364 typename std::deque<T>::iterator insert(typename std::deque<T>::const_iterator pos, std::initializer_list<T> ilist)
365 {
366 std::scoped_lock lk(_mutex);
367 return _queue.insert(pos, ilist);
368 }
369
374 template<class... Args>
375 typename std::deque<T>::iterator emplace(typename std::deque<T>::const_iterator pos, Args&&... args)
376 {
377 std::scoped_lock lk(_mutex);
378 return _queue.emplace(pos, std::forward<Args>(args)...);
379 }
380
384 typename std::deque<T>::iterator erase(typename std::deque<T>::const_iterator pos)
385 {
386 std::scoped_lock lk(_mutex);
387 return _queue.erase(pos);
388 }
395 typename std::deque<T>::iterator erase(typename std::deque<T>::const_iterator first, typename std::deque<T>::const_iterator last)
396 {
397 std::scoped_lock lk(_mutex);
398 return _queue.erase(first, last);
399 }
400
403 void push_back(const T& value)
404 {
405 std::scoped_lock lk(_mutex);
406 _queue.push_back(value);
407 }
410 void push_back(T&& value)
411 {
412 std::scoped_lock lk(_mutex);
413 _queue.push_back(value);
414 }
415
419 template<class... Args>
420 auto& emplace_back(Args&&... args)
421 {
422 std::scoped_lock lk(_mutex);
423 return _queue.emplace_back(std::forward<Args>(args)...);
424 }
425
427 void pop_back()
428 {
429 std::scoped_lock lk(_mutex);
430 _queue.pop_back();
431 }
432
436 void push_front(const T& value)
437 {
438 std::scoped_lock lk(_mutex);
439 _queue.push_front(value);
440 }
444 void push_front(T&& value)
445 {
446 std::scoped_lock lk(_mutex);
447 _queue.push_front(value);
448 }
449
453 template<class... Args>
454 auto& emplace_front(Args&&... args)
455 {
456 std::scoped_lock lk(_mutex);
457 return _queue.emplace_front(std::forward<Args>(args)...);
458 }
459
462 {
463 std::scoped_lock lk(_mutex);
464 _queue.pop_front();
465 }
466
469 void resize(typename std::deque<T, Alloc>::size_type count)
470 {
471 std::scoped_lock lk(_mutex);
472 _queue.resize(count);
473 }
477 void resize(typename std::deque<T, Alloc>::size_type count, const T& value)
478 {
479 std::scoped_lock lk(_mutex);
480 _queue.resize(count, value);
481 }
482
486 void swap(std::deque<T>& other) noexcept
487 {
488 std::scoped_lock lk(_mutex);
489 _queue.swap(other);
490 }
491
495 {
496 T front;
497 {
498 std::scoped_lock lk(_mutex);
499 front = _queue.front();
500 }
501 pop_front();
502 return front;
503 }
504
505 private:
507 std::deque<T, Alloc> _queue;
508
510 mutable std::mutex _mutex;
511};
512
513} // namespace NAV
Thread-safe deque.
Definition TsDeque.hpp:27
void push_front(const T &value)
Prepends the given element value to the beginning of the container. The new element is initialized as...
Definition TsDeque.hpp:436
std::deque< T >::const_iterator begin() const noexcept
Returns an iterator to the first element of the deque. If the deque is empty, the returned iterator w...
Definition TsDeque.hpp:221
auto & emplace_back(Args &&... args)
Appends a new element to the end of the container.
Definition TsDeque.hpp:420
const auto & front() const
Returns a reference to the first element in the container.
Definition TsDeque.hpp:201
std::deque< T >::reverse_iterator rend() noexcept
Returns a reverse iterator to the element following the last element of the reversed deque....
Definition TsDeque.hpp:257
auto & back()
Returns a reference to the last element in the container.
Definition TsDeque.hpp:205
void assign(typename std::deque< T, Alloc >::size_type count, const T &value)
Replaces the contents with count copies of value value.
Definition TsDeque.hpp:138
std::deque< T >::reverse_iterator rbegin() noexcept
Returns a reverse iterator to the first element of the reversed deque. It corresponds to the last ele...
Definition TsDeque.hpp:243
TsDeque(typename std::deque< T, Alloc >::size_type count, const Alloc &alloc=Alloc())
Constructs the container with count default-inserted instances of T. No copies are made.
Definition TsDeque.hpp:54
const auto & operator[](typename std::deque< T, Alloc >::size_type pos) const
Returns a reference to the element at specified location pos. No bounds checking is performed.
Definition TsDeque.hpp:194
void resize(typename std::deque< T, Alloc >::size_type count)
Resizes the container to contain count elements.
Definition TsDeque.hpp:469
std::deque< T >::iterator insert(typename std::deque< T >::const_iterator pos, std::initializer_list< T > ilist)
Inserts elements from initializer list ilist before pos.
Definition TsDeque.hpp:364
std::deque< T >::iterator insert(typename std::deque< T >::const_iterator pos, InputIt first, InputIt last)
inserts elements from range [first, last) before pos.
Definition TsDeque.hpp:354
TsDeque(InputIt first, InputIt last, const Alloc &alloc=Alloc())
Constructs the container with the contents of the range [first, last).
Definition TsDeque.hpp:62
std::deque< T >::iterator begin() noexcept
Returns an iterator to the first element of the deque. If the deque is empty, the returned iterator w...
Definition TsDeque.hpp:217
TsDeque(TsDeque &&other) noexcept
Move constructor. Constructs the container with the contents of other using move semantics....
Definition TsDeque.hpp:84
TsDeque & operator=(std::initializer_list< T > ilist)
Replaces the contents with those identified by initializer list ilist.
Definition TsDeque.hpp:128
void resize(typename std::deque< T, Alloc >::size_type count, const T &value)
Resizes the container to contain count elements.
Definition TsDeque.hpp:477
std::deque< T >::const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the first element of the reversed deque. It corresponds to the last ele...
Definition TsDeque.hpp:247
TsDeque & operator=(TsDeque &&other) noexcept
Move assignment operator.
Definition TsDeque.hpp:116
auto & at(typename std::deque< T, Alloc >::size_type pos)
Returns a reference to the element at specified location pos, with bounds checking....
Definition TsDeque.hpp:180
TsDeque(const Alloc &alloc)
Constructs an empty container with the given allocator alloc.
Definition TsDeque.hpp:41
void assign(std::initializer_list< T > ilist)
Replaces the contents with the elements from the initializer list ilist.
Definition TsDeque.hpp:159
void shrink_to_fit()
Requests the removal of unused capacity.
Definition TsDeque.hpp:298
void swap(std::deque< T > &other) noexcept
Exchanges the contents of the container with those of other. Does not invoke any move,...
Definition TsDeque.hpp:486
std::deque< T, Alloc >::size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition TsDeque.hpp:283
void push_front(T &&value)
Prepends the given element value to the beginning of the container. Value is moved into the new eleme...
Definition TsDeque.hpp:444
void push_back(const T &value)
Appends the given element value to the end of the container. The new element is initialized as a copy...
Definition TsDeque.hpp:403
const auto & at(typename std::deque< T, Alloc >::size_type pos) const
Returns a reference to the element at specified location pos, with bounds checking....
Definition TsDeque.hpp:185
const auto & back() const
Returns a reference to the last element in the container.
Definition TsDeque.hpp:208
std::deque< T >::const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the element following the last element of the reversed deque....
Definition TsDeque.hpp:262
std::deque< T >::iterator insert(typename std::deque< T >::const_iterator pos, T &&value)
Insert value before pos in the container.
Definition TsDeque.hpp:330
void clear() noexcept
Definition TsDeque.hpp:310
~TsDeque()=default
Destructor.
void pop_back()
Removes the last element of the container.
Definition TsDeque.hpp:427
TsDeque(const TsDeque &other)
Copy constructor. Constructs the container with the copy of the contents of other.
Definition TsDeque.hpp:68
void assign(InputIt first, InputIt last)
Replaces the contents with copies of those in the range [first, last). The behavior is undefined if e...
Definition TsDeque.hpp:150
std::deque< T >::const_iterator cend() const noexcept
Returns an iterator to the element following the last element of the deque. This element acts as a pl...
Definition TsDeque.hpp:238
std::deque< T >::iterator insert(typename std::deque< T >::const_iterator pos, const T &value)
Insert value before pos in the container.
Definition TsDeque.hpp:320
TsDeque(TsDeque &&other, const Alloc &alloc) noexcept
Allocator-extended move constructor. Using alloc as the allocator for the new container,...
Definition TsDeque.hpp:93
TsDeque(typename std::deque< T, Alloc >::size_type count, const T &value, const Alloc &alloc=Alloc())
Constructs the container with count copies of elements with value value.
Definition TsDeque.hpp:48
TsDeque(std::initializer_list< T > init, const Alloc &alloc=Alloc())
Constructs the container with the contents of the initializer list init.
Definition TsDeque.hpp:101
std::deque< T >::const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the first element of the reversed deque. It corresponds to the last ele...
Definition TsDeque.hpp:251
auto & front()
Returns a reference to the first element in the container.
Definition TsDeque.hpp:198
auto & emplace_front(Args &&... args)
Inserts a new element to the beginning of the container.
Definition TsDeque.hpp:454
std::deque< T >::iterator emplace(typename std::deque< T >::const_iterator pos, Args &&... args)
Inserts a new element into the container directly before pos.
Definition TsDeque.hpp:375
auto & operator[](typename std::deque< T, Alloc >::size_type pos)
Returns a reference to the element at specified location pos. No bounds checking is performed.
Definition TsDeque.hpp:190
std::deque< T >::iterator insert(typename std::deque< T >::const_iterator pos, typename std::deque< T, Alloc >::size_type count, const T &value)
inserts count copies of the value before pos
Definition TsDeque.hpp:341
TsDeque()=default
Default Constructor. Constructs an empty container with a default-constructed allocator.
std::deque< T >::iterator end() noexcept
Returns an iterator to the element following the last element of the deque. This element acts as a pl...
Definition TsDeque.hpp:230
auto extract_front()
Returns a copy of the first element in the container and removes it from the container.
Definition TsDeque.hpp:494
TsDeque & operator=(const TsDeque &other)
Copy assignment operator.
Definition TsDeque.hpp:105
bool empty() const noexcept
Checks if the container has no elements, i.e. whether 'begin() == end()'.
Definition TsDeque.hpp:275
std::deque< T >::iterator erase(typename std::deque< T >::const_iterator pos)
Removes the element at pos.
Definition TsDeque.hpp:384
TsDeque(const TsDeque &other, const Alloc &alloc)
Constructs the container with the copy of the contents of other, using alloc as the allocator.
Definition TsDeque.hpp:76
std::deque< T >::const_iterator end() const noexcept
Returns an iterator to the element following the last element of the deque. This element acts as a pl...
Definition TsDeque.hpp:234
std::deque< T, Alloc >::size_type max_size() const noexcept
Returns the maximum number of elements the container is able to hold due to system or library impleme...
Definition TsDeque.hpp:291
std::deque< T >::const_iterator cbegin() const noexcept
Returns an iterator to the first element of the deque. If the deque is empty, the returned iterator w...
Definition TsDeque.hpp:225
std::deque< T >::iterator erase(typename std::deque< T >::const_iterator first, typename std::deque< T >::const_iterator last)
Removes the elements in the range [first, last). All iterators and references are invalidated,...
Definition TsDeque.hpp:395
void pop_front()
Removes the first element of the container. If there are no elements in the container,...
Definition TsDeque.hpp:461
std::deque< T >::const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the element following the last element of the reversed deque....
Definition TsDeque.hpp:267
std::deque< T, Alloc >::allocator_type get_allocator() const noexcept
Returns the allocator associated with the container.
Definition TsDeque.hpp:167
void push_back(T &&value)
Appends the given element value to the end of the container. Value is moved into the new element.
Definition TsDeque.hpp:410