00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef BRICK_ITERATOR_H
00012 #define BRICK_ITERATOR_H
00013
00014
00015 #include "Utility/Vec.h"
00016 #include "Utility/RefCounted.h"
00017 #include "Utility/Pooled.h"
00018 #include "Message/Message.h"
00019 #include "PETE/IpplExpressions.h"
00020
00021
00022 template <unsigned Dim> class NDIndex;
00023
00024
00025 #ifdef IPPL_NO_RESTRICT
00026 #define restrict
00027 #endif
00028
00029
00030
00031
00032 template<unsigned Dim>
00033 class BrickCounter
00034 {
00035 public:
00036
00037
00038 BrickCounter() {}
00039
00040
00041 BrickCounter(const NDIndex<Dim>&);
00042
00043
00044 BrickCounter& operator++() { op_pp(); return *this; }
00045
00046
00047 bool done(unsigned d) const { return Counters[d] == Counts[d]; }
00048
00049
00050 void step(unsigned d) { Counters[d] += 1; }
00051 void rewind(unsigned d) { Counters[d] = 0; }
00052
00053
00054 inline int size(unsigned d) const { return Counts[d]; }
00055
00056
00057 int GetOffset(unsigned d) const { return Counters[d]; }
00058
00059 protected:
00060 void op_pp();
00061 vec<int,Dim> Counters;
00062 vec<int,Dim> Counts;
00063
00064 };
00065
00066
00067
00068
00069 template<class T, unsigned Dim>
00070 class BrickIterator : public BrickCounter<Dim>
00071 {
00072 public:
00073
00074
00075 BrickIterator() : Current(0), Whole(true) {}
00076
00077
00078 BrickIterator(T*, const NDIndex<Dim>&, const NDIndex<Dim>&);
00079
00080
00081 BrickIterator(T*, const vec<int,Dim>&);
00082
00083
00084 BrickIterator& operator++() { op_pp(); return *this; }
00085
00086
00087 bool whole() const { return Whole; }
00088
00089
00090 bool operator==(const BrickIterator<T,Dim>& a) const
00091 {
00092 return Current == a.Current;
00093 }
00094 bool operator!=(const BrickIterator<T,Dim>& a) const
00095 {
00096 return Current != a.Current;
00097 }
00098
00099
00100 T& operator*() const
00101 {
00102 return *Current;
00103 }
00104
00105
00106 void step(unsigned d)
00107 {
00108 BrickCounter<Dim>::step( d );
00109 Current += Strides[ d ];
00110 }
00111 void rewind(unsigned d)
00112 {
00113 BrickCounter<Dim>::rewind( d );
00114 Current -= Strides[d]*BrickCounter<Dim>::Counts[d];
00115 }
00116
00117
00118 T& offset(int i) const
00119 {
00120 return Current[ i*Strides[0] ];
00121 }
00122 T& offset(int i, int j) const
00123 {
00124 return Current[ i*Strides[0] + j*Strides[1] ];
00125 }
00126 T& offset(int i,int j,int k) const
00127 {
00128 return Current[ i*Strides[0]+ j*Strides[1] + k*Strides[2] ];
00129 }
00130 T& offset(int *i) const
00131 {
00132 return Current[ vec<int,Dim>::dot(i,&Strides[0]) ];
00133 }
00134 T& unit_offset(int i) const
00135 {
00136 return Current[ i ];
00137 }
00138 T& unit_offset(int i, int j) const
00139 {
00140 return Current[ i + j*Strides[1] ];
00141 }
00142 T& unit_offset(int i,int j,int k) const
00143 {
00144 return Current[ i+ j*Strides[1] + k*Strides[2] ];
00145 }
00146 void moveBy(int i)
00147 {
00148 Current += i*Strides[0];
00149 BrickCounter<Dim>::Counters[0] += i;
00150 }
00151 void moveBy(int i, int j)
00152 {
00153 Current += (i*Strides[0] + j*Strides[1]);
00154 BrickCounter<Dim>::Counters[0] += i;
00155 BrickCounter<Dim>::Counters[1] += j;
00156 }
00157 void moveBy(int i, int j, int k)
00158 {
00159 Current += (i*Strides[0] + j*Strides[1] + k*Strides[2]);
00160 BrickCounter<Dim>::Counters[0] += i;
00161 BrickCounter<Dim>::Counters[1] += j;
00162 BrickCounter<Dim>::Counters[2] += k;
00163 }
00164 void moveBy(const int *i)
00165 {
00166 for (unsigned int d=0; d < Dim; ++d) {
00167 Current += i[d] * Strides[d];
00168 BrickCounter<Dim>::Counters[d] += i[d];
00169 }
00170 }
00171 int Stride(int i) const { return Strides[i]; }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 Message& putMessage(Message&, bool makecopy = true);
00182 Message& getMessage(Message&);
00183
00184
00185 typedef T PETE_Return_t;
00186 typedef BrickIterator<T,Dim> PETE_Expr_t;
00187 PETE_Expr_t MakeExpression() const { return *this; }
00188
00189 protected:
00190 void op_pp();
00191 T* restrict Current;
00192 vec<int,Dim> Strides;
00193 bool Whole;
00194 };
00195
00197
00198 #include "Field/BrickIterator.cpp"
00199
00200 #endif // BRICK_ITERATOR_H
00201
00202
00203
00204
00205
00206