aboutsummaryrefslogtreecommitdiff
path: root/libstdc++/std/std_valarray.h
blob: b3006555547d620443546dd09a1f3cca4782d00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// The template and inlines for the -*- C++ -*- valarray class.

// Copyright (C) 1997-1999 Cygnus Solutions
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

// Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>

#ifndef __STD_VALARRAY__
#define __STD_VALARRAY__
#define _G_NO_VALARRAY_TEMPLATE_EXPORT 1

#include <cstddef>
#include <cmath>
#include <cstdlib>
#include <numeric>
#include <functional>
#include <algorithm>

#ifndef alloca
#ifdef __GNUC__
#define alloca __builtin_alloca
#else /* not GNU C.  */
#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi)
#include <alloca.h>
#else /* not sparc */
#if defined (MSDOS) && !defined (__TURBOC__)
#include <malloc.h>
#else /* not MSDOS, or __TURBOC__ */
#if defined(_AIX)
#include <malloc.h>
 #pragma alloca
#else /* not MSDOS, __TURBOC__, or _AIX */
#ifdef __hpux
#endif /* __hpux */
#endif /* not _AIX */
#endif /* not MSDOS, or __TURBOC__ */
#endif /* not sparc.  */
#endif /* not GNU C.  */
#endif /* alloca not defined.  */

extern "C" {
    void* alloca(size_t);
}


extern "C++" {

template<class _Clos, typename _Tp> class _Expr;

template<typename _Tp1, typename _Tp2> class _ValArray;    

template<template<class> class _Oper,
    template<class, class> class _Meta, class _Dom> struct _UnClos;

template<template<class> class _Oper,
    template<class, class> class _Meta1,
    template<class, class> class _Meta2,
    class _Dom1, class _Dom2> class _BinClos;

template<template<class, class> class _Meta, class _Dom> class _SClos;

template<template<class, class> class _Meta, class _Dom> class _GClos;
    
template<template<class, class> class _Meta, class _Dom> class _IClos;
    
template<template<class, class> class _Meta, class _Dom> class _ValFunClos;

template<template<class, class> class _Meta, class _Dom> class _RefFunClos;

template<class _Tp> struct _Unary_plus;
template<class _Tp> struct _Bitwise_and;
template<class _Tp> struct _Bitwise_or;
template<class _Tp> struct _Bitwise_xor;  
template<class _Tp> struct _Bitwise_not;
template<class _Tp> struct _Shift_left;
template<class _Tp> struct _Shift_right;

template<class _Tp> class valarray;   // An array of type _Tp
class slice;                          // BLAS-like slice out of an array
template<class _Tp> class slice_array;
class gslice;                         // generalized slice out of an array
template<class _Tp> class gslice_array;
template<class _Tp> class mask_array;     // masked array
template<class _Tp> class indirect_array; // indirected array

} // extern "C++"

#include <std/valarray_array.h>
#include <std/valarray_meta.h>

