Teuchos - Trilinos Tools Package Version of the Day
Teuchos_Range1D.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 // Range1D class used for representing a range of positive integers.
00030 // Its primary usage is in accessing vectors and matrices by subregions
00031 // of rows and columns
00032 //
00033 
00034 #ifndef TEUCHOS_RANGE1D_HPP
00035 #define TEUCHOS_RANGE1D_HPP
00036 
00041 #include "Teuchos_ScalarTraits.hpp"
00042 #include "Teuchos_TestForException.hpp"
00043 
00044 namespace Teuchos {
00045 
00074 class Range1D {
00075 public:
00076 
00078   typedef Teuchos_Ordinal  Index;
00079 
00081   typedef Teuchos_Ordinal  Ordinal;
00082 
00084   enum EInvalidRange { INVALID };
00085 
00087   static const Range1D Invalid;
00088 
00098   inline Range1D();
00099 
00109   inline Range1D( EInvalidRange );
00110 
00125   inline Range1D(Ordinal lbound, Ordinal ubound);
00126 
00130   inline bool full_range() const;
00131 
00133   inline Ordinal lbound() const;
00134 
00136   inline Ordinal ubound() const;
00137 
00139   inline Ordinal size() const;
00140 
00142   inline bool in_range(Ordinal i) const;
00143 
00145   inline Range1D& operator+=( Ordinal incr );
00146 
00148   inline Range1D& operator-=( Ordinal incr );
00149 
00150 private:
00151   Ordinal lbound_;
00152   Ordinal ubound_;  // = INT_MAX-1 flag for entire range
00153   // lbound == ubound == 0 flag for invalid range.
00154   
00155   // assert that the range is valid
00156   inline void assert_valid_range(Ordinal lbound, Ordinal ubound) const;
00157   
00158 }; // end class Range1D
00159   
00166 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00167 {
00168   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00169 }
00170 
00182 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
00183 {
00184     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00185 }
00186 
00198 inline Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
00199 {
00200     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00201 }
00202 
00214 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
00215 {
00216     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00217 }
00218 
00234 inline Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
00235 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00236 
00237 // //////////////////////////////////////////////////////////
00238 // Inline members
00239 
00240 inline
00241 Range1D::Range1D()
00242   : lbound_(0), ubound_(INT_MAX-1)
00243 {}
00244 
00245 inline
00246 Range1D::Range1D( EInvalidRange )
00247   : lbound_(0), ubound_(-1)
00248 {}
00249 
00250 
00251 inline
00252 Range1D::Range1D(Ordinal lbound_in, Ordinal ubound_in)
00253   : lbound_(lbound_in), ubound_(ubound_in)
00254 {
00255   assert_valid_range(lbound_in,ubound_in);
00256 }
00257 
00258 inline
00259 bool Range1D::full_range() const {
00260   return ubound_ == INT_MAX-1;
00261 }
00262 
00263 inline
00264 Range1D::Ordinal Range1D::lbound() const {
00265   return lbound_;
00266 }
00267 
00268 inline
00269 Range1D::Ordinal Range1D::ubound() const {
00270   return ubound_;
00271 }
00272 
00273 inline
00274 Range1D::Ordinal Range1D::size() const {
00275   return ubound_ - lbound_ + 1;
00276 }
00277 
00278 inline
00279 bool Range1D::in_range(Ordinal i) const {
00280   return lbound_ <= i && i <= ubound_;
00281 }
00282 
00283 inline
00284 Range1D& Range1D::operator+=( Ordinal incr ) {
00285   assert_valid_range( lbound_ + incr, ubound_ + incr );
00286   lbound_ += incr;
00287   ubound_ += incr;
00288   return *this;
00289 }
00290 
00291 inline
00292 Range1D& Range1D::operator-=( Ordinal incr )
00293 {
00294   assert_valid_range( lbound_ - incr, ubound_ - incr );
00295   lbound_ -= incr;
00296   ubound_ -= incr;
00297   return *this;
00298 }
00299 
00300 
00301 // See Range1D.cpp
00302 inline
00303 void Range1D::assert_valid_range(Ordinal lbound_in, Ordinal ubound_in) const
00304 {
00305   (void)lbound_in; (void)ubound_in;
00306 #ifdef TEUCHOS_DEBUG
00307   TEST_FOR_EXCEPTION(
00308     lbound_in < 0, std::range_error
00309     ,"Range1D::assert_valid_range(): Error, lbound ="
00310     <<lbound_in<<" must be greater than or equal to 0."
00311     );
00312   TEST_FOR_EXCEPTION(
00313     lbound_in > ubound_in, std::range_error
00314     ,"Range1D::assert_valid_range(): Error, lbound = "
00315     <<lbound_in<<" > ubound = "<<ubound_in
00316     );
00317 #endif
00318 }
00319 
00320 } // end namespace Teuchos
00321 
00322 #endif // end TEUCHOS_RANGE1D_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines