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