SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
range.hpp
1// This file is part of slideio project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://slideio.com/license.html.
4#pragma once
5#include <cstdint>
6#include <ostream>
7#include <algorithm>
8#include <climits>
9
10namespace cv { class Range; }
11
12namespace slideio
13{
14 class Range
15 {
16 public:
17 Range() : start(0), end(0) {}
18 Range(int32_t _start, int32_t _end) : start(_start), end(_end) {}
19 Range(const cv::Range& r);
20 Range(const Range& r) = default;
21 Range(Range&& r) noexcept {
22 start = r.start;
23 end = r.end;
24 r.start = 0;
25 r.end = 0;
26 }
27 ~Range() = default;
28 Range& operator = (const Range& r) = default;
29 Range& operator = (const cv::Range& r);
30 Range& operator = (Range&& r) noexcept {
31 if (this != &r) {
32 start = r.start;
33 end = r.end;
34 r.start = 0;
35 r.end = 0;
36 }
37 return *this;
38 }
39
40 operator cv::Range() const;
41
42 int32_t size() const {
43 return end - start;
44 }
45
46 bool empty() const {
47 return start == end;
48 }
49
50 static Range all() {
51 return Range(INT_MIN, INT_MAX);
52 }
53
54 bool operator == (const Range& other) const {
55 return start == other.start && end == other.end;
56 }
57
58 bool operator != (const Range& other) const {
59 return !(*this == other);
60 }
61
62 friend Range operator & (const Range& r1, const Range& r2) {
63 Range r(std::max(r1.start, r2.start), std::min(r1.end, r2.end));
64 r.end = std::max(r.end, r.start);
65 return r;
66 }
67
68 Range& operator &= (const Range& r) {
69 *this = *this & r;
70 return *this;
71 }
72
73 friend Range operator + (const Range& r, int32_t delta) {
74 return Range(r.start + delta, r.end + delta);
75 }
76
77 friend Range operator + (int32_t delta, const Range& r) {
78 return Range(r.start + delta, r.end + delta);
79 }
80
81 friend Range operator - (const Range& r, int32_t delta) {
82 return r + (-delta);
83 }
84
85 friend std::ostream& operator<<(std::ostream& os, const Range& range) {
86 os << "Range (start: " << range.start << ", end: " << range.end << ")";
87 return os;
88 }
89
90 int32_t start;
91 int32_t end;
92 };
93}
94
95#if defined(SLIDEIO_INTERNAL_HEADER)
96#include "range.inl"
97#endif
Definition: exceptions.hpp:15