OpenWalnut  1.5.0dev
WInterval.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WINTERVAL_H
26 #define WINTERVAL_H
27 
28 #include <algorithm>
29 #include <memory>
30 #include <utility>
31 
32 
33 #include "../WTypeTraits.h"
34 
35 /**
36  * Basic class for encapsulating a std::pair to be interpreted as interval. This class intentionally does not include a parameter telling whether
37  * the interval is open or not (mathematically: [],][,[[,]])
38  *
39  * \tparam T the type used for this interval
40  */
41 template< typename T >
42 class WInterval
43 {
44 public:
45  /**
46  * Convenience typedef for a std::shared_ptr< WInterval >.
47  */
48  typedef std::shared_ptr< WInterval< T > > SPtr;
49 
50  /**
51  * Convenience typedef for a std::shared_ptr< const WInterval >.
52  */
53  typedef std::shared_ptr< const WInterval< T > > ConstSPtr;
54 
55  /**
56  * Type used to store the information
57  */
58  typedef std::pair< T, T > StoreType;
59 
60  /**
61  * My own type.
62  */
64 
65  /**
66  * Copy constructor to create a WInterval using a std::pair
67  *
68  * \param c the pair to use
69  */
70  explicit WInterval( const StoreType& c );
71 
72  /**
73  * Create a new interval instance using the given values.
74  *
75  * \param l the lower border
76  * \param u the upper border
77  */
78  WInterval( const T& l, const T& u );
79 
80  /**
81  * Destructor.
82  */
83  virtual ~WInterval();
84 
85  /**
86  * Convert the WInterval instance to a std::pair again.
87  *
88  * \return the pair
89  */
90  operator const StoreType& () const;
91 
92  /**
93  * Get the lower value of the interval.
94  *
95  * \return the lower value
96  */
97  const T& getLower() const;
98 
99  /**
100  * Return the upper value of the interval
101  *
102  * \return the upper value
103  */
104  const T& getUpper() const;
105 
106  /**
107  * The length of the interval. This is upper - lower.
108  *
109  * \return length
110  */
111  T getLength() const;
112 
113  /**
114  * Compare this interval with another one
115  *
116  * \param interval the other one
117  *
118  * \return true if lower and upper bounds are equal
119  */
120  bool operator==( Type interval ) const;
121 
122  /**
123  * Compare this interval with another one
124  *
125  * \param interval the other one
126  *
127  * \return true if lower and upper bounds are equal
128  */
129  bool operator!=( Type interval ) const;
130 
131 protected:
132 private:
133  /**
134  * The interval itself.
135  */
137 };
138 
139 /**
140  * Abbreviation for an double interval.
141  */
143 
144 /**
145  * Abbreviation for an integer interval
146  */
148 
149 /**
150  * Create an interval instance similar to make_pair.
151  *
152  * \tparam T1 the lower bound type
153  * \tparam T2 the upper bound type
154  * \param l lower bound
155  * \param u upper bound
156  *
157  * \return the interval
158  */
159 template < typename T1, typename T2 >
161 {
163 }
164 
165 /**
166  * Check whether a value is in the interval or not. This function interprets the interval as closed at both bounds.
167  *
168  * \tparam IntervalType type if the interval
169  * \tparam T type of the value to use for checking
170  * \param interval the interval to check against
171  * \param value the value
172  *
173  * \return true if inside
174  */
175 template < typename IntervalType, typename T >
176 bool isInClosed( const IntervalType& interval, const T& value )
177 {
178  return ( ( interval.getLower() <= value ) && ( interval.getUpper() >= value ) );
179 }
180 
181 /**
182  * Check whether a value is in the interval or not. This function interprets the interval as open at both bounds.
183  *
184  * \tparam IntervalType type if the interval
185  * \tparam T type of the value to use for checking
186  * \param interval the interval to check against
187  * \param value the value
188  *
189  * \return true if inside
190  */
191 template < typename IntervalType, typename T >
192 bool isInOpen( const IntervalType& interval, const T& value )
193 {
194  return ( ( interval.getLower() < value ) && ( interval.getUpper() > value ) );
195 }
196 
197 /**
198  * Check whether a value is in the interval or not. This function interprets the interval as open at the lower bound and closed at the upper one.
199  *
200  * \tparam IntervalType type if the interval
201  * \tparam T type of the value to use for checking
202  * \param interval the interval to check against
203  * \param value the value
204  *
205  * \return true if inside
206  */
207 template < typename IntervalType, typename T >
208 bool isInOpenClosed( const IntervalType& interval, const T& value )
209 {
210  return ( ( interval.getLower() < value ) && ( interval.getUpper() >= value ) );
211 }
212 
213 /**
214  * Check whether a value is in the interval or not. This function interprets the interval as closed at the lower bound and open at the upper one.
215  *
216  * \tparam IntervalType type if the interval
217  * \tparam T type of the value to use for checking
218  * \param interval the interval to check against
219  * \param value the value
220  *
221  * \return true if inside
222  */
223 template < typename IntervalType, typename T >
224 bool isInClosedOpen( const IntervalType& interval, const T& value )
225 {
226  return ( ( interval.getLower() <= value ) && ( interval.getUpper() > value ) );
227 }
228 
229 template < typename T >
231 {
232  // ensure order
233  m_interval.first = std::min( c.first, c.second );
234  m_interval.second = std::min( c.first, c.second );
235 }
236 
237 template < typename T >
238 WInterval< T >::WInterval( const T& l, const T& u ):
239  m_interval( std::min( l, u ), std::max( l, u ) )
240 {
241  // nothing else to do
242 }
243 
244 template < typename T >
246 {
247  // nothing else to do
248 }
249 
250 template < typename T >
252 {
253  return m_interval;
254 }
255 
256 template < typename T >
257 const T& WInterval< T >::getLower() const
258 {
259  return m_interval.first;
260 }
261 
262 template < typename T >
263 const T& WInterval< T >::getUpper() const
264 {
265  return m_interval.second;
266 }
267 
268 template < typename T >
270 {
271  return getUpper() - getLower();
272 }
273 
274 template < typename T >
275 bool WInterval< T >::operator==( Type interval ) const
276 {
277  return ( ( interval.getLower() == getLower() ) && ( interval.getUpper() == getUpper() ) );
278 }
279 
280 template < typename T >
281 bool WInterval< T >::operator!=( Type interval ) const
282 {
283  return !operator==( interval );
284 }
285 
286 #endif // WINTERVAL_H
287 
Basic class for encapsulating a std::pair to be interpreted as interval.
Definition: WInterval.h:43
std::shared_ptr< const WInterval< T > > ConstSPtr
Convenience typedef for a std::shared_ptr< const WInterval >.
Definition: WInterval.h:53
WInterval(const StoreType &c)
Copy constructor to create a WInterval using a std::pair.
Definition: WInterval.h:230
T getLength() const
The length of the interval.
Definition: WInterval.h:269
bool operator==(Type interval) const
Compare this interval with another one.
Definition: WInterval.h:275
WInterval(const T &l, const T &u)
Create a new interval instance using the given values.
Definition: WInterval.h:238
const T & getUpper() const
Return the upper value of the interval.
Definition: WInterval.h:263
bool operator!=(Type interval) const
Compare this interval with another one.
Definition: WInterval.h:281
const T & getLower() const
Get the lower value of the interval.
Definition: WInterval.h:257
virtual ~WInterval()
Destructor.
Definition: WInterval.h:245
StoreType m_interval
The interval itself.
Definition: WInterval.h:136
std::shared_ptr< WInterval< T > > SPtr
Convenience typedef for a std::shared_ptr< WInterval >.
Definition: WInterval.h:48
WInterval< T > Type
My own type.
Definition: WInterval.h:63
std::pair< T, T > StoreType
Type used to store the information.
Definition: WInterval.h:58