Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TVector.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cinttypes>
5#include <climits>
6#include <iostream>
7#include <iterator>
8#include <limits>
9#include <sstream>
10#include <vector>
11
12namespace sf
13{
14
15template<typename T>
16class TIterator;
17
26template<typename T>
27class TVector : public std::vector<T>
28{
29 public:
33 typedef typename std::vector<T> base_type;
37 typedef typename base_type::size_type size_type;
41 typedef typename base_type::value_type value_type;
50
54 static const size_t npos = static_cast<size_type>(-1);
55
59 TVector() = default;
60
64 TVector(const TVector& v)
65 : base_type(v)
66 {}
67
71 TVector(TVector&&) noexcept = default;
72
76 TVector& operator=(const TVector& v) noexcept
77 {
78 *((base_type*) this) = (base_type) v;
79 return *this;
80 }
81
85 TVector& operator=(TVector&& v) noexcept
86 {
87 *((base_type*) this) = (base_type) v;
88 return *this;
89 }
90
94 template<typename InputIterator>
95 TVector(InputIterator first, InputIterator last)
97 {}
98
103 TVector(std::initializer_list<value_type> list)
104 : base_type(list)
105 {}
106
110 explicit TVector(const base_type& sv)
111 : base_type(sv)
112 {}
113
119 explicit TVector(size_type sz)
120 : base_type(sz)
121 {}
122
129 size_type add(const T& t);
130
138
144 TVector<T>& append(const T& t);
145
152
159
167 bool addAt(const T& t, size_type index);
168
176 bool addAt(T&& t, size_type index);
177
185 bool detach(const T& t);
186
193 bool detachAt(size_type index);
194
200 [[nodiscard]] bool isEmpty() const
201 {
202 return base_type::empty();
203 }
204
208 void flush()
209 {
210 base_type::erase(base_type::begin(), base_type::end());
211 }
212
219 void flush(size_type stop, size_type start = 0)
220 {
221 base_type::erase(base_type::begin() + start, base_type::end() + ((stop >= base_type::size()) ? (base_type::size() - 1) : stop));
222 }
223
229 size_type find(const T&) const;
230
236 [[nodiscard]] size_type count() const
237 {
238 return base_type::size();
239 }
240
245 inline T& first()
246 {
247 assert(!base_type::empty());
248 return *base_type::begin();
249 }
250
255 inline const T& first() const noexcept
256 {
257 static_assert(!base_type::empty());
258 return *base_type::begin();
259 }
260
265 inline T& last()
266 {
267 assert(!base_type::empty());
268 return *(base_type::end() - 1);
269 }
270
275 inline const T& last() const noexcept
276 {
277 assert(!base_type::empty());
278 return *(base_type::end() - 1);
279 }
280
284 inline bool startsWith(T t) const
285 {
286 return !base_type::empty() && first() == t;
287 }
288
292 inline bool endsWith(T t) const
293 {
294 return !base_type::empty() && last() == t;
295 }
296
303 inline T& get(size_type i)
304 {
305 return base_type::at(i);
306 }
307
314 const T& get(size_type i) const
315 {
316 return base_type::at(i);
317 }
318
325 {
326 return this;
327 }
328
335 {
336 return this;
337 }
338
348 {
349 return base_type::at(i);
350 }
351
360 const T& operator[](size_type i) const
361 {
362 return base_type::at(i);
363 }
364
371 std::ostream& write(std::ostream& os, bool inc_hex) const;
372};
373
374template<typename T>
375std::ostream& TVector<T>::write(std::ostream& os, bool inc_hex) const
376{
377 os << '{';
378 for (const auto& x: *this)
379 {
380 os << std::dec << x;
381 if (inc_hex && std::is_integral<T>::value)
382 {
383 uint64_t _x{0};
384 for (int i = 0; i < sizeof(T); i++)
385 {
386 ((uint8_t*) &_x)[i] = ((uint8_t*) &x)[i];
387 }
388 os << " (" << std::hex << "0x" << _x << ')';
389 }
390 // Check if this is the last entry in the list using pointer comparison instead of value.
391 if (&(*(base_type::end() - 1)) != &x)
392 {
393 os << ", ";
394 }
395 }
396 return os << std::dec << '}';
397}
398
407template<typename T>
408std::ostream& operator<<(std::ostream& os, TVector<T> const& v)
409{
410 return v.write(os, true);
411}
412
417template<typename T>
419{
420 public:
421 typedef std::vector<T> base_t;
422
423 explicit TIterator(const base_t& v)
424 {
425 _vector = const_cast<base_t*>(&v);
426 restart(0, _vector->size());
427 }
428
429 TIterator(const base_t& v, size_t start, size_t stop)
430 {
431 _vector = &v;
432 restart(start, stop);
433 }
434
435 operator int() const
436 {
437 return _cur < _upper;
438 }
439
440 const T& current() const
441 {
442 return (*_vector)[_cur];
443 }
444
446 {
447 return (*_vector)[_cur];
448 }
449
450 const T& operator++(int)
451 {
452 const T& temp = current();
453 _cur++;
454 return temp;
455 }
456
457 const T& operator++()
458 {
459 _cur++;
460 return current();
461 }
462
463 void restart()
464 {
465 restart(_lower, _upper);
466 }
467
468 void restart(unsigned start, unsigned stop)
469 {
470 _cur = _lower = start;
471 _upper = stop;
472 }
473
474 private:
475 base_t* _vector;
476 unsigned _cur{};
477 unsigned _lower{};
478 unsigned _upper{};
479};
480
481template<typename T>
483{
484 // Insert item at the end.
485 base_type::push_back(t);
486 // Index it the size minus one.
487 return base_type::size() - 1;
488}
489
490template<typename T>
492{
493 // Insert item at the end.
494 base_type::push_back(t);
495 // Index it the size minus one.
496 return base_type::size() - 1;
497}
498
499template<typename T>
501{
502 // Insert item at the end.
503 base_type::push_back(t);
504 return *this;
505}
506
507template<typename T>
509{
510 // Insert item at the end.
511 base_type::push_back(std::move(t));
512 return *this;
513}
514
515template<typename T>
517{
518 // The insertion index is the current size.
519 auto index = base_type::size();
520 // Insert items at the end.
521 base_type::insert(base_type::end(), tv.begin(), tv.end());
522 return index;
523}
524
525template<typename T>
526bool TVector<T>::addAt(const T& t, size_type index)
527{
528 // Check if index is in range.
529 if (index > base_type::size())
530 {
531 return false;
532 }
533 if (index == base_type::size())
534 {
535 base_type::insert(base_type::end(), t);
536 }
537 else
538 {
539 base_type::insert(base_type::begin() + index, t);
540 }
541 return true;
542}
543
544template<typename T>
545bool TVector<T>::addAt(T&& t, size_type index)
546{
547 // Check if index is in range.
548 if (index > base_type::size())
549 {
550 return false;
551 }
552 if (index == base_type::size())
553 {
554 base_type::insert(base_type::end(), t);
555 }
556 else
557 {
558 base_type::insert(base_type::begin() + index, t);
559 }
560 return true;
561}
562
563template<typename T>
565{
566 // Check if index is in range.
567 if (index >= base_type::size())
568 {
569 return false;
570 }
571 base_type::erase(base_type::begin() + index, base_type::begin() + index + 1);
572 return true;
573}
574
575template<typename T>
576bool TVector<T>::detach(const T& t)
577{
578 for (size_type i = 0; i < base_type::size(); i++)
579 {
580 if (base_type::at(i) == t)
581 {
582 base_type::erase(base_type::begin() + i, base_type::begin() + i + 1);
583 return true;
584 }
585 }
586 return false;
587}
588
589template<typename T>
590typename TVector<T>::size_type TVector<T>::find(const T& t) const
591{
592 for (size_type i = 0; i < base_type::size(); i++)
593 {
594 if (base_type::at(i) == t)
595 {
596 return i;
597 }
598 }
599 return npos;
600}
601
602}// namespace sf
Counted vector having function names compatible with Borland C++ templates.
Definition TVector.h:419
void restart(unsigned start, unsigned stop)
Definition TVector.h:468
TIterator(const base_t &v)
Definition TVector.h:423
std::vector< T > base_t
Definition TVector.h:421
void restart()
Definition TVector.h:463
T & current()
Definition TVector.h:445
const T & operator++()
Definition TVector.h:457
const T & operator++(int)
Definition TVector.h:450
const T & current() const
Definition TVector.h:440
TIterator(const base_t &v, size_t start, size_t stop)
Definition TVector.h:429
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:28
base_type::size_type size_type
Size type of this template.
Definition TVector.h:37
base_type getBase()
Returns the base type to access it methods explicitly.
Definition TVector.h:324
T & first()
Gets the first element of the vector.
Definition TVector.h:245
TVector(TVector &&) noexcept=default
Move constructor.
void flush(size_type stop, size_type start=0)
Removes specific range of entries from the vector.
Definition TVector.h:219
std::vector< T > base_type
Base type of this template .
Definition TVector.h:33
const T & last() const noexcept
Gets the last element of the vector.
Definition TVector.h:275
TVector< T > & append(const T &t)
Appends an entry to the vectors items at the end of the vector but returns itself.
Definition TVector.h:500
bool detachAt(size_type index)
Removes specific item from the list by index.
Definition TVector.h:564
T & get(size_type i)
Gets entry from index position.
Definition TVector.h:303
bool endsWith(T t) const
Checks if the last element is of the passed value.
Definition TVector.h:292
const T & operator[](size_type i) const
Const array operator.
Definition TVector.h:360
base_type::value_type value_type
Value type contained by this vector template.
Definition TVector.h:41
const iter_type const_iter_type
Iteration const type of the template.
Definition TVector.h:49
bool isEmpty() const
Returns true when empty false otherwise.
Definition TVector.h:200
bool addAt(const T &t, size_type index)
Adds an item at index position.
Definition TVector.h:526
static const size_t npos
Value returned by various member functions when they fail.
Definition TVector.h:54
size_type find(const T &) const
Finds an entry by instance in the vector.
Definition TVector.h:590
TVector(const TVector &v)
Copy constructor.
Definition TVector.h:64
std::ostream & write(std::ostream &os, bool inc_hex) const
Definition TVector.h:375
TVector()=default
Default constructor.
size_type add(T &&t)
Adds item at the end of the vector.
Definition TVector.h:491
TVector(std::initializer_list< value_type > list)
Initializing constructor using list like: TVector vect{1,2,3,4,5,6,7}
Definition TVector.h:103
void flush()
Removes all entries from the vector.
Definition TVector.h:208
const T & first() const noexcept
Gets the first element of the vector.
Definition TVector.h:255
TVector(InputIterator first, InputIterator last)
Initializing constructor using an iterator.
Definition TVector.h:95
size_type count() const
Returns the amount of entries in the vector.
Definition TVector.h:236
bool addAt(T &&t, size_type index)
Adds an item at index position.
Definition TVector.h:545
bool startsWith(T t) const
Checks if the first element is of the passed value.
Definition TVector.h:284
size_type add(const T &t)
Adds item at the end of the vector.
Definition TVector.h:482
TVector & operator=(TVector &&v) noexcept
Assignment move operator.
Definition TVector.h:85
TVector(const base_type &sv)
Copy constructor for base type.
Definition TVector.h:110
bool detach(const T &t)
Removes specific item from the list by instance.
Definition TVector.h:576
TVector< T > & append(T &&t)
Appends an entry to the vectors items at the end of the vector but returns itself.
Definition TVector.h:508
TIterator< value_type > iter_type
Iteration type of the template.
Definition TVector.h:45
size_type add(const TVector< T > &)
Adds the vectors items at the end of the vector.
Definition TVector.h:516
base_type getBase() const
Returns the constant const base type.
Definition TVector.h:334
TVector(size_type sz)
Initializing constructor.
Definition TVector.h:119
const T & get(size_type i) const
Const version of getting entry from index position.
Definition TVector.h:314
T & last()
Gets the last element of the vector.
Definition TVector.h:265
T & operator[](size_type i)
Array operator.
Definition TVector.h:347
Definition Application.h:11
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.