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 // NEW
140 inline
141 void insert(T x) {
142 if (this->min <= this->max){
143 if (x < this->min){
144 this->min = x;
145 }
146 else if (x > this->max){
147 this->max = x;
148 }
149 }
150 else {
151 // "start collecting"
152 this->min = this->max = x;
153 }
154 }
155
156};
157
158/*
159template <class T>
160std::ostream & operator<<(std::ostream & ostr, const Range<T> & r){
161 ostr << r.min << ':' << r.max;
162 return ostr;
163}
164*/
165
166DRAIN_TYPENAME_T(Range,T);
167
168
169template <typename T>
170class SteppedRange : public UniTuple<T,3> {
171
172public:
173
174 typedef Range<T> range_t;
175 T & step;
176 range_t range;
177
178 SteppedRange(T step=1, T min=0, T max=0) : UniTuple<T,3>(step, min, max),
179 step(this->next()), range(this->tuple(),1) {
180 }
181
182 SteppedRange(const SteppedRange & range) : UniTuple<T,3>(range),
183 step(this->next()), range(this->tuple(),1) {
184 }
185
186 inline
187 SteppedRange & operator=(const SteppedRange & range){
188 this->set(range.tuple());
189 return *this;
190 }
191
192
193};
194
195DRAIN_TYPENAME_T(SteppedRange,T);
196
197} // namespace drain
198
199
200#endif /* RECTANGLE_H_ */
201
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
Definition Range.h:170
Tuple of N elements of type T.
Definition UniTuple.h:65
Definition DataSelector.cpp:1277