1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/array/iter.h Declaration of FastArrayIterator<P_numtype,N_rank> |
---|
4 | * |
---|
5 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * Suggestions: blitz-dev@oonumerics.org |
---|
18 | * Bugs: blitz-bugs@oonumerics.org |
---|
19 | * |
---|
20 | * For more information, please see the Blitz++ Home Page: |
---|
21 | * http://oonumerics.org/blitz/ |
---|
22 | * |
---|
23 | ****************************************************************************/ |
---|
24 | #ifndef BZ_ARRAY_FASTITER_H |
---|
25 | #define BZ_ARRAY_FASTITER_H |
---|
26 | |
---|
27 | #ifdef BZ_HAVE_STD |
---|
28 | #include <sstream> |
---|
29 | #else |
---|
30 | #include <strstream.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | BZ_NAMESPACE(blitz) |
---|
34 | |
---|
35 | #ifndef BZ_ARRAY_H |
---|
36 | #error <blitz/array/iter.h> must be included via <blitz/array.h> |
---|
37 | #endif |
---|
38 | |
---|
39 | template<typename P_numtype, int N_rank> |
---|
40 | class FastArrayIterator { |
---|
41 | public: |
---|
42 | typedef P_numtype T_numtype; |
---|
43 | typedef Array<T_numtype, N_rank> T_array; |
---|
44 | typedef FastArrayIterator<P_numtype, N_rank> T_iterator; |
---|
45 | typedef const T_array& T_ctorArg1; |
---|
46 | typedef int T_ctorArg2; // dummy |
---|
47 | |
---|
48 | static const int |
---|
49 | numArrayOperands = 1, |
---|
50 | numIndexPlaceholders = 0, |
---|
51 | rank = N_rank; |
---|
52 | |
---|
53 | // NB: this ctor does NOT preserve stack and stride |
---|
54 | // parameters. This is for speed purposes. |
---|
55 | FastArrayIterator(const FastArrayIterator<P_numtype, N_rank>& x) |
---|
56 | : data_(x.data_), array_(x.array_) |
---|
57 | { } |
---|
58 | |
---|
59 | void operator=(const FastArrayIterator<P_numtype, N_rank>& x) |
---|
60 | { |
---|
61 | array_ = x.array_; |
---|
62 | data_ = x.data_; |
---|
63 | stack_ = x.stack_; |
---|
64 | stride_ = x.stride_; |
---|
65 | } |
---|
66 | |
---|
67 | FastArrayIterator(const T_array& array) |
---|
68 | : array_(array) |
---|
69 | { |
---|
70 | data_ = array.data(); |
---|
71 | } |
---|
72 | |
---|
73 | ~FastArrayIterator() |
---|
74 | { } |
---|
75 | |
---|
76 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE |
---|
77 | T_numtype operator()(TinyVector<int, N_rank> i) |
---|
78 | { return array_(i); } |
---|
79 | #else |
---|
80 | T_numtype operator()(const TinyVector<int, N_rank>& i) |
---|
81 | { return array_(i); } |
---|
82 | #endif |
---|
83 | |
---|
84 | int ascending(int rank) |
---|
85 | { |
---|
86 | if (rank < N_rank) |
---|
87 | return array_.isRankStoredAscending(rank); |
---|
88 | else |
---|
89 | return INT_MIN; // tiny(int()); |
---|
90 | } |
---|
91 | |
---|
92 | int ordering(int rank) |
---|
93 | { |
---|
94 | if (rank < N_rank) |
---|
95 | return array_.ordering(rank); |
---|
96 | else |
---|
97 | return INT_MIN; // tiny(int()); |
---|
98 | } |
---|
99 | |
---|
100 | int lbound(int rank) |
---|
101 | { |
---|
102 | if (rank < N_rank) |
---|
103 | return array_.lbound(rank); |
---|
104 | else |
---|
105 | return INT_MIN; // tiny(int()); |
---|
106 | } |
---|
107 | |
---|
108 | int ubound(int rank) |
---|
109 | { |
---|
110 | if (rank < N_rank) |
---|
111 | return array_.ubound(rank); |
---|
112 | else |
---|
113 | return INT_MAX; // huge(int()); |
---|
114 | } |
---|
115 | |
---|
116 | T_numtype operator*() |
---|
117 | { return *data_; } |
---|
118 | |
---|
119 | T_numtype operator[](int i) |
---|
120 | { return data_[i * stride_]; } |
---|
121 | |
---|
122 | T_numtype fastRead(int i) |
---|
123 | { return data_[i]; } |
---|
124 | |
---|
125 | int suggestStride(int rank) const |
---|
126 | { return array_.stride(rank); } |
---|
127 | |
---|
128 | bool isStride(int rank, int stride) const |
---|
129 | { return array_.stride(rank) == stride; } |
---|
130 | |
---|
131 | void push(int position) |
---|
132 | { |
---|
133 | stack_[position] = data_; |
---|
134 | } |
---|
135 | |
---|
136 | void pop(int position) |
---|
137 | { |
---|
138 | data_ = stack_[position]; |
---|
139 | } |
---|
140 | |
---|
141 | void advance() |
---|
142 | { |
---|
143 | data_ += stride_; |
---|
144 | } |
---|
145 | |
---|
146 | void advance(int n) |
---|
147 | { |
---|
148 | data_ += n * stride_; |
---|
149 | } |
---|
150 | |
---|
151 | void loadStride(int rank) |
---|
152 | { |
---|
153 | stride_ = array_.stride(rank); |
---|
154 | } |
---|
155 | |
---|
156 | const T_numtype * restrict data() const |
---|
157 | { return data_; } |
---|
158 | |
---|
159 | void _bz_setData(const T_numtype* ptr) |
---|
160 | { data_ = ptr; } |
---|
161 | |
---|
162 | int stride() const |
---|
163 | { return stride_; } |
---|
164 | |
---|
165 | bool isUnitStride(int rank) const |
---|
166 | { return array_.stride(rank) == 1; } |
---|
167 | |
---|
168 | void advanceUnitStride() |
---|
169 | { ++data_; } |
---|
170 | |
---|
171 | bool canCollapse(int outerLoopRank, int innerLoopRank) const |
---|
172 | { return array_.canCollapse(outerLoopRank, innerLoopRank); } |
---|
173 | |
---|
174 | void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
175 | prettyPrintFormat& format) const |
---|
176 | { |
---|
177 | if (format.tersePrintingSelected()) |
---|
178 | str += format.nextArrayOperandSymbol(); |
---|
179 | else if (format.dumpArrayShapesMode()) |
---|
180 | { |
---|
181 | #ifdef BZ_HAVE_STD |
---|
182 | BZ_STD_SCOPE(ostringstream) ostr; |
---|
183 | #else |
---|
184 | ostrstream ostr; |
---|
185 | #endif |
---|
186 | ostr << array_.shape(); |
---|
187 | str += ostr.str(); |
---|
188 | } |
---|
189 | else { |
---|
190 | str += "Array<"; |
---|
191 | str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype); |
---|
192 | str += ","; |
---|
193 | |
---|
194 | char tmpBuf[10]; |
---|
195 | sprintf(tmpBuf, "%d", N_rank); |
---|
196 | |
---|
197 | str += tmpBuf; |
---|
198 | str += ">"; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | template<typename T_shape> |
---|
203 | bool shapeCheck(const T_shape& shape) |
---|
204 | { return areShapesConformable(shape, array_.length()); } |
---|
205 | |
---|
206 | |
---|
207 | // Experimental |
---|
208 | T_numtype& operator()(int i) |
---|
209 | { |
---|
210 | return (T_numtype&)data_[i*array_.stride(0)]; |
---|
211 | } |
---|
212 | |
---|
213 | // Experimental |
---|
214 | T_numtype& operator()(int i, int j) |
---|
215 | { |
---|
216 | return (T_numtype&)data_[i*array_.stride(0) + j*array_.stride(1)]; |
---|
217 | } |
---|
218 | |
---|
219 | // Experimental |
---|
220 | T_numtype& operator()(int i, int j, int k) |
---|
221 | { |
---|
222 | return (T_numtype&)data_[i*array_.stride(0) + j*array_.stride(1) |
---|
223 | + k*array_.stride(2)]; |
---|
224 | } |
---|
225 | |
---|
226 | // Experimental |
---|
227 | |
---|
228 | void moveTo(int i, int j) |
---|
229 | { |
---|
230 | data_ = &const_cast<T_array&>(array_)(i,j); |
---|
231 | } |
---|
232 | |
---|
233 | void moveTo(int i, int j, int k) |
---|
234 | { |
---|
235 | data_ = &const_cast<T_array&>(array_)(i,j,k); |
---|
236 | } |
---|
237 | |
---|
238 | void moveTo(const TinyVector<int,N_rank>& i) |
---|
239 | { |
---|
240 | data_ = &const_cast<T_array&>(array_)(i); |
---|
241 | } |
---|
242 | |
---|
243 | // Experimental |
---|
244 | void operator=(T_numtype x) |
---|
245 | { *const_cast<T_numtype*>(data_) = x; } |
---|
246 | |
---|
247 | // Experimental |
---|
248 | template<typename T_value> |
---|
249 | void operator=(T_value x) |
---|
250 | { *const_cast<T_numtype*>(data_) = x; } |
---|
251 | |
---|
252 | // Experimental |
---|
253 | template<typename T_value> |
---|
254 | void operator+=(T_value x) |
---|
255 | { *const_cast<T_numtype*>(data_) += x; } |
---|
256 | |
---|
257 | // NEEDS_WORK: other operators |
---|
258 | |
---|
259 | // Experimental |
---|
260 | operator T_numtype() const |
---|
261 | { return *data_; } |
---|
262 | |
---|
263 | // Experimental |
---|
264 | T_numtype shift(int offset, int dim) |
---|
265 | { |
---|
266 | return data_[offset*array_.stride(dim)]; |
---|
267 | } |
---|
268 | |
---|
269 | // Experimental |
---|
270 | T_numtype shift(int offset1, int dim1, int offset2, int dim2) |
---|
271 | { |
---|
272 | return data_[offset1*array_.stride(dim1) |
---|
273 | + offset2*array_.stride(dim2)]; |
---|
274 | } |
---|
275 | |
---|
276 | private: |
---|
277 | const T_numtype * restrict data_; |
---|
278 | const T_array& array_; |
---|
279 | ConstPointerStack<T_numtype,N_rank> stack_; |
---|
280 | int stride_; |
---|
281 | }; |
---|
282 | |
---|
283 | BZ_NAMESPACE_END |
---|
284 | |
---|
285 | #endif // BZ_ARRAY_FASTITER_H |
---|