1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/array/expr.h Array<T,N> expression templates |
---|
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_ARRAYEXPR_H |
---|
25 | #define BZ_ARRAYEXPR_H |
---|
26 | |
---|
27 | #ifndef BZ_ARRAY_H |
---|
28 | #error <blitz/array/expr.h> must be included via <blitz/array.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <blitz/ops.h> |
---|
32 | #include <blitz/prettyprint.h> |
---|
33 | #include <blitz/shapecheck.h> |
---|
34 | #include <blitz/numinquire.h> |
---|
35 | |
---|
36 | /* |
---|
37 | * The array expression templates iterator interface is followed by |
---|
38 | * these classes: |
---|
39 | * |
---|
40 | * FastArrayIterator <blitz/array/fastiter.h> |
---|
41 | * _bz_ArrayExpr <blitz/array/expr.h> |
---|
42 | * _bz_ArrayExprUnaryOp " |
---|
43 | * _bz_ArrayExprBinaryOp " |
---|
44 | * _bz_ArrayExprTernaryOp " |
---|
45 | * _bz_ArrayExprConstant " |
---|
46 | * _bz_ArrayMap <blitz/array/map.h> |
---|
47 | * _bz_ArrayExprReduce <blitz/array/reduce.h> |
---|
48 | * IndexPlaceholder <blitz/indexexpr.h> |
---|
49 | */ |
---|
50 | |
---|
51 | BZ_NAMESPACE(blitz) |
---|
52 | |
---|
53 | template<typename T1, typename T2> |
---|
54 | class _bz_ExprPair { |
---|
55 | public: |
---|
56 | _bz_ExprPair(const T1& a, const T2& b) |
---|
57 | : first_(a), second_(b) |
---|
58 | { } |
---|
59 | |
---|
60 | const T1& first() const |
---|
61 | { return first_; } |
---|
62 | |
---|
63 | const T2& second() const |
---|
64 | { return second_; } |
---|
65 | |
---|
66 | protected: |
---|
67 | T1 first_; |
---|
68 | T2 second_; |
---|
69 | }; |
---|
70 | |
---|
71 | template<typename T1, typename T2> |
---|
72 | inline _bz_ExprPair<T1,T2> makeExprPair(const T1& a, const T2& b) |
---|
73 | { |
---|
74 | return _bz_ExprPair<T1,T2>(a,b); |
---|
75 | } |
---|
76 | |
---|
77 | template<typename P_expr> |
---|
78 | class _bz_ArrayExpr |
---|
79 | #ifdef BZ_NEW_EXPRESSION_TEMPLATES |
---|
80 | : public ETBase<_bz_ArrayExpr<P_expr> > |
---|
81 | #endif |
---|
82 | { |
---|
83 | |
---|
84 | public: |
---|
85 | typedef P_expr T_expr; |
---|
86 | typedef _bz_typename T_expr::T_numtype T_numtype; |
---|
87 | typedef T_expr T_ctorArg1; |
---|
88 | typedef int T_ctorArg2; // dummy |
---|
89 | |
---|
90 | static const int |
---|
91 | numArrayOperands = T_expr::numArrayOperands, |
---|
92 | numIndexPlaceholders = T_expr::numIndexPlaceholders, |
---|
93 | rank = T_expr::rank; |
---|
94 | |
---|
95 | _bz_ArrayExpr(const _bz_ArrayExpr<T_expr>& a) |
---|
96 | #ifdef BZ_NEW_EXPRESSION_TEMPLATES |
---|
97 | : ETBase< _bz_ArrayExpr<T_expr> >(a), iter_(a.iter_) |
---|
98 | #else |
---|
99 | : iter_(a.iter_) |
---|
100 | #endif |
---|
101 | { } |
---|
102 | |
---|
103 | #if defined(BZ_NEW_EXPRESSION_TEMPLATES) && ! defined(__MWERKS__) |
---|
104 | template<typename T> |
---|
105 | _bz_ArrayExpr(const T& a) |
---|
106 | : iter_(a) |
---|
107 | { } |
---|
108 | #else |
---|
109 | |
---|
110 | _bz_ArrayExpr(BZ_ETPARM(T_expr) a) |
---|
111 | : iter_(a) |
---|
112 | { } |
---|
113 | #if !defined(__MWERKS__) |
---|
114 | _bz_ArrayExpr(BZ_ETPARM(_bz_typename T_expr::T_ctorArg1) a) |
---|
115 | : iter_(a) |
---|
116 | { } |
---|
117 | #endif |
---|
118 | #endif |
---|
119 | |
---|
120 | template<typename T1, typename T2> |
---|
121 | _bz_ArrayExpr(BZ_ETPARM(T1) a, BZ_ETPARM(T2) b) |
---|
122 | : iter_(a, b) |
---|
123 | { } |
---|
124 | |
---|
125 | template<typename T1, typename T2, typename T3> |
---|
126 | _bz_ArrayExpr(BZ_ETPARM(T1) a, BZ_ETPARM(T2) b, BZ_ETPARM(T3) c) |
---|
127 | : iter_(a, b, c) |
---|
128 | { } |
---|
129 | |
---|
130 | template<typename T1, typename T2, typename T3, typename T4> |
---|
131 | _bz_ArrayExpr(BZ_ETPARM(T1) a, BZ_ETPARM(T2) b, BZ_ETPARM(T3) c, |
---|
132 | BZ_ETPARM(T4) d) : iter_(a, b, c, d) |
---|
133 | { } |
---|
134 | |
---|
135 | template<typename T1, typename T2> |
---|
136 | _bz_ArrayExpr(const _bz_ExprPair<T1,T2>& pair) |
---|
137 | : iter_(pair.first(), pair.second()) |
---|
138 | { } |
---|
139 | |
---|
140 | T_numtype operator*() |
---|
141 | { return *iter_; } |
---|
142 | |
---|
143 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE |
---|
144 | template<int N_rank> |
---|
145 | T_numtype operator()(TinyVector<int, N_rank> i) |
---|
146 | { return iter_(i); } |
---|
147 | #else |
---|
148 | template<int N_rank> |
---|
149 | T_numtype operator()(const TinyVector<int, N_rank>& i) |
---|
150 | { return iter_(i); } |
---|
151 | #endif |
---|
152 | |
---|
153 | int ascending(int rank) |
---|
154 | { return iter_.ascending(rank); } |
---|
155 | |
---|
156 | int ordering(int rank) |
---|
157 | { return iter_.ordering(rank); } |
---|
158 | |
---|
159 | int lbound(int rank) |
---|
160 | { return iter_.lbound(rank); } |
---|
161 | |
---|
162 | int ubound(int rank) |
---|
163 | { return iter_.ubound(rank); } |
---|
164 | |
---|
165 | void push(int position) |
---|
166 | { iter_.push(position); } |
---|
167 | |
---|
168 | void pop(int position) |
---|
169 | { iter_.pop(position); } |
---|
170 | |
---|
171 | void advance() |
---|
172 | { iter_.advance(); } |
---|
173 | |
---|
174 | void advance(int n) |
---|
175 | { iter_.advance(n); } |
---|
176 | |
---|
177 | void loadStride(int rank) |
---|
178 | { iter_.loadStride(rank); } |
---|
179 | |
---|
180 | bool isUnitStride(int rank) const |
---|
181 | { return iter_.isUnitStride(rank); } |
---|
182 | |
---|
183 | void advanceUnitStride() |
---|
184 | { iter_.advanceUnitStride(); } |
---|
185 | |
---|
186 | bool canCollapse(int outerLoopRank, int innerLoopRank) const |
---|
187 | { |
---|
188 | // BZ_DEBUG_MESSAGE("_bz_ArrayExpr<>::canCollapse()"); |
---|
189 | return iter_.canCollapse(outerLoopRank, innerLoopRank); |
---|
190 | } |
---|
191 | |
---|
192 | T_numtype operator[](int i) |
---|
193 | { return iter_[i]; } |
---|
194 | |
---|
195 | T_numtype fastRead(int i) |
---|
196 | { return iter_.fastRead(i); } |
---|
197 | |
---|
198 | int suggestStride(int rank) const |
---|
199 | { return iter_.suggestStride(rank); } |
---|
200 | |
---|
201 | bool isStride(int rank, int stride) const |
---|
202 | { return iter_.isStride(rank,stride); } |
---|
203 | |
---|
204 | void prettyPrint(BZ_STD_SCOPE(string) &str) const |
---|
205 | { |
---|
206 | prettyPrintFormat format(true); // Terse formatting by default |
---|
207 | iter_.prettyPrint(str, format); |
---|
208 | } |
---|
209 | |
---|
210 | void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
211 | prettyPrintFormat& format) const |
---|
212 | { iter_.prettyPrint(str, format); } |
---|
213 | |
---|
214 | template<typename T_shape> |
---|
215 | bool shapeCheck(const T_shape& shape) |
---|
216 | { return iter_.shapeCheck(shape); } |
---|
217 | |
---|
218 | template<int N_rank> |
---|
219 | void moveTo(const TinyVector<int,N_rank>& i) |
---|
220 | { |
---|
221 | iter_.moveTo(i); |
---|
222 | } |
---|
223 | |
---|
224 | protected: |
---|
225 | _bz_ArrayExpr() { } |
---|
226 | |
---|
227 | T_expr iter_; |
---|
228 | }; |
---|
229 | |
---|
230 | struct bounds { |
---|
231 | static int compute_ascending(int BZ_DEBUG_PARAM(rank), |
---|
232 | int ascending1, int ascending2) |
---|
233 | { |
---|
234 | // The value INT_MIN indicates that there are no arrays |
---|
235 | // in a subtree of the expression. This logic returns |
---|
236 | // whichever ascending is available. If there are two |
---|
237 | // conflicting ascending values, this is an error. |
---|
238 | |
---|
239 | if (ascending1 == ascending2) |
---|
240 | return ascending1; |
---|
241 | else if (ascending1 == INT_MIN) |
---|
242 | return ascending2; |
---|
243 | else if (ascending2 == INT_MIN) |
---|
244 | return ascending1; |
---|
245 | |
---|
246 | BZ_DEBUG_MESSAGE("Two array operands have different" |
---|
247 | << endl << "ascending flags: for rank " << rank |
---|
248 | << ", the flags are " << ascending1 << " and " |
---|
249 | << ascending2 << endl); |
---|
250 | BZ_PRE_FAIL; |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | static int compute_ordering(int BZ_DEBUG_PARAM(rank), |
---|
255 | int order1, int order2) |
---|
256 | { |
---|
257 | // The value INT_MIN indicates that there are no arrays |
---|
258 | // in a subtree of the expression. This logic returns |
---|
259 | // whichever ordering is available. If there are two |
---|
260 | // conflicting ordering values, this is an error. |
---|
261 | |
---|
262 | if (order1 == order2) |
---|
263 | return order1; |
---|
264 | else if (order1 == INT_MIN) |
---|
265 | return order2; |
---|
266 | else if (order2 == INT_MIN) |
---|
267 | return order1; |
---|
268 | |
---|
269 | BZ_DEBUG_MESSAGE("Two array operands have different" |
---|
270 | << endl << "orders: for rank " << rank << ", the orders are " |
---|
271 | << order1 << " and " << order2 << endl); |
---|
272 | BZ_PRE_FAIL; |
---|
273 | return 0; |
---|
274 | } |
---|
275 | |
---|
276 | static int compute_lbound(int BZ_DEBUG_PARAM(rank), |
---|
277 | int lbound1, int lbound2) |
---|
278 | { |
---|
279 | // The value INT_MIN indicates that there are no arrays |
---|
280 | // in a subtree of the expression. This logic returns |
---|
281 | // whichever lbound is available. If there are two |
---|
282 | // conflicting lbound values, this is an error. |
---|
283 | |
---|
284 | if (lbound1 == lbound2) |
---|
285 | return lbound1; |
---|
286 | else if (lbound1 == INT_MIN) |
---|
287 | return lbound2; |
---|
288 | else if (lbound2 == INT_MIN) |
---|
289 | return lbound1; |
---|
290 | |
---|
291 | BZ_DEBUG_MESSAGE("Two array operands have different" |
---|
292 | << endl << "lower bounds: in rank " << rank << ", the bounds are " |
---|
293 | << lbound1 << " and " << lbound2 << endl); |
---|
294 | BZ_PRE_FAIL; |
---|
295 | return 0; |
---|
296 | } |
---|
297 | |
---|
298 | static int compute_ubound(int BZ_DEBUG_PARAM(rank), |
---|
299 | int ubound1, int ubound2) |
---|
300 | { |
---|
301 | // The value INT_MAX indicates that there are no arrays |
---|
302 | // in a subtree of the expression. This logic returns |
---|
303 | // whichever ubound is available. If there are two |
---|
304 | // conflicting ubound values, this is an error. |
---|
305 | |
---|
306 | if (ubound1 == ubound2) |
---|
307 | return ubound1; |
---|
308 | else if (ubound1 == INT_MAX) |
---|
309 | return ubound2; |
---|
310 | else if (ubound2 == INT_MAX) |
---|
311 | return ubound1; |
---|
312 | |
---|
313 | BZ_DEBUG_MESSAGE("Two array operands have different" |
---|
314 | << endl << "upper bounds: in rank " << rank << ", the bounds are " |
---|
315 | << ubound1 << " and " << ubound2 << endl); |
---|
316 | BZ_PRE_FAIL; |
---|
317 | return 0; |
---|
318 | } |
---|
319 | }; |
---|
320 | |
---|
321 | template<typename P_expr, typename P_op> |
---|
322 | class _bz_ArrayExprUnaryOp { |
---|
323 | public: |
---|
324 | typedef P_expr T_expr; |
---|
325 | typedef P_op T_op; |
---|
326 | typedef _bz_typename T_expr::T_numtype T_numtype1; |
---|
327 | typedef _bz_typename T_op::T_numtype T_numtype; |
---|
328 | typedef T_expr T_ctorArg1; |
---|
329 | typedef int T_ctorArg2; // dummy |
---|
330 | |
---|
331 | static const int |
---|
332 | numArrayOperands = T_expr::numArrayOperands, |
---|
333 | numIndexPlaceholders = T_expr::numIndexPlaceholders, |
---|
334 | rank = T_expr::rank; |
---|
335 | |
---|
336 | _bz_ArrayExprUnaryOp(const _bz_ArrayExprUnaryOp<T_expr, T_op>& a) |
---|
337 | : iter_(a.iter_) |
---|
338 | { } |
---|
339 | |
---|
340 | _bz_ArrayExprUnaryOp(BZ_ETPARM(T_expr) a) |
---|
341 | : iter_(a) |
---|
342 | { } |
---|
343 | |
---|
344 | _bz_ArrayExprUnaryOp(_bz_typename T_expr::T_ctorArg1 a) |
---|
345 | : iter_(a) |
---|
346 | { } |
---|
347 | |
---|
348 | #if BZ_TEMPLATE_CTOR_DOESNT_CAUSE_HAVOC |
---|
349 | template<typename T1> |
---|
350 | explicit _bz_ArrayExprUnaryOp(BZ_ETPARM(T1) a) |
---|
351 | : iter_(a) |
---|
352 | { } |
---|
353 | #endif |
---|
354 | |
---|
355 | int ascending(int rank) |
---|
356 | { return iter_.ascending(rank); } |
---|
357 | |
---|
358 | int ordering(int rank) |
---|
359 | { return iter_.ordering(rank); } |
---|
360 | |
---|
361 | int lbound(int rank) |
---|
362 | { return iter_.lbound(rank); } |
---|
363 | |
---|
364 | int ubound(int rank) |
---|
365 | { return iter_.ubound(rank); } |
---|
366 | |
---|
367 | T_numtype operator*() |
---|
368 | { return T_op::apply(*iter_); } |
---|
369 | |
---|
370 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE |
---|
371 | template<int N_rank> |
---|
372 | T_numtype operator()(TinyVector<int, N_rank> i) |
---|
373 | { return T_op::apply(iter_(i)); } |
---|
374 | #else |
---|
375 | template<int N_rank> |
---|
376 | T_numtype operator()(const TinyVector<int, N_rank>& i) |
---|
377 | { return T_op::apply(iter_(i)); } |
---|
378 | #endif |
---|
379 | |
---|
380 | void push(int position) |
---|
381 | { |
---|
382 | iter_.push(position); |
---|
383 | } |
---|
384 | |
---|
385 | void pop(int position) |
---|
386 | { |
---|
387 | iter_.pop(position); |
---|
388 | } |
---|
389 | |
---|
390 | void advance() |
---|
391 | { |
---|
392 | iter_.advance(); |
---|
393 | } |
---|
394 | |
---|
395 | void advance(int n) |
---|
396 | { |
---|
397 | iter_.advance(n); |
---|
398 | } |
---|
399 | |
---|
400 | void loadStride(int rank) |
---|
401 | { |
---|
402 | iter_.loadStride(rank); |
---|
403 | } |
---|
404 | |
---|
405 | bool isUnitStride(int rank) const |
---|
406 | { return iter_.isUnitStride(rank); } |
---|
407 | |
---|
408 | void advanceUnitStride() |
---|
409 | { |
---|
410 | iter_.advanceUnitStride(); |
---|
411 | } |
---|
412 | |
---|
413 | template<int N_rank> |
---|
414 | void moveTo(const TinyVector<int,N_rank>& i) |
---|
415 | { |
---|
416 | iter_.moveTo(i); |
---|
417 | } |
---|
418 | |
---|
419 | bool canCollapse(int outerLoopRank, int innerLoopRank) const |
---|
420 | { |
---|
421 | // BZ_DEBUG_MESSAGE("_bz_ArrayExprUnaryOp<>::canCollapse"); |
---|
422 | return iter_.canCollapse(outerLoopRank, innerLoopRank); |
---|
423 | } |
---|
424 | |
---|
425 | T_numtype operator[](int i) |
---|
426 | { return T_op::apply(iter_[i]); } |
---|
427 | |
---|
428 | T_numtype fastRead(int i) |
---|
429 | { return T_op::apply(iter_.fastRead(i)); } |
---|
430 | |
---|
431 | int suggestStride(int rank) const |
---|
432 | { return iter_.suggestStride(rank); } |
---|
433 | |
---|
434 | bool isStride(int rank, int stride) const |
---|
435 | { return iter_.isStride(rank,stride); } |
---|
436 | |
---|
437 | void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
438 | prettyPrintFormat& format) const |
---|
439 | { T_op::prettyPrint(str, format, iter_); } |
---|
440 | |
---|
441 | template<typename T_shape> |
---|
442 | bool shapeCheck(const T_shape& shape) |
---|
443 | { return iter_.shapeCheck(shape); } |
---|
444 | |
---|
445 | protected: |
---|
446 | _bz_ArrayExprUnaryOp() { } |
---|
447 | |
---|
448 | T_expr iter_; |
---|
449 | }; |
---|
450 | |
---|
451 | |
---|
452 | template<typename P_expr1, typename P_expr2, typename P_op> |
---|
453 | class _bz_ArrayExprBinaryOp { |
---|
454 | public: |
---|
455 | typedef P_expr1 T_expr1; |
---|
456 | typedef P_expr2 T_expr2; |
---|
457 | typedef P_op T_op; |
---|
458 | typedef _bz_typename T_expr1::T_numtype T_numtype1; |
---|
459 | typedef _bz_typename T_expr2::T_numtype T_numtype2; |
---|
460 | typedef _bz_typename T_op::T_numtype T_numtype; |
---|
461 | typedef T_expr1 T_ctorArg1; |
---|
462 | typedef T_expr2 T_ctorArg2; |
---|
463 | |
---|
464 | static const int |
---|
465 | numArrayOperands = T_expr1::numArrayOperands |
---|
466 | + T_expr2::numArrayOperands, |
---|
467 | numIndexPlaceholders = T_expr1::numIndexPlaceholders |
---|
468 | + T_expr2::numIndexPlaceholders, |
---|
469 | rank = (T_expr1::rank > T_expr2::rank) |
---|
470 | ? T_expr1::rank : T_expr2::rank; |
---|
471 | |
---|
472 | _bz_ArrayExprBinaryOp( |
---|
473 | const _bz_ArrayExprBinaryOp<T_expr1, T_expr2, T_op>& a) |
---|
474 | : iter1_(a.iter1_), iter2_(a.iter2_) |
---|
475 | { } |
---|
476 | |
---|
477 | template<typename T1, typename T2> |
---|
478 | _bz_ArrayExprBinaryOp(BZ_ETPARM(T1) a, BZ_ETPARM(T2) b) |
---|
479 | : iter1_(a), iter2_(b) |
---|
480 | { } |
---|
481 | |
---|
482 | T_numtype operator*() |
---|
483 | { return T_op::apply(*iter1_, *iter2_); } |
---|
484 | |
---|
485 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE |
---|
486 | template<int N_rank> |
---|
487 | T_numtype operator()(TinyVector<int, N_rank> i) |
---|
488 | { return T_op::apply(iter1_(i), iter2_(i)); } |
---|
489 | #else |
---|
490 | template<int N_rank> |
---|
491 | T_numtype operator()(const TinyVector<int, N_rank>& i) |
---|
492 | { return T_op::apply(iter1_(i), iter2_(i)); } |
---|
493 | #endif |
---|
494 | |
---|
495 | int ascending(int rank) |
---|
496 | { |
---|
497 | return bounds::compute_ascending(rank, iter1_.ascending(rank), |
---|
498 | iter2_.ascending(rank)); |
---|
499 | } |
---|
500 | |
---|
501 | int ordering(int rank) |
---|
502 | { |
---|
503 | return bounds::compute_ordering(rank, iter1_.ordering(rank), |
---|
504 | iter2_.ordering(rank)); |
---|
505 | } |
---|
506 | |
---|
507 | int lbound(int rank) |
---|
508 | { |
---|
509 | return bounds::compute_lbound(rank, iter1_.lbound(rank), |
---|
510 | iter2_.lbound(rank)); |
---|
511 | } |
---|
512 | |
---|
513 | int ubound(int rank) |
---|
514 | { |
---|
515 | return bounds::compute_ubound(rank, iter1_.ubound(rank), |
---|
516 | iter2_.ubound(rank)); |
---|
517 | } |
---|
518 | |
---|
519 | void push(int position) |
---|
520 | { |
---|
521 | iter1_.push(position); |
---|
522 | iter2_.push(position); |
---|
523 | } |
---|
524 | |
---|
525 | void pop(int position) |
---|
526 | { |
---|
527 | iter1_.pop(position); |
---|
528 | iter2_.pop(position); |
---|
529 | } |
---|
530 | |
---|
531 | void advance() |
---|
532 | { |
---|
533 | iter1_.advance(); |
---|
534 | iter2_.advance(); |
---|
535 | } |
---|
536 | |
---|
537 | void advance(int n) |
---|
538 | { |
---|
539 | iter1_.advance(n); |
---|
540 | iter2_.advance(n); |
---|
541 | } |
---|
542 | |
---|
543 | void loadStride(int rank) |
---|
544 | { |
---|
545 | iter1_.loadStride(rank); |
---|
546 | iter2_.loadStride(rank); |
---|
547 | } |
---|
548 | |
---|
549 | bool isUnitStride(int rank) const |
---|
550 | { return iter1_.isUnitStride(rank) && iter2_.isUnitStride(rank); } |
---|
551 | |
---|
552 | void advanceUnitStride() |
---|
553 | { |
---|
554 | iter1_.advanceUnitStride(); |
---|
555 | iter2_.advanceUnitStride(); |
---|
556 | } |
---|
557 | |
---|
558 | bool canCollapse(int outerLoopRank, int innerLoopRank) const |
---|
559 | { |
---|
560 | // BZ_DEBUG_MESSAGE("_bz_ArrayExprBinaryOp<>::canCollapse"); |
---|
561 | return iter1_.canCollapse(outerLoopRank, innerLoopRank) |
---|
562 | && iter2_.canCollapse(outerLoopRank, innerLoopRank); |
---|
563 | } |
---|
564 | |
---|
565 | T_numtype operator[](int i) |
---|
566 | { return T_op::apply(iter1_[i], iter2_[i]); } |
---|
567 | |
---|
568 | T_numtype fastRead(int i) |
---|
569 | { return T_op::apply(iter1_.fastRead(i), iter2_.fastRead(i)); } |
---|
570 | |
---|
571 | int suggestStride(int rank) const |
---|
572 | { |
---|
573 | int stride1 = iter1_.suggestStride(rank); |
---|
574 | int stride2 = iter2_.suggestStride(rank); |
---|
575 | return (stride1 > stride2) ? stride1 : stride2; |
---|
576 | } |
---|
577 | |
---|
578 | bool isStride(int rank, int stride) const |
---|
579 | { |
---|
580 | return iter1_.isStride(rank,stride) && iter2_.isStride(rank,stride); |
---|
581 | } |
---|
582 | |
---|
583 | template<int N_rank> |
---|
584 | void moveTo(const TinyVector<int,N_rank>& i) |
---|
585 | { |
---|
586 | iter1_.moveTo(i); |
---|
587 | iter2_.moveTo(i); |
---|
588 | } |
---|
589 | |
---|
590 | void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
591 | prettyPrintFormat& format) const |
---|
592 | { |
---|
593 | T_op::prettyPrint(str, format, iter1_, iter2_); |
---|
594 | } |
---|
595 | |
---|
596 | template<typename T_shape> |
---|
597 | bool shapeCheck(const T_shape& shape) |
---|
598 | { return iter1_.shapeCheck(shape) && iter2_.shapeCheck(shape); } |
---|
599 | |
---|
600 | protected: |
---|
601 | _bz_ArrayExprBinaryOp() { } |
---|
602 | |
---|
603 | T_expr1 iter1_; |
---|
604 | T_expr2 iter2_; |
---|
605 | }; |
---|
606 | |
---|
607 | template<typename P_expr1, typename P_expr2, typename P_expr3, typename P_op> |
---|
608 | class _bz_ArrayExprTernaryOp { |
---|
609 | public: |
---|
610 | typedef P_expr1 T_expr1; |
---|
611 | typedef P_expr2 T_expr2; |
---|
612 | typedef P_expr3 T_expr3; |
---|
613 | typedef P_op T_op; |
---|
614 | typedef _bz_typename T_expr1::T_numtype T_numtype1; |
---|
615 | typedef _bz_typename T_expr2::T_numtype T_numtype2; |
---|
616 | typedef _bz_typename T_expr3::T_numtype T_numtype3; |
---|
617 | typedef _bz_typename T_op::T_numtype T_numtype; |
---|
618 | typedef T_expr1 T_ctorArg1; |
---|
619 | typedef T_expr2 T_ctorArg2; |
---|
620 | typedef T_expr3 T_ctorArg3; |
---|
621 | |
---|
622 | static const int |
---|
623 | numArrayOperands = T_expr1::numArrayOperands |
---|
624 | + T_expr2::numArrayOperands |
---|
625 | + T_expr3::numArrayOperands, |
---|
626 | numIndexPlaceholders = T_expr1::numIndexPlaceholders |
---|
627 | + T_expr2::numIndexPlaceholders |
---|
628 | + T_expr3::numIndexPlaceholders, |
---|
629 | rank = (T_expr1::rank > T_expr2::rank) |
---|
630 | ? ((T_expr1::rank > T_expr3::rank) |
---|
631 | ? T_expr1::rank : T_expr3::rank) |
---|
632 | : ((T_expr2::rank > T_expr3::rank) |
---|
633 | ? T_expr2::rank : T_expr3::rank); |
---|
634 | |
---|
635 | _bz_ArrayExprTernaryOp( |
---|
636 | const _bz_ArrayExprTernaryOp<T_expr1, T_expr2, T_expr3, T_op>& a) |
---|
637 | : iter1_(a.iter1_), iter2_(a.iter2_), iter3_(a.iter3_) |
---|
638 | { } |
---|
639 | |
---|
640 | template<typename T1, typename T2, typename T3> |
---|
641 | _bz_ArrayExprTernaryOp(BZ_ETPARM(T1) a, BZ_ETPARM(T2) b, BZ_ETPARM(T3) c) |
---|
642 | : iter1_(a), iter2_(b), iter3_(c) |
---|
643 | { } |
---|
644 | |
---|
645 | T_numtype operator*() |
---|
646 | { return T_op::apply(*iter1_, *iter2_, *iter3_); } |
---|
647 | |
---|
648 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE |
---|
649 | template<int N_rank> |
---|
650 | T_numtype operator()(TinyVector<int, N_rank> i) |
---|
651 | { return T_op::apply(iter1_(i), iter2_(i), iter3_(i)); } |
---|
652 | #else |
---|
653 | template<int N_rank> |
---|
654 | T_numtype operator()(const TinyVector<int, N_rank>& i) |
---|
655 | { return T_op::apply(iter1_(i), iter2_(i), iter3_(i)); } |
---|
656 | #endif |
---|
657 | |
---|
658 | int ascending(int rank) |
---|
659 | { |
---|
660 | return bounds::compute_ascending(rank, bounds::compute_ascending( |
---|
661 | rank, iter1_.ascending(rank), iter2_.ascending(rank)), |
---|
662 | iter3_.ascending(rank)); |
---|
663 | } |
---|
664 | |
---|
665 | int ordering(int rank) |
---|
666 | { |
---|
667 | return bounds::compute_ordering(rank, bounds::compute_ordering( |
---|
668 | rank, iter1_.ordering(rank), iter2_.ordering(rank)), |
---|
669 | iter3_.ordering(rank)); |
---|
670 | } |
---|
671 | |
---|
672 | int lbound(int rank) |
---|
673 | { |
---|
674 | return bounds::compute_lbound(rank, bounds::compute_lbound( |
---|
675 | rank, iter1_.lbound(rank), iter2_.lbound(rank)), |
---|
676 | iter3_.lbound(rank)); |
---|
677 | } |
---|
678 | |
---|
679 | int ubound(int rank) |
---|
680 | { |
---|
681 | return bounds::compute_ubound(rank, bounds::compute_ubound( |
---|
682 | rank, iter1_.ubound(rank), iter2_.ubound(rank)), |
---|
683 | iter3_.ubound(rank)); |
---|
684 | } |
---|
685 | |
---|
686 | void push(int position) |
---|
687 | { |
---|
688 | iter1_.push(position); |
---|
689 | iter2_.push(position); |
---|
690 | iter3_.push(position); |
---|
691 | } |
---|
692 | |
---|
693 | void pop(int position) |
---|
694 | { |
---|
695 | iter1_.pop(position); |
---|
696 | iter2_.pop(position); |
---|
697 | iter3_.pop(position); |
---|
698 | } |
---|
699 | |
---|
700 | void advance() |
---|
701 | { |
---|
702 | iter1_.advance(); |
---|
703 | iter2_.advance(); |
---|
704 | iter3_.advance(); |
---|
705 | } |
---|
706 | |
---|
707 | void advance(int n) |
---|
708 | { |
---|
709 | iter1_.advance(n); |
---|
710 | iter2_.advance(n); |
---|
711 | iter3_.advance(n); |
---|
712 | } |
---|
713 | |
---|
714 | void loadStride(int rank) |
---|
715 | { |
---|
716 | iter1_.loadStride(rank); |
---|
717 | iter2_.loadStride(rank); |
---|
718 | iter3_.loadStride(rank); |
---|
719 | } |
---|
720 | |
---|
721 | bool isUnitStride(int rank) const |
---|
722 | { |
---|
723 | return iter1_.isUnitStride(rank) |
---|
724 | && iter2_.isUnitStride(rank) |
---|
725 | && iter3_.isUnitStride(rank); |
---|
726 | } |
---|
727 | |
---|
728 | void advanceUnitStride() |
---|
729 | { |
---|
730 | iter1_.advanceUnitStride(); |
---|
731 | iter2_.advanceUnitStride(); |
---|
732 | iter3_.advanceUnitStride(); |
---|
733 | } |
---|
734 | |
---|
735 | bool canCollapse(int outerLoopRank, int innerLoopRank) const |
---|
736 | { |
---|
737 | // BZ_DEBUG_MESSAGE("_bz_ArrayExprTernaryOp<>::canCollapse"); |
---|
738 | return iter1_.canCollapse(outerLoopRank, innerLoopRank) |
---|
739 | && iter2_.canCollapse(outerLoopRank, innerLoopRank) |
---|
740 | && iter3_.canCollapse(outerLoopRank, innerLoopRank); |
---|
741 | } |
---|
742 | |
---|
743 | T_numtype operator[](int i) |
---|
744 | { return T_op::apply(iter1_[i], iter2_[i], iter3_[i]); } |
---|
745 | |
---|
746 | T_numtype fastRead(int i) |
---|
747 | { |
---|
748 | return T_op::apply(iter1_.fastRead(i), |
---|
749 | iter2_.fastRead(i), |
---|
750 | iter3_.fastRead(i)); |
---|
751 | } |
---|
752 | |
---|
753 | int suggestStride(int rank) const |
---|
754 | { |
---|
755 | int stride1 = iter1_.suggestStride(rank); |
---|
756 | int stride2 = iter2_.suggestStride(rank); |
---|
757 | int stride3 = iter3_.suggestStride(rank); |
---|
758 | return stride1 > ( stride2 = (stride2>stride3 ? stride2 : stride3) ) ? |
---|
759 | stride1 : stride2; |
---|
760 | } |
---|
761 | |
---|
762 | bool isStride(int rank, int stride) const |
---|
763 | { |
---|
764 | return iter1_.isStride(rank,stride) |
---|
765 | && iter2_.isStride(rank,stride) |
---|
766 | && iter3_.isStride(rank,stride); |
---|
767 | } |
---|
768 | |
---|
769 | template<int N_rank> |
---|
770 | void moveTo(const TinyVector<int,N_rank>& i) |
---|
771 | { |
---|
772 | iter1_.moveTo(i); |
---|
773 | iter2_.moveTo(i); |
---|
774 | iter3_.moveTo(i); |
---|
775 | } |
---|
776 | |
---|
777 | void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
778 | prettyPrintFormat& format) const |
---|
779 | { |
---|
780 | T_op::prettyPrint(str, format, iter1_, iter2_, iter3_); |
---|
781 | } |
---|
782 | |
---|
783 | template<typename T_shape> |
---|
784 | bool shapeCheck(const T_shape& shape) |
---|
785 | { |
---|
786 | return iter1_.shapeCheck(shape) |
---|
787 | && iter2_.shapeCheck(shape) |
---|
788 | && iter3_.shapeCheck(shape); |
---|
789 | } |
---|
790 | |
---|
791 | protected: |
---|
792 | _bz_ArrayExprTernaryOp() { } |
---|
793 | |
---|
794 | T_expr1 iter1_; |
---|
795 | T_expr2 iter2_; |
---|
796 | T_expr3 iter3_; |
---|
797 | }; |
---|
798 | |
---|
799 | |
---|
800 | template<typename P_numtype> |
---|
801 | class _bz_ArrayExprConstant { |
---|
802 | public: |
---|
803 | typedef P_numtype T_numtype; |
---|
804 | typedef T_numtype T_ctorArg1; |
---|
805 | typedef int T_ctorArg2; // dummy |
---|
806 | |
---|
807 | static const int |
---|
808 | numArrayOperands = 0, |
---|
809 | numIndexPlaceholders = 0, |
---|
810 | rank = 0; |
---|
811 | |
---|
812 | _bz_ArrayExprConstant(const _bz_ArrayExprConstant<T_numtype>& a) |
---|
813 | : value_(a.value_) |
---|
814 | { } |
---|
815 | |
---|
816 | _bz_ArrayExprConstant(T_numtype value) |
---|
817 | : value_(BZ_NO_PROPAGATE(value)) |
---|
818 | { |
---|
819 | } |
---|
820 | |
---|
821 | // tiny() and huge() return the smallest and largest representable |
---|
822 | // integer values. See <blitz/numinquire.h> |
---|
823 | // NEEDS_WORK: use tiny(int()) once numeric_limits<T> available on |
---|
824 | // all platforms |
---|
825 | |
---|
826 | int ascending(int) |
---|
827 | { return INT_MIN; } |
---|
828 | |
---|
829 | int ordering(int) |
---|
830 | { return INT_MIN; } |
---|
831 | |
---|
832 | int lbound(int) |
---|
833 | { return INT_MIN; } |
---|
834 | |
---|
835 | int ubound(int) |
---|
836 | { return INT_MAX; } |
---|
837 | // NEEDS_WORK: use huge(int()) once numeric_limits<T> available on |
---|
838 | // all platforms |
---|
839 | |
---|
840 | T_numtype operator*() |
---|
841 | { return value_; } |
---|
842 | |
---|
843 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE |
---|
844 | template<int N_rank> |
---|
845 | T_numtype operator()(TinyVector<int,N_rank>) |
---|
846 | { return value_; } |
---|
847 | #else |
---|
848 | template<int N_rank> |
---|
849 | T_numtype operator()(const TinyVector<int,N_rank>&) |
---|
850 | { return value_; } |
---|
851 | #endif |
---|
852 | |
---|
853 | void push(int) { } |
---|
854 | void pop(int) { } |
---|
855 | void advance() { } |
---|
856 | void advance(int) { } |
---|
857 | void loadStride(int) { } |
---|
858 | |
---|
859 | bool isUnitStride(int) const |
---|
860 | { return true; } |
---|
861 | |
---|
862 | void advanceUnitStride() |
---|
863 | { } |
---|
864 | |
---|
865 | bool canCollapse(int,int) const |
---|
866 | { return true; } |
---|
867 | |
---|
868 | T_numtype operator[](int) |
---|
869 | { return value_; } |
---|
870 | |
---|
871 | T_numtype fastRead(int) |
---|
872 | { return value_; } |
---|
873 | |
---|
874 | int suggestStride(int) const |
---|
875 | { return 1; } |
---|
876 | |
---|
877 | bool isStride(int,int) const |
---|
878 | { return true; } |
---|
879 | |
---|
880 | template<int N_rank> |
---|
881 | void moveTo(const TinyVector<int,N_rank>&) |
---|
882 | { |
---|
883 | } |
---|
884 | |
---|
885 | void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
886 | prettyPrintFormat& format) const |
---|
887 | { |
---|
888 | if (format.tersePrintingSelected()) |
---|
889 | str += format.nextScalarOperandSymbol(); |
---|
890 | else |
---|
891 | str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype); |
---|
892 | } |
---|
893 | |
---|
894 | template<typename T_shape> |
---|
895 | bool shapeCheck(const T_shape&) |
---|
896 | { return true; } |
---|
897 | |
---|
898 | protected: |
---|
899 | _bz_ArrayExprConstant() { } |
---|
900 | |
---|
901 | T_numtype value_; |
---|
902 | }; |
---|
903 | |
---|
904 | BZ_NAMESPACE_END |
---|
905 | |
---|
906 | #include <blitz/array/asexpr.h> |
---|
907 | |
---|
908 | #endif // BZ_ARRAYEXPR_H |
---|
909 | |
---|