blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/veciter.h Iterator classes for Vector<P_numtype> 00004 * 00005 * $Id: veciter.h,v 1.9 2011/03/25 22:41:16 julianc Exp $ 00006 * 00007 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> 00008 * 00009 * This file is a part of Blitz. 00010 * 00011 * Blitz is free software: you can redistribute it and/or modify 00012 * it under the terms of the GNU Lesser General Public License 00013 * as published by the Free Software Foundation, either version 3 00014 * of the License, or (at your option) any later version. 00015 * 00016 * Blitz is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. 00023 * 00024 * Suggestions: blitz-devel@lists.sourceforge.net 00025 * Bugs: blitz-support@lists.sourceforge.net 00026 * 00027 * For more information, please see the Blitz++ Home Page: 00028 * https://sourceforge.net/projects/blitz/ 00029 * 00030 ***************************************************************************/ 00031 00032 00033 #ifndef BZ_VECITER_H 00034 #define BZ_VECITER_H 00035 00036 #ifndef BZ_VECTOR_H 00037 #error <blitz/veciter.h> should be included via <blitz/vector.h> 00038 #endif 00039 00040 BZ_NAMESPACE(blitz) 00041 00042 // Declaration of class VectorIter 00043 template<typename P_numtype> 00044 class VectorIter { 00045 public: 00046 typedef P_numtype T_numtype; 00047 00048 explicit VectorIter(Vector<P_numtype>& x) 00049 : data_(x.data()) 00050 { 00051 stride_ = x.stride(); 00052 length_ = x.length(); 00053 } 00054 00055 VectorIter(P_numtype* restrict data, int stride, int length) 00056 : data_(data), stride_(stride), length_(length) 00057 { } 00058 00059 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR 00060 VectorIter(const VectorIter<P_numtype>& x) 00061 { 00062 data_ = x.data_; 00063 stride_ = x.stride_; 00064 length_ = x.length_; 00065 } 00066 #endif 00067 00068 P_numtype operator[](int i) const 00069 { 00070 BZPRECONDITION(i < length_); 00071 return data_[i*stride_]; 00072 } 00073 00074 P_numtype& restrict operator[](int i) 00075 { 00076 BZPRECONDITION(i < length_); 00077 return data_[i*stride_]; 00078 } 00079 00080 P_numtype operator()(int i) const 00081 { 00082 BZPRECONDITION(i < length_); 00083 return data_[i*stride_]; 00084 } 00085 00086 P_numtype& restrict operator()(int i) 00087 { 00088 BZPRECONDITION(i < length_); 00089 return data_[i*stride_]; 00090 } 00091 00092 P_numtype operator*() const 00093 { return *data_; } 00094 00095 P_numtype& operator*() 00096 { return *data_; } 00097 00098 VectorIter<P_numtype> operator+(int i) const 00099 { 00100 BZPRECONDITION(i <= length_); // JCC: Allow creation of "end" iterator 00101 return VectorIter<P_numtype>(data_+i*stride_, stride_, length_-i); 00102 } 00103 00104 int length(int) const 00105 { return length_; } 00106 00107 bool isUnitStride() const 00108 { return (stride_ == 1); } 00109 00111 // Library-internal member functions 00112 // These are undocumented and may change or 00113 // disappear in future releases. 00115 00116 static const int 00117 _bz_staticLengthCount = 0, 00118 _bz_dynamicLengthCount = 1, 00119 _bz_staticLength = 0; 00120 00121 bool _bz_hasFastAccess() const 00122 { return isUnitStride(); } 00123 00124 P_numtype _bz_fastAccess(int i) const 00125 { return data_[i]; } 00126 00127 P_numtype& restrict _bz_fastAccess(int i) 00128 { return data_[i]; } 00129 00130 int _bz_suggestLength() const 00131 { return length_; } 00132 00133 private: 00134 VectorIter() { } 00135 P_numtype * restrict data_; 00136 int stride_; 00137 int length_; 00138 }; 00139 00140 00141 template<typename P_numtype> 00142 class VectorIterConst { 00143 public: 00144 typedef P_numtype T_numtype; 00145 00146 explicit VectorIterConst(const Vector<P_numtype>& x) 00147 : data_(x.data()) 00148 { 00149 stride_ = x.stride(); 00150 length_ = x.length(); 00151 } 00152 00153 VectorIterConst(P_numtype* restrict data, int stride, int length) 00154 : data_(data), stride_(stride), length_(length) 00155 { } 00156 00157 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR 00158 VectorIterConst(const VectorIterConst<P_numtype>& x) 00159 { 00160 data_ = x.data_; 00161 stride_ = x.stride_; 00162 length_ = x.length_; 00163 } 00164 #endif 00165 00166 P_numtype operator[](int i) const 00167 { 00168 BZPRECONDITION(i < length_); 00169 return data_[i*stride_]; 00170 } 00171 00172 P_numtype operator()(int i) const 00173 { 00174 BZPRECONDITION(i < length_); 00175 return data_[i*stride_]; 00176 } 00177 00178 P_numtype operator*() const 00179 { return *data_; } 00180 00181 VectorIterConst<P_numtype> operator+(int i) const 00182 { 00183 BZPRECONDITION(i <= length_); // JCC: Allow creation of "end" iterator 00184 return VectorIterConst<P_numtype>(data_+i*stride_, stride_, length_-i); 00185 } 00186 00187 int length(int) const 00188 { return length_; } 00189 00190 bool isUnitStride() const 00191 { return (stride_ == 1); } 00192 00194 // Library-internal member functions 00195 // These are undocumented and may change or 00196 // disappear in future releases. 00198 00199 static const int 00200 _bz_staticLengthCount = 0, 00201 _bz_dynamicLengthCount = 1, 00202 _bz_staticLength = 0; 00203 00204 bool _bz_hasFastAccess() const 00205 { return isUnitStride(); } 00206 00207 P_numtype _bz_fastAccess(int i) const 00208 { 00209 return data_[i]; 00210 } 00211 00212 int _bz_suggestLength() const 00213 { return length_; } 00214 00215 private: 00216 const P_numtype * restrict data_; 00217 int stride_; 00218 int length_; 00219 }; 00220 00221 BZ_NAMESPACE_END 00222 00223 #endif // BZ_VECITER_H