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 // 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 STRIDE_ITER_H
00030 #define STRIDE_ITER_H
00031 
00032 #include "Moocho_ConfigDefs.hpp"
00033 
00034 namespace StrideIterPack {
00035 
00044 template<class T_iterator_type, class T_value_type, class T_reference_type
00045   , class T_pointer_type, class T_difference_type>
00046 class stride_iter {
00047 public:
00050 
00052   typedef std::random_access_iterator_tag           iterator_category;
00054   typedef T_iterator_type                   iterator_type;
00056   typedef T_value_type                    value_type;
00058   typedef T_reference_type                  reference;
00060   typedef T_pointer_type                    pointer;
00062   typedef T_difference_type                 difference_type;
00063   
00065     
00068 
00070   stride_iter() : current_(0), stride_(0)
00071   {}
00073   stride_iter(iterator_type current) :  current_(current), stride_(1)
00074   {}  
00076   stride_iter(iterator_type current, difference_type stride) :  current_(current), stride_(stride)
00077   {}
00079   template<class Iter, class Val, class Ref, class Ptr, class Diff>
00080   stride_iter(const stride_iter<Iter,Val,Ref,Ptr,Diff>& rhs) :current_(rhs.current()),stride_(rhs.stride())
00081   {}
00083   template<class Iter, class Val, class Ref, class Ptr, class Diff>
00084   stride_iter & operator=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& rhs) {
00085     current_ = rhs.current(); stride_ = rhs.stride(); return *this;
00086   }
00087 
00089 
00092 
00094   reference   operator*() const {
00095     return *current_;
00096   }
00098   pointer   operator->() const {
00099     return  current_;
00100   }
00102   reference   operator[](difference_type n) const {
00103     return current_[n * stride_];
00104   }
00105 
00107 
00110 
00112   stride_iter&    operator++() {
00113     current_ += stride_;
00114     return *this;
00115   }
00117   const stride_iter operator++(int) {
00118     stride_iter tmp = *this;
00119     ++*this; return tmp;
00120   }
00122   stride_iter&    operator--() {
00123     current_ -= stride_;
00124     return *this;
00125   }
00127   const stride_iter operator--(int) {
00128     stride_iter tmp = *this;
00129     --*this; return tmp;
00130   }
00132   stride_iter     operator+(difference_type n) {
00133     return stride_iter(current_ + n * stride_, stride_);
00134   }
00136   const stride_iter operator+(difference_type n) const {
00137     return stride_iter(current_ + n * stride_, stride_);
00138   }
00140   stride_iter&    operator+=(difference_type n) {
00141     current_ += n * stride_;
00142     return *this;
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   }
00157 
00159 
00161   difference_type   operator-(const stride_iter& itr) const {
00162     return (current_ - itr.current_)/stride_;
00163   }
00164 
00167 
00169   iterator_type current() const {
00170     return current_;
00171   }
00172 
00174   difference_type stride() const {
00175     return stride_;
00176   }
00177 
00179 
00180 private:
00182   iterator_type     current_;
00184   difference_type     stride_;
00185 };
00186 
00187 // RAB: 2001/11/16: For some reason this \defgroup causing doxygen to crash
00188 // when compiling RTOpPack?
00189 
00190 /* \defgroup stride_iter_funcs_grp Non-member functions for StrideIterPack::stride_itr
00191  * \ingroup Misc_grp
00192  */
00193 // @{
00194 
00196 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00197 inline const stride_iter<Iter,Val,Ref,Ptr,Diff> operator+(Diff n
00198   , const stride_iter<Iter,Val,Ref,Ptr,Diff> itr) 
00199 { 
00200   return itr + n;
00201 }
00202 
00204 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00205 inline bool operator<(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00206             const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00207 { 
00208   return itr1.operator->() < itr2.operator->();
00209 }
00211 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00212 inline bool operator<=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00213              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00214 {
00215   return itr1.operator->() <= itr2.operator->();
00216 }
00218 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00219 inline bool operator>(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00220             const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00221 {
00222   return itr1.operator->() > itr2.operator->();
00223 }
00225 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00226 inline bool operator>=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00227              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00228 {
00229   return itr1.operator->() >= itr2.operator->();
00230 }
00232 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00233 inline bool operator==(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00234              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00235 {
00236   return itr1.operator->() == itr2.operator->();
00237 }
00239 template<class Iter, class Val, class Ref, class Ptr, class Diff>
00240 inline bool operator!=(const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr1, 
00241              const stride_iter<Iter,Val,Ref,Ptr,Diff>& itr2)
00242 {
00243   return itr1.operator->() != itr2.operator->();
00244 }
00245 
00246 // @}
00247 
00248 } // end namespace StrideIterPack
00249 
00250 #endif // STRIDE_ITER_H

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