extern "C++" {

template<class _Tp> class valarray
{
public:
    typedef _Tp value_type;
    
    // _lib.valarray.cons_ construct/destroy:
    valarray();
    explicit valarray(size_t);
    valarray(const _Tp&, size_t);
    valarray(const _Tp* __restrict__, size_t);
    valarray(const valarray&);
    valarray(const slice_array<_Tp>&);
    valarray(const gslice_array<_Tp>&);
    valarray(const mask_array<_Tp>&);
    valarray(const indirect_array<_Tp>&);
    template<class _Dom>
    valarray(const _Expr<_Dom,_Tp>& __e);
    ~valarray();
    
    // _lib.valarray.assign_ assignment:
    valarray<_Tp>& operator=(const valarray<_Tp>&);
    valarray<_Tp>& operator=(const _Tp&);
    valarray<_Tp>& operator=(const slice_array<_Tp>&);
    valarray<_Tp>& operator=(const gslice_array<_Tp>&);
    valarray<_Tp>& operator=(const mask_array<_Tp>&);
    valarray<_Tp>& operator=(const indirect_array<_Tp>&);
    
    template<class _Dom> valarray<_Tp>&
    operator= (const _Expr<_Dom,_Tp>&);
    
    // _lib.valarray.access_ element access:
    _Tp                 operator[](size_t) const;
    _Tp&                operator[](size_t);		
    // _lib.valarray.sub_ subset operations:
    _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
    slice_array<_Tp>    operator[](slice);
    _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
    gslice_array<_Tp>   operator[](const gslice&);
    valarray<_Tp>     	 operator[](const valarray<bool>&) const;
    mask_array<_Tp>     operator[](const valarray<bool>&);
    _Expr<_IClos<_ValArray, _Tp>, _Tp>
    operator[](const valarray<size_t>&) const;
    indirect_array<_Tp> operator[](const valarray<size_t>&);
    
    // _lib.valarray.unary_ unary operators:
    _Expr<_UnClos<_Unary_plus,_ValArray,_Tp>,_Tp>  operator+ () const;
    _Expr<_UnClos<negate,_ValArray,_Tp>,_Tp> operator- () const;
    _Expr<_UnClos<_Bitwise_not,_ValArray,_Tp>,_Tp> operator~ () const;
    _Expr<_UnClos<logical_not,_ValArray,_Tp>,bool> operator! () const;
    
    // _lib.valarray.cassign_ computed assignment:
    valarray<_Tp>& operator*= (const _Tp&);
    valarray<_Tp>& operator/= (const _Tp&);
    valarray<_Tp>& operator%= (const _Tp&);
    valarray<_Tp>& operator+= (const _Tp&);
    valarray<_Tp>& operator-= (const _Tp&);
    valarray<_Tp>& operator^= (const _Tp&);
    valarray<_Tp>& operator&= (const _Tp&);
    valarray<_Tp>& operator|= (const _Tp&);
    valarray<_Tp>& operator<<=(const _Tp&);
    valarray<_Tp>& operator>>=(const _Tp&);
    valarray<_Tp>& operator*= (const valarray<_Tp>&);
    valarray<_Tp>& operator/= (const valarray<_Tp>&);
    valarray<_Tp>& operator%= (const valarray<_Tp>&);
    valarray<_Tp>& operator+= (const valarray<_Tp>&);
    valarray<_Tp>& operator-= (const valarray<_Tp>&);
    valarray<_Tp>& operator^= (const valarray<_Tp>&);
    valarray<_Tp>& operator|= (const valarray<_Tp>&);
    valarray<_Tp>& operator&= (const valarray<_Tp>&);
    valarray<_Tp>& operator<<=(const valarray<_Tp>&);
    valarray<_Tp>& operator>>=(const valarray<_Tp>&);

    template<class _Dom>
    valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
    template<class _Dom>
       valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);

      
    // _lib.valarray.members_ member functions:
    size_t size() const;
    _Tp    sum() const;	
    _Tp    min() const;	
    _Tp    max() const;	
    
    // FIXME: Extension
    _Tp    product () const;

    valarray<_Tp> shift (int) const;
    valarray<_Tp> cshift(int) const;
    _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
    _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
    void resize(size_t __size, _Tp __c = _Tp());
    
private:
    size_t _M_size;
    _Tp* __restrict__ _M_data;

    friend class _Array<_Tp>;
};


template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
    _Tp operator() (const _Tp& __t) const { return __t; }
};

template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
    _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
};

template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
    _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
};

template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
    _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
};

template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
    _Tp operator() (_Tp __t) const { return ~__t; }
};

template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
    _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
};

template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
    _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
};

  
template<typename _Tp>
inline _Tp
valarray<_Tp>::operator[] (size_t __i) const
{ return _M_data[__i]; }

template<typename _Tp>
inline _Tp&
valarray<_Tp>::operator[] (size_t __i)
{ return _M_data[__i]; }

} // extern "C++"

#include <std/slice.h>
#include <std/slice_array.h>
#include <std/gslice.h>
#include <std/gslice_array.h>
#include <std/mask_array.h>
#include <std/indirect_array.h>

extern "C++" {

template<typename _Tp>
inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}

template<typename _Tp>
inline valarray<_Tp>::valarray (size_t __n) 
        : _M_size (__n), _M_data (new _Tp[__n]) {}

template<typename _Tp>
inline valarray<_Tp>::valarray (const _Tp& __t, size_t __n)
        : _M_size (__n), _M_data (new _Tp[__n])
{ __valarray_fill (_M_data, _M_size, __t); }

template<typename _Tp>
inline valarray<_Tp>::valarray (const _Tp* __restrict__ __pT, size_t __n)
        : _M_size (__n), _M_data (new _Tp[__n])
{ __valarray_copy (__pT, __n, _M_data); }

template<typename _Tp>
inline valarray<_Tp>::valarray (const valarray<_Tp>& __v)
        : _M_size (__v._M_size), _M_data (new _Tp[__v._M_size])
{ __valarray_copy (__v._M_data, _M_size, _M_data); }

template<typename _Tp>
inline valarray<_Tp>::valarray (const slice_array<_Tp>& __sa)
        : _M_size (__sa._M_sz), _M_data (new _Tp[__sa._M_sz])
{ __valarray_copy (__sa._M_array, __sa._M_sz, __sa._M_stride,
                   _Array<_Tp>(_M_data)); }

template<typename _Tp>
inline valarray<_Tp>::valarray (const gslice_array<_Tp>& __ga)
        : _M_size (__ga._M_index.size()), _M_data (new _Tp[_M_size])
{ __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index), 
                   _Array<_Tp>(_M_data), _M_size); }

template<typename _Tp>
inline valarray<_Tp>::valarray (const mask_array<_Tp>& __ma)
        : _M_size (__ma._M_sz), _M_data (new _Tp[__ma._M_sz])
{ __valarray_copy (__ma._M_array, __ma._M_mask,
                   _Array<_Tp>(_M_data), _M_size); }

template<typename _Tp>
inline valarray<_Tp>::valarray (const indirect_array<_Tp>& __ia)
        : _M_size (__ia._M_sz), _M_data (new _Tp[__ia._M_sz])
{ __valarray_copy (__ia._M_array, __ia._M_index, 
                   _Array<_Tp>(_M_data), _M_size); }

template<typename _Tp> template<class _Dom>
inline valarray<_Tp>::valarray (const _Expr<_Dom, _Tp>& __e)
        : _M_size (__e.size ()), _M_data (new _Tp[_M_size])
{ __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data)); }

template<typename _Tp>
inline valarray<_Tp>::~valarray () { delete[] _M_data; }

template<typename _Tp>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const valarray<_Tp>& __v)
{
    __valarray_copy(__v._M_data, _M_size, _M_data);
    return *this;
}

template<typename _Tp>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const _Tp& __t)
{
    __valarray_fill (_M_data, _M_size, __t);
    return *this;
}

template<typename _Tp>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const slice_array<_Tp>& __sa)
{
    __valarray_copy (__sa._M_array, __sa._M_sz,
                     __sa._M_stride, _Array<_Tp>(_M_data));
    return *this;
}

template<typename _Tp>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const gslice_array<_Tp>& __ga)
{
    __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index),
                     _Array<_Tp>(_M_data), _M_size);
    return *this;
}

template<typename _Tp>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const mask_array<_Tp>& __ma)
{
    __valarray_copy (__ma._M_array, __ma._M_mask,
                     _Array<_Tp>(_M_data), _M_size);
    return *this;
}

template<typename _Tp>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const indirect_array<_Tp>& __ia)
{
    __valarray_copy (__ia._M_array, __ia._M_index,
                     _Array<_Tp>(_M_data), _M_size);
    return *this;
}

template<typename _Tp> template<class _Dom>
inline valarray<_Tp>&
valarray<_Tp>::operator= (const _Expr<_Dom, _Tp>& __e)
{
    __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data));
    return *this;
}

template<typename _Tp>
inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
valarray<_Tp>::operator[] (slice __s) const
{
    typedef _SClos<_ValArray,_Tp> _Closure;
    return _Expr<_Closure, _Tp> (_Closure (_Array<_Tp>(_M_data), __s));
}

template<typename _Tp>
inline slice_array<_Tp>
valarray<_Tp>::operator[] (slice __s)
{
    return slice_array<_Tp> (_Array<_Tp>(_M_data), __s);
}

template<typename _Tp>
inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
valarray<_Tp>::operator[] (const gslice& __gs) const
{
    typedef _GClos<_ValArray,_Tp> _Closure;
    return _Expr<_Closure, _Tp>
        (_Closure (_Array<_Tp>(_M_data), __gs._M_index->_M_index));
}

