Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Range.h
Go to the documentation of this file.
1#pragma once
2#include <iostream>
3#include <limits>
4#include <misc/gen/TVector.h>
5#include <misc/global.h>
6
7namespace sf
8{
9
15
16// Packed structure for binary storage.
17#pragma pack(push, 1)
18
22struct RANGE
23{
27 typedef uint64_t size_type;
31 typedef int64_t id_type;
44};
45// Restore the pack option.
46#pragma pack(pop)
47
51class _MISC_CLASS Range : private RANGE
52{
53 public:
62
66 class _MISC_CLASS Vector : public TVector<Range>
67 {
68 public:
74
79 [[nodiscard]] Vector sort() const;
80
85
92 [[nodiscard]] Vector exclude(const Vector& rel) const;
93
98 Vector& exclude(const Vector& rl_ex);
99
107 Vector extract(const Vector& rl, Vector& rl_ex) const;
108
113 Vector& merge(const Vector& rl_add);
114 };
115
120
124 inline Range();
125
129 inline Range& operator=(Range&&) = default;
130
135 inline Range(const Range& r);
136
140 explicit inline Range(const RANGE& r);
141
148 inline Range(size_type start, size_type stop, id_type id = 0);
149
153 void normalize();
154
162 Range& assign(size_type start, size_type stop, id_type id = 0);
163
167 inline Range& assign(const Range& r);
168
172 Range& set(const RANGE& r);
173
177 inline const Range& copyTo(RANGE& dst) const;
178
182 [[nodiscard]] inline size_type getStart() const;
183
187 [[nodiscard]] inline size_type getStop() const;
188
192 inline void clear();
193
197 [[nodiscard]] inline size_type getSize() const;
198
202 [[nodiscard]] inline bool isEmpty() const;
203
207 [[nodiscard]] bool isOverlapped(const Range& r) const;
208
212 [[nodiscard]] bool isWithinOther(const Range& r) const;
213
217 [[nodiscard]] bool isWithinSelf(const Range& r) const;
218
222 [[nodiscard]] bool isExtension(const Range& r) const;
223
227 [[nodiscard]] bool isMergeable(const Range& r) const;
228
237 inline size_type split(size_type seg_sz, Vector& rl_dst) const;
238
248 static size_type split(size_type seg_sz, const Range& req, Vector& rl_dst);
249
258 static size_type split(size_type seg_sz, const Vector& req, Vector& rl);
259
263 [[nodiscard]] inline bool isInRange(size_type idx) const;
264
268 const RANGE& getBase() const;
269
273 explicit inline operator bool() const;
274
281 enum ECompare : int
282 {
286 cmpError = std::numeric_limits<int>::max(),
294 cmpSame = 0,
302 cmpWithinSelf = 1,
310 cmpWithinOther = -1,
318 cmpOverlapsOther = 2,
326 cmpOverlapsSelf = -2,
334 cmpExtendsOther = 3,
342 cmpExtendsSelf = -3,
350 cmpBeforeSelf = 4,
358 cmpAfterSelf = -4,
366 cmpSelfEmpty = 5,
373 cmpOtherEmpty = -5,
374 };
375
379 [[nodiscard]] ECompare compare(const Range& other) const;
380
384 [[nodiscard]] inline id_type getId() const;
385
389 inline Range& setId(id_type id);
390
394 inline Range& operator=(const Range& r);
395
399 inline Range& operator=(const RANGE& r);
400
404 inline bool operator==(const Range& r) const;
405
409 inline bool operator!=(const Range& r) const;
410
417 inline bool operator<(const Range& r) const;
418
422 Range operator&(const Range& r) const;
423
427 inline Range& operator&=(const Range& r);
428
434 Range operator+(const Range& r) const;
435
441 inline Range& operator+=(const Range& r);
442
452 int exclude(const Range& r, Range& rest, Range::ECompare* cmp = nullptr);
453
460 inline Range& offsetBy(size_type ofs);
461
467 [[nodiscard]] inline Range offset(size_type ofs) const;
468
472 bool operator>(const Range& r) const = delete;
473
477 bool operator>(const Range& r) = delete;
478
482 bool operator<=(const Range& r) const = delete;
483
487 bool operator<=(const Range& r) = delete;
488
492 bool operator>=(const Range& r) const = delete;
493
497 bool operator>=(const Range& r) = delete;
498
499 private:
500 friend std::ostream& operator<<(std::ostream& os, const Range& r);
501
502 friend std::istream& operator>>(std::istream& is, Range& r);
503};
504
508_MISC_FUNC std::ostream& operator<<(std::ostream& os, const Range& r);
509
513_MISC_FUNC std::istream& operator>>(std::istream& is, Range& r);
514
518_MISC_FUNC std::ostream& operator<<(std::ostream& os, const Range::Vector& rl);
519
523_MISC_FUNC std::istream& operator>>(std::istream& is, Range::Vector& rl);
524
526 : RANGE()
527{
528 clear();
529}
530
531inline Range::Range(const Range& r)
532 : RANGE(r)
533{
534 assign(r);
535}
536
537inline Range::Range(const RANGE& r)
538 : RANGE()
539{
540 set(r);
541}
542
544 : RANGE()
545{
546 assign(start, stop, id);
547}
548
550{
551 return _start;
552}
553
555{
556 return _stop;
557}
558
560{
561 return _id;
562}
563
565{
566 _id = id;
567 return *this;
568}
569
570inline void Range::clear()
571{
572 _start = 0;
573 _stop = 0;
574 _id = 0;
575}
576
577inline Range& Range::assign(const Range& r)
578{
579 *(RANGE*) this = *(RANGE*) &r;
580 return *this;
581}
582
584{
585 return _stop - _start;
586}
587
588inline Range::size_type Range::split(size_type seg_sz, Vector& rl_dst) const
589{
590 return split(seg_sz, *this, rl_dst);
591}
592
593inline bool Range::isEmpty() const
594{
595 return (_stop == _start);
596}
597
598inline Range& Range::operator=(const Range& r)
599{
600 assign(r);
601 return *this;
602}
603
604inline Range& Range::operator=(const RANGE& r)
605{
606 set(r);
607 return *this;
608}
609
610inline const RANGE& Range::getBase() const
611{
612 return *this;
613}
614
615inline Range::operator bool() const
616{
617 return isEmpty();
618}
619
620inline bool Range::operator==(const Range& r) const
621{
622 return compare(r) == cmpSame && (!RangeCompareExact || _id == r._id);
623}
624
625inline bool Range::operator!=(const Range& r) const
626{
627 return !(compare(r) == cmpSame && (!RangeCompareExact || _id == r._id));
628}
629
630inline bool Range::operator<(const Range& r) const
631{
632 return !isEmpty() && (r.isEmpty() || ((_start == r._start) ? _stop < r._stop : _start < r._start));
633}
634
636{
637 return assign(*this + r);
638}
639
641{
642 return assign(*this & r);
643}
644
646{
647 _start += ofs;
648 _stop += ofs;
649 return *this;
650}
651
653{
654 return Range(*this).offsetBy(ofs);
655}
656
657inline bool Range::isInRange(size_type idx) const
658{
659 return idx >= _start && idx < _stop;
660}
661
662inline const Range& Range::copyTo(RANGE& dst) const
663{
664 dst = *this;
665 return *this;
666}
667
668}// namespace sf
Type to contain and manipulate range lists.
Definition Range.h:67
Vector sort() const
Sorts the vector according the Range operator '<'.
Vector & rearrange()
Rearranges vector of ranges which means the vector is sorted and ranges are merged if possible.
Vector extract(const Vector &rl, Vector &rl_ex) const
Extracts the covered ranges in the vector using the 'rl' vector and puts them in the 'rl_ex' vector.
Vector & sort()
Sorts the vector according the Range operator '<'.
Vector & merge(const Vector &rl_add)
Adds the passed list to this vector and rearranges it.
Class to manage 64-bit integer ranges.
Definition Range.h:52
ECompare compare(const Range &other) const
Base function for comparing ranges.
bool operator>=(const Range &r)=delete
Not allowed operator.
RANGE::size_type size_type
Integer type used for start and stop.
Definition Range.h:57
Range & set(const RANGE &r)
InitializeBase instance with other instance.
const Range & copyTo(RANGE &dst) const
Definition Range.h:662
bool isOverlapped(const Range &r) const
Returns if the passed range have some overlap.
void clear()
Clears the range to an empty state.
Definition Range.h:570
size_type getStop() const
Const function to access the stop of the range.
Definition Range.h:554
Range operator+(const Range &r) const
Returns a new range which is a super Set of the two ranges.
static size_type split(size_type seg_sz, const Range &req, Vector &rl_dst)
Splits a range into segments bounded ranges according to the passed segment size.
bool operator>(const Range &r)=delete
Not allowed operator.
friend std::istream & operator>>(std::istream &is, Range &r)
Input stream operator for a range.
Range & assign(size_type start, size_type stop, id_type id=0)
Assigns the data members.
Range & setId(id_type id)
Set the owner id of this range.
Definition Range.h:564
bool operator>=(const Range &r) const =delete
Not allowed operator.
const RANGE & getBase() const
Gets the underlying base structure.
Definition Range.h:610
Range & operator=(Range &&)=default
Move assignment operator is default.
id_type getId() const
Returns the owner id of this range.
Definition Range.h:559
RANGE::id_type id_type
Integer type used for the ID.
Definition Range.h:61
void normalize()
Swaps start and stop if the order is wrong.
bool operator==(const Range &r) const
Tests if the range start and stop members are the same.
Definition Range.h:620
Range operator&(const Range &r) const
And operator which is a subset of both range where elements of the one also exist in the other range.
bool operator<=(const Range &r) const =delete
Not allowed operator.
bool operator>(const Range &r) const =delete
Not allowed operator.
size_type split(size_type seg_sz, Vector &rl_dst) const
Splits this range into segments bounded ranges according to the passed segment size.
Definition Range.h:588
size_type getSize() const
Returns the size of the range minimum is 1.
Definition Range.h:583
static size_type split(size_type seg_sz, const Vector &req, Vector &rl)
Same as split but now for a complete vector of ranges.
Vector::iter_type Iterator
Iteration type for lists of ranges.
Definition Range.h:119
bool operator<=(const Range &r)=delete
Not allowed operator.
bool operator<(const Range &r) const
Operator used for sorting. Depends only on the start position first and then the stop position....
Definition Range.h:630
bool isInRange(size_type idx) const
Check if idx is within this range.
Definition Range.h:657
friend std::ostream & operator<<(std::ostream &os, const Range &r)
Output stream operator for a range.
bool isWithinOther(const Range &r) const
Returns true if the passed range is part of this range.
ECompare
All possible comparison results.
Definition Range.h:282
@ cmpSame
Definition Range.h:294
Range()
Default constructor.
Definition Range.h:525
Range & operator+=(const Range &r)
Returns a new range which is a super Set of the two ranges.
Definition Range.h:635
bool isWithinSelf(const Range &r) const
Returns true if this range is part of the passed range.
bool isEmpty() const
Return true if the range is empty.
Definition Range.h:593
size_type getStart() const
Const function to access the start of the range.
Definition Range.h:549
Range offset(size_type ofs) const
Shifts the range using the passed offset.
Definition Range.h:652
int exclude(const Range &r, Range &rest, Range::ECompare *cmp=nullptr)
Exclude the range in the other which could result in an single range (0), an additional second range ...
bool operator!=(const Range &r) const
Tests if the range start and stop members are not the same.
Definition Range.h:625
Range & offsetBy(size_type ofs)
Shifts this range using the passed offset.
Definition Range.h:645
Range & operator&=(const Range &r)
And operator which is a subset of both range where elements of the one also exist in the other range.
Definition Range.h:640
bool isExtension(const Range &r) const
Returns if the passed range an extension of this one.
bool isMergeable(const Range &r) const
Returns true if the passed range can be combined to this range without having to bridge a gap.
Counted vector having function names compatible with Borland C++ templates.
Definition TVector.h:398
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:25
#define _MISC_FUNC
Definition misc/global.h:39
#define _MISC_CLASS
Definition misc/global.h:40
#define _MISC_DATA
Definition misc/global.h:38
Definition Application.h:10
_GII_FUNC std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
_MISC_DATA bool RangeCompareExact
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
_MISC_FUNC bool operator==(const Md5Hash::hash_type &h1, const Md5Hash::hash_type &h2)
Compare operator for storing an MD5 hash.
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:66
Definition Range.h:23
uint64_t size_type
Integer type used for start and stop.
Definition Range.h:27
id_type _id
Identifier of this range.
Definition Range.h:43
int64_t id_type
Integer type used for the ID.
Definition Range.h:31
size_type _start
Start position of the range or interval.
Definition Range.h:35
size_type _stop
Stop position of the range or interval.
Definition Range.h:39