MoochoPack: Miscellaneous Utilities for MOOCHO Version of the Day
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 // 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_;  // = INT_MAX flag for entire range
00138   // lbound == ubound == 0 flag for invalid range.
00139   
00140   // assert that the range is valid
00141   inline void assert_valid_range(Index lbound, Index ubound) const;
00142   
00143 }; // end class Range1D
00144   
00151 inline bool operator==(const Range1D& rng1, const Range1D& rng2 )
00152 {
00153   return rng1.lbound() == rng2.lbound() && rng1.ubound() == rng2.ubound();
00154 }
00155 
00167 inline Range1D operator+(const Range1D &rng_rhs, Range1D::Index i)
00168 {
00169     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00170 }
00171 
00183 inline Range1D operator+(Range1D::Index i, const Range1D &rng_rhs)
00184 {
00185     return Range1D(i+rng_rhs.lbound(), i+rng_rhs.ubound());
00186 }
00187 
00199 inline Range1D operator-(const Range1D &rng_rhs, Range1D::Index i)
00200 {
00201     return Range1D(rng_rhs.lbound()-i, rng_rhs.ubound()-i);
00202 }
00203 
00218 inline Range1D full_range(const Range1D &rng, Range1D::Index lbound, Range1D::Index ubound)
00219 { return rng.full_range() ? Range1D(lbound,ubound) : rng; }
00220 
00225 inline Range1D convert( const Teuchos::Range1D &rng )
00226 { return Range1D(rng.lbound()+1,rng.ubound()+1); }
00227 
00232 inline Teuchos::Range1D convert( const Range1D &rng )
00233 {
00234   return Teuchos::Range1D(rng.lbound()-1,rng.ubound()-1);
00235 }
00236 
00238 
00239 // //////////////////////////////////////////////////////////
00240 // Inline members
00241 
00242 inline
00243 Range1D::Range1D()
00244   : lbound_(1), ubound_(INT_MAX)  // RAB: 2004/08/24: Replace this with std::numeric_traits<Index>::max() when supported everywhere!
00245 {}
00246 
00247 inline
00248 Range1D::Range1D( EInvalidRange )
00249   : lbound_(1), ubound_(0)
00250 {}
00251 
00252 
00253 inline
00254 Range1D::Range1D(Index lbound, Index ubound)
00255   : lbound_(lbound), ubound_(ubound)
00256 {
00257   assert_valid_range(lbound,ubound);
00258 }
00259 
00260 inline
00261 bool Range1D::full_range() const {
00262   return ubound_ == INT_MAX;
00263 }
00264 
00265 inline
00266 Range1D::Index Range1D::lbound() const {
00267   return lbound_;
00268 }
00269 
00270 inline
00271 Range1D::Index Range1D::ubound() const {
00272   return ubound_;
00273 }
00274 
00275 inline
00276 Range1D::Index Range1D::size() const {
00277   return 1 + ubound_ - lbound_;
00278 }
00279 
00280 inline
00281 bool Range1D::in_range(Index i) const {
00282   return lbound_ <= i && i <= ubound_;
00283 }
00284 
00285 inline
00286 Range1D& Range1D::operator+=( Index incr ) {
00287   assert_valid_range( lbound_ + incr, ubound_ + incr );
00288   lbound_ += incr;
00289   ubound_ += incr;
00290   return *this;
00291 }
00292 
00293 inline
00294 Range1D& Range1D::operator-=( Index incr ) {
00295   assert_valid_range( lbound_ - incr, ubound_ - incr );
00296   lbound_ -= incr;
00297   ubound_ -= incr;
00298   return *this;
00299 }
00300 
00301 // See Range1D.cpp
00302 inline
00303 void Range1D::assert_valid_range(Index lbound, Index ubound) const {
00304 #ifdef TEUCHOS_DEBUG
00305   TEUCHOS_TEST_FOR_EXCEPTION(
00306     lbound < 1, std::range_error
00307     ,"Range1D::assert_valid_range(): Error, lbound ="<<lbound<<" must be greater than 0." );
00308   TEUCHOS_TEST_FOR_EXCEPTION(
00309     lbound > ubound, std::range_error
00310     ,"Range1D::assert_valid_range(): Error, lbound = "<<lbound<<" > ubound = "<<ubound );
00311 #endif
00312 }
00313 
00314 } // end namespace RangePack
00315 
00316 #endif // end RANGE1D_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends