MOOCHO (Single Doxygen Collection) Version of the Day
RangePack_Range1D.hpp
Go to the documentation of this file.
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 // 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 Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef RANGE1D_H
00043 #define RANGE1D_H
00044 
00045 #include "RTOpPack_Types.hpp"
00046 #include "Teuchos_Range1D.hpp"
00047 #include "Teuchos_ScalarTraits.hpp"
00048 
00049 namespace RangePack {
00050 
00076 class Range1D {
00077 public:
00079   typedef RTOpPack::Ordinal  Index;
00081   enum EInvalidRange { INVALID };
00083   static const Range1D Invalid;
00094   inline Range1D();
00104   inline Range1D( EInvalidRange );
00119   inline Range1D(Index lbound, Index ubound);
00121   inline bool full_range() const;
00123   inline Index lbound() const;
00125   inline Index ubound() const;
00127   inline Index size() const;
00129   inline bool in_range(Index i) const;
00131   inline Range1D& operator+=( Index incr );
00133   inline Range1D& operator-=( Index incr );
00134 
00135 private:
00136   Index lbound_;
00137   Index ubound_;
00138   
00139   // assert that the range is valid
00140   inline void assert_valid_range(Index lbound, Index ubound) const;
00141   
00142 }; // end class Range1D
00143   
00150 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00151 {
00152   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00153 }
00154 
00166 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
00167 {
00168     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00169 }
00170 
00182 inline Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
00183 {
00184     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00185 }
00186 
00198 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
00199 {
00200     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00201 }
00202 
00217 inline Range1D full_range(const Range1D &rng, Range1D::Index lbound, Range1D::Index ubound)
00218 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00219 
00224 inline Range1D convert( const Teuchos::Range1D &rng )
00225 {
00226   Range1D rngOut;
00227   if (rng.full_range()) {
00228     rngOut = Range1D();
00229   }
00230   else if (rng.size() == -1) {
00231     rngOut = Range1D::Invalid;
00232   }
00233   else if (rng.size() == 0) {
00234     rngOut = Range1D::Invalid;
00235   }
00236   else {
00237     rngOut = Range1D(rng.lbound()+1, rng.ubound()+1);
00238   }
00239   return rngOut;
00240 }
00241 
00246 inline Teuchos::Range1D convert( const Range1D &rng )
00247 {
00248   Teuchos::Range1D rngOut;
00249   if (rng.full_range()) {
00250     rngOut = Teuchos::Range1D();
00251   }
00252   else if (rng.size() == 0) {
00253     rngOut = Teuchos::Range1D::Invalid;
00254   }
00255   else {
00256     rngOut = Teuchos::Range1D(rng.lbound()-1, rng.ubound()-1);
00257   }
00258   return rngOut;
00259 }
00260 
00261 
00263 
00264 // //////////////////////////////////////////////////////////
00265 // Inline members
00266 
00267 inline
00268 Range1D::Range1D()
00269   : lbound_(1), ubound_(std::numeric_limits<Index>::max()-1)
00270 {}
00271 
00272 inline
00273 Range1D::Range1D( EInvalidRange )
00274   : lbound_(1), ubound_(0)
00275 {}
00276 
00277 
00278 inline
00279 Range1D::Range1D(Index lbound, Index ubound)
00280   : lbound_(lbound), ubound_(ubound)
00281 {
00282   assert_valid_range(lbound,ubound);
00283 }
00284 
00285 inline
00286 bool Range1D::full_range() const {
00287   return ubound_ == (std::numeric_limits<Index>::max()-1);
00288 }
00289 
00290 inline
00291 Range1D::Index Range1D::lbound() const {
00292   return lbound_;
00293 }
00294 
00295 inline
00296 Range1D::Index Range1D::ubound() const {
00297   return ubound_;
00298 }
00299 
00300 inline
00301 Range1D::Index Range1D::size() const {
00302   return 1 + ubound_ - lbound_;
00303 }
00304 
00305 inline
00306 bool Range1D::in_range(Index i) const {
00307   return lbound_ <= i && i <= ubound_;
00308 }
00309 
00310 inline
00311 Range1D& Range1D::operator+=( Index incr ) {
00312   assert_valid_range( lbound_ + incr, ubound_ + incr );
00313   lbound_ += incr;
00314   ubound_ += incr;
00315   return *this;
00316 }
00317 
00318 inline
00319 Range1D& Range1D::operator-=( Index incr ) {
00320   assert_valid_range( lbound_ - incr, ubound_ - incr );
00321   lbound_ -= incr;
00322   ubound_ -= incr;
00323   return *this;
00324 }
00325 
00326 // See Range1D.cpp
00327 inline
00328 void Range1D::assert_valid_range(Index lbound, Index ubound) const {
00329 #ifdef TEUCHOS_DEBUG
00330   TEUCHOS_TEST_FOR_EXCEPTION(
00331     lbound < 1, std::range_error
00332     ,"Range1D::assert_valid_range(): Error, lbound ="<<lbound<<" must be greater than 0." );
00333   TEUCHOS_TEST_FOR_EXCEPTION(
00334     lbound > ubound, std::range_error
00335     ,"Range1D::assert_valid_range(): Error, lbound = "<<lbound<<" > ubound = "<<ubound );
00336 #endif
00337 }
00338 
00339 } // end namespace RangePack
00340 
00341 #endif // end RANGE1D_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines