source: Sophya/trunk/Poubelle/DPC:FitsIOServer/Blitz/blitz/vecpick.cc@ 1036

Last change on this file since 1036 was 658, checked in by ansari, 26 years ago

no message

File size: 18.6 KB
Line 
1/*
2 * $Id: vecpick.cc,v 1.1.1.1 1999-11-26 16:37:06 ansari Exp $
3 *
4 * Copyright (C) 1997 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
5 * All rights reserved. Please see <blitz/blitz.h> for terms and
6 * conditions of use.
7 *
8 * $Log: not supported by cvs2svn $
9// Revision 1.1.1.1 1999/04/09 17:58:59 ansari
10// Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
11//
12 * Revision 1.5 1998/03/14 00:04:47 tveldhui
13 * 0.2-alpha-05
14 *
15 * Revision 1.4 1997/07/16 14:51:20 tveldhui
16 * Update: Alpha release 0.2 (Arrays)
17 *
18 * Revision 1.3 1997/01/24 14:42:00 tveldhui
19 * Periodic RCS update
20 *
21 */
22
23#ifndef BZ_VECPICK_CC
24#define BZ_VECPICK_CC
25
26#ifndef BZ_VECPICK_H
27 #include <blitz/vecpick.h>
28#endif
29
30#ifndef BZ_UPDATE_H
31 #include <blitz/update.h>
32#endif
33
34#ifndef BZ_RANDOM_H
35 #include <blitz/random.h>
36#endif
37
38#ifndef BZ_VECEXPR_H
39 #include <blitz/vecexpr.h>
40#endif
41
42#ifndef BZ_RANDOM_H
43 #include <blitz/random.h>
44#endif
45BZ_NAMESPACE(blitz)
46
47/*****************************************************************************
48 * Assignment operators with vector expression operand
49 */
50
51template<class P_numtype> template<class P_expr, class P_updater>
52inline
53void VectorPick<P_numtype>::_bz_assign(P_expr expr, P_updater)
54{
55 BZPRECONDITION(expr.length(length()) == length());
56
57 // If all vectors in the expression, plus the vector to which the
58 // result is being assigned have unit stride, then avoid stride
59 // calculations.
60 if (_bz_hasFastAccess() && expr._bz_hasFastAccess())
61 {
62#ifndef BZ_PARTIAL_LOOP_UNROLL
63 for (int i=0; i < length(); ++i)
64 P_updater::update(vector_(index_(i)), expr._bz_fastAccess(i));
65#else
66 // Unwind the inner loop, five elements at a time.
67 int leftover = length() % 5;
68
69 int i=0;
70 for (; i < leftover; ++i)
71 P_updater::update(vector_(index_(i)), expr._bz_fastAccess(i));
72
73 for (; i < length(); i += 5)
74 {
75 P_updater::update(vector_(index_(i)), expr._bz_fastAccess(i));
76 P_updater::update(vector_(index_(i+1)), expr._bz_fastAccess(i+1));
77 P_updater::update(vector_(index_(i+2)), expr._bz_fastAccess(i+2));
78 P_updater::update(vector_(index_(i+3)), expr._bz_fastAccess(i+3));
79 P_updater::update(vector_(index_(i+4)), expr._bz_fastAccess(i+4));
80 }
81#endif
82 }
83 else {
84 // Not all unit strides -- have to access all the vector elements
85 // as data_[i*stride_], which is slower.
86 for (int i=0; i < length(); ++i)
87 P_updater::update(vector_[index_[i]], expr[i]);
88 }
89}
90
91template<class P_numtype> template<class P_expr>
92inline VectorPick<P_numtype>&
93VectorPick<P_numtype>::operator=(_bz_VecExpr<P_expr> expr)
94{
95 BZPRECONDITION(expr.length(length()) == length());
96
97 // If all vectors in the expression, plus the vector to which the
98 // result is being assigned have unit stride, then avoid stride
99 // calculations.
100 if (_bz_hasFastAccess() && expr._bz_hasFastAccess())
101 {
102#ifndef BZ_PARTIAL_LOOP_UNROLL
103 for (int i=0; i < length(); ++i)
104 (*this)(i) = expr._bz_fastAccess(i);
105#else
106 // Unwind the inner loop, five elements at a time.
107 int leftover = length() % 5;
108
109 int i=0;
110 for (; i < leftover; ++i)
111 (*this)(i) = expr._bz_fastAccess(i);
112
113 for (; i < length(); i += 5)
114 {
115 (*this)(i) = expr._bz_fastAccess(i);
116 (*this)(i+1) = expr._bz_fastAccess(i+1);
117 (*this)(i+2) = expr._bz_fastAccess(i+2);
118 (*this)(i+3) = expr._bz_fastAccess(i+3);
119 (*this)(i+4) = expr._bz_fastAccess(i+4);
120 }
121#endif
122 }
123 else {
124 // Not all unit strides -- have to access all the vector elements
125 // as data_[i*stride_], which is slower.
126 for (int i=0; i < length(); ++i)
127 (*this)[i] = expr[i];
128 }
129 return *this;
130}
131
132
133template<class P_numtype> template<class P_expr>
134inline VectorPick<P_numtype>&
135VectorPick<P_numtype>::operator+=(_bz_VecExpr<P_expr> expr)
136{
137 _bz_assign(expr, _bz_plus_update<P_numtype,
138 _bz_typename P_expr::T_numtype>());
139 return *this;
140}
141
142template<class P_numtype> template<class P_expr>
143inline VectorPick<P_numtype>&
144VectorPick<P_numtype>::operator-=(_bz_VecExpr<P_expr> expr)
145{
146 _bz_assign(expr, _bz_minus_update<P_numtype,
147 _bz_typename P_expr::T_numtype>());
148 return *this;
149}
150
151template<class P_numtype> template<class P_expr>
152inline VectorPick<P_numtype>&
153VectorPick<P_numtype>::operator*=(_bz_VecExpr<P_expr> expr)
154{
155 _bz_assign(expr, _bz_multiply_update<P_numtype,
156 _bz_typename P_expr::T_numtype>());
157 return *this;
158}
159
160template<class P_numtype> template<class P_expr>
161inline VectorPick<P_numtype>&
162VectorPick<P_numtype>::operator/=(_bz_VecExpr<P_expr> expr)
163{
164 _bz_assign(expr, _bz_divide_update<P_numtype,
165 _bz_typename P_expr::T_numtype>());
166 return *this;
167}
168
169template<class P_numtype> template<class P_expr>
170inline VectorPick<P_numtype>&
171VectorPick<P_numtype>::operator%=(_bz_VecExpr<P_expr> expr)
172{
173 _bz_assign(expr, _bz_mod_update<P_numtype,
174 _bz_typename P_expr::T_numtype>());
175 return *this;
176}
177
178template<class P_numtype> template<class P_expr>
179inline VectorPick<P_numtype>&
180VectorPick<P_numtype>::operator^=(_bz_VecExpr<P_expr> expr)
181{
182 _bz_assign(expr, _bz_xor_update<P_numtype,
183 _bz_typename P_expr::T_numtype>());
184 return *this;
185}
186
187template<class P_numtype> template<class P_expr>
188inline VectorPick<P_numtype>&
189VectorPick<P_numtype>::operator&=(_bz_VecExpr<P_expr> expr)
190{
191 _bz_assign(expr, _bz_bitand_update<P_numtype,
192 _bz_typename P_expr::T_numtype>());
193 return *this;
194}
195
196template<class P_numtype> template<class P_expr>
197inline VectorPick<P_numtype>&
198VectorPick<P_numtype>::operator|=(_bz_VecExpr<P_expr> expr)
199{
200 _bz_assign(expr, _bz_bitor_update<P_numtype,
201 _bz_typename P_expr::T_numtype>());
202 return *this;
203}
204
205template<class P_numtype> template<class P_expr>
206inline VectorPick<P_numtype>&
207VectorPick<P_numtype>::operator>>=(_bz_VecExpr<P_expr> expr)
208{
209 _bz_assign(expr, _bz_shiftr_update<P_numtype,
210 _bz_typename P_expr::T_numtype>());
211 return *this;
212}
213
214template<class P_numtype> template<class P_expr>
215inline VectorPick<P_numtype>&
216VectorPick<P_numtype>::operator<<=(_bz_VecExpr<P_expr> expr)
217{
218 _bz_assign(expr, _bz_shiftl_update<P_numtype,
219 _bz_typename P_expr::T_numtype>());
220 return *this;
221}
222
223/*****************************************************************************
224 * Assignment operators with scalar operand
225 */
226
227template<class P_numtype>
228inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator=(P_numtype x)
229{
230 typedef _bz_VecExprConstant<P_numtype> T_expr;
231 (*this) = _bz_VecExpr<T_expr>(T_expr(x));
232 return *this;
233}
234
235template<class P_numtype>
236inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator+=(P_numtype x)
237{
238 typedef _bz_VecExprConstant<P_numtype> T_expr;
239 (*this) += _bz_VecExpr<T_expr>(T_expr(x));
240 return *this;
241}
242
243template<class P_numtype>
244inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator-=(P_numtype x)
245{
246 typedef _bz_VecExprConstant<P_numtype> T_expr;
247 (*this) -= _bz_VecExpr<T_expr>(T_expr(x));
248 return *this;
249}
250
251template<class P_numtype>
252inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator*=(P_numtype x)
253{
254 typedef _bz_VecExprConstant<P_numtype> T_expr;
255 (*this) *= _bz_VecExpr<T_expr>(T_expr(x));
256 return *this;
257}
258
259template<class P_numtype>
260inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator/=(P_numtype x)
261{
262 typedef _bz_VecExprConstant<P_numtype> T_expr;
263 (*this) /= _bz_VecExpr<T_expr>(T_expr(x));
264 return *this;
265}
266
267template<class P_numtype>
268inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator%=(P_numtype x)
269{
270 typedef _bz_VecExprConstant<P_numtype> T_expr;
271 (*this) %= _bz_VecExpr<T_expr>(T_expr(x));
272 return *this;
273}
274
275template<class P_numtype>
276inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator^=(P_numtype x)
277{
278 typedef _bz_VecExprConstant<P_numtype> T_expr;
279 (*this) ^= _bz_VecExpr<T_expr>(T_expr(x));
280 return *this;
281}
282
283template<class P_numtype>
284inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator&=(P_numtype x)
285{
286 typedef _bz_VecExprConstant<P_numtype> T_expr;
287 (*this) &= _bz_VecExpr<T_expr>(T_expr(x));
288 return *this;
289}
290
291template<class P_numtype>
292inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator|=(P_numtype x)
293{
294 typedef _bz_VecExprConstant<P_numtype> T_expr;
295 (*this) |= _bz_VecExpr<T_expr>(T_expr(x));
296 return *this;
297}
298
299template<class P_numtype>
300inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator>>=(int x)
301{
302 typedef _bz_VecExprConstant<int> T_expr;
303 (*this) >>= _bz_VecExpr<T_expr>(T_expr(x));
304 return *this;
305}
306
307template<class P_numtype>
308inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator<<=(int x)
309{
310 typedef _bz_VecExprConstant<P_numtype> T_expr;
311 (*this) <<= _bz_VecExpr<T_expr>(T_expr(x));
312 return *this;
313}
314
315/*****************************************************************************
316 * Assignment operators with vector operand
317 */
318
319template<class P_numtype> template<class P_numtype2>
320inline VectorPick<P_numtype>&
321VectorPick<P_numtype>::operator=(const Vector<P_numtype2>& x)
322{
323 (*this) = _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
324 return *this;
325}
326
327template<class P_numtype> template<class P_numtype2>
328inline VectorPick<P_numtype>&
329VectorPick<P_numtype>::operator+=(const Vector<P_numtype2>& x)
330{
331 (*this) += _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
332 return *this;
333}
334
335template<class P_numtype> template<class P_numtype2>
336inline VectorPick<P_numtype>&
337VectorPick<P_numtype>::operator-=(const Vector<P_numtype2>& x)
338{
339 (*this) -= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
340 return *this;
341}
342
343template<class P_numtype> template<class P_numtype2>
344inline VectorPick<P_numtype>&
345VectorPick<P_numtype>::operator*=(const Vector<P_numtype2>& x)
346{
347 (*this) *= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
348 return *this;
349}
350
351template<class P_numtype> template<class P_numtype2>
352inline VectorPick<P_numtype>&
353VectorPick<P_numtype>::operator/=(const Vector<P_numtype2>& x)
354{
355 (*this) /= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
356 return *this;
357}
358
359template<class P_numtype> template<class P_numtype2>
360inline VectorPick<P_numtype>&
361VectorPick<P_numtype>::operator%=(const Vector<P_numtype2>& x)
362{
363 (*this) %= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
364 return *this;
365}
366
367template<class P_numtype> template<class P_numtype2>
368inline VectorPick<P_numtype>&
369VectorPick<P_numtype>::operator^=(const Vector<P_numtype2>& x)
370{
371 (*this) ^= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
372 return *this;
373}
374
375template<class P_numtype> template<class P_numtype2>
376inline VectorPick<P_numtype>&
377VectorPick<P_numtype>::operator&=(const Vector<P_numtype2>& x)
378{
379 (*this) &= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
380 return *this;
381}
382
383template<class P_numtype> template<class P_numtype2>
384inline VectorPick<P_numtype>&
385VectorPick<P_numtype>::operator|=(const Vector<P_numtype2>& x)
386{
387 (*this) |= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
388 return *this;
389}
390
391template<class P_numtype> template<class P_numtype2>
392inline VectorPick<P_numtype>&
393VectorPick<P_numtype>::operator<<=(const Vector<P_numtype2>& x)
394{
395 (*this) <<= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
396 return *this;
397}
398
399template<class P_numtype> template<class P_numtype2>
400inline VectorPick<P_numtype>&
401VectorPick<P_numtype>::operator>>=(const Vector<P_numtype2>& x)
402{
403 (*this) >>= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.begin());
404 return *this;
405}
406
407/*****************************************************************************
408 * Assignment operators with Range operand
409 */
410
411template<class P_numtype>
412inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator=(Range r)
413{
414 (*this) = _bz_VecExpr<Range>(r);
415 return *this;
416}
417
418template<class P_numtype>
419inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator+=(Range r)
420{
421 (*this) += _bz_VecExpr<Range>(r);
422 return *this;
423}
424
425template<class P_numtype>
426inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator-=(Range r)
427{
428 (*this) -= _bz_VecExpr<Range>(r);
429 return *this;
430}
431
432template<class P_numtype>
433inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator*=(Range r)
434{
435 (*this) *= _bz_VecExpr<Range>(r);
436 return *this;
437}
438
439template<class P_numtype>
440inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator/=(Range r)
441{
442 (*this) /= _bz_VecExpr<Range>(r);
443 return *this;
444}
445
446template<class P_numtype>
447inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator%=(Range r)
448{
449 (*this) %= _bz_VecExpr<Range>(r);
450 return *this;
451}
452
453template<class P_numtype>
454inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator^=(Range r)
455{
456 (*this) ^= _bz_VecExpr<Range>(r);
457 return *this;
458}
459
460template<class P_numtype>
461inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator&=(Range r)
462{
463 (*this) &= _bz_VecExpr<Range>(r);
464 return *this;
465}
466
467template<class P_numtype>
468inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator|=(Range r)
469{
470 (*this) |= _bz_VecExpr<Range>(r);
471 return *this;
472}
473
474template<class P_numtype>
475inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator>>=(Range r)
476{
477 (*this) >>= _bz_VecExpr<Range>(r);
478 return *this;
479}
480
481template<class P_numtype>
482inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator<<=(Range r)
483{
484 (*this) <<= _bz_VecExpr<Range>(r);
485 return *this;
486}
487
488/*****************************************************************************
489 * Assignment operators with VectorPick operand
490 */
491
492template<class P_numtype> template<class P_numtype2>
493inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator=(const
494 VectorPick<P_numtype2>& x)
495{
496 typedef VectorPickIterConst<P_numtype2> T_expr;
497 (*this) = _bz_VecExpr<T_expr>(x.begin());
498 return *this;
499}
500
501template<class P_numtype> template<class P_numtype2>
502inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator+=(const
503 VectorPick<P_numtype2>& x)
504{
505 typedef VectorPickIterConst<P_numtype2> T_expr;
506 (*this) += _bz_VecExpr<T_expr>(x.begin());
507 return *this;
508}
509
510
511template<class P_numtype> template<class P_numtype2>
512inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator-=(const
513 VectorPick<P_numtype2>& x)
514{
515 typedef VectorPickIterConst<P_numtype2> T_expr;
516 (*this) -= _bz_VecExpr<T_expr>(x.begin());
517 return *this;
518}
519
520template<class P_numtype> template<class P_numtype2>
521inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator*=(const
522 VectorPick<P_numtype2>& x)
523{
524 typedef VectorPickIterConst<P_numtype2> T_expr;
525 (*this) *= _bz_VecExpr<T_expr>(x.begin());
526 return *this;
527}
528
529template<class P_numtype> template<class P_numtype2>
530inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator/=(const
531 VectorPick<P_numtype2>& x)
532{
533 typedef VectorPickIterConst<P_numtype2> T_expr;
534 (*this) /= _bz_VecExpr<T_expr>(x.begin());
535 return *this;
536}
537
538template<class P_numtype> template<class P_numtype2>
539inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator%=(const
540 VectorPick<P_numtype2>& x)
541{
542 typedef VectorPickIterConst<P_numtype2> T_expr;
543 (*this) %= _bz_VecExpr<T_expr>(x.begin());
544 return *this;
545}
546
547template<class P_numtype> template<class P_numtype2>
548inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator^=(const
549 VectorPick<P_numtype2>& x)
550{
551 typedef VectorPickIterConst<P_numtype2> T_expr;
552 (*this) ^= _bz_VecExpr<T_expr>(x.begin());
553 return *this;
554}
555
556template<class P_numtype> template<class P_numtype2>
557inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator&=(const
558 VectorPick<P_numtype2>& x)
559{
560 typedef VectorPickIterConst<P_numtype2> T_expr;
561 (*this) &= _bz_VecExpr<T_expr>(x.begin());
562 return *this;
563}
564
565template<class P_numtype> template<class P_numtype2>
566inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator|=(const
567 VectorPick<P_numtype2>& x)
568{
569 typedef VectorPickIterConst<P_numtype2> T_expr;
570 (*this) |= _bz_VecExpr<T_expr>(x.begin());
571 return *this;
572}
573
574/*****************************************************************************
575 * Assignment operators with Random operand
576 */
577
578template<class P_numtype> template<class P_distribution>
579VectorPick<P_numtype>&
580VectorPick<P_numtype>::operator=(Random<P_distribution>& rand)
581{
582 (*this) = _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
583 (_bz_VecExprRandom<P_distribution>(rand));
584 return *this;
585}
586
587template<class P_numtype> template<class P_distribution>
588VectorPick<P_numtype>&
589VectorPick<P_numtype>::operator+=(Random<P_distribution>& rand)
590{
591 (*this) += _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
592 (_bz_VecExprRandom<P_distribution>(rand));
593 return *this;
594}
595
596template<class P_numtype> template<class P_distribution>
597VectorPick<P_numtype>&
598VectorPick<P_numtype>::operator-=(Random<P_distribution>& rand)
599{
600 (*this) -= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
601 (_bz_VecExprRandom<P_distribution>(rand));
602 return *this;
603}
604
605template<class P_numtype> template<class P_distribution>
606VectorPick<P_numtype>&
607VectorPick<P_numtype>::operator*=(Random<P_distribution>& rand)
608{
609 (*this) *= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
610 (_bz_VecExprRandom<P_distribution>(rand));
611 return *this;
612}
613
614template<class P_numtype> template<class P_distribution>
615VectorPick<P_numtype>&
616VectorPick<P_numtype>::operator/=(Random<P_distribution>& rand)
617{
618 (*this) /= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
619 (_bz_VecExprRandom<P_distribution>(rand));
620 return *this;
621}
622
623template<class P_numtype> template<class P_distribution>
624VectorPick<P_numtype>&
625VectorPick<P_numtype>::operator%=(Random<P_distribution>& rand)
626{
627 (*this) %= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
628 (_bz_VecExprRandom<P_distribution>(rand));
629 return *this;
630}
631
632template<class P_numtype> template<class P_distribution>
633VectorPick<P_numtype>&
634VectorPick<P_numtype>::operator^=(Random<P_distribution>& rand)
635{
636 (*this) ^= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
637 (_bz_VecExprRandom<P_distribution>(rand));
638 return *this;
639}
640
641template<class P_numtype> template<class P_distribution>
642VectorPick<P_numtype>&
643VectorPick<P_numtype>::operator&=(Random<P_distribution>& rand)
644{
645 (*this) &= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
646 (_bz_VecExprRandom<P_distribution>(rand));
647 return *this;
648}
649
650template<class P_numtype> template<class P_distribution>
651VectorPick<P_numtype>&
652VectorPick<P_numtype>::operator|=(Random<P_distribution>& rand)
653{
654 (*this) |= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
655 (_bz_VecExprRandom<P_distribution>(rand));
656 return *this;
657}
658
659BZ_NAMESPACE_END
660
661#endif // BZ_VECPICK_CC
Note: See TracBrowser for help on using the repository browser.