template<typename _Tp>
inline gslice_array<_Tp>
valarray<_Tp>::operator[] (const gslice& __gs)
{
    return gslice_array<_Tp>
        (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
}

template<typename _Tp>
inline valarray<_Tp>
valarray<_Tp>::operator[] (const valarray<bool>& __m) const
{
    size_t __s (0);
    size_t __e (__m.size ());
    for (size_t __i=0; __i<__e; ++__i)
        if (__m[__i]) ++__s;
    return valarray<_Tp> (mask_array<_Tp> (_Array<_Tp>(_M_data), __s,
                                           _Array<bool> (__m)));
}

template<typename _Tp>
inline mask_array<_Tp>
valarray<_Tp>::operator[] (const valarray<bool>& __m)
{
    size_t __s (0);
    size_t __e (__m.size ());
    for (size_t __i=0; __i<__e; ++__i)
        if (__m[__i]) ++__s;
    return mask_array<_Tp> (_Array<_Tp>(_M_data), __s, _Array<bool> (__m));
}

template<typename _Tp>
inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
valarray<_Tp>::operator[] (const valarray<size_t>& __i) const
{
    typedef _IClos<_ValArray,_Tp> _Closure;
    return _Expr<_Closure, _Tp> (_Closure (*this, __i));
}

template<typename _Tp>
inline indirect_array<_Tp>
valarray<_Tp>::operator[] (const valarray<size_t>& __i)
{
    return indirect_array<_Tp> (_Array<_Tp>(_M_data), __i.size(),
                                _Array<size_t> (__i));
}

template<class _Tp>
inline size_t valarray<_Tp>::size () const { return _M_size; }

template<class _Tp>
inline _Tp
valarray<_Tp>::sum () const
{
    return accumulate (_M_data, _M_data + _M_size, _Tp ());
}

template<typename _Tp>
inline _Tp
valarray<_Tp>::product () const
{
    return accumulate (_M_data, _M_data+_M_size, _Tp(1), multiplies<_Tp> ());
}

template <class _Tp>
inline valarray<_Tp>
valarray<_Tp>::shift (int __n) const
{
    _Tp* const __a = static_cast<_Tp*> (alloca (sizeof(_Tp) * _M_size));
    if (! __n)                          // __n == 0: no shift
        __valarray_copy (_M_data, _M_size, __a);
    else if (__n > 0) {                  // __n > 0: shift left
        if (__n > _M_size)
            __valarray_fill(__a, __n, _Tp());
        else {
            __valarray_copy (_M_data+__n, _M_size-__n, __a);
            __valarray_fill (__a+_M_size-__n, __n, _Tp());
        }
    }
    else {                             // __n < 0: shift right
        __valarray_copy (_M_data, _M_size+__n, __a-__n);
        __valarray_fill(__a, -__n, _Tp());
    }
    return valarray<_Tp> (__a, _M_size);
}

template <class _Tp>
inline valarray<_Tp>
valarray<_Tp>::cshift (int __n) const
{
    _Tp* const __a = static_cast<_Tp*> (alloca (sizeof(_Tp) * _M_size));
    if (! __n)                          // __n == 0: no cshift
        __valarray_copy(_M_data, _M_size, __a);
    else if (__n > 0) {                 // __n > 0: cshift left
        __valarray_copy (_M_data, __n, __a + _M_size-__n);
        __valarray_copy (_M_data + __n, _M_size-__n, __a);
    }
    else {                            // __n < 0: cshift right
        __valarray_copy (_M_data + _M_size + __n, -__n, __a);
        __valarray_copy (_M_data, _M_size + __n, __a - __n);
    }
    return valarray<_Tp> (__a, _M_size);
}

template <class _Tp>
inline void
valarray<_Tp>::resize (size_t __n, _Tp __c)
{
    if (_M_size != __n) {
        delete[] _M_data;
        _M_size = __n;
        _M_data = new _Tp[_M_size];
    }
    __valarray_fill (_M_data, _M_size, __c);
}

template<typename _Tp>
inline _Tp
valarray<_Tp>::min() const
{
    return *min_element (_M_data, _M_data+_M_size);
}

template<typename _Tp>
inline _Tp
valarray<_Tp>::max() const
{
    return *max_element (_M_data, _M_data+_M_size);
}

template<class _Tp>
inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
valarray<_Tp>::apply (_Tp func (_Tp)) const
{
    typedef _ValFunClos<_ValArray,_Tp> _Closure;
    return _Expr<_Closure,_Tp> (_Closure (*this, func));
}

template<class _Tp>
inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
valarray<_Tp>::apply (_Tp func (const _Tp &)) const
{
    typedef _RefFunClos<_ValArray,_Tp> _Closure;
    return _Expr<_Closure,_Tp> (_Closure (*this, func));
}

#define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
  template<typename _Tp>						\
  inline _Expr<_UnClos<_Name,_ValArray,_Tp>, _Tp>               	\
  valarray<_Tp>::operator##_Op() const					\
  {									\
      typedef _UnClos<_Name,_ValArray,_Tp> _Closure;	                \
      return _Expr<_Closure, _Tp> (_Closure (*this));			\
  }

    _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
    _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
    _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)

#undef _DEFINE_VALARRAY_UNARY_OPERATOR
  
  template<typename _Tp>
  inline _Expr<_UnClos<logical_not,_ValArray,_Tp>, bool>
  valarray<_Tp>::operator!() const
  {
      typedef _UnClos<logical_not,_ValArray,_Tp> _Closure;
      return _Expr<_Closure, bool> (_Closure (*this));
  }

