blitz Version 0.10
blitz/vecpickiter.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  * blitz/vecpickiter.h      Declaration of VectorPickIter<T_numtype> and
00004  *                          VectorPickIterConst<T_numtype> classes
00005  *
00006  * $Id: vecpickiter.h,v 1.6 2011/03/25 22:41:16 julianc Exp $
00007  *
00008  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
00009  *
00010  * This file is a part of Blitz.
00011  *
00012  * Blitz is free software: you can redistribute it and/or modify 
00013  * it under the terms of the GNU Lesser General Public License
00014  * as published by the Free Software Foundation, either version 3
00015  * of the License, or (at your option) any later version.
00016  *
00017  * Blitz is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public 
00023  * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
00024  * 
00025  * Suggestions:          blitz-devel@lists.sourceforge.net
00026  * Bugs:                 blitz-support@lists.sourceforge.net    
00027  *
00028  * For more information, please see the Blitz++ Home Page:
00029  *    https://sourceforge.net/projects/blitz/
00030  *
00031  ***************************************************************************/
00032 
00033 #ifndef BZ_VECPICKITER_H
00034 #define BZ_VECPICKITER_H
00035 
00036 #ifndef BZ_VECPICK_H
00037  #include <blitz/vecpick.h>
00038 #endif
00039 
00040 BZ_NAMESPACE(blitz)
00041 
00042 template<typename P_numtype>
00043 class VectorPickIter {
00044 
00045 public:
00046     typedef P_numtype  T_numtype;
00047 
00048     explicit VectorPickIter(VectorPick<T_numtype>& x)
00049         : data_(x.vector().data()), index_(x.indexSet().data())
00050     {
00051         dataStride_  = x.vector().stride();
00052         indexStride_ = x.indexSet().stride();
00053         length_ = x.indexSet().length();
00054     }
00055 
00056 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
00057     VectorPickIter(const VectorPickIter<T_numtype>& x)
00058     {
00059         data_ = x.data_;
00060         index_ = x.index_;
00061         dataStride_ = x.dataStride_;
00062         indexStride_ = x.indexStride_;
00063         length_ = x.length_;
00064     }
00065 #endif
00066 
00067     T_numtype operator[](int i) const
00068     {
00069         BZPRECONDITION(i < length_);
00070         return data_[dataStride_ * index_[i * indexStride_]];
00071     }
00072 
00073     T_numtype& operator[](int i)
00074     {
00075         BZPRECONDITION(i < length_);
00076         return data_[dataStride_ * index_[i * indexStride_]];
00077     }
00078 
00079     int length(int) const
00080     { return length_; }
00081 
00082     int _bz_suggestLength() const
00083     { return length_; }
00084 
00085     bool isUnitStride() const
00086     { return (dataStride_  == 1) && (indexStride_ == 1); }
00087 
00088     bool _bz_hasFastAccess() const
00089     { return isUnitStride(); }
00090 
00091     T_numtype _bz_fastAccess(int i) const
00092     {    
00093          return data_[index_[i]];
00094     }
00095 
00096     T_numtype&  _bz_fastAccess(int i)
00097     {
00098          return data_[index_[i]];
00099     }
00100 
00101     static const int
00102         _bz_staticLengthCount = 0,
00103         _bz_dynamicLengthCount = 1,
00104         _bz_staticLength = 0;
00105 
00106 private:
00107     T_numtype * restrict data_;
00108     int dataStride_;
00109     const int * restrict index_;
00110     int indexStride_;
00111     int length_;
00112 };
00113 
00114 template<typename P_numtype>
00115 class VectorPickIterConst {
00116 
00117 public:
00118     typedef P_numtype  T_numtype;
00119 
00120     explicit VectorPickIterConst(const VectorPick<T_numtype>& x)
00121         : data_(x.vector().data()), index_(x.indexSet().data())
00122     {
00123         dataStride_  = x.vector().stride();
00124         indexStride_ = x.indexSet().stride();
00125         length_ = x.indexSet().length();
00126     }
00127 
00128 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
00129     VectorPickIterConst(const VectorPickIterConst<T_numtype>& x)
00130     {
00131         data_ = x.data_;
00132         index_ = x.index_;
00133         dataStride_ = x.dataStride_;
00134         indexStride_ = x.indexStride_;
00135         length_ = x.length_;
00136     }
00137 #endif
00138 
00139     T_numtype operator[](int i) const
00140     {
00141         BZPRECONDITION(i < length_);
00142         return data_[dataStride_ * index_[i * indexStride_]];
00143     }
00144 
00145     int length(int) const
00146     { return length_; }
00147 
00148     int _bz_suggestLength() const
00149     { return length_; }
00150 
00151     bool isUnitStride() const
00152     { return (dataStride_  == 1) && (indexStride_ == 1); }
00153 
00154     bool _bz_hasFastAccess() const
00155     { return isUnitStride(); }
00156 
00157     T_numtype _bz_fastAccess(int i) const
00158     {
00159          return data_[index_[i]];
00160     }
00161 
00162     static const int 
00163         _bz_staticLengthCount = 0,
00164         _bz_dynamicLengthCount = 1,
00165         _bz_staticLength = 0;
00166 
00167 private:
00168     const T_numtype * restrict data_;
00169     int dataStride_;
00170     const int * restrict index_;
00171     int indexStride_;
00172     int length_;
00173 };
00174 
00175 BZ_NAMESPACE_END
00176 
00177 #endif // BZ_VECPICKITER_H
00178 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines