Teuchos - Trilinos Tools Package  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 
58 namespace Teuchos {
59 
60 
88 class Range1D {
89 public:
90 
92  typedef Teuchos_Ordinal Index;
93 
95  typedef Teuchos_Ordinal Ordinal;
96 
98  enum EInvalidRange { INVALID };
99 
101  static const Range1D Invalid;
102 
112  inline Range1D();
113 
123  inline Range1D(EInvalidRange);
124 
144 
146  inline bool full_range() const;
147 
149  inline Ordinal lbound() const;
150 
152  inline Ordinal ubound() const;
153 
155  inline Ordinal size() const;
156 
158  inline bool in_range(Ordinal i) const;
159 
164  inline Range1D& operator+=( Ordinal incr );
165 
170  inline Range1D& operator-=( Ordinal incr );
171 
172 private:
173 
174  Ordinal lbound_;
175  Ordinal ubound_;
176 
177  inline void assert_valid_range(Ordinal lbound, Ordinal ubound) const;
178 
179 }; // end class Range1D
180 
181 
188 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
189 {
190  return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
191 }
192 
193 
200 inline bool operator!=(const Range1D& rng1, const Range1D& rng2 )
201 {
202  return !(rng1 == rng2);
203 }
204 
205 
217 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
218 {
219  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
220 }
221 
222 
234 inline Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
235 {
236  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
237 }
238 
239 
251 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
252 {
253  return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
254 }
255 
256 
272 inline Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
273 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
274 
275 
280 TEUCHOSCORE_LIB_DLL_EXPORT
281 std::ostream& operator<<(std::ostream &out, const Range1D& rng);
282 
283 
284 // //////////////////////////////////////////////////////////
285 // Inline members
286 
287 inline
289  : lbound_(0), ubound_(std::numeric_limits<Ordinal>::max()-1)
290 {}
291 
292 inline
294  : lbound_(0), ubound_(-2)
295 {}
296 
297 
298 inline
299 Range1D::Range1D(Ordinal lbound_in, Ordinal ubound_in)
300  : lbound_(lbound_in), ubound_(ubound_in)
301 {
302  assert_valid_range(lbound_in,ubound_in);
303 }
304 
305 inline
306 bool Range1D::full_range() const {
307  return (lbound_ == 0 && ubound_ == std::numeric_limits<Ordinal>::max()-1);
308 }
309 
310 inline
312  return lbound_;
313 }
314 
315 inline
317  return ubound_;
318 }
319 
320 inline
322  return ubound_ - lbound_ + 1;
323 }
324 
325 inline
326 bool Range1D::in_range(Ordinal i) const {
327  return lbound_ <= i && i <= ubound_;
328 }
329 
330 inline
332  assert_valid_range( lbound_ + incr, ubound_ + incr );
333  lbound_ += incr;
334  ubound_ += incr;
335  return *this;
336 }
337 
338 inline
340 {
341  assert_valid_range( lbound_ - incr, ubound_ - incr );
342  lbound_ -= incr;
343  ubound_ -= incr;
344  return *this;
345 }
346 
347 
348 // See Range1D.cpp
349 inline
350 void Range1D::assert_valid_range(Ordinal lbound_in, Ordinal ubound_in) const
351 {
352  (void)lbound_in; (void)ubound_in;
353 #ifdef TEUCHOS_DEBUG
354  TEUCHOS_ASSERT_INEQUALITY(lbound_in, >=, 0);
355  TEUCHOS_ASSERT_INEQUALITY(ubound_in, >=, lbound_in - 1);
356 #endif
357 }
358 
359 } // end namespace Teuchos
360 
361 #endif // end TEUCHOS_RANGE1D_HPP
bool in_range(Ordinal i) const
Return true if the index is in range.
Subregion Index Range Class.
#define TEUCHOS_ASSERT_INEQUALITY(val1, comp, val2)
This macro is checks that an inequality between two numbers is satisified and if not then throws a go...
bool operator!=(const Range1D &rng1, const Range1D &rng2)
rng1 == rng2.
Ordinal ubound() const
Return upper bound of the range.
Range1D()
Construct a full range.
bool full_range() const
Returns true if the range represents the entire region.
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.