OpenWalnut  1.4.0
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 <utility>
29 #include <algorithm>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 
35 #include "../WTypeTraits.h"
36 
37 /**
38  * Basic class for encapsulating a std::pair to be interpreted as interval. This class intentionally does not include a parameter telling whether
39  * the interval is open or not (mathematically: [],][,[[,]])
40  *
41  * \tparam T the type used for this interval
42  */
43 template< typename T >
44 class WInterval
45 {
46 public:
47  /**
48  * Convenience typedef for a boost::shared_ptr< WInterval >.
49  */
50  typedef boost::shared_ptr< WInterval< T > > SPtr;
51 
52  /**
53  * Convenience typedef for a boost::shared_ptr< const WInterval >.
54  */
55  typedef boost::shared_ptr< const WInterval< T > > ConstSPtr;
56 
57  /**
58  * Type used to store the information
59  */
60  typedef std::pair< T, T > StoreType;
61 
62  /**
63  * My own type.
64  */
66 
67  /**
68  * Copy constructor to create a WInterval using a std::pair
69  *
70  * \param c the pair to use
71  */
72  explicit WInterval( const StoreType& c );
73 
74  /**
75  * Copy constructor.
76  *
77  * \param c the interval to copy
78  */
79  WInterval( const Type& c ); // NOLINT
80 
81  /**
82  * Create a new interval instance using the given values.
83  *
84  * \param l the lower border
85  * \param u the upper border
86  */
87  WInterval( const T& l, const T& u );
88 
89  /**
90  * Destructor.
91  */
92  virtual ~WInterval();
93 
94  /**
95  * Convert the WInterval instance to a std::pair again.
96  *
97  * \return the pair
98  */
99  operator const StoreType& () const;
100 
101  /**
102  * Get the lower value of the interval.
103  *
104  * \return the lower value
105  */
106  const T& getLower() const;
107 
108  /**
109  * Return the upper value of the interval
110  *
111  * \return the upper value
112  */
113  const T& getUpper() const;
114 
115  /**
116  * The length of the interval. This is upper - lower.
117  *
118  * \return length
119  */
120  T getLength() 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  /**
132  * Compare this interval with another one
133  *
134  * \param interval the other one
135  *
136  * \return true if lower and upper bounds are equal
137  */
138  bool operator!=( Type interval ) const;
139 
140 protected:
141 private:
142  /**
143  * The interval itself.
144  */
145  StoreType m_interval;
146 };
147 
148 /**
149  * Abbreviation for an double interval.
150  */
152 
153 /**
154  * Abbreviation for an integer interval
155  */
157 
158 /**
159  * Create an interval instance similar to make_pair.
160  *
161  * \tparam T1 the lower bound type
162  * \tparam T2 the upper bound type
163  * \param l lower bound
164  * \param u upper bound
165  *
166  * \return the interval
167  */
168 template < typename T1, typename T2 >
170 {
172 }
173 
174 /**
175  * Check whether a value is in the interval or not. This function interprets the interval as closed at both bounds.
176  *
177  * \tparam IntervalType type if the interval
178  * \tparam T type of the value to use for checking
179  * \param interval the interval to check against
180  * \param value the value
181  *
182  * \return true if inside
183  */
184 template < typename IntervalType, typename T >
185 bool isInClosed( const IntervalType& interval, const T& value )
186 {
187  return ( ( interval.getLower() <= value ) && ( interval.getUpper() >= value ) );
188 }
189 
190 /**
191  * Check whether a value is in the interval or not. This function interprets the interval as open at both bounds.
192  *
193  * \tparam IntervalType type if the interval
194  * \tparam T type of the value to use for checking
195  * \param interval the interval to check against
196  * \param value the value
197  *
198  * \return true if inside
199  */
200 template < typename IntervalType, typename T >
201 bool isInOpen( const IntervalType& interval, const T& value )
202 {
203  return ( ( interval.getLower() < value ) && ( interval.getUpper() > value ) );
204 }
205 
206 /**
207  * 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.
208  *
209  * \tparam IntervalType type if the interval
210  * \tparam T type of the value to use for checking
211  * \param interval the interval to check against
212  * \param value the value
213  *
214  * \return true if inside
215  */
216 template < typename IntervalType, typename T >
217 bool isInOpenClosed( const IntervalType& interval, const T& value )
218 {
219  return ( ( interval.getLower() < value ) && ( interval.getUpper() >= value ) );
220 }
221 
222 /**
223  * 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.
224  *
225  * \tparam IntervalType type if the interval
226  * \tparam T type of the value to use for checking
227  * \param interval the interval to check against
228  * \param value the value
229  *
230  * \return true if inside
231  */
232 template < typename IntervalType, typename T >
233 bool isInClosedOpen( const IntervalType& interval, const T& value )
234 {
235  return ( ( interval.getLower() <= value ) && ( interval.getUpper() > value ) );
236 }
237 
238 template < typename T >
240 {
241  // ensure order
242  m_interval.first = std::min( c.first, c.second );
243  m_interval.second = std::min( c.first, c.second );
244 }
245 
246 template < typename T >
248  m_interval( c.m_interval )
249 {
250  // nothing else to do
251 }
252 
253 template < typename T >
254 WInterval< T >::WInterval( const T& l, const T& u ):
255  m_interval( std::min( l, u ), std::max( l, u ) )
256 {
257  // nothing else to do
258 }
259 
260 template < typename T >
262 {
263  // nothing else to do
264 }
265 
266 template < typename T >
268 {
269  return m_interval;
270 }
271 
272 template < typename T >
273 const T& WInterval< T >::getLower() const
274 {
275  return m_interval.first;
276 }
277 
278 template < typename T >
279 const T& WInterval< T >::getUpper() const
280 {
281  return m_interval.second;
282 }
283 
284 template < typename T >
286 {
287  return getUpper() - getLower();
288 }
289 
290 template < typename T >
291 bool WInterval< T >::operator==( Type interval ) const
292 {
293  return ( ( interval.getLower() == getLower() ) && ( interval.getUpper() == getUpper() ) );
294 }
295 
296 template < typename T >
297 bool WInterval< T >::operator!=( Type interval ) const
298 {
299  return !operator==( interval );
300 }
301 
302 #endif // WINTERVAL_H
303 
const T & getLower() const
Get the lower value of the interval.
Definition: WInterval.h:273
STL namespace.
bool operator==(Type interval) const
Compare this interval with another one.
Definition: WInterval.h:291
Basic class for encapsulating a std::pair to be interpreted as interval.
Definition: WInterval.h:44
virtual ~WInterval()
Destructor.
Definition: WInterval.h:261
boost::shared_ptr< const WInterval< T > > ConstSPtr
Convenience typedef for a boost::shared_ptr< const WInterval >.
Definition: WInterval.h:55
bool operator!=(Type interval) const
Compare this interval with another one.
Definition: WInterval.h:297
T getLength() const
The length of the interval.
Definition: WInterval.h:285
WInterval(const StoreType &c)
Copy constructor to create a WInterval using a std::pair.
Definition: WInterval.h:239
StoreType m_interval
The interval itself.
Definition: WInterval.h:145
std::pair< T, T > StoreType
Type used to store the information.
Definition: WInterval.h:60
const T & getUpper() const
Return the upper value of the interval.
Definition: WInterval.h:279
WInterval< T > Type
My own type.
Definition: WInterval.h:65
boost::shared_ptr< WInterval< T > > SPtr
Convenience typedef for a boost::shared_ptr< WInterval >.
Definition: WInterval.h:50