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_Index  Index;
00079 
00081   enum EInvalidRange { INVALID };
00082 
00084   static const Range1D Invalid;
00085 
00095   inline Range1D();
00096 
00106   inline Range1D( EInvalidRange );
00107 
00122   inline Range1D(Index lbound, Index ubound);
00123 
00127   inline bool full_range() const;
00128 
00130   inline Index lbound() const;
00131 
00133   inline Index ubound() const;
00134 
00136   inline Index size() const;
00137 
00139   inline bool in_range(Index i) const;
00140 
00142   inline Range1D& operator+=( Index incr );
00143 
00145   inline Range1D& operator-=( Index incr );
00146 
00147 private:
00148   Index lbound_;
00149   Index ubound_;  // = INT_MAX-1 flag for entire range
00150   // lbound == ubound == 0 flag for invalid range.
00151   
00152   // assert that the range is valid
00153   inline void assert_valid_range(Index lbound, Index ubound) const;
00154   
00155 }; // end class Range1D
00156   
00163 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00164 {
00165   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00166 }
00167 
00179 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
00180 {
00181     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00182 }
00183 
00195 inline Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
00196 {
00197     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00198 }
00199 
00211 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
00212 {
00213     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00214 }
00215 
00231 inline Range1D full_range(const Range1D &rng, Range1D::Index lbound, Range1D::Index ubound)
00232 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00233 
00234 // //////////////////////////////////////////////////////////
00235 // Inline members
00236 
00237 inline
00238 Range1D::Range1D()
00239   : lbound_(0), ubound_(INT_MAX-1)
00240 {}
00241 
00242 inline
00243 Range1D::Range1D( EInvalidRange )
00244   : lbound_(0), ubound_(-1)
00245 {}
00246 
00247 
00248 inline
00249 Range1D::Range1D(Index lbound_in, Index ubound_in)
00250   : lbound_(lbound_in), ubound_(ubound_in)
00251 {
00252   assert_valid_range(lbound_in,ubound_in);
00253 }
00254 
00255 inline
00256 bool Range1D::full_range() const {
00257   return ubound_ == INT_MAX-1;
00258 }
00259 
00260 inline
00261 Range1D::Index Range1D::lbound() const {
00262   return lbound_;
00263 }
00264 
00265 inline
00266 Range1D::Index Range1D::ubound() const {
00267   return ubound_;
00268 }
00269 
00270 inline
00271 Range1D::Index Range1D::size() const {
00272   return ubound_ - lbound_ + 1;
00273 }
00274 
00275 inline
00276 bool Range1D::in_range(Index i) const {
00277   return lbound_ <= i && i <= ubound_;
00278 }
00279 
00280 inline
00281 Range1D& Range1D::operator+=( Index incr ) {
00282   assert_valid_range( lbound_ + incr, ubound_ + incr );
00283   lbound_ += incr;
00284   ubound_ += incr;
00285   return *this;
00286 }
00287 
00288 inline
00289 Range1D& Range1D::operator-=( Index incr )
00290 {
00291   assert_valid_range( lbound_ - incr, ubound_ - incr );
00292   lbound_ -= incr;
00293   ubound_ -= incr;
00294   return *this;
00295 }
00296 
00297 
00298 // See Range1D.cpp
00299 inline
00300 void Range1D::assert_valid_range(int lbound_in, int ubound_in) const
00301 {
00302   (void)lbound_in; (void)ubound_in;
00303 #ifdef TEUCHOS_DEBUG
00304   TEST_FOR_EXCEPTION(
00305     lbound_in < 0, std::range_error
00306     ,"Range1D::assert_valid_range(): Error, lbound ="
00307     <<lbound_in<<" must be greater than or equal to 0."
00308     );
00309   TEST_FOR_EXCEPTION(
00310     lbound_in > ubound_in, std::range_error
00311     ,"Range1D::assert_valid_range(): Error, lbound = "
00312     <<lbound_in<<" > ubound = "<<ubound_in
00313     );
00314 #endif
00315 }
00316 
00317 } // end namespace Teuchos
00318 
00319 #endif // end TEUCHOS_RANGE1D_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:14:00 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1