1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/vecexpr.h Vector<P_numtype> expression templates |
---|
4 | * |
---|
5 | * $Id: vecexpr.h,v 1.11 2005/10/06 23:30:51 julianc Exp $ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * Suggestions: blitz-dev@oonumerics.org |
---|
20 | * Bugs: blitz-bugs@oonumerics.org |
---|
21 | * |
---|
22 | * For more information, please see the Blitz++ Home Page: |
---|
23 | * http://oonumerics.org/blitz/ |
---|
24 | * |
---|
25 | ***************************************************************************/ |
---|
26 | |
---|
27 | |
---|
28 | #ifndef BZ_VECEXPR_H |
---|
29 | #define BZ_VECEXPR_H |
---|
30 | |
---|
31 | #include <blitz/vector.h> |
---|
32 | #include <blitz/applics.h> |
---|
33 | #include <blitz/meta/metaprog.h> |
---|
34 | #include <blitz/vecexprwrap.h> // _bz_VecExpr wrapper class |
---|
35 | |
---|
36 | BZ_NAMESPACE(blitz) |
---|
37 | |
---|
38 | template<typename P_expr1, typename P_expr2, typename P_op> |
---|
39 | class _bz_VecExprOp { |
---|
40 | |
---|
41 | public: |
---|
42 | typedef P_expr1 T_expr1; |
---|
43 | typedef P_expr2 T_expr2; |
---|
44 | typedef _bz_typename T_expr1::T_numtype T_numtype1; |
---|
45 | typedef _bz_typename T_expr2::T_numtype T_numtype2; |
---|
46 | typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype; |
---|
47 | typedef P_op T_op; |
---|
48 | |
---|
49 | #ifdef BZ_PASS_EXPR_BY_VALUE |
---|
50 | _bz_VecExprOp(T_expr1 a, T_expr2 b) |
---|
51 | : iter1_(a), iter2_(b) |
---|
52 | { } |
---|
53 | #else |
---|
54 | _bz_VecExprOp(const T_expr1& a, const T_expr2& b) |
---|
55 | : iter1_(a), iter2_(b) |
---|
56 | { } |
---|
57 | #endif |
---|
58 | |
---|
59 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR |
---|
60 | _bz_VecExprOp(const _bz_VecExprOp<P_expr1, P_expr2, P_op>& x) |
---|
61 | : iter1_(x.iter1_), iter2_(x.iter2_) |
---|
62 | { } |
---|
63 | #endif |
---|
64 | |
---|
65 | T_numtype operator[](int i) const |
---|
66 | { return T_op::apply(iter1_[i], iter2_[i]); } |
---|
67 | |
---|
68 | T_numtype operator()(int i) const |
---|
69 | { return T_op::apply(iter1_(i), iter2_(i)); } |
---|
70 | |
---|
71 | int length(int recommendedLength) const |
---|
72 | { |
---|
73 | BZPRECONDITION(iter2_.length(recommendedLength) == |
---|
74 | iter1_.length(recommendedLength)); |
---|
75 | return iter1_.length(recommendedLength); |
---|
76 | } |
---|
77 | |
---|
78 | static const int |
---|
79 | _bz_staticLengthCount = P_expr1::_bz_staticLengthCount |
---|
80 | + P_expr2::_bz_staticLengthCount, |
---|
81 | _bz_dynamicLengthCount = P_expr1::_bz_dynamicLengthCount |
---|
82 | + P_expr2::_bz_dynamicLengthCount, |
---|
83 | _bz_staticLength = |
---|
84 | (P_expr1::_bz_staticLength > P_expr2::_bz_staticLength) ? |
---|
85 | P_expr1::_bz_staticLength : P_expr2::_bz_staticLength; |
---|
86 | |
---|
87 | // _bz_meta_max<P_expr1::_bz_staticLength, P_expr2::_bz_staticLength>::max |
---|
88 | |
---|
89 | int _bz_suggestLength() const |
---|
90 | { |
---|
91 | int length1 = iter1_._bz_suggestLength(); |
---|
92 | if (length1 != 0) |
---|
93 | return length1; |
---|
94 | return iter2_._bz_suggestLength(); |
---|
95 | } |
---|
96 | |
---|
97 | bool _bz_hasFastAccess() const |
---|
98 | { return iter1_._bz_hasFastAccess() && iter2_._bz_hasFastAccess(); } |
---|
99 | |
---|
100 | T_numtype _bz_fastAccess(int i) const |
---|
101 | { |
---|
102 | return T_op::apply(iter1_._bz_fastAccess(i), |
---|
103 | iter2_._bz_fastAccess(i)); |
---|
104 | } |
---|
105 | |
---|
106 | private: |
---|
107 | _bz_VecExprOp(); |
---|
108 | |
---|
109 | T_expr1 iter1_; |
---|
110 | T_expr2 iter2_; |
---|
111 | }; |
---|
112 | |
---|
113 | template<typename P_expr, typename P_unaryOp> |
---|
114 | class _bz_VecExprUnaryOp { |
---|
115 | |
---|
116 | public: |
---|
117 | typedef P_expr T_expr; |
---|
118 | typedef P_unaryOp T_unaryOp; |
---|
119 | typedef _bz_typename T_unaryOp::T_numtype T_numtype; |
---|
120 | |
---|
121 | #ifdef BZ_PASS_EXPR_BY_VALUE |
---|
122 | _bz_VecExprUnaryOp(T_expr iter) |
---|
123 | : iter_(iter) |
---|
124 | { } |
---|
125 | #else |
---|
126 | _bz_VecExprUnaryOp(const T_expr& iter) |
---|
127 | : iter_(iter) |
---|
128 | { } |
---|
129 | #endif |
---|
130 | |
---|
131 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR |
---|
132 | _bz_VecExprUnaryOp(const _bz_VecExprUnaryOp<P_expr, P_unaryOp>& x) |
---|
133 | : iter_(x.iter_) |
---|
134 | { } |
---|
135 | #endif |
---|
136 | |
---|
137 | T_numtype operator[](int i) const |
---|
138 | { return T_unaryOp::apply(iter_[i]); } |
---|
139 | |
---|
140 | T_numtype operator()(int i) const |
---|
141 | { return T_unaryOp::apply(iter_(i)); } |
---|
142 | |
---|
143 | int length(int recommendedLength) const |
---|
144 | { return iter_.length(recommendedLength); } |
---|
145 | |
---|
146 | static const int |
---|
147 | _bz_staticLengthCount = P_expr::_bz_staticLengthCount, |
---|
148 | _bz_dynamicLengthCount = P_expr::_bz_dynamicLengthCount, |
---|
149 | _bz_staticLength = P_expr::_bz_staticLength; |
---|
150 | |
---|
151 | int _bz_suggestLength() const |
---|
152 | { return iter_._bz_suggestLength(); } |
---|
153 | |
---|
154 | bool _bz_hasFastAccess() const |
---|
155 | { return iter_._bz_hasFastAccess(); } |
---|
156 | |
---|
157 | T_numtype _bz_fastAccess(int i) const |
---|
158 | { return T_unaryOp::apply(iter_._bz_fastAccess(i)); } |
---|
159 | |
---|
160 | private: |
---|
161 | _bz_VecExprUnaryOp() { } |
---|
162 | |
---|
163 | T_expr iter_; |
---|
164 | }; |
---|
165 | |
---|
166 | template<typename P_numtype> |
---|
167 | class _bz_VecExprConstant { |
---|
168 | public: |
---|
169 | typedef P_numtype T_numtype; |
---|
170 | |
---|
171 | _bz_VecExprConstant(P_numtype value) |
---|
172 | : value_(BZ_NO_PROPAGATE(value)) |
---|
173 | { |
---|
174 | } |
---|
175 | |
---|
176 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR |
---|
177 | _bz_VecExprConstant(const _bz_VecExprConstant<P_numtype>& x) |
---|
178 | : value_(x.value_) |
---|
179 | { } |
---|
180 | #endif |
---|
181 | |
---|
182 | T_numtype operator[](int) const |
---|
183 | { return value_; } |
---|
184 | |
---|
185 | T_numtype operator()(int) const |
---|
186 | { return value_; } |
---|
187 | |
---|
188 | int length(int recommendedLength) const |
---|
189 | { return recommendedLength; } |
---|
190 | |
---|
191 | static const int |
---|
192 | _bz_staticLengthCount = 0, |
---|
193 | _bz_dynamicLengthCount = 0, |
---|
194 | _bz_staticLength = 0; |
---|
195 | |
---|
196 | int _bz_suggestLength() const |
---|
197 | { return 0; } |
---|
198 | |
---|
199 | bool _bz_hasFastAccess() const |
---|
200 | { return 1; } |
---|
201 | |
---|
202 | T_numtype _bz_fastAccess(int) const |
---|
203 | { return value_; } |
---|
204 | |
---|
205 | private: |
---|
206 | |
---|
207 | _bz_VecExprConstant() { } |
---|
208 | |
---|
209 | T_numtype value_; |
---|
210 | }; |
---|
211 | |
---|
212 | BZ_NAMESPACE_END |
---|
213 | |
---|
214 | |
---|
215 | #ifndef BZ_TINYVEC_H |
---|
216 | #include <blitz/tinyvec.h> |
---|
217 | #endif |
---|
218 | |
---|
219 | |
---|
220 | BZ_NAMESPACE(blitz) |
---|
221 | |
---|
222 | // Some miscellaneous operators that don't seem to belong anywhere else. |
---|
223 | |
---|
224 | template<typename P_expr> |
---|
225 | inline |
---|
226 | _bz_VecExpr<_bz_VecExprUnaryOp<_bz_VecExpr<P_expr>, |
---|
227 | _bz_negate<_bz_typename P_expr::T_numtype> > > |
---|
228 | operator-(_bz_VecExpr<P_expr> a) |
---|
229 | { |
---|
230 | typedef _bz_VecExprUnaryOp<_bz_VecExpr<P_expr>, |
---|
231 | _bz_negate<_bz_typename P_expr::T_numtype> > T_expr; |
---|
232 | return _bz_VecExpr<T_expr>(T_expr(a)); |
---|
233 | } |
---|
234 | |
---|
235 | template<typename P_numtype> |
---|
236 | inline |
---|
237 | _bz_VecExpr<_bz_VecExprUnaryOp<VectorIterConst<P_numtype>, |
---|
238 | _bz_negate<P_numtype> > > |
---|
239 | operator-(const Vector<P_numtype>& a) |
---|
240 | { |
---|
241 | typedef _bz_VecExprUnaryOp<VectorIterConst<P_numtype>, |
---|
242 | _bz_negate<P_numtype> > T_expr; |
---|
243 | |
---|
244 | return _bz_VecExpr<T_expr>(T_expr(a.beginFast())); |
---|
245 | } |
---|
246 | |
---|
247 | inline |
---|
248 | _bz_VecExpr<_bz_VecExprUnaryOp<Range, _bz_negate<Range::T_numtype> > > |
---|
249 | operator-(Range r) |
---|
250 | { |
---|
251 | typedef _bz_VecExprUnaryOp<Range, _bz_negate<Range::T_numtype> > T_expr; |
---|
252 | |
---|
253 | return _bz_VecExpr<T_expr>(T_expr(r)); |
---|
254 | } |
---|
255 | |
---|
256 | template<typename P_numtype> |
---|
257 | inline |
---|
258 | _bz_VecExpr<_bz_VecExprUnaryOp<VectorPickIterConst<P_numtype>, |
---|
259 | _bz_negate<P_numtype> > > |
---|
260 | operator-(const VectorPick<P_numtype>& a) |
---|
261 | { |
---|
262 | typedef _bz_VecExprUnaryOp<VectorPickIterConst<P_numtype>, |
---|
263 | _bz_negate<P_numtype> > T_expr; |
---|
264 | |
---|
265 | return _bz_VecExpr<T_expr>(T_expr(a.beginFast())); |
---|
266 | } |
---|
267 | |
---|
268 | template<typename P_numtype, int N_length> |
---|
269 | inline |
---|
270 | _bz_VecExpr<_bz_VecExprUnaryOp<TinyVectorIterConst<P_numtype,N_length>, |
---|
271 | _bz_negate<P_numtype> > > |
---|
272 | operator-(const TinyVector<P_numtype,N_length>& a) |
---|
273 | { |
---|
274 | typedef _bz_VecExprUnaryOp<TinyVectorIterConst<P_numtype,N_length>, |
---|
275 | _bz_negate<P_numtype> > T_expr; |
---|
276 | |
---|
277 | return _bz_VecExpr<T_expr>(T_expr(a.beginFast())); |
---|
278 | } |
---|
279 | |
---|
280 | BZ_NAMESPACE_END |
---|
281 | |
---|
282 | #endif // BZ_VECEXPR_H |
---|