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