Teuchos Package Browser (Single Doxygen Collection) 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 // Range1D class used for representing a range of positive integers.
00043 // Its primary usage is in accessing vectors and matrices by subregions
00044 // of rows and columns
00045 //
00046 
00047 #ifndef TEUCHOS_RANGE1D_HPP
00048 #define TEUCHOS_RANGE1D_HPP
00049 
00054 #include "Teuchos_ScalarTraits.hpp"
00055 #include "Teuchos_TestForException.hpp"
00056 
00057 namespace Teuchos {
00058 
00087 class Range1D {
00088 public:
00089 
00091   typedef Teuchos_Ordinal  Index;
00092 
00094   typedef Teuchos_Ordinal  Ordinal;
00095 
00097   enum EInvalidRange { INVALID };
00098 
00100   static const Range1D Invalid;
00101 
00111   inline Range1D();
00112 
00122   inline Range1D( EInvalidRange );
00123 
00138   inline Range1D(Ordinal lbound, Ordinal ubound);
00139 
00143   inline bool full_range() const;
00144 
00146   inline Ordinal lbound() const;
00147 
00149   inline Ordinal ubound() const;
00150 
00152   inline Ordinal size() const;
00153 
00155   inline bool in_range(Ordinal i) const;
00156 
00158   inline Range1D& operator+=( Ordinal incr );
00159 
00161   inline Range1D& operator-=( Ordinal incr );
00162 
00163 private:
00164   Ordinal lbound_;
00165   Ordinal ubound_;  // = INT_MAX-1 flag for entire range
00166   // lbound == ubound == 0 flag for invalid range.
00167   
00168   // assert that the range is valid
00169   inline void assert_valid_range(Ordinal lbound, Ordinal ubound) const;
00170   
00171 }; // end class Range1D
00172   
00179 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00180 {
00181   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00182 }
00183 
00195 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
00196 {
00197     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00198 }
00199 
00211 inline Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
00212 {
00213     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00214 }
00215 
00227 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
00228 {
00229     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00230 }
00231 
00247 inline Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
00248 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00249 
00250 // //////////////////////////////////////////////////////////
00251 // Inline members
00252 
00253 inline
00254 Range1D::Range1D()
00255   : lbound_(0), ubound_(INT_MAX-1)
00256 {}
00257 
00258 inline
00259 Range1D::Range1D( EInvalidRange )
00260   : lbound_(0), ubound_(-1)
00261 {}
00262 
00263 
00264 inline
00265 Range1D::Range1D(Ordinal lbound_in, Ordinal ubound_in)
00266   : lbound_(lbound_in), ubound_(ubound_in)
00267 {
00268   assert_valid_range(lbound_in,ubound_in);
00269 }
00270 
00271 inline
00272 bool Range1D::full_range() const {
00273   return ubound_ == INT_MAX-1;
00274 }
00275 
00276 inline
00277 Range1D::Ordinal Range1D::lbound() const {
00278   return lbound_;
00279 }
00280 
00281 inline
00282 Range1D::Ordinal Range1D::ubound() const {
00283   return ubound_;
00284 }
00285 
00286 inline
00287 Range1D::Ordinal Range1D::size() const {
00288   return ubound_ - lbound_ + 1;
00289 }
00290 
00291 inline
00292 bool Range1D::in_range(Ordinal i) const {
00293   return lbound_ <= i && i <= ubound_;
00294 }
00295 
00296 inline
00297 Range1D& Range1D::operator+=( Ordinal incr ) {
00298   assert_valid_range( lbound_ + incr, ubound_ + incr );
00299   lbound_ += incr;
00300   ubound_ += incr;
00301   return *this;
00302 }
00303 
00304 inline
00305 Range1D& Range1D::operator-=( Ordinal incr )
00306 {
00307   assert_valid_range( lbound_ - incr, ubound_ - incr );
00308   lbound_ -= incr;
00309   ubound_ -= incr;
00310   return *this;
00311 }
00312 
00313 
00314 // See Range1D.cpp
00315 inline
00316 void Range1D::assert_valid_range(Ordinal lbound_in, Ordinal ubound_in) const
00317 {
00318   (void)lbound_in; (void)ubound_in;
00319 #ifdef TEUCHOS_DEBUG
00320   TEST_FOR_EXCEPTION(
00321     lbound_in < 0, std::range_error
00322     ,"Range1D::assert_valid_range(): Error, lbound ="
00323     <<lbound_in<<" must be greater than or equal to 0."
00324     );
00325   TEST_FOR_EXCEPTION(
00326     lbound_in > ubound_in, std::range_error
00327     ,"Range1D::assert_valid_range(): Error, lbound = "
00328     <<lbound_in<<" > ubound = "<<ubound_in
00329     );
00330 #endif
00331 }
00332 
00333 } // end namespace Teuchos
00334 
00335 #endif // end TEUCHOS_RANGE1D_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines