source: XIOS/dev/dev_olga/src/extern/blitz/include/blitz/array/ops.cc @ 1022

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 13.1 KB
Line 
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
40BZ_NAMESPACE(blitz)
41
42/*
43 * Constant operands
44 */
45
46template<typename P_numtype, int N_rank>
47_bz_forceinline
48Array<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.
62template<typename P_numtype, int N_rank> template<typename T_expr>
63_bz_forceinline
64Array<P_numtype,N_rank>&
65Array<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...
74template<typename P_numtype, int N_rank>
75_bz_forceinline
76Array<P_numtype, N_rank>&
77Array<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
118BZ_ARRAY_UPDATE(+=, _bz_plus_update)
119BZ_ARRAY_UPDATE(-=, _bz_minus_update)
120BZ_ARRAY_UPDATE(*=, _bz_multiply_update)
121BZ_ARRAY_UPDATE(/=, _bz_divide_update)
122BZ_ARRAY_UPDATE(%=, _bz_mod_update)
123BZ_ARRAY_UPDATE(^=, _bz_xor_update)
124BZ_ARRAY_UPDATE(&=, _bz_bitand_update)
125BZ_ARRAY_UPDATE(|=, _bz_bitor_update)
126BZ_ARRAY_UPDATE(<<=, _bz_shiftl_update)
127BZ_ARRAY_UPDATE(>>=, _bz_shiftr_update)
128
129#else
130
131template<typename P_numtype, int N_rank>
132inline Array<P_numtype, N_rank>& 
133Array<P_numtype,N_rank>::operator+=(T_numtype x)
134{
135    (*this) += _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
136    return *this;
137}
138
139template<typename P_numtype, int N_rank>
140inline Array<P_numtype, N_rank>&
141Array<P_numtype,N_rank>::operator-=(T_numtype x)
142{
143    (*this) -= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
144    return *this;
145}
146
147template<typename P_numtype, int N_rank>
148inline Array<P_numtype, N_rank>&
149Array<P_numtype,N_rank>::operator*=(T_numtype x)
150{
151    (*this) *= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
152    return *this;
153}
154
155template<typename P_numtype, int N_rank>
156inline Array<P_numtype, N_rank>&
157Array<P_numtype,N_rank>::operator/=(T_numtype x)
158{
159    (*this) /= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
160    return *this;
161}
162
163template<typename P_numtype, int N_rank>
164inline Array<P_numtype, N_rank>&
165Array<P_numtype,N_rank>::operator%=(T_numtype x)
166{
167    (*this) %= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
168    return *this;
169}
170
171template<typename P_numtype, int N_rank>
172inline Array<P_numtype, N_rank>&
173Array<P_numtype,N_rank>::operator^=(T_numtype x)
174{
175    (*this) ^= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
176    return *this;
177}
178
179template<typename P_numtype, int N_rank>
180inline Array<P_numtype, N_rank>&
181Array<P_numtype,N_rank>::operator&=(T_numtype x)
182{
183    (*this) &= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
184    return *this;
185}
186
187template<typename P_numtype, int N_rank>
188inline Array<P_numtype, N_rank>&
189Array<P_numtype,N_rank>::operator|=(T_numtype x)
190{
191    (*this) |= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
192    return *this;
193}
194
195template<typename P_numtype, int N_rank>
196inline Array<P_numtype, N_rank>&
197Array<P_numtype,N_rank>::operator>>=(T_numtype x)
198{
199    (*this) <<= _bz_ArrayExpr<_bz_ArrayExprConstant<T_numtype> >(x);
200    return *this;
201}
202
203template<typename P_numtype, int N_rank>
204inline Array<P_numtype, N_rank>&
205Array<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
215template<typename P_numtype, int N_rank>
216inline Array<P_numtype, N_rank>&
217Array<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
223template<typename P_numtype, int N_rank> template<typename P_numtype2>
224inline Array<P_numtype, N_rank>&
225Array<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
231template<typename P_numtype, int N_rank> template<typename P_numtype2>
232inline Array<P_numtype, N_rank>&
233Array<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
239template<typename P_numtype, int N_rank> template<typename P_numtype2>
240inline Array<P_numtype, N_rank>&
241Array<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
247template<typename P_numtype, int N_rank> template<typename P_numtype2>
248inline Array<P_numtype, N_rank>&
249Array<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
255template<typename P_numtype, int N_rank> template<typename P_numtype2>
256inline Array<P_numtype, N_rank>&
257Array<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
263template<typename P_numtype, int N_rank> template<typename P_numtype2>
264inline Array<P_numtype, N_rank>&
265Array<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
271template<typename P_numtype, int N_rank> template<typename P_numtype2>
272inline Array<P_numtype, N_rank>&
273Array<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
279template<typename P_numtype, int N_rank> template<typename P_numtype2>
280inline Array<P_numtype, N_rank>&
281Array<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
287template<typename P_numtype, int N_rank> template<typename P_numtype2>
288inline Array<P_numtype, N_rank>&
289Array<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
295template<typename P_numtype, int N_rank> template<typename P_numtype2>
296inline Array<P_numtype, N_rank>&
297Array<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
303template<typename P_numtype, int N_rank> template<typename P_numtype2>
304inline Array<P_numtype, N_rank>&
305Array<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
315template<typename P_numtype, int N_rank> template<typename T_expr>
316inline Array<P_numtype, N_rank>&
317Array<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
323template<typename P_numtype, int N_rank> template<typename T_expr>
324inline Array<P_numtype, N_rank>&
325Array<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
331template<typename P_numtype, int N_rank> template<typename T_expr>
332inline Array<P_numtype, N_rank>&
333Array<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
340template<typename P_numtype, int N_rank> template<typename T_expr>
341inline Array<P_numtype, N_rank>&
342Array<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
349template<typename P_numtype, int N_rank> template<typename T_expr>
350inline Array<P_numtype, N_rank>&
351Array<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
358template<typename P_numtype, int N_rank> template<typename T_expr>
359inline Array<P_numtype, N_rank>&
360Array<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
367template<typename P_numtype, int N_rank> template<typename T_expr>
368inline Array<P_numtype, N_rank>&
369Array<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
376template<typename P_numtype, int N_rank> template<typename T_expr>
377inline Array<P_numtype, N_rank>&
378Array<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
385template<typename P_numtype, int N_rank> template<typename T_expr>
386inline Array<P_numtype, N_rank>&
387Array<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
394template<typename P_numtype, int N_rank> template<typename T_expr>
395inline Array<P_numtype, N_rank>&
396Array<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
403template<typename P_numtype, int N_rank> template<typename T_expr>
404inline Array<P_numtype, N_rank>&
405Array<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
414BZ_NAMESPACE_END
415
416#endif // BZ_ARRAYOPS_CC
Note: See TracBrowser for help on using the repository browser.