1 // The template and inlines for the -*- C++ -*- valarray class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 * This is a Standard C++ Library header.
36 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
38 #ifndef _GLIBCXX_VALARRAY
39 #define _GLIBCXX_VALARRAY 1
41 #pragma GCC system_header
43 #include <bits/c++config.h>
47 #include <debug/debug.h>
48 #include <initializer_list>
50 _GLIBCXX_BEGIN_NAMESPACE(std)
52 template<class _Clos, typename _Tp>
55 template<typename _Tp1, typename _Tp2>
58 template<class _Oper, template<class, class> class _Meta, class _Dom>
62 template<class, class> class _Meta1,
63 template<class, class> class _Meta2,
64 class _Dom1, class _Dom2>
67 template<template<class, class> class _Meta, class _Dom>
70 template<template<class, class> class _Meta, class _Dom>
73 template<template<class, class> class _Meta, class _Dom>
76 template<template<class, class> class _Meta, class _Dom>
79 template<template<class, class> class _Meta, class _Dom>
82 template<class _Tp> class valarray; // An array of type _Tp
83 class slice; // BLAS-like slice out of an array
84 template<class _Tp> class slice_array;
85 class gslice; // generalized slice out of an array
86 template<class _Tp> class gslice_array;
87 template<class _Tp> class mask_array; // masked array
88 template<class _Tp> class indirect_array; // indirected array
90 _GLIBCXX_END_NAMESPACE
92 #include <bits/valarray_array.h>
93 #include <bits/valarray_before.h>
95 _GLIBCXX_BEGIN_NAMESPACE(std)
98 * @brief Smart array designed to support numeric processing.
100 * A valarray is an array that provides constraints intended to allow for
101 * effective optimization of numeric array processing by reducing the
102 * aliasing that can result from pointer representations. It represents a
103 * one-dimensional array from which different multidimensional subsets can
104 * be accessed and modified.
106 * @param Tp Type of object in the array.
114 typedef typename __fun<_Op, _Tp>::result_type __rt;
115 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
118 typedef _Tp value_type;
120 // _lib.valarray.cons_ construct/destroy:
121 /// Construct an empty array.
124 /// Construct an array with @a n elements.
125 explicit valarray(size_t);
127 /// Construct an array with @a n elements initialized to @a t.
128 valarray(const _Tp&, size_t);
130 /// Construct an array initialized to the first @a n elements of @a t.
131 valarray(const _Tp* __restrict__, size_t);
133 /// Copy constructor.
134 valarray(const valarray&);
136 /// Construct an array with the same size and values in @a sa.
137 valarray(const slice_array<_Tp>&);
139 /// Construct an array with the same size and values in @a ga.
140 valarray(const gslice_array<_Tp>&);
142 /// Construct an array with the same size and values in @a ma.
143 valarray(const mask_array<_Tp>&);
145 /// Construct an array with the same size and values in @a ia.
146 valarray(const indirect_array<_Tp>&);
148 #ifdef __GXX_EXPERIMENTAL_CXX0X__
149 /// Construct an array with an initializer_list of values.
150 valarray(initializer_list<_Tp>);
154 valarray(const _Expr<_Dom, _Tp>& __e);
158 // _lib.valarray.assign_ assignment:
160 * @brief Assign elements to an array.
162 * Assign elements of array to values in @a v. Results are undefined
163 * if @a v does not have the same size as this array.
165 * @param v Valarray to get values from.
167 valarray<_Tp>& operator=(const valarray<_Tp>&);
170 * @brief Assign elements to a value.
172 * Assign all elements of array to @a t.
174 * @param t Value for elements.
176 valarray<_Tp>& operator=(const _Tp&);
179 * @brief Assign elements to an array subset.
181 * Assign elements of array to values in @a sa. Results are undefined
182 * if @a sa does not have the same size as this array.
184 * @param sa Array slice to get values from.
186 valarray<_Tp>& operator=(const slice_array<_Tp>&);
189 * @brief Assign elements to an array subset.
191 * Assign elements of array to values in @a ga. Results are undefined
192 * if @a ga does not have the same size as this array.
194 * @param ga Array slice to get values from.
196 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
199 * @brief Assign elements to an array subset.
201 * Assign elements of array to values in @a ma. Results are undefined
202 * if @a ma does not have the same size as this array.
204 * @param ma Array slice to get values from.
206 valarray<_Tp>& operator=(const mask_array<_Tp>&);
209 * @brief Assign elements to an array subset.
211 * Assign elements of array to values in @a ia. Results are undefined
212 * if @a ia does not have the same size as this array.
214 * @param ia Array slice to get values from.
216 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
218 #ifdef __GXX_EXPERIMENTAL_CXX0X__
220 * @brief Assign elements to an initializer_list.
222 * Assign elements of array to values in @a l. Results are undefined
223 * if @a l does not have the same size as this array.
225 * @param l initializer_list to get values from.
227 valarray& operator=(initializer_list<_Tp>);
230 template<class _Dom> valarray<_Tp>&
231 operator= (const _Expr<_Dom, _Tp>&);
233 // _lib.valarray.access_ element access:
235 * Return a reference to the i'th array element.
237 * @param i Index of element to return.
238 * @return Reference to the i'th element.
240 _Tp& operator[](size_t);
242 // _GLIBCXX_RESOLVE_LIB_DEFECTS
243 // 389. Const overload of valarray::operator[] returns by value.
244 const _Tp& operator[](size_t) const;
246 // _lib.valarray.sub_ subset operations:
248 * @brief Return an array subset.
250 * Returns a new valarray containing the elements of the array
251 * indicated by the slice argument. The new valarray has the same size
252 * as the input slice. @see slice.
254 * @param s The source slice.
255 * @return New valarray containing elements in @a s.
257 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
260 * @brief Return a reference to an array subset.
262 * Returns a new valarray containing the elements of the array
263 * indicated by the slice argument. The new valarray has the same size
264 * as the input slice. @see slice.
266 * @param s The source slice.
267 * @return New valarray containing elements in @a s.
269 slice_array<_Tp> operator[](slice);
272 * @brief Return an array subset.
274 * Returns a slice_array referencing the elements of the array
275 * indicated by the slice argument. @see gslice.
277 * @param s The source slice.
278 * @return Slice_array referencing elements indicated by @a s.
280 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
283 * @brief Return a reference to an array subset.
285 * Returns a new valarray containing the elements of the array
286 * indicated by the gslice argument. The new valarray has
287 * the same size as the input gslice. @see gslice.
289 * @param s The source gslice.
290 * @return New valarray containing elements in @a s.
292 gslice_array<_Tp> operator[](const gslice&);
295 * @brief Return an array subset.
297 * Returns a new valarray containing the elements of the array
298 * indicated by the argument. The input is a valarray of bool which
299 * represents a bitmask indicating which elements should be copied into
300 * the new valarray. Each element of the array is added to the return
301 * valarray if the corresponding element of the argument is true.
303 * @param m The valarray bitmask.
304 * @return New valarray containing elements indicated by @a m.
306 valarray<_Tp> operator[](const valarray<bool>&) const;
309 * @brief Return a reference to an array subset.
311 * Returns a new mask_array referencing the elements of the array
312 * indicated by the argument. The input is a valarray of bool which
313 * represents a bitmask indicating which elements are part of the
314 * subset. Elements of the array are part of the subset if the
315 * corresponding element of the argument is true.
317 * @param m The valarray bitmask.
318 * @return New valarray containing elements indicated by @a m.
320 mask_array<_Tp> operator[](const valarray<bool>&);
323 * @brief Return an array subset.
325 * Returns a new valarray containing the elements of the array
326 * indicated by the argument. The elements in the argument are
327 * interpreted as the indices of elements of this valarray to copy to
328 * the return valarray.
330 * @param i The valarray element index list.
331 * @return New valarray containing elements in @a s.
333 _Expr<_IClos<_ValArray, _Tp>, _Tp>
334 operator[](const valarray<size_t>&) const;
337 * @brief Return a reference to an array subset.
339 * Returns an indirect_array referencing the elements of the array
340 * indicated by the argument. The elements in the argument are
341 * interpreted as the indices of elements of this valarray to include
342 * in the subset. The returned indirect_array refers to these
345 * @param i The valarray element index list.
346 * @return Indirect_array referencing elements in @a i.
348 indirect_array<_Tp> operator[](const valarray<size_t>&);
350 // _lib.valarray.unary_ unary operators:
351 /// Return a new valarray by applying unary + to each element.
352 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
354 /// Return a new valarray by applying unary - to each element.
355 typename _UnaryOp<__negate>::_Rt operator-() const;
357 /// Return a new valarray by applying unary ~ to each element.
358 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
360 /// Return a new valarray by applying unary ! to each element.
361 typename _UnaryOp<__logical_not>::_Rt operator!() const;
363 // _lib.valarray.cassign_ computed assignment:
364 /// Multiply each element of array by @a t.
365 valarray<_Tp>& operator*=(const _Tp&);
367 /// Divide each element of array by @a t.
368 valarray<_Tp>& operator/=(const _Tp&);
370 /// Set each element e of array to e % @a t.
371 valarray<_Tp>& operator%=(const _Tp&);
373 /// Add @a t to each element of array.
374 valarray<_Tp>& operator+=(const _Tp&);
376 /// Subtract @a t to each element of array.
377 valarray<_Tp>& operator-=(const _Tp&);
379 /// Set each element e of array to e ^ @a t.
380 valarray<_Tp>& operator^=(const _Tp&);
382 /// Set each element e of array to e & @a t.
383 valarray<_Tp>& operator&=(const _Tp&);
385 /// Set each element e of array to e | @a t.
386 valarray<_Tp>& operator|=(const _Tp&);
388 /// Left shift each element e of array by @a t bits.
389 valarray<_Tp>& operator<<=(const _Tp&);
391 /// Right shift each element e of array by @a t bits.
392 valarray<_Tp>& operator>>=(const _Tp&);
394 /// Multiply elements of array by corresponding elements of @a v.
395 valarray<_Tp>& operator*=(const valarray<_Tp>&);
397 /// Divide elements of array by corresponding elements of @a v.
398 valarray<_Tp>& operator/=(const valarray<_Tp>&);
400 /// Modulo elements of array by corresponding elements of @a v.
401 valarray<_Tp>& operator%=(const valarray<_Tp>&);
403 /// Add corresponding elements of @a v to elements of array.
404 valarray<_Tp>& operator+=(const valarray<_Tp>&);
406 /// Subtract corresponding elements of @a v from elements of array.
407 valarray<_Tp>& operator-=(const valarray<_Tp>&);
409 /// Logical xor corresponding elements of @a v with elements of array.
410 valarray<_Tp>& operator^=(const valarray<_Tp>&);
412 /// Logical or corresponding elements of @a v with elements of array.
413 valarray<_Tp>& operator|=(const valarray<_Tp>&);
415 /// Logical and corresponding elements of @a v with elements of array.
416 valarray<_Tp>& operator&=(const valarray<_Tp>&);
418 /// Left shift elements of array by corresponding elements of @a v.
419 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
421 /// Right shift elements of array by corresponding elements of @a v.
422 valarray<_Tp>& operator>>=(const valarray<_Tp>&);
425 valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
427 valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
429 valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
431 valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
433 valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
435 valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
437 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
439 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
441 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
443 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
445 // _lib.valarray.members_ member functions:
446 /// Return the number of elements in array.
450 * @brief Return the sum of all elements in the array.
452 * Accumulates the sum of all elements into a Tp using +=. The order
453 * of adding the elements is unspecified.
457 /// Return the minimum element using operator<().
460 /// Return the maximum element using operator<().
464 * @brief Return a shifted array.
466 * A new valarray is constructed as a copy of this array with elements
467 * in shifted positions. For an element with index i, the new position
468 * is i - n. The new valarray has the same size as the current one.
469 * New elements without a value are set to 0. Elements whose new
470 * position is outside the bounds of the array are discarded.
472 * Positive arguments shift toward index 0, discarding elements [0, n).
473 * Negative arguments discard elements from the top of the array.
475 * @param n Number of element positions to shift.
476 * @return New valarray with elements in shifted positions.
478 valarray<_Tp> shift (int) const;
481 * @brief Return a rotated array.
483 * A new valarray is constructed as a copy of this array with elements
484 * in shifted positions. For an element with index i, the new position
485 * is (i - n) % size(). The new valarray has the same size as the
486 * current one. Elements that are shifted beyond the array bounds are
487 * shifted into the other end of the array. No elements are lost.
489 * Positive arguments shift toward index 0, wrapping around the top.
490 * Negative arguments shift towards the top, wrapping around to 0.
492 * @param n Number of element positions to rotate.
493 * @return New valarray with elements in shifted positions.
495 valarray<_Tp> cshift(int) const;
498 * @brief Apply a function to the array.
500 * Returns a new valarray with elements assigned to the result of
501 * applying func to the corresponding element of this array. The new
502 * array has the same size as this one.
504 * @param func Function of Tp returning Tp to apply.
505 * @return New valarray with transformed elements.
507 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
510 * @brief Apply a function to the array.
512 * Returns a new valarray with elements assigned to the result of
513 * applying func to the corresponding element of this array. The new
514 * array has the same size as this one.
516 * @param func Function of const Tp& returning Tp to apply.
517 * @return New valarray with transformed elements.
519 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
522 * @brief Resize array.
524 * Resize this array to @a size and set all elements to @a c. All
525 * references and iterators are invalidated.
527 * @param size New array size.
528 * @param c New value for all elements.
530 void resize(size_t __size, _Tp __c = _Tp());
534 _Tp* __restrict__ _M_data;
536 friend class _Array<_Tp>;
539 template<typename _Tp>
541 valarray<_Tp>::operator[](size_t __i) const
543 __glibcxx_requires_subscript(__i);
547 template<typename _Tp>
549 valarray<_Tp>::operator[](size_t __i)
551 __glibcxx_requires_subscript(__i);
555 _GLIBCXX_END_NAMESPACE
557 #include <bits/valarray_after.h>
558 #include <bits/slice_array.h>
559 #include <bits/gslice.h>
560 #include <bits/gslice_array.h>
561 #include <bits/mask_array.h>
562 #include <bits/indirect_array.h>
564 _GLIBCXX_BEGIN_NAMESPACE(std)
566 template<typename _Tp>
568 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
570 template<typename _Tp>
572 valarray<_Tp>::valarray(size_t __n)
573 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
574 { std::__valarray_default_construct(_M_data, _M_data + __n); }
576 template<typename _Tp>
578 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
579 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
580 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
582 template<typename _Tp>
584 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
585 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
587 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
588 std::__valarray_copy_construct(__p, __p + __n, _M_data);
591 template<typename _Tp>
593 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
594 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
595 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
598 template<typename _Tp>
600 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
601 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
603 std::__valarray_copy_construct
604 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
607 template<typename _Tp>
609 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
610 : _M_size(__ga._M_index.size()),
611 _M_data(__valarray_get_storage<_Tp>(_M_size))
613 std::__valarray_copy_construct
614 (__ga._M_array, _Array<size_t>(__ga._M_index),
615 _Array<_Tp>(_M_data), _M_size);
618 template<typename _Tp>
620 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
621 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
623 std::__valarray_copy_construct
624 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
627 template<typename _Tp>
629 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
630 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
632 std::__valarray_copy_construct
633 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
636 #ifdef __GXX_EXPERIMENTAL_CXX0X__
637 template<typename _Tp>
639 valarray<_Tp>::valarray(initializer_list<_Tp> __l)
640 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
641 { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
644 template<typename _Tp> template<class _Dom>
646 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
647 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
648 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
650 template<typename _Tp>
652 valarray<_Tp>::~valarray()
654 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
655 std::__valarray_release_memory(_M_data);
658 template<typename _Tp>
659 inline valarray<_Tp>&
660 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
662 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
663 std::__valarray_copy(__v._M_data, _M_size, _M_data);
667 #ifdef __GXX_EXPERIMENTAL_CXX0X__
668 template<typename _Tp>
669 inline valarray<_Tp>&
670 valarray<_Tp>::operator=(initializer_list<_Tp> __l)
672 _GLIBCXX_DEBUG_ASSERT(_M_size == __l.size());
673 std::__valarray_copy(__l.begin(), __l.size(), _M_data);
677 template<typename _Tp>
678 inline valarray<_Tp>&
679 valarray<_Tp>::operator=(const _Tp& __t)
681 std::__valarray_fill(_M_data, _M_size, __t);
685 template<typename _Tp>
686 inline valarray<_Tp>&
687 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
689 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
690 std::__valarray_copy(__sa._M_array, __sa._M_sz,
691 __sa._M_stride, _Array<_Tp>(_M_data));
695 template<typename _Tp>
696 inline valarray<_Tp>&
697 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
699 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
700 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
701 _Array<_Tp>(_M_data), _M_size);
705 template<typename _Tp>
706 inline valarray<_Tp>&
707 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
709 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
710 std::__valarray_copy(__ma._M_array, __ma._M_mask,
711 _Array<_Tp>(_M_data), _M_size);
715 template<typename _Tp>
716 inline valarray<_Tp>&
717 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
719 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
720 std::__valarray_copy(__ia._M_array, __ia._M_index,
721 _Array<_Tp>(_M_data), _M_size);
725 template<typename _Tp> template<class _Dom>
726 inline valarray<_Tp>&
727 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
729 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
730 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
734 template<typename _Tp>
735 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
736 valarray<_Tp>::operator[](slice __s) const
738 typedef _SClos<_ValArray,_Tp> _Closure;
739 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
742 template<typename _Tp>
743 inline slice_array<_Tp>
744 valarray<_Tp>::operator[](slice __s)
745 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
747 template<typename _Tp>
748 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
749 valarray<_Tp>::operator[](const gslice& __gs) const
751 typedef _GClos<_ValArray,_Tp> _Closure;
752 return _Expr<_Closure, _Tp>
753 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
756 template<typename _Tp>
757 inline gslice_array<_Tp>
758 valarray<_Tp>::operator[](const gslice& __gs)
760 return gslice_array<_Tp>
761 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
764 template<typename _Tp>
766 valarray<_Tp>::operator[](const valarray<bool>& __m) const
769 size_t __e = __m.size();
770 for (size_t __i=0; __i<__e; ++__i)
772 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
773 _Array<bool> (__m)));
776 template<typename _Tp>
777 inline mask_array<_Tp>
778 valarray<_Tp>::operator[](const valarray<bool>& __m)
781 size_t __e = __m.size();
782 for (size_t __i=0; __i<__e; ++__i)
784 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
787 template<typename _Tp>
788 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
789 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
791 typedef _IClos<_ValArray,_Tp> _Closure;
792 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
795 template<typename _Tp>
796 inline indirect_array<_Tp>
797 valarray<_Tp>::operator[](const valarray<size_t>& __i)
799 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
800 _Array<size_t>(__i));
805 valarray<_Tp>::size() const
810 valarray<_Tp>::sum() const
812 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
813 return std::__valarray_sum(_M_data, _M_data + _M_size);
818 valarray<_Tp>::shift(int __n) const
825 _Tp* __restrict__ __tmp_M_data =
826 std::__valarray_get_storage<_Tp>(_M_size);
829 std::__valarray_copy_construct(_M_data,
830 _M_data + _M_size, __tmp_M_data);
831 else if (__n > 0) // shift left
833 if (size_t(__n) > _M_size)
836 std::__valarray_copy_construct(_M_data + __n,
837 _M_data + _M_size, __tmp_M_data);
838 std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
839 __tmp_M_data + _M_size);
843 if (-size_t(__n) > _M_size)
846 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
848 std::__valarray_default_construct(__tmp_M_data,
852 __ret._M_size = _M_size;
853 __ret._M_data = __tmp_M_data;
859 valarray<_Tp>::cshift(int __n) const
866 _Tp* __restrict__ __tmp_M_data =
867 std::__valarray_get_storage<_Tp>(_M_size);
870 std::__valarray_copy_construct(_M_data,
871 _M_data + _M_size, __tmp_M_data);
872 else if (__n > 0) // cshift left
874 if (size_t(__n) > _M_size)
875 __n = int(__n % _M_size);
877 std::__valarray_copy_construct(_M_data, _M_data + __n,
878 __tmp_M_data + _M_size - __n);
879 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
884 if (-size_t(__n) > _M_size)
885 __n = -int(-size_t(__n) % _M_size);
887 std::__valarray_copy_construct(_M_data + _M_size + __n,
888 _M_data + _M_size, __tmp_M_data);
889 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
893 __ret._M_size = _M_size;
894 __ret._M_data = __tmp_M_data;
900 valarray<_Tp>::resize(size_t __n, _Tp __c)
902 // This complication is so to make valarray<valarray<T> > work
903 // even though it is not required by the standard. Nobody should
904 // be saying valarray<valarray<T> > anyway. See the specs.
905 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
908 std::__valarray_release_memory(_M_data);
910 _M_data = __valarray_get_storage<_Tp>(__n);
912 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
915 template<typename _Tp>
917 valarray<_Tp>::min() const
919 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
920 return *std::min_element(_M_data, _M_data + _M_size);
923 template<typename _Tp>
925 valarray<_Tp>::max() const
927 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
928 return *std::max_element(_M_data, _M_data + _M_size);
932 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
933 valarray<_Tp>::apply(_Tp func(_Tp)) const
935 typedef _ValFunClos<_ValArray, _Tp> _Closure;
936 return _Expr<_Closure, _Tp>(_Closure(*this, func));
940 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
941 valarray<_Tp>::apply(_Tp func(const _Tp &)) const
943 typedef _RefFunClos<_ValArray, _Tp> _Closure;
944 return _Expr<_Closure, _Tp>(_Closure(*this, func));
947 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
948 template<typename _Tp> \
949 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
950 valarray<_Tp>::operator _Op() const \
952 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
953 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
954 return _Expr<_Closure, _Rt>(_Closure(*this)); \
957 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
958 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
959 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
960 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
962 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
964 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
965 template<class _Tp> \
966 inline valarray<_Tp>& \
967 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
969 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
973 template<class _Tp> \
974 inline valarray<_Tp>& \
975 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
977 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
978 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
979 _Array<_Tp>(__v._M_data)); \
983 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
984 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
985 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
986 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
987 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
988 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
989 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
990 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
991 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
992 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
994 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
996 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
997 template<class _Tp> template<class _Dom> \
998 inline valarray<_Tp>& \
999 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
1001 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
1005 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1006 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1007 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1008 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1009 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1010 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1011 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1012 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1013 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1014 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1016 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1019 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
1020 template<typename _Tp> \
1021 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
1022 typename __fun<_Name, _Tp>::result_type> \
1023 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1025 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
1026 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1027 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1028 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
1031 template<typename _Tp> \
1032 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
1033 typename __fun<_Name, _Tp>::result_type> \
1034 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
1036 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1037 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1038 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
1041 template<typename _Tp> \
1042 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
1043 typename __fun<_Name, _Tp>::result_type> \
1044 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
1046 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1047 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1048 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
1051 _DEFINE_BINARY_OPERATOR(+, __plus)
1052 _DEFINE_BINARY_OPERATOR(-, __minus)
1053 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1054 _DEFINE_BINARY_OPERATOR(/, __divides)
1055 _DEFINE_BINARY_OPERATOR(%, __modulus)
1056 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1057 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1058 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1059 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1060 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1061 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1062 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1063 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1064 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1065 _DEFINE_BINARY_OPERATOR(<, __less)
1066 _DEFINE_BINARY_OPERATOR(>, __greater)
1067 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1068 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1070 #undef _DEFINE_BINARY_OPERATOR
1072 _GLIBCXX_END_NAMESPACE
1074 #endif /* _GLIBCXX_VALARRAY */