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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // Range1D class used for representing a range of positive integers.
11 // Its primary usage is in accessing vectors and matrices by subregions
12 // of rows and columns
13 //
14 
15 #ifndef TEUCHOS_RANGE1D_HPP
16 #define TEUCHOS_RANGE1D_HPP
17 
22 #include "Teuchos_ScalarTraits.hpp"
23 #include "Teuchos_Assert.hpp"
24 
25 
26 namespace Teuchos {
27 
28 
56 class Range1D {
57 public:
58 
60  typedef Teuchos_Ordinal Index;
61 
63  typedef Teuchos_Ordinal Ordinal;
64 
66  enum EInvalidRange { INVALID };
67 
69  static const Range1D Invalid;
70 
80  inline Range1D();
81 
91  inline Range1D(EInvalidRange);
92 
112 
114  inline bool full_range() const;
115 
117  inline Ordinal lbound() const;
118 
120  inline Ordinal ubound() const;
121 
123  inline Ordinal size() const;
124 
126  inline bool in_range(Ordinal i) const;
127 
132  inline Range1D& operator+=( Ordinal incr );
133 
138  inline Range1D& operator-=( Ordinal incr );
139 
140 private:
141 
142  Ordinal lbound_;
143  Ordinal ubound_;
144 
145  inline void assert_valid_range(Ordinal lbound, Ordinal ubound) const;
146 
147 }; // end class Range1D
148 
149 
156 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
157 {
158  return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
159 }
160 
161 
168 inline bool operator!=(const Range1D& rng1, const Range1D& rng2 )
169 {
170  return !(rng1 == rng2);
171 }
172 
173 
185 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
186 {
187  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
188 }
189 
190 
202 inline Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
203 {
204  return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
205 }
206 
207 
219 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
220 {
221  return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
222 }
223 
224 
240 inline Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
241 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
242 
243 
248 TEUCHOSCORE_LIB_DLL_EXPORT
249 std::ostream& operator<<(std::ostream &out, const Range1D& rng);
250 
251 
252 // //////////////////////////////////////////////////////////
253 // Inline members
254 
255 inline
257  : lbound_(0), ubound_(std::numeric_limits<Ordinal>::max()-1)
258 {}
259 
260 inline
262  : lbound_(0), ubound_(-2)
263 {}
264 
265 
266 inline
267 Range1D::Range1D(Ordinal lbound_in, Ordinal ubound_in)
268  : lbound_(lbound_in), ubound_(ubound_in)
269 {
270  assert_valid_range(lbound_in,ubound_in);
271 }
272 
273 inline
274 bool Range1D::full_range() const {
275  return (lbound_ == 0 && ubound_ == std::numeric_limits<Ordinal>::max()-1);
276 }
277 
278 inline
280  return lbound_;
281 }
282 
283 inline
285  return ubound_;
286 }
287 
288 inline
290  return ubound_ - lbound_ + 1;
291 }
292 
293 inline
294 bool Range1D::in_range(Ordinal i) const {
295  return lbound_ <= i && i <= ubound_;
296 }
297 
298 inline
300  assert_valid_range( lbound_ + incr, ubound_ + incr );
301  lbound_ += incr;
302  ubound_ += incr;
303  return *this;
304 }
305 
306 inline
308 {
309  assert_valid_range( lbound_ - incr, ubound_ - incr );
310  lbound_ -= incr;
311  ubound_ -= incr;
312  return *this;
313 }
314 
315 
316 // See Range1D.cpp
317 inline
318 void Range1D::assert_valid_range(Ordinal lbound_in, Ordinal ubound_in) const
319 {
320  (void)lbound_in; (void)ubound_in;
321 #ifdef TEUCHOS_DEBUG
322  TEUCHOS_ASSERT_INEQUALITY(lbound_in, >=, 0);
323  TEUCHOS_ASSERT_INEQUALITY(ubound_in, >=, lbound_in - 1);
324 #endif
325 }
326 
327 } // end namespace Teuchos
328 
329 #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.