MoochoPack: Miscellaneous Utilities for MOOCHO  Version of the Day
RangePack_Range1D.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef RANGE1D_H
43 #define RANGE1D_H
44 
45 #include "RTOpPack_Types.hpp"
46 #include "Teuchos_Range1D.hpp"
47 #include "Teuchos_ScalarTraits.hpp"
48 
49 namespace RangePack {
50 
76 class Range1D {
77 public:
79  typedef RTOpPack::Ordinal Index;
81  enum EInvalidRange { INVALID };
83  static const Range1D Invalid;
94  inline Range1D();
104  inline Range1D( EInvalidRange );
119  inline Range1D(Index lbound, Index ubound);
121  inline bool full_range() const;
123  inline Index lbound() const;
125  inline Index ubound() const;
127  inline Index size() const;
129  inline bool in_range(Index i) const;
131  inline Range1D& operator+=( Index incr );
133  inline Range1D& operator-=( Index incr );
134 
135 private:
136  Index lbound_;
137  Index ubound_; // = INT_MAX flag for entire range
138  // lbound == ubound == 0 flag for invalid range.
139 
140  // assert that the range is valid
141  inline void assert_valid_range(Index lbound, Index ubound) const;
142 
143 }; // end class Range1D
144 
151 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
152 {
153  return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
154 }
155 
167 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
168 {
169  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
170 }
171 
183 inline Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
184 {
185  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
186 }
187 
199 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
200 {
201  return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
202 }
203 
219 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
220 
225 inline Range1D convert( const Teuchos::Range1D &rng )
226 { return Range1D(rng.lbound()+1,rng.ubound()+1); }
227 
232 inline Teuchos::Range1D convert( const Range1D &rng )
233 {
234  return Teuchos::Range1D(rng.lbound()-1,rng.ubound()-1);
235 }
236 
238 
239 // //////////////////////////////////////////////////////////
240 // Inline members
241 
242 inline
244  : lbound_(1), ubound_(INT_MAX) // RAB: 2004/08/24: Replace this with std::numeric_traits<Index>::max() when supported everywhere!
245 {}
246 
247 inline
249  : lbound_(1), ubound_(0)
250 {}
251 
252 
253 inline
255  : lbound_(lbound), ubound_(ubound)
256 {
257  assert_valid_range(lbound,ubound);
258 }
259 
260 inline
261 bool Range1D::full_range() const {
262  return ubound_ == INT_MAX;
263 }
264 
265 inline
267  return lbound_;
268 }
269 
270 inline
272  return ubound_;
273 }
274 
275 inline
277  return 1 + ubound_ - lbound_;
278 }
279 
280 inline
281 bool Range1D::in_range(Index i) const {
282  return lbound_ <= i && i <= ubound_;
283 }
284 
285 inline
287  assert_valid_range( lbound_ + incr, ubound_ + incr );
288  lbound_ += incr;
289  ubound_ += incr;
290  return *this;
291 }
292 
293 inline
295  assert_valid_range( lbound_ - incr, ubound_ - incr );
296  lbound_ -= incr;
297  ubound_ -= incr;
298  return *this;
299 }
300 
301 // See Range1D.cpp
302 inline
303 void Range1D::assert_valid_range(Index lbound, Index ubound) const {
304 #ifdef TEUCHOS_DEBUG
305  TEUCHOS_TEST_FOR_EXCEPTION(
306  lbound < 1, std::range_error
307  ,"Range1D::assert_valid_range(): Error, lbound ="<<lbound<<" must be greater than 0." );
308  TEUCHOS_TEST_FOR_EXCEPTION(
309  lbound > ubound, std::range_error
310  ,"Range1D::assert_valid_range(): Error, lbound = "<<lbound<<" > ubound = "<<ubound );
311 #endif
312 }
313 
314 } // end namespace RangePack
315 
316 #endif // end RANGE1D_H
bool operator==(const Range1D &rng1, const Range1D &rng2)
rng1 == rng2.
Index size() const
Return the size of the range (ubound() - lbound() + 1)
Index ubound() const
Return upper bound of the range.
RTOpPack::Ordinal Index
Range1D & operator+=(Index incr)
Increment the range by a constant.
Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
rng_lhs = rng_rhs - i.
. One-based subregion index range class.
Range1D()
Constructs a range representing the entire range.
Teuchos::Range1D convert(const Range1D &rng)
Convert from a 1-based RangePack::Range1D object to a 0-based Teuchos::Range1D object.
Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
rng_lhs = i + rng_rhs.
bool full_range() const
Returns true if the range represents the entire region (constructed from Range1D()) ...
Range1D full_range(const Range1D &rng, Range1D::Index lbound, Range1D::Index ubound)
Return a bounded index range from a potentially unbounded index range.
bool in_range(Index i) const
Return true if the index is in range.
static const Range1D Invalid
Range1D(INVALID)
Index lbound() const
Return lower bound of the range.
Range1D convert(const Teuchos::Range1D &rng)
Convert from a 0-based Teuchos::Range1D object to a 1-based RangePack::Range1D object.
Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
rng_lhs = rng_rhs + i.
Range1D & operator-=(Index incr)
Deincrement the range by a constant.