Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Range1D.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) 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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // Range1D class used for representing a range of positive integers.
43 // Its primary usage is in accessing vectors and matrices by subregions
44 // of rows and columns
45 //
46 
47 #ifndef TEUCHOS_RANGE1D_HPP
48 #define TEUCHOS_RANGE1D_HPP
49 
54 #include "Teuchos_ScalarTraits.hpp"
55 #include "Teuchos_Assert.hpp"
56 
57 namespace Teuchos {
58 
87 class Range1D {
88 public:
89 
92 
95 
98 
100  static const Range1D Invalid;
101 
111  inline Range1D();
112 
122  inline Range1D( EInvalidRange );
123 
139 
143  inline bool full_range() const;
144 
146  inline Ordinal lbound() const;
147 
149  inline Ordinal ubound() const;
150 
152  inline Ordinal size() const;
153 
155  inline bool in_range(Ordinal i) const;
156 
158  inline Range1D& operator+=( Ordinal incr );
159 
161  inline Range1D& operator-=( Ordinal incr );
162 
163 private:
165  Ordinal ubound_; // = INT_MAX-1 flag for entire range
166  // lbound == ubound == 0 flag for invalid range.
167 
168  // assert that the range is valid
169  inline void assert_valid_range(Ordinal lbound, Ordinal ubound) const;
170 
171 }; // end class Range1D
172 
179 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
180 {
181  return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
182 }
183 
195 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
196 {
197  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
198 }
199 
211 inline Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
212 {
213  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
214 }
215 
227 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
228 {
229  return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
230 }
231 
247 inline Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
248 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
249 
250 // //////////////////////////////////////////////////////////
251 // Inline members
252 
253 inline
255  : lbound_(0), ubound_(INT_MAX-1)
256 {}
257 
258 inline
260  : lbound_(0), ubound_(-1)
261 {}
262 
263 
264 inline
265 Range1D::Range1D(Ordinal lbound_in, Ordinal ubound_in)
266  : lbound_(lbound_in), ubound_(ubound_in)
267 {
268  assert_valid_range(lbound_in,ubound_in);
269 }
270 
271 inline
272 bool Range1D::full_range() const {
273  return ubound_ == INT_MAX-1;
274 }
275 
276 inline
278  return lbound_;
279 }
280 
281 inline
283  return ubound_;
284 }
285 
286 inline
288  return ubound_ - lbound_ + 1;
289 }
290 
291 inline
292 bool Range1D::in_range(Ordinal i) const {
293  return lbound_ <= i && i <= ubound_;
294 }
295 
296 inline
298  assert_valid_range( lbound_ + incr, ubound_ + incr );
299  lbound_ += incr;
300  ubound_ += incr;
301  return *this;
302 }
303 
304 inline
306 {
307  assert_valid_range( lbound_ - incr, ubound_ - incr );
308  lbound_ -= incr;
309  ubound_ -= incr;
310  return *this;
311 }
312 
313 
314 // See Range1D.cpp
315 inline
316 void Range1D::assert_valid_range(Ordinal lbound_in, Ordinal ubound_in) const
317 {
318  (void)lbound_in; (void)ubound_in;
319 #ifdef TEUCHOS_DEBUG
321  lbound_in < 0, std::range_error
322  ,"Range1D::assert_valid_range(): Error, lbound ="
323  <<lbound_in<<" must be greater than or equal to 0."
324  );
326  lbound_in > ubound_in, std::range_error
327  ,"Range1D::assert_valid_range(): Error, lbound = "
328  <<lbound_in<<" > ubound = "<<ubound_in
329  );
330 #endif
331 }
332 
333 } // end namespace Teuchos
334 
335 #endif // end TEUCHOS_RANGE1D_HPP
bool in_range(Ordinal i) const
Return true if the index is in range.
void assert_valid_range(Ordinal lbound, Ordinal ubound) const
Subregion Index Range Class.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Ordinal ubound() const
Return upper bound of the range.
Range1D()
Constructs a range representing the entire range.
bool full_range() const
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
rng_lhs = rng_rhs - i.
static const Range1D Invalid
Used for Range1D(INVALID)
Teuchos_Ordinal Ordinal
Deprecated.
Range1D & operator+=(Ordinal incr)
Increment the range by a constant.
Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
Return a bounded index range from a potentially unbounded index range.
Ordinal lbound() const
Return lower bound of the range.
Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
rng_lhs = rng_rhs + i.
bool operator==(const Range1D &rng1, const Range1D &rng2)
rng1 == rng2.
Ordinal size() const
Return the size of the range (ubound() - lbound() + 1)
Range1D & operator-=(Ordinal incr)
Deincrement the range by a constant.
Defines basic traits for the scalar field type.
Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
rng_lhs = i + rng_rhs.
Teuchos_Ordinal Index
Deprecated.