RangePack_Range1D.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) 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 Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef RANGE1D_H
00030 #define RANGE1D_H
00031 
00032 #include "RTOpPack_Types.hpp"
00033 #include "Teuchos_Range1D.hpp"
00034 #include "Teuchos_ScalarTraits.hpp"
00035 
00036 namespace RangePack {
00037 
00063 class Range1D {
00064 public:
00066   typedef RTOpPack::Index  Index;
00068   enum EInvalidRange { INVALID };
00070   static const Range1D Invalid;
00081   inline Range1D();
00091   inline Range1D( EInvalidRange );
00106   inline Range1D(Index lbound, Index ubound);
00108   inline bool full_range() const;
00110   inline Index lbound() const;
00112   inline Index ubound() const;
00114   inline Index size() const;
00116   inline bool in_range(Index i) const;
00118   inline Range1D& operator+=( Index incr );
00120   inline Range1D& operator-=( Index incr );
00121 
00122 private:
00123   Index lbound_;
00124   Index ubound_;  // = INT_MAX flag for entire range
00125   // lbound == ubound == 0 flag for invalid range.
00126   
00127   // assert that the range is valid
00128   inline void assert_valid_range(Index lbound, Index ubound) const;
00129   
00130 }; // end class Range1D
00131   
00138 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00139 {
00140   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00141 }
00142 
00154 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
00155 {
00156     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00157 }
00158 
00170 inline Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
00171 {
00172     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00173 }
00174 
00186 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
00187 {
00188     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00189 }
00190 
00205 inline Range1D full_range(const Range1D &rng, Range1D::Index lbound, Range1D::Index ubound)
00206 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00207 
00212 inline Range1D convert( const Teuchos::Range1D &rng )
00213 { return Range1D(rng.lbound()+1,rng.ubound()+1); }
00214 
00219 inline Teuchos::Range1D convert( const Range1D &rng )
00220 {
00221   return Teuchos::Range1D(rng.lbound()-1,rng.ubound()-1);
00222 }
00223 
00225 
00226 // //////////////////////////////////////////////////////////
00227 // Inline members
00228 
00229 inline
00230 Range1D::Range1D()
00231   : lbound_(1), ubound_(INT_MAX)  // RAB: 2004/08/24: Replace this with std::numeric_traits<Index>::max() when supported everywhere!
00232 {}
00233 
00234 inline
00235 Range1D::Range1D( EInvalidRange )
00236   : lbound_(1), ubound_(0)
00237 {}
00238 
00239 
00240 inline
00241 Range1D::Range1D(Index lbound, Index ubound)
00242   : lbound_(lbound), ubound_(ubound)
00243 {
00244   assert_valid_range(lbound,ubound);
00245 }
00246 
00247 inline
00248 bool Range1D::full_range() const {
00249   return ubound_ == INT_MAX;
00250 }
00251 
00252 inline
00253 Range1D::Index Range1D::lbound() const {
00254   return lbound_;
00255 }
00256 
00257 inline
00258 Range1D::Index Range1D::ubound() const {
00259   return ubound_;
00260 }
00261 
00262 inline
00263 Range1D::Index Range1D::size() const {
00264   return 1 + ubound_ - lbound_;
00265 }
00266 
00267 inline
00268 bool Range1D::in_range(Index i) const {
00269   return lbound_ <= i && i <= ubound_;
00270 }
00271 
00272 inline
00273 Range1D& Range1D::operator+=( Index incr ) {
00274   assert_valid_range( lbound_ + incr, ubound_ + incr );
00275   lbound_ += incr;
00276   ubound_ += incr;
00277   return *this;
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 // See Range1D.cpp
00289 inline
00290 void Range1D::assert_valid_range(int lbound, int ubound) const {
00291 #ifdef TEUCHOS_DEBUG
00292   TEST_FOR_EXCEPTION(
00293     lbound < 1, std::range_error
00294     ,"Range1D::assert_valid_range(): Error, lbound ="<<lbound<<" must be greater than 0." );
00295   TEST_FOR_EXCEPTION(
00296     lbound > ubound, std::range_error
00297     ,"Range1D::assert_valid_range(): Error, lbound = "<<lbound<<" > ubound = "<<ubound );
00298 #endif
00299 }
00300 
00301 } // end namespace RangePack
00302 
00303 #endif // end RANGE1D_H

Generated on Wed May 12 21:50:25 2010 for MoochoPack: Miscellaneous Utilities for MOOCHO by  doxygen 1.4.7