1 | /*************************************************************************** |
---|
2 | * blitz/array/ops.cc Basic operators for arrays. |
---|
3 | * |
---|
4 | * $Id$ |
---|
5 | * |
---|
6 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
7 | * |
---|
8 | * This file is a part of Blitz. |
---|
9 | * |
---|
10 | * Blitz is free software: you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU Lesser General Public License |
---|
12 | * as published by the Free Software Foundation, either version 3 |
---|
13 | * of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * Blitz is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU Lesser General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU Lesser General Public |
---|
21 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | * |
---|
23 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
24 | * Bugs: blitz-support@lists.sourceforge.net |
---|
25 | * |
---|
26 | * For more information, please see the Blitz++ Home Page: |
---|
27 | * https://sourceforge.net/projects/blitz/ |
---|
28 | * |
---|
29 | ****************************************************************************/ |
---|
30 | #ifndef BZ_ARRAYOPS_CC |
---|
31 | #define BZ_ARRAYOPS_CC |
---|
32 | |
---|
33 | #ifndef BZ_ARRAY_H |
---|
34 | #error <blitz/array/ops.cc> must be included via <blitz/array.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | #include <blitz/update.h> |
---|
38 | #include <blitz/globeval.cc> |
---|
39 | |
---|
40 | BZ_NAMESPACE(blitz) |
---|
41 | |
---|
42 | /* |
---|
43 | * Constant operands |
---|
44 | */ |
---|
45 | |
---|
46 | template<typename P_numtype, int N_rank> |
---|
47 | _bz_forceinline |
---|
48 | Array<P_numtype, N_rank>& Array<P_numtype,N_rank>::initialize(T_numtype x) |
---|
49 | { |
---|
50 | // we can't use asExpr here, because if we are initializing an array |
---|
51 | // whose components are also ETBase, it would parse as an array |
---|
52 | // expression, not as an initialization with a scalar. |
---|
53 | (*this) = _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
54 | return *this; |
---|
55 | } |
---|
56 | |
---|
57 | #ifdef BZ_NEW_EXPRESSION_TEMPLATES |
---|
58 | |
---|
59 | // need to do operator= separately from the generic templates below so |
---|
60 | // that operator=(T_numtype) will be the best match for list |
---|
61 | // initializations. |
---|
62 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
63 | _bz_forceinline |
---|
64 | Array<P_numtype,N_rank>& |
---|
65 | Array<P_numtype,N_rank>::operator=(const ETBase<T_expr>& expr) |
---|
66 | { |
---|
67 | _bz_evaluate(*this, _bz_typename asExpr<T_expr>::T_expr(expr.unwrap()), |
---|
68 | _bz_update<T_numtype, |
---|
69 | _bz_typename asExpr<T_expr>::T_expr::T_result>()); |
---|
70 | return *this; |
---|
71 | } |
---|
72 | |
---|
73 | // do NOT remove this operator. it won't work without it, trust me... |
---|
74 | template<typename P_numtype, int N_rank> |
---|
75 | _bz_forceinline |
---|
76 | Array<P_numtype, N_rank>& |
---|
77 | Array<P_numtype, N_rank>::operator=(const Array<T_numtype,N_rank>& x) |
---|
78 | { |
---|
79 | typedef typename asExpr<Array<T_numtype,N_rank> >::T_expr T_expr; |
---|
80 | _bz_evaluate(*this, asExpr<Array<T_numtype,N_rank> >::getExpr(x), |
---|
81 | _bz_update<T_numtype, |
---|
82 | _bz_typename T_expr::T_result>()); |
---|
83 | return *this; |
---|
84 | } |
---|
85 | |
---|
86 | #define BZ_ARRAY_UPDATE(op,name) \ |
---|
87 | template<typename P_numtype, int N_rank> \ |
---|
88 | template<typename T_expr> \ |
---|
89 | _bz_forceinline \ |
---|
90 | Array<P_numtype,N_rank>& \ |
---|
91 | Array<P_numtype,N_rank>::operator op(const ETBase<T_expr>& expr) \ |
---|
92 | { \ |
---|
93 | _bz_evaluate(*this, _bz_typename asExpr<T_expr>::T_expr(expr.unwrap()), \ |
---|
94 | name<T_numtype, _bz_typename asExpr<T_expr>::T_expr::T_result>()); \ |
---|
95 | return *this; \ |
---|
96 | } \ |
---|
97 | template<typename P_numtype, int N_rank> \ |
---|
98 | _bz_forceinline \ |
---|
99 | Array<P_numtype,N_rank>& \ |
---|
100 | Array<P_numtype,N_rank>::operator op(const Array<T_numtype, N_rank>& x) \ |
---|
101 | { \ |
---|
102 | typedef typename asExpr<Array<T_numtype,N_rank> >::T_expr T_expr; \ |
---|
103 | _bz_evaluate(*this, asExpr<Array<T_numtype, N_rank> >::getExpr(x), \ |
---|
104 | name<T_numtype, _bz_typename T_expr::T_result>()); \ |
---|
105 | return *this; \ |
---|
106 | } \ |
---|
107 | template<typename P_numtype, int N_rank> \ |
---|
108 | _bz_forceinline \ |
---|
109 | Array<P_numtype,N_rank>& \ |
---|
110 | Array<P_numtype,N_rank>::operator op(const T_numtype& x) \ |
---|
111 | { \ |
---|
112 | typedef typename asExpr<T_numtype>::T_expr T_expr; \ |
---|
113 | _bz_evaluate(*this, asExpr<T_numtype>::getExpr(x), \ |
---|
114 | name<T_numtype, _bz_typename T_expr::T_result>()); \ |
---|
115 | return *this; \ |
---|
116 | } |
---|
117 | |
---|
118 | BZ_ARRAY_UPDATE(+=, _bz_plus_update) |
---|
119 | BZ_ARRAY_UPDATE(-=, _bz_minus_update) |
---|
120 | BZ_ARRAY_UPDATE(*=, _bz_multiply_update) |
---|
121 | BZ_ARRAY_UPDATE(/=, _bz_divide_update) |
---|
122 | BZ_ARRAY_UPDATE(%=, _bz_mod_update) |
---|
123 | BZ_ARRAY_UPDATE(^=, _bz_xor_update) |
---|
124 | BZ_ARRAY_UPDATE(&=, _bz_bitand_update) |
---|
125 | BZ_ARRAY_UPDATE(|=, _bz_bitor_update) |
---|
126 | BZ_ARRAY_UPDATE(<<=, _bz_shiftl_update) |
---|
127 | BZ_ARRAY_UPDATE(>>=, _bz_shiftr_update) |
---|
128 | |
---|
129 | #else |
---|
130 | |
---|
131 | template<typename P_numtype, int N_rank> |
---|
132 | inline Array<P_numtype, N_rank>& |
---|
133 | Array<P_numtype,N_rank>::operator+=(T_numtype x) |
---|
134 | { |
---|
135 | (*this) += _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
136 | return *this; |
---|
137 | } |
---|
138 | |
---|
139 | template<typename P_numtype, int N_rank> |
---|
140 | inline Array<P_numtype, N_rank>& |
---|
141 | Array<P_numtype,N_rank>::operator-=(T_numtype x) |
---|
142 | { |
---|
143 | (*this) -= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | template<typename P_numtype, int N_rank> |
---|
148 | inline Array<P_numtype, N_rank>& |
---|
149 | Array<P_numtype,N_rank>::operator*=(T_numtype x) |
---|
150 | { |
---|
151 | (*this) *= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
152 | return *this; |
---|
153 | } |
---|
154 | |
---|
155 | template<typename P_numtype, int N_rank> |
---|
156 | inline Array<P_numtype, N_rank>& |
---|
157 | Array<P_numtype,N_rank>::operator/=(T_numtype x) |
---|
158 | { |
---|
159 | (*this) /= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
160 | return *this; |
---|
161 | } |
---|
162 | |
---|
163 | template<typename P_numtype, int N_rank> |
---|
164 | inline Array<P_numtype, N_rank>& |
---|
165 | Array<P_numtype,N_rank>::operator%=(T_numtype x) |
---|
166 | { |
---|
167 | (*this) %= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
168 | return *this; |
---|
169 | } |
---|
170 | |
---|
171 | template<typename P_numtype, int N_rank> |
---|
172 | inline Array<P_numtype, N_rank>& |
---|
173 | Array<P_numtype,N_rank>::operator^=(T_numtype x) |
---|
174 | { |
---|
175 | (*this) ^= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
176 | return *this; |
---|
177 | } |
---|
178 | |
---|
179 | template<typename P_numtype, int N_rank> |
---|
180 | inline Array<P_numtype, N_rank>& |
---|
181 | Array<P_numtype,N_rank>::operator&=(T_numtype x) |
---|
182 | { |
---|
183 | (*this) &= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
184 | return *this; |
---|
185 | } |
---|
186 | |
---|
187 | template<typename P_numtype, int N_rank> |
---|
188 | inline Array<P_numtype, N_rank>& |
---|
189 | Array<P_numtype,N_rank>::operator|=(T_numtype x) |
---|
190 | { |
---|
191 | (*this) |= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
192 | return *this; |
---|
193 | } |
---|
194 | |
---|
195 | template<typename P_numtype, int N_rank> |
---|
196 | inline Array<P_numtype, N_rank>& |
---|
197 | Array<P_numtype,N_rank>::operator>>=(T_numtype x) |
---|
198 | { |
---|
199 | (*this) <<= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
200 | return *this; |
---|
201 | } |
---|
202 | |
---|
203 | template<typename P_numtype, int N_rank> |
---|
204 | inline Array<P_numtype, N_rank>& |
---|
205 | Array<P_numtype,N_rank>::operator<<=(T_numtype x) |
---|
206 | { |
---|
207 | (*this) <<= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x); |
---|
208 | return *this; |
---|
209 | } |
---|
210 | |
---|
211 | /* |
---|
212 | * Array operands |
---|
213 | */ |
---|
214 | |
---|
215 | template<typename P_numtype, int N_rank> |
---|
216 | inline Array<P_numtype, N_rank>& |
---|
217 | Array<P_numtype, N_rank>::operator=(const Array<T_numtype,N_rank>& x) |
---|
218 | { |
---|
219 | (*this) = _bz_ArrayExpr<FastArrayIterator<T_numtype, N_rank> >(x.beginFast()); |
---|
220 | return *this; |
---|
221 | } |
---|
222 | |
---|
223 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
224 | inline Array<P_numtype, N_rank>& |
---|
225 | Array<P_numtype, N_rank>::operator=(const Array<P_numtype2,N_rank>& x) |
---|
226 | { |
---|
227 | (*this) = _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
228 | return *this; |
---|
229 | } |
---|
230 | |
---|
231 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
232 | inline Array<P_numtype, N_rank>& |
---|
233 | Array<P_numtype, N_rank>::operator+=(const Array<P_numtype2,N_rank>& x) |
---|
234 | { |
---|
235 | (*this) += _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
236 | return *this; |
---|
237 | } |
---|
238 | |
---|
239 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
240 | inline Array<P_numtype, N_rank>& |
---|
241 | Array<P_numtype, N_rank>::operator-=(const Array<P_numtype2,N_rank>& x) |
---|
242 | { |
---|
243 | (*this) -= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
244 | return *this; |
---|
245 | } |
---|
246 | |
---|
247 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
248 | inline Array<P_numtype, N_rank>& |
---|
249 | Array<P_numtype, N_rank>::operator*=(const Array<P_numtype2,N_rank>& x) |
---|
250 | { |
---|
251 | (*this) *= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
252 | return *this; |
---|
253 | } |
---|
254 | |
---|
255 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
256 | inline Array<P_numtype, N_rank>& |
---|
257 | Array<P_numtype, N_rank>::operator/=(const Array<P_numtype2,N_rank>& x) |
---|
258 | { |
---|
259 | (*this) /= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
260 | return *this; |
---|
261 | } |
---|
262 | |
---|
263 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
264 | inline Array<P_numtype, N_rank>& |
---|
265 | Array<P_numtype, N_rank>::operator%=(const Array<P_numtype2,N_rank>& x) |
---|
266 | { |
---|
267 | (*this) %= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
268 | return *this; |
---|
269 | } |
---|
270 | |
---|
271 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
272 | inline Array<P_numtype, N_rank>& |
---|
273 | Array<P_numtype, N_rank>::operator^=(const Array<P_numtype2,N_rank>& x) |
---|
274 | { |
---|
275 | (*this) ^= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
276 | return *this; |
---|
277 | } |
---|
278 | |
---|
279 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
280 | inline Array<P_numtype, N_rank>& |
---|
281 | Array<P_numtype, N_rank>::operator&=(const Array<P_numtype2,N_rank>& x) |
---|
282 | { |
---|
283 | (*this) &= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
284 | return *this; |
---|
285 | } |
---|
286 | |
---|
287 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
288 | inline Array<P_numtype, N_rank>& |
---|
289 | Array<P_numtype, N_rank>::operator|=(const Array<P_numtype2,N_rank>& x) |
---|
290 | { |
---|
291 | (*this) |= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
292 | return *this; |
---|
293 | } |
---|
294 | |
---|
295 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
296 | inline Array<P_numtype, N_rank>& |
---|
297 | Array<P_numtype, N_rank>::operator>>=(const Array<P_numtype2,N_rank>& x) |
---|
298 | { |
---|
299 | (*this) >>= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
300 | return *this; |
---|
301 | } |
---|
302 | |
---|
303 | template<typename P_numtype, int N_rank> template<typename P_numtype2> |
---|
304 | inline Array<P_numtype, N_rank>& |
---|
305 | Array<P_numtype, N_rank>::operator<<=(const Array<P_numtype2,N_rank>& x) |
---|
306 | { |
---|
307 | (*this) <<= _bz_ArrayExpr<FastArrayIterator<P_numtype2, N_rank> >(x.beginFast()); |
---|
308 | return *this; |
---|
309 | } |
---|
310 | |
---|
311 | /* |
---|
312 | * Array expression operands |
---|
313 | */ |
---|
314 | |
---|
315 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
316 | inline Array<P_numtype, N_rank>& |
---|
317 | Array<P_numtype, N_rank>::operator=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
318 | { |
---|
319 | evaluate(expr, _bz_update<T_numtype, _bz_typename T_expr::T_numtype>()); |
---|
320 | return *this; |
---|
321 | } |
---|
322 | |
---|
323 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
324 | inline Array<P_numtype, N_rank>& |
---|
325 | Array<P_numtype, N_rank>::operator+=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
326 | { |
---|
327 | evaluate(expr, _bz_plus_update<T_numtype, _bz_typename T_expr::T_numtype>()); |
---|
328 | return *this; |
---|
329 | } |
---|
330 | |
---|
331 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
332 | inline Array<P_numtype, N_rank>& |
---|
333 | Array<P_numtype, N_rank>::operator-=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
334 | { |
---|
335 | evaluate(expr, _bz_minus_update<T_numtype, |
---|
336 | _bz_typename T_expr::T_numtype>()); |
---|
337 | return *this; |
---|
338 | } |
---|
339 | |
---|
340 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
341 | inline Array<P_numtype, N_rank>& |
---|
342 | Array<P_numtype, N_rank>::operator*=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
343 | { |
---|
344 | evaluate(expr, _bz_multiply_update<T_numtype, |
---|
345 | _bz_typename T_expr::T_numtype>()); |
---|
346 | return *this; |
---|
347 | } |
---|
348 | |
---|
349 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
350 | inline Array<P_numtype, N_rank>& |
---|
351 | Array<P_numtype, N_rank>::operator/=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
352 | { |
---|
353 | evaluate(expr, _bz_divide_update<T_numtype, |
---|
354 | _bz_typename T_expr::T_numtype>()); |
---|
355 | return *this; |
---|
356 | } |
---|
357 | |
---|
358 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
359 | inline Array<P_numtype, N_rank>& |
---|
360 | Array<P_numtype, N_rank>::operator%=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
361 | { |
---|
362 | evaluate(expr, _bz_mod_update<T_numtype, |
---|
363 | _bz_typename T_expr::T_numtype>()); |
---|
364 | return *this; |
---|
365 | } |
---|
366 | |
---|
367 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
368 | inline Array<P_numtype, N_rank>& |
---|
369 | Array<P_numtype, N_rank>::operator^=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
370 | { |
---|
371 | evaluate(expr, _bz_xor_update<T_numtype, |
---|
372 | _bz_typename T_expr::T_numtype>()); |
---|
373 | return *this; |
---|
374 | } |
---|
375 | |
---|
376 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
377 | inline Array<P_numtype, N_rank>& |
---|
378 | Array<P_numtype, N_rank>::operator&=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
379 | { |
---|
380 | evaluate(expr, _bz_bitand_update<T_numtype, |
---|
381 | _bz_typename T_expr::T_numtype>()); |
---|
382 | return *this; |
---|
383 | } |
---|
384 | |
---|
385 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
386 | inline Array<P_numtype, N_rank>& |
---|
387 | Array<P_numtype, N_rank>::operator|=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
388 | { |
---|
389 | evaluate(expr, _bz_bitor_update<T_numtype, |
---|
390 | _bz_typename T_expr::T_numtype>()); |
---|
391 | return *this; |
---|
392 | } |
---|
393 | |
---|
394 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
395 | inline Array<P_numtype, N_rank>& |
---|
396 | Array<P_numtype, N_rank>::operator>>=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
397 | { |
---|
398 | evaluate(expr, _bz_shiftr_update<T_numtype, |
---|
399 | _bz_typename T_expr::T_numtype>()); |
---|
400 | return *this; |
---|
401 | } |
---|
402 | |
---|
403 | template<typename P_numtype, int N_rank> template<typename T_expr> |
---|
404 | inline Array<P_numtype, N_rank>& |
---|
405 | Array<P_numtype, N_rank>::operator<<=(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) |
---|
406 | { |
---|
407 | evaluate(expr, _bz_shiftl_update<T_numtype, |
---|
408 | _bz_typename T_expr::T_numtype>()); |
---|
409 | return *this; |
---|
410 | } |
---|
411 | |
---|
412 | #endif // BZ_NEW_EXPRESSION_TEMPLATES |
---|
413 | |
---|
414 | BZ_NAMESPACE_END |
---|
415 | |
---|
416 | #endif // BZ_ARRAYOPS_CC |
---|