Loading...
Searching...
No Matches
Range.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * Rectangle.h
33 *
34 * Created on: Sep 9, 2010
35 * Author: mpeura
36 */
37
38#ifndef DRAIN_RANGE_H_
39#define DRAIN_RANGE_H_
40
41//#include <ostream>
42//
43//#include <utility>
44
45#include <drain/UniTuple.h>
46
47namespace drain {
48
49
50
51template <class T>
52class Range : public UniTuple<T,2> {
53public:
54
55 T & min;
56 T & max;
57
59 Range(T min=T(), T max=T()) : min(this->next()), max(this->next()) {
60 this->set(min, max);
61 };
62
64 Range(const Range<T> & r) : min(this->next()), max(this->next()){
65 this->set(r.tuple());
66 };
67
69 template <class T2>
70 Range(const Range<T2> & r) : min(this->next()), max(this->next()){
71 this->set(r.tuple());
72 };
73
74 // Reference (parasite)
75 template <size_t N>
76 Range(drain::UniTuple<T,N> & tuple, size_t i) :
77 drain::UniTuple<T,2>(tuple, i), // start in new pos!
78 min(this->next()),max(this->next()){
79 };
80
81
83
84 Range & operator=(const Range<T> & range){
85 this->set(range); // 2024/09/!!
86 return *this;
87 };
88
89
90
91 Range & operator=(const UniTuple<T,2> & range){
92 this->set(range); // 2024/09/!!
93 //this->set(range);
94 return *this;
95 };
96
97
98 Range<T> & operator=(T x){
99 this->set(x, x);
100 return *this;
101 };
102
103 inline
104 bool empty() const {
105 return (this->min == this->max);
106 }
107
108
109 inline
110 bool contains(T x) const {
111 return (this->min <= x) && (x <= this->max);
112 }
113
114 inline
115 bool limit(T x) const {
116 if (x < this->min)
117 return this->min;
118 else if (x > this->max)
119 return this->max;
120 else
121 return x;
122 }
123
124 // Returns (max - min) which may be negative if max
125 inline
126 T width() const {
127 return this->max - this->min;
128 }
129
130 // Returns abs(max - min).
131 inline
132 T span() const {
133 if (this->max > this->min)
134 return this->max - this->min;
135 else
136 return this->min - this->max;
137 }
138
139};
140
141/*
142template <class T>
143std::ostream & operator<<(std::ostream & ostr, const Range<T> & r){
144 ostr << r.min << ':' << r.max;
145 return ostr;
146}
147*/
148
149DRAIN_TYPENAME_T(Range,T);
150
151} // namespace drain
152
153
154
155#endif /* RECTANGLE_H_ */
156
Definition Range.h:52
Range(T min=T(), T max=T())
Default constructor.
Definition Range.h:59
Range(const Range< T > &r)
Copy constructor.
Definition Range.h:64
Range & operator=(const Range< T > &range)
Default assignment operator.
Definition Range.h:84
Range(const Range< T2 > &r)
Copy constructor variants.
Definition Range.h:70
Tuple of N elements of type T.
Definition UniTuple.h:65
Definition DataSelector.cpp:1277