MoochoPack: Miscellaneous Utilities for MOOCHO Version of the Day
StrideIterPack_StrideIter.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 STRIDE_ITER_H
00043 #define STRIDE_ITER_H
00044 
00045 #include "Moocho_ConfigDefs.hpp"
00046 
00047 namespace StrideIterPack {
00048 
00057 template<class T_iterator_type, class T_value_type, class T_reference_type
00058   , class T_pointer_type, class T_difference_type>
00059 class stride_iter {
00060 public:
00063 
00065   typedef std::random_access_iterator_tag           iterator_category;
00067   typedef T_iterator_type                   iterator_type;
00069   typedef T_value_type                    value_type;
00071   typedef T_reference_type                  reference;
00073   typedef T_pointer_type                    pointer;
00075   typedef T_difference_type                 difference_type;
00076   
00078     
00081 
00083   stride_iter() : current_(0), stride_(0)
00084   {}
00086   stride_iter(iterator_type current) :  current_(current), stride_(1)
00087   {}  
00089   stride_iter(iterator_type current, difference_type stride) :  current_(current), stride_(stride)
00090   {}
00092   template<class Iter, class Val, class Ref, class Ptr, class Diff>
00093   stride_iter(const stride_iter<Iter,Val,Ref,Ptr,Diff>& rhs) :current_(rhs.current()),stride_(rhs.stride())
00094   {}
00096   template<class Iter, class Val, class Ref, class Ptr, class Diff>
00097   stride_iter & operator=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& rhs) {
00098     current_ = rhs.current(); stride_ = rhs.stride(); return *this;
00099   }
00100 
00102 
00105 
00107   reference   operator*() const {
00108     return *current_;
00109   }
00111   pointer   operator->() const {
00112     return  current_;
00113   }
00115   reference   operator[](difference_type n) const {
00116     return current_[n * stride_];
00117   }
00118 
00120 
00123 
00125   stride_iter&    operator++() {
00126     current_ += stride_;
00127     return *this;
00128   }
00130   const stride_iter operator++(int) {
00131     stride_iter tmp = *this;
00132     ++*this; return tmp;
00133   }
00135   stride_iter&    operator--() {
00136     current_ -= stride_;
00137     return *this;
00138   }
00140   const stride_iter operator--(int) {
00141     stride_iter tmp = *this;
00142     --*this; return tmp;
00143   }
00145   stride_iter     operator+(difference_type n) {
00146     return stride_iter(current_ + n * stride_, stride_);
00147   }
00149   const stride_iter operator+(difference_type n) const {
00150     return stride_iter(current_ + n * stride_, stride_);
00151   }
00153   stride_iter&    operator+=(difference_type n) {
00154     current_ += n * stride_;
00155     return *this;
00156   }
00158   stride_iter     operator-(difference_type n) {
00159     return stride_iter(current_ - n * stride_, stride_);
00160   }
00162   const stride_iter operator-(difference_type n) const {
00163     return stride_iter(current_ - n * stride_, stride_);
00164   }
00166   stride_iter&    operator-=(difference_type n) {
00167     current_ -= n * stride_;
00168     return *this;
00169   }
00170 
00172 
00174   difference_type   operator-(const stride_iter& itr) const {
00175     return (current_ - itr.current_)/stride_;
00176   }
00177 
00180 
00182   iterator_type current() const {
00183     return current_;
00184   }
00185 
00187   difference_type stride() const {
00188     return stride_;
00189   }
00190 
00192 
00193 private:
00195   iterator_type     current_;
00197   difference_type     stride_;
00198 };
00199 
00200 // RAB: 2001/11/16: For some reason this \defgroup causing doxygen to crash
00201 // when compiling RTOpPack?
00202 
00203 /* \defgroup stride_iter_funcs_grp Non-member functions for StrideIterPack::stride_itr
00204  * \ingroup Misc_grp
00205  */
00206 // @{
00207 
00209 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00210 inline const stride_iter<Iter,Val,Ref,Ptr,Diff> operator+(Diff n
00211   , const stride_iter<Iter,Val,Ref,Ptr,Diff> itr) 
00212 { 
00213   return itr + n;
00214 }
00215 
00217 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00218 inline bool operator<(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00219             const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00220 { 
00221   return itr1.operator->() < itr2.operator->();
00222 }
00224 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00225 inline bool operator<=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00226              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00227 {
00228   return itr1.operator->() <= itr2.operator->();
00229 }
00231 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00232 inline bool operator>(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00233             const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00234 {
00235   return itr1.operator->() > itr2.operator->();
00236 }
00238 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00239 inline bool operator>=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00240              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00241 {
00242   return itr1.operator->() >= itr2.operator->();
00243 }
00245 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00246 inline bool operator==(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00247              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00248 {
00249   return itr1.operator->() == itr2.operator->();
00250 }
00252 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00253 inline bool operator!=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00254              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00255 {
00256   return itr1.operator->() != itr2.operator->();
00257 }
00258 
00259 // @}
00260 
00261 } // end namespace StrideIterPack
00262 
00263 #endif // STRIDE_ITER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends