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_Assert.hpp"
00056 
00057 
00058 namespace Teuchos {
00059 
00060 
00088 class Range1D {
00089 public:
00090 
00092   typedef Teuchos_Ordinal  Index;
00093 
00095   typedef Teuchos_Ordinal  Ordinal;
00096 
00098   enum EInvalidRange { INVALID };
00099 
00101   static const Range1D Invalid;
00102 
00112   inline Range1D();
00113 
00123   inline Range1D(EInvalidRange);
00124 
00143   inline Range1D(Ordinal lbound, Ordinal ubound);
00144 
00146   inline bool full_range() const;
00147 
00149   inline Ordinal lbound() const;
00150 
00152   inline Ordinal ubound() const;
00153 
00155   inline Ordinal size() const;
00156 
00158   inline bool in_range(Ordinal i) const;
00159 
00164   inline Range1D& operator+=( Ordinal incr );
00165 
00170   inline Range1D& operator-=( Ordinal incr );
00171 
00172 private:
00173 
00174   Ordinal lbound_;
00175   Ordinal ubound_;
00176   
00177   inline void assert_valid_range(Ordinal lbound, Ordinal ubound) const;
00178   
00179 }; // end class Range1D
00180 
00181   
00188 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00189 {
00190   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00191 }
00192 
00193   
00200 inline bool operator!=(const Range1D& rng1, const Range1D& rng2 )
00201 {
00202   return !(rng1 == rng2);
00203 }
00204 
00205 
00217 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Ordinal i)
00218 {
00219     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00220 }
00221 
00222 
00234 inline Range1D operator+(Range1D::Ordinal i, const Range1D &rng_rhs)
00235 {
00236     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00237 }
00238 
00239 
00251 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Ordinal i)
00252 {
00253     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00254 }
00255 
00256 
00272 inline Range1D full_range(const Range1D &rng, Range1D::Ordinal lbound, Range1D::Ordinal ubound)
00273 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00274 
00275   
00280 TEUCHOSCORE_LIB_DLL_EXPORT
00281 std::ostream& operator<<(std::ostream &out, const Range1D& rng);
00282 
00283 
00284 // //////////////////////////////////////////////////////////
00285 // Inline members
00286 
00287 inline
00288 Range1D::Range1D()
00289   : lbound_(0), ubound_(std::numeric_limits<Ordinal>::max()-1)
00290 {}
00291 
00292 inline
00293 Range1D::Range1D( EInvalidRange )
00294   : lbound_(0), ubound_(-2)
00295 {}
00296 
00297 
00298 inline
00299 Range1D::Range1D(Ordinal lbound_in, Ordinal ubound_in)
00300   : lbound_(lbound_in), ubound_(ubound_in)
00301 {
00302   assert_valid_range(lbound_in,ubound_in);
00303 }
00304 
00305 inline
00306 bool Range1D::full_range() const {
00307   return (lbound_ == 0 && ubound_ == std::numeric_limits<Ordinal>::max()-1);
00308 }
00309 
00310 inline
00311 Range1D::Ordinal Range1D::lbound() const {
00312   return lbound_;
00313 }
00314 
00315 inline
00316 Range1D::Ordinal Range1D::ubound() const {
00317   return ubound_;
00318 }
00319 
00320 inline
00321 Range1D::Ordinal Range1D::size() const {
00322   return ubound_ - lbound_ + 1;
00323 }
00324 
00325 inline
00326 bool Range1D::in_range(Ordinal i) const {
00327   return lbound_ <= i && i <= ubound_;
00328 }
00329 
00330 inline
00331 Range1D& Range1D::operator+=( Ordinal incr ) {
00332   assert_valid_range( lbound_ + incr, ubound_ + incr );
00333   lbound_ += incr;
00334   ubound_ += incr;
00335   return *this;
00336 }
00337 
00338 inline
00339 Range1D& Range1D::operator-=( Ordinal incr )
00340 {
00341   assert_valid_range( lbound_ - incr, ubound_ - incr );
00342   lbound_ -= incr;
00343   ubound_ -= incr;
00344   return *this;
00345 }
00346 
00347 
00348 // See Range1D.cpp
00349 inline
00350 void Range1D::assert_valid_range(Ordinal lbound_in, Ordinal ubound_in) const
00351 {
00352   (void)lbound_in; (void)ubound_in;
00353 #ifdef TEUCHOS_DEBUG
00354   TEUCHOS_ASSERT_INEQUALITY(lbound_in, >=, 0);
00355   TEUCHOS_ASSERT_INEQUALITY(ubound_in, >=, lbound_in - 1);
00356 #endif
00357 }
00358 
00359 } // end namespace Teuchos
00360 
00361 #endif // end TEUCHOS_RANGE1D_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines