Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TDeque.h
Go to the documentation of this file.
1#pragma once
2
3#include <climits>
4#include <deque>
5#include <iostream>
6#include <iterator>
7#include <sstream>
8
9namespace sf
10{
11
20template<typename T>
21class TDeque : public std::deque<T>
22{
23 public:
27 typedef typename std::deque<T> base_type;
31 typedef typename base_type::size_type size_type;
35 typedef typename base_type::value_type value_type;
36
40 static const size_t npos = static_cast<size_type>(-1);
41
45 TDeque() = default;
46
50 template<typename InputIterator>
51 TDeque(InputIterator first, InputIterator last)
52 : base_type(first, last)
53 {}
54
59 TDeque(std::initializer_list<value_type> list)
60 : base_type(list)
61 {}
62
66 explicit TDeque(const base_type& sv)
67 : base_type(sv)
68 {}
69
75 explicit TDeque(size_type sz)
76 : base_type(sz)
77 {}
78
84 size_type add(const T&);
85
91 size_type add(const TDeque<T>&);
92
100 bool addAt(const T& t, size_type index);
101
109 bool detach(const T& t);
110
117 bool detachAt(size_type index);
118
124 [[nodiscard]] bool isEmpty() const
125 {
126 return base_type::empty();
127 }
128
132 void flush()
133 {
134 base_type::erase(base_type::begin(), base_type::end());
135 }
136
143 void flush(size_type stop, size_type start = 0)
144 {
145 base_type::erase(base_type::begin() + start, base_type::end() + ((stop >= base_type::size()) ? (base_type::size() - 1) : stop));
146 }
147
153 size_type find(const T&) const;
154
160 [[nodiscard]] size_type count() const
161 {
162 return base_type::size();
163 }
164
172 {
173 return base_type::at(i);
174 }
175
182 const T& get(size_type i) const
183 {
184 return base_type::at(i);
185 }
186
193 {
194 return this;
195 }
196
203 {
204 return this;
205 }
206
207#pragma clang diagnostic push
208#pragma ide diagnostic ignored "HidingNonVirtualFunction"
209
219 {
220 return base_type::at(i);
221 }
222
231 const T& operator[](size_type i) const
232 {
233 return base_type::at(i);
234 }
235
236#pragma clang diagnostic pop
237
244 std::ostream& write(std::ostream& os, bool inc_hex) const;
245};
246
247template<typename T>
248std::ostream& TDeque<T>::write(std::ostream& os, bool inc_hex) const
249{
250 os << '{';
251 for (const auto& x: *this)
252 {
253 os << std::dec << x;
254 if (inc_hex && std::is_integral<T>::value)
255 {
256 os << " (" << std::hex << "0x" << x << ')';
257 }
258 // Check if this is the last entry in the list using pointer comparison instead of value.
259 if (&(*(base_type::end() - 1)) != &x)
260 {
261 os << ", ";
262 }
263 }
264 return os << std::dec << '}';
265}
266
275template<typename T>
276std::ostream& operator<<(std::ostream& os, TDeque<T> const& v)
277{
278 return v.write(os, true);
279}
280
281template<typename T>
283{
284 // Insert item at the end using iterator.
285 return std::distance(base_type::begin(), base_type::insert(base_type::end(), t));
286}
287
288template<typename T>
290{
291 // Insert item at the end.
292 return std::distance(base_type::begin(), base_type::insert(base_type::end(), tv.begin(), tv.end()));
293}
294
295template<typename T>
296bool TDeque<T>::addAt(const T& t, size_type index)
297{
298 // Check if index is in range.
299 if (index > base_type::size())
300 {
301 return false;
302 }
303 if (index == base_type::size())
304 {
305 base_type::insert(base_type::end(), t);
306 }
307 else
308 {
309 base_type::insert(base_type::begin() + index, t);
310 }
311 return true;
312}
313
314template<typename T>
316{
317 // Check if index is in range.
318 if (index >= base_type::size())
319 {
320 return false;
321 }
322 base_type::erase(base_type::begin() + index, base_type::begin() + index + 1);
323 return true;
324}
325
326template<typename T>
327bool TDeque<T>::detach(const T& t)
328{
329 for (unsigned i = 0; i < base_type::size(); i++)
330 {
331 if (base_type::at(i) == t)
332 {
333 base_type::erase(base_type::begin() + i, base_type::begin() + i + 1);
334 return true;
335 }
336 }
337 return false;
338}
339
340template<typename T>
341typename TDeque<T>::size_type TDeque<T>::find(const T& t) const
342{
343 for (size_type i = 0; i < base_type::size(); i++)
344 {
345 if (base_type::at(i) == t)
346 {
347 return i;
348 }
349 }
350 return npos;
351}
352
353}// namespace sf
Counted deque having additional methods and operators for ease of usage.
Definition TDeque.h:22
std::deque< T > base_type
Base type of this template .
Definition TDeque.h:27
void flush(size_type stop, size_type start=0)
Removes specific range of entries from the deque.
Definition TDeque.h:143
bool isEmpty() const
Returns true when empty false otherwise.
Definition TDeque.h:124
base_type::size_type size_type
Size type of this template.
Definition TDeque.h:31
size_type add(const T &)
Adds item at the end of the deque.
Definition TDeque.h:282
base_type getBase() const
Returns the constant const base type.
Definition TDeque.h:202
T & operator[](size_type i)
Array operator.
Definition TDeque.h:218
TDeque(std::initializer_list< value_type > list)
Initializing constructor using list like: TDeque que{1,2,3,4,5,6,7}
Definition TDeque.h:59
size_type count() const
Returns the amount of entries in the deque.
Definition TDeque.h:160
TDeque(size_type sz)
Initializing constructor.
Definition TDeque.h:75
base_type::value_type value_type
Value type contained by this deque template.
Definition TDeque.h:35
bool detachAt(size_type index)
Removes specific item from the list by index.
Definition TDeque.h:315
bool detach(const T &t)
Removes specific item from the list by instance.
Definition TDeque.h:327
const T & get(size_type i) const
Const version of getting entry from index position.
Definition TDeque.h:182
size_type find(const T &) const
Finds an entry by instance in the deque.
Definition TDeque.h:341
base_type getBase()
Returns the base type to access it methods explicitly.
Definition TDeque.h:192
std::ostream & write(std::ostream &os, bool inc_hex) const
Definition TDeque.h:248
TDeque(InputIterator first, InputIterator last)
Initializing constructor using an iterator.
Definition TDeque.h:51
const T & operator[](size_type i) const
Const array operator.
Definition TDeque.h:231
bool addAt(const T &t, size_type index)
Adds an item at index position.
Definition TDeque.h:296
void flush()
Removes all entries from the deque.
Definition TDeque.h:132
T & get(size_type i)
Gets entry from index position.
Definition TDeque.h:171
static const size_t npos
Value returned by various member functions when they fail.
Definition TDeque.h:40
TDeque()=default
Default constructor.
TDeque(const base_type &sv)
Copy constructor for base type.
Definition TDeque.h:66
Definition Application.h:10
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.