Thyra_Range1D.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //      Thyra: Interfaces and Support Code for the Interoperability of Abstract Numerical Algorithms 
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 RANGE1D_H
00035 #define RANGE1D_H
00036 
00037 #include "RTOpPack_Types.hpp"
00038 #include "Teuchos_ScalarTraits.hpp"
00039 
00040 namespace RangePack {
00041 
00043 
00068 class Range1D {
00069 public:
00071   typedef RTOpPack::Index  Index;
00073   enum EInvalidRange { INVALID };
00075   static const Range1D Invalid;
00077 
00086 
00087   inline Range1D();
00097   inline Range1D( EInvalidRange );
00112   inline Range1D(Index lbound, Index ubound);
00114   inline bool full_range() const;
00116   inline Index lbound() const;
00118   inline Index ubound() const;
00120   inline Index size() const;
00122   inline bool in_range(Index i) const;
00124   inline Range1D& operator+=( Index incr );
00126   inline Range1D& operator-=( Index incr );
00127 
00128 private:
00129   Index lbound_;
00130   Index ubound_;  // = INT_MAX flag for entire range
00131   // lbound == ubound == 0 flag for invalid range.
00132   
00133   // assert that the range is valid
00134   inline void assert_valid_range(Index lbound, Index ubound) const;
00135   
00136 }; // end class Range1D
00137   
00144   
00149 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00150 {
00151   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00152 }
00153 
00163 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
00164 {
00165     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00166 }
00167 
00177 inline Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
00178 {
00179     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00180 }
00181 
00191 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
00192 {
00193     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00194 }
00195 
00197 
00209 inline Range1D full_range(const Range1D &rng, Range1D::Index lbound, Range1D::Index ubound)
00210 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00211 
00213 
00214 // //////////////////////////////////////////////////////////
00215 // Inline members
00216 
00217 inline
00218 Range1D::Range1D()
00219   : lbound_(1), ubound_(INT_MAX)  // RAB: 2004/08/24: Replace this with std::numeric_traits<Index>::max() when supported everywhere!
00220 {}
00221 
00222 inline
00223 Range1D::Range1D( EInvalidRange )
00224   : lbound_(1), ubound_(0)
00225 {}
00226 
00227 
00228 inline
00229 Range1D::Range1D(Index lbound, Index ubound)
00230   : lbound_(lbound), ubound_(ubound)
00231 {
00232   assert_valid_range(lbound,ubound);
00233 }
00234 
00235 inline
00236 bool Range1D::full_range() const {
00237   return ubound_ == INT_MAX;
00238 }
00239 
00240 inline
00241 Range1D::Index Range1D::lbound() const {
00242   return lbound_;
00243 }
00244 
00245 inline
00246 Range1D::Index Range1D::ubound() const {
00247   return ubound_;
00248 }
00249 
00250 inline
00251 Range1D::Index Range1D::size() const {
00252   return 1 + ubound_ - lbound_;
00253 }
00254 
00255 inline
00256 bool Range1D::in_range(Index i) const {
00257   return lbound_ <= i && i <= ubound_;
00258 }
00259 
00260 inline
00261 Range1D& Range1D::operator+=( Index incr ) {
00262   assert_valid_range( lbound_ + incr, ubound_ + incr );
00263   lbound_ += incr;
00264   ubound_ += incr;
00265   return *this;
00266 }
00267 
00268 inline
00269 Range1D& Range1D::operator-=( Index incr ) {
00270   assert_valid_range( lbound_ - incr, ubound_ - incr );
00271   lbound_ -= incr;
00272   ubound_ -= incr;
00273   return *this;
00274 }
00275 
00276 // See Range1D.cpp
00277 inline
00278 void Range1D::assert_valid_range(int lbound, int ubound) const {
00279 #ifdef _DEBUG
00280   TEST_FOR_EXCEPTION(
00281     lbound < 1, std::range_error
00282     ,"Range1D::assert_valid_range(): Error, lbound ="<<lbound<<" must be greater than 0." );
00283   TEST_FOR_EXCEPTION(
00284     lbound > ubound, std::range_error
00285     ,"Range1D::assert_valid_range(): Error, lbound = "<<lbound<<" > ubound = "<<ubound );
00286 #endif
00287 }
00288 
00289 } // end namespace RangePack
00290 
00291 #endif // end RANGE1D_H

Generated on Thu Sep 18 12:39:42 2008 for Thyra Miscellaneous Utilities by doxygen 1.3.9.1