#define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
  template<class _Tp>							\
  inline valarray<_Tp> &						\
  valarray<_Tp>::operator##_Op##= (const _Tp &__t)			\
  {									\
      _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, __t);	\
      return *this;							\
  }									\
									\
  template<class _Tp>							\
  inline valarray<_Tp> &						\
  valarray<_Tp>::operator##_Op##= (const valarray<_Tp> &__v)		\
  {									\
      _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, 		\
                               _Array<_Tp>(__v._M_data));		\
      return *this;							\
  }

_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, multiplies)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, divides)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, modulus)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, xor)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, and)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, or)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, shift_left)
_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, shift_right)

#undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT


#define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
  template<class _Tp> template<class _Dom>				\
  inline valarray<_Tp> &						\
  valarray<_Tp>::operator##_Op##= (const _Expr<_Dom,_Tp> &__e)		\
  {									\
      _Array_augmented_##_Name (_Array<_Tp>(_M_data), __e, _M_size);	\
      return *this;							\
  }

_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, plus)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, minus)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, multiplies)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, divides)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, modulus)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, xor)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, and)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, or)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, shift_left)
_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, shift_right)

#undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
    

#define _DEFINE_BINARY_OPERATOR(_Op, _Name)				\
  template<typename _Tp>						\
  inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, _Tp>        \
  operator##_Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w)	\
  {									\
      typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
      return _Expr<_Closure, _Tp> (_Closure (__v, __w));		\
  }									\
									\
  template<typename _Tp>						\
  inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,_Tp>         \
  operator##_Op (const valarray<_Tp> &__v, const _Tp &__t)		\
  {									\
      typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure;	\
      return _Expr<_Closure, _Tp> (_Closure (__v, __t));	        \
  }									\
									\
  template<typename _Tp>						\
  inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,_Tp>         \
  operator##_Op (const _Tp &__t, const valarray<_Tp> &__v)		\
  {									\
      typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
      return _Expr<_Closure, _Tp> (_Closure (__t, __v));        	\
  }

_DEFINE_BINARY_OPERATOR(+, plus)
_DEFINE_BINARY_OPERATOR(-, minus)
_DEFINE_BINARY_OPERATOR(*, multiplies)
_DEFINE_BINARY_OPERATOR(/, divides)
_DEFINE_BINARY_OPERATOR(%, modulus)
_DEFINE_BINARY_OPERATOR(^, _Bitwise_xor)
_DEFINE_BINARY_OPERATOR(&, _Bitwise_and)
_DEFINE_BINARY_OPERATOR(|, _Bitwise_or)
_DEFINE_BINARY_OPERATOR(<<, _Shift_left)
_DEFINE_BINARY_OPERATOR(>>, _Shift_right)

#undef _DEFINE_BINARY_OPERATOR

#define _DEFINE_LOGICAL_OPERATOR(_Op, _Name)				\
  template<typename _Tp>						\
  inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>,bool>        \
  operator##_Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w)	\
  {									\
      typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
      return _Expr<_Closure, bool> (_Closure (__v, __w));               \
  }									\
									\
  template<class _Tp>							\
  inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,bool>        \
  operator##_Op (const valarray<_Tp> &__v, const _Tp &__t)		\
  {									\
      typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure;     \
      return _Expr<_Closure, bool> (_Closure (__v, __t));       	\
  }									\
									\
  template<class _Tp>							\
  inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,bool>        \
  operator##_Op (const _Tp &__t, const valarray<_Tp> &__v)		\
  {									\
      typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
      return _Expr<_Closure, bool> (_Closure (__t, __v));	        \
  }

_DEFINE_LOGICAL_OPERATOR(&&, logical_and)
_DEFINE_LOGICAL_OPERATOR(||, logical_or)
_DEFINE_LOGICAL_OPERATOR(==, equal_to)
_DEFINE_LOGICAL_OPERATOR(!=, not_equal_to)
_DEFINE_LOGICAL_OPERATOR(<, less)
_DEFINE_LOGICAL_OPERATOR(>, greater)
_DEFINE_LOGICAL_OPERATOR(<=, less_equal)
_DEFINE_LOGICAL_OPERATOR(>=, greater_equal)

#undef _DEFINE_VALARRAY_OPERATOR

#undef _G_NO_VALARRAY_TEMPLATE_EXPORT

} // extern "C++"

#endif // __STD_VALARRAY__

// Local Variables:
// mode:c++
// End: