source: Sophya/trunk/SophyaExt/Blitz/blitz/vecpick.cc@ 3534

Last change on this file since 3534 was 221, checked in by ansari, 26 years ago

Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99

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