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 * @defgroup numeric_arrays Numeric Arrays
101 * Classes and functions for representing and manipulating arrays of elements.
106 * @brief Smart array designed to support numeric processing.
108 * A valarray is an array that provides constraints intended to allow for
109 * effective optimization of numeric array processing by reducing the
110 * aliasing that can result from pointer representations. It represents a
111 * one-dimensional array from which different multidimensional subsets can
112 * be accessed and modified.
114 * @param Tp Type of object in the array.
122 typedef typename __fun<_Op, _Tp>::result_type __rt;
123 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
126 typedef _Tp value_type;
128 // _lib.valarray.cons_ construct/destroy:
129 /// Construct an empty array.
132 /// Construct an array with @a n elements.
133 explicit valarray(size_t);
135 /// Construct an array with @a n elements initialized to @a t.
136 valarray(const _Tp&, size_t);
138 /// Construct an array initialized to the first @a n elements of @a t.
139 valarray(const _Tp* __restrict__, size_t);
141 /// Copy constructor.
142 valarray(const valarray&);
144 /// Construct an array with the same size and values in @a sa.
145 valarray(const slice_array<_Tp>&);
147 /// Construct an array with the same size and values in @a ga.
148 valarray(const gslice_array<_Tp>&);
150 /// Construct an array with the same size and values in @a ma.
151 valarray(const mask_array<_Tp>&);
153 /// Construct an array with the same size and values in @a ia.
154 valarray(const indirect_array<_Tp>&);
156 #ifdef __GXX_EXPERIMENTAL_CXX0X__
157 /// Construct an array with an initializer_list of values.
158 valarray(initializer_list<_Tp>);
162 valarray(const _Expr<_Dom, _Tp>& __e);
166 // _lib.valarray.assign_ assignment:
168 * @brief Assign elements to an array.
170 * Assign elements of array to values in @a v. Results are undefined
171 * if @a v does not have the same size as this array.
173 * @param v Valarray to get values from.
175 valarray<_Tp>& operator=(const valarray<_Tp>&);
178 * @brief Assign elements to a value.
180 * Assign all elements of array to @a t.
182 * @param t Value for elements.
184 valarray<_Tp>& operator=(const _Tp&);
187 * @brief Assign elements to an array subset.
189 * Assign elements of array to values in @a sa. Results are undefined
190 * if @a sa does not have the same size as this array.
192 * @param sa Array slice to get values from.
194 valarray<_Tp>& operator=(const slice_array<_Tp>&);
197 * @brief Assign elements to an array subset.
199 * Assign elements of array to values in @a ga. Results are undefined
200 * if @a ga does not have the same size as this array.
202 * @param ga Array slice to get values from.
204 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
207 * @brief Assign elements to an array subset.
209 * Assign elements of array to values in @a ma. Results are undefined
210 * if @a ma does not have the same size as this array.
212 * @param ma Array slice to get values from.
214 valarray<_Tp>& operator=(const mask_array<_Tp>&);
217 * @brief Assign elements to an array subset.
219 * Assign elements of array to values in @a ia. Results are undefined
220 * if @a ia does not have the same size as this array.
222 * @param ia Array slice to get values from.
224 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
228 * @brief Assign elements to an initializer_list.
230 * Assign elements of array to values in @a l. Results are undefined
231 * if @a l does not have the same size as this array.
233 * @param l initializer_list to get values from.
235 valarray& operator=(initializer_list<_Tp>);
238 template<class _Dom> valarray<_Tp>&
239 operator= (const _Expr<_Dom, _Tp>&);
241 // _lib.valarray.access_ element access:
243 * Return a reference to the i'th array element.
245 * @param i Index of element to return.
246 * @return Reference to the i'th element.
248 _Tp& operator[](size_t);
250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
251 // 389. Const overload of valarray::operator[] returns by value.
252 const _Tp& operator[](size_t) const;
254 // _lib.valarray.sub_ subset operations:
256 * @brief Return an array subset.
258 * Returns a new valarray containing the elements of the array
259 * indicated by the slice argument. The new valarray has the same size
260 * as the input slice. @see slice.
262 * @param s The source slice.
263 * @return New valarray containing elements in @a s.
265 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
268 * @brief Return a reference to an array subset.
270 * Returns a new valarray containing the elements of the array
271 * indicated by the slice argument. The new valarray has the same size
272 * as the input slice. @see slice.
274 * @param s The source slice.
275 * @return New valarray containing elements in @a s.
277 slice_array<_Tp> operator[](slice);
280 * @brief Return an array subset.
282 * Returns a slice_array referencing the elements of the array
283 * indicated by the slice argument. @see gslice.
285 * @param s The source slice.
286 * @return Slice_array referencing elements indicated by @a s.
288 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
291 * @brief Return a reference to an array subset.
293 * Returns a new valarray containing the elements of the array
294 * indicated by the gslice argument. The new valarray has
295 * the same size as the input gslice. @see gslice.
297 * @param s The source gslice.
298 * @return New valarray containing elements in @a s.
300 gslice_array<_Tp> operator[](const gslice&);
303 * @brief Return an array subset.
305 * Returns a new valarray containing the elements of the array
306 * indicated by the argument. The input is a valarray of bool which
307 * represents a bitmask indicating which elements should be copied into
308 * the new valarray. Each element of the array is added to the return
309 * valarray if the corresponding element of the argument is true.
311 * @param m The valarray bitmask.
312 * @return New valarray containing elements indicated by @a m.
314 valarray<_Tp> operator[](const valarray<bool>&) const;
317 * @brief Return a reference to an array subset.
319 * Returns a new mask_array referencing the elements of the array
320 * indicated by the argument. The input is a valarray of bool which
321 * represents a bitmask indicating which elements are part of the
322 * subset. Elements of the array are part of the subset if the
323 * corresponding element of the argument is true.
325 * @param m The valarray bitmask.
326 * @return New valarray containing elements indicated by @a m.
328 mask_array<_Tp> operator[](const valarray<bool>&);
331 * @brief Return an array subset.
333 * Returns a new valarray containing the elements of the array
334 * indicated by the argument. The elements in the argument are
335 * interpreted as the indices of elements of this valarray to copy to
336 * the return valarray.
338 * @param i The valarray element index list.
339 * @return New valarray containing elements in @a s.
341 _Expr<_IClos<_ValArray, _Tp>, _Tp>
342 operator[](const valarray<size_t>&) const;
345 * @brief Return a reference to an array subset.
347 * Returns an indirect_array referencing the elements of the array
348 * indicated by the argument. The elements in the argument are
349 * interpreted as the indices of elements of this valarray to include
350 * in the subset. The returned indirect_array refers to these
353 * @param i The valarray element index list.
354 * @return Indirect_array referencing elements in @a i.
356 indirect_array<_Tp> operator[](const valarray<size_t>&);
358 // _lib.valarray.unary_ unary operators:
359 /// Return a new valarray by applying unary + to each element.
360 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
362 /// Return a new valarray by applying unary - to each element.
363 typename _UnaryOp<__negate>::_Rt operator-() const;
365 /// Return a new valarray by applying unary ~ to each element.
366 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
368 /// Return a new valarray by applying unary ! to each element.
369 typename _UnaryOp<__logical_not>::_Rt operator!() const;
371 // _lib.valarray.cassign_ computed assignment:
372 /// Multiply each element of array by @a t.
373 valarray<_Tp>& operator*=(const _Tp&);
375 /// Divide each element of array by @a t.
376 valarray<_Tp>& operator/=(const _Tp&);
378 /// Set each element e of array to e % @a t.
379 valarray<_Tp>& operator%=(const _Tp&);
381 /// Add @a t to each element of array.
382 valarray<_Tp>& operator+=(const _Tp&);
384 /// Subtract @a t to each element of array.
385 valarray<_Tp>& operator-=(const _Tp&);
387 /// Set each element e of array to e ^ @a t.
388 valarray<_Tp>& operator^=(const _Tp&);
390 /// Set each element e of array to e & @a t.
391 valarray<_Tp>& operator&=(const _Tp&);
393 /// Set each element e of array to e | @a t.
394 valarray<_Tp>& operator|=(const _Tp&);
396 /// Left shift each element e of array by @a t bits.
397 valarray<_Tp>& operator<<=(const _Tp&);
399 /// Right shift each element e of array by @a t bits.
400 valarray<_Tp>& operator>>=(const _Tp&);
402 /// Multiply elements of array by corresponding elements of @a v.
403 valarray<_Tp>& operator*=(const valarray<_Tp>&);
405 /// Divide elements of array by corresponding elements of @a v.
406 valarray<_Tp>& operator/=(const valarray<_Tp>&);
408 /// Modulo elements of array by corresponding elements of @a v.
409 valarray<_Tp>& operator%=(const valarray<_Tp>&);
411 /// Add corresponding elements of @a v to elements of array.
412 valarray<_Tp>& operator+=(const valarray<_Tp>&);
414 /// Subtract corresponding elements of @a v from elements of array.
415 valarray<_Tp>& operator-=(const valarray<_Tp>&);
417 /// Logical xor corresponding elements of @a v with elements of array.
418 valarray<_Tp>& operator^=(const valarray<_Tp>&);
420 /// Logical or corresponding elements of @a v with elements of array.
421 valarray<_Tp>& operator|=(const valarray<_Tp>&);
423 /// Logical and corresponding elements of @a v with elements of array.
424 valarray<_Tp>& operator&=(const valarray<_Tp>&);
426 /// Left shift elements of array by corresponding elements of @a v.
427 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
429 /// Right shift elements of array by corresponding elements of @a v.
430 valarray<_Tp>& operator>>=(const valarray<_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 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
447 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
449 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
451 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
453 // _lib.valarray.members_ member functions:
454 /// Return the number of elements in array.
458 * @brief Return the sum of all elements in the array.
460 * Accumulates the sum of all elements into a Tp using +=. The order
461 * of adding the elements is unspecified.
465 /// Return the minimum element using operator<().
468 /// Return the maximum element using operator<().
472 * @brief Return a shifted array.
474 * A new valarray is constructed as a copy of this array with elements
475 * in shifted positions. For an element with index i, the new position
476 * is i - n. The new valarray has the same size as the current one.
477 * New elements without a value are set to 0. Elements whose new
478 * position is outside the bounds of the array are discarded.
480 * Positive arguments shift toward index 0, discarding elements [0, n).
481 * Negative arguments discard elements from the top of the array.
483 * @param n Number of element positions to shift.
484 * @return New valarray with elements in shifted positions.
486 valarray<_Tp> shift (int) const;
489 * @brief Return a rotated array.
491 * A new valarray is constructed as a copy of this array with elements
492 * in shifted positions. For an element with index i, the new position
493 * is (i - n) % size(). The new valarray has the same size as the
494 * current one. Elements that are shifted beyond the array bounds are
495 * shifted into the other end of the array. No elements are lost.
497 * Positive arguments shift toward index 0, wrapping around the top.
498 * Negative arguments shift towards the top, wrapping around to 0.
500 * @param n Number of element positions to rotate.
501 * @return New valarray with elements in shifted positions.
503 valarray<_Tp> cshift(int) const;
506 * @brief Apply a function to the array.
508 * Returns a new valarray with elements assigned to the result of
509 * applying func to the corresponding element of this array. The new
510 * array has the same size as this one.
512 * @param func Function of Tp returning Tp to apply.
513 * @return New valarray with transformed elements.
515 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
518 * @brief Apply a function to the array.
520 * Returns a new valarray with elements assigned to the result of
521 * applying func to the corresponding element of this array. The new
522 * array has the same size as this one.
524 * @param func Function of const Tp& returning Tp to apply.
525 * @return New valarray with transformed elements.
527 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
530 * @brief Resize array.
532 * Resize this array to @a size and set all elements to @a c. All
533 * references and iterators are invalidated.
535 * @param size New array size.
536 * @param c New value for all elements.
538 void resize(size_t __size, _Tp __c = _Tp());
542 _Tp* __restrict__ _M_data;
544 friend class _Array<_Tp>;
547 template<typename _Tp>
549 valarray<_Tp>::operator[](size_t __i) const
551 __glibcxx_requires_subscript(__i);
555 template<typename _Tp>
557 valarray<_Tp>::operator[](size_t __i)
559 __glibcxx_requires_subscript(__i);
563 // @} group numeric_arrays
565 _GLIBCXX_END_NAMESPACE
567 #include <bits/valarray_after.h>
568 #include <bits/slice_array.h>
569 #include <bits/gslice.h>
570 #include <bits/gslice_array.h>
571 #include <bits/mask_array.h>
572 #include <bits/indirect_array.h>
574 _GLIBCXX_BEGIN_NAMESPACE(std)
577 * @addtogroup numeric_arrays
581 template<typename _Tp>
583 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
585 template<typename _Tp>
587 valarray<_Tp>::valarray(size_t __n)
588 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
589 { std::__valarray_default_construct(_M_data, _M_data + __n); }
591 template<typename _Tp>
593 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
594 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
595 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
597 template<typename _Tp>
599 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
600 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
602 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
603 std::__valarray_copy_construct(__p, __p + __n, _M_data);
606 template<typename _Tp>
608 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
609 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
610 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
613 template<typename _Tp>
615 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
616 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
618 std::__valarray_copy_construct
619 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
622 template<typename _Tp>
624 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
625 : _M_size(__ga._M_index.size()),
626 _M_data(__valarray_get_storage<_Tp>(_M_size))
628 std::__valarray_copy_construct
629 (__ga._M_array, _Array<size_t>(__ga._M_index),
630 _Array<_Tp>(_M_data), _M_size);
633 template<typename _Tp>
635 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
636 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
638 std::__valarray_copy_construct
639 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
642 template<typename _Tp>
644 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
645 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
647 std::__valarray_copy_construct
648 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
651 #ifdef __GXX_EXPERIMENTAL_CXX0X__
652 template<typename _Tp>
654 valarray<_Tp>::valarray(initializer_list<_Tp> __l)
655 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
656 { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
659 template<typename _Tp> template<class _Dom>
661 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
662 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
663 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
665 template<typename _Tp>
667 valarray<_Tp>::~valarray()
669 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
670 std::__valarray_release_memory(_M_data);
673 template<typename _Tp>
674 inline valarray<_Tp>&
675 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
677 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
678 std::__valarray_copy(__v._M_data, _M_size, _M_data);
682 #ifdef __GXX_EXPERIMENTAL_CXX0X__
683 template<typename _Tp>
684 inline valarray<_Tp>&
685 valarray<_Tp>::operator=(initializer_list<_Tp> __l)
687 _GLIBCXX_DEBUG_ASSERT(_M_size == __l.size());
688 std::__valarray_copy(__l.begin(), __l.size(), _M_data);
692 template<typename _Tp>
693 inline valarray<_Tp>&
694 valarray<_Tp>::operator=(const _Tp& __t)
696 std::__valarray_fill(_M_data, _M_size, __t);
700 template<typename _Tp>
701 inline valarray<_Tp>&
702 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
704 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
705 std::__valarray_copy(__sa._M_array, __sa._M_sz,
706 __sa._M_stride, _Array<_Tp>(_M_data));
710 template<typename _Tp>
711 inline valarray<_Tp>&
712 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
714 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
715 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
716 _Array<_Tp>(_M_data), _M_size);
720 template<typename _Tp>
721 inline valarray<_Tp>&
722 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
724 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
725 std::__valarray_copy(__ma._M_array, __ma._M_mask,
726 _Array<_Tp>(_M_data), _M_size);
730 template<typename _Tp>
731 inline valarray<_Tp>&
732 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
734 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
735 std::__valarray_copy(__ia._M_array, __ia._M_index,
736 _Array<_Tp>(_M_data), _M_size);
740 template<typename _Tp> template<class _Dom>
741 inline valarray<_Tp>&
742 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
744 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
745 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
749 template<typename _Tp>
750 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
751 valarray<_Tp>::operator[](slice __s) const
753 typedef _SClos<_ValArray,_Tp> _Closure;
754 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
757 template<typename _Tp>
758 inline slice_array<_Tp>
759 valarray<_Tp>::operator[](slice __s)
760 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
762 template<typename _Tp>
763 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
764 valarray<_Tp>::operator[](const gslice& __gs) const
766 typedef _GClos<_ValArray,_Tp> _Closure;
767 return _Expr<_Closure, _Tp>
768 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
771 template<typename _Tp>
772 inline gslice_array<_Tp>
773 valarray<_Tp>::operator[](const gslice& __gs)
775 return gslice_array<_Tp>
776 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
779 template<typename _Tp>
781 valarray<_Tp>::operator[](const valarray<bool>& __m) const
784 size_t __e = __m.size();
785 for (size_t __i=0; __i<__e; ++__i)
787 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
788 _Array<bool> (__m)));
791 template<typename _Tp>
792 inline mask_array<_Tp>
793 valarray<_Tp>::operator[](const valarray<bool>& __m)
796 size_t __e = __m.size();
797 for (size_t __i=0; __i<__e; ++__i)
799 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
802 template<typename _Tp>
803 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
804 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
806 typedef _IClos<_ValArray,_Tp> _Closure;
807 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
810 template<typename _Tp>
811 inline indirect_array<_Tp>
812 valarray<_Tp>::operator[](const valarray<size_t>& __i)
814 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
815 _Array<size_t>(__i));
820 valarray<_Tp>::size() const
825 valarray<_Tp>::sum() const
827 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
828 return std::__valarray_sum(_M_data, _M_data + _M_size);
833 valarray<_Tp>::shift(int __n) const
840 _Tp* __restrict__ __tmp_M_data =
841 std::__valarray_get_storage<_Tp>(_M_size);
844 std::__valarray_copy_construct(_M_data,
845 _M_data + _M_size, __tmp_M_data);
846 else if (__n > 0) // shift left
848 if (size_t(__n) > _M_size)
851 std::__valarray_copy_construct(_M_data + __n,
852 _M_data + _M_size, __tmp_M_data);
853 std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
854 __tmp_M_data + _M_size);
858 if (-size_t(__n) > _M_size)
861 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
863 std::__valarray_default_construct(__tmp_M_data,
867 __ret._M_size = _M_size;
868 __ret._M_data = __tmp_M_data;
874 valarray<_Tp>::cshift(int __n) const
881 _Tp* __restrict__ __tmp_M_data =
882 std::__valarray_get_storage<_Tp>(_M_size);
885 std::__valarray_copy_construct(_M_data,
886 _M_data + _M_size, __tmp_M_data);
887 else if (__n > 0) // cshift left
889 if (size_t(__n) > _M_size)
890 __n = int(__n % _M_size);
892 std::__valarray_copy_construct(_M_data, _M_data + __n,
893 __tmp_M_data + _M_size - __n);
894 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
899 if (-size_t(__n) > _M_size)
900 __n = -int(-size_t(__n) % _M_size);
902 std::__valarray_copy_construct(_M_data + _M_size + __n,
903 _M_data + _M_size, __tmp_M_data);
904 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
908 __ret._M_size = _M_size;
909 __ret._M_data = __tmp_M_data;
915 valarray<_Tp>::resize(size_t __n, _Tp __c)
917 // This complication is so to make valarray<valarray<T> > work
918 // even though it is not required by the standard. Nobody should
919 // be saying valarray<valarray<T> > anyway. See the specs.
920 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
923 std::__valarray_release_memory(_M_data);
925 _M_data = __valarray_get_storage<_Tp>(__n);
927 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
930 template<typename _Tp>
932 valarray<_Tp>::min() const
934 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
935 return *std::min_element(_M_data, _M_data + _M_size);
938 template<typename _Tp>
940 valarray<_Tp>::max() const
942 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
943 return *std::max_element(_M_data, _M_data + _M_size);
947 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
948 valarray<_Tp>::apply(_Tp func(_Tp)) const
950 typedef _ValFunClos<_ValArray, _Tp> _Closure;
951 return _Expr<_Closure, _Tp>(_Closure(*this, func));
955 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
956 valarray<_Tp>::apply(_Tp func(const _Tp &)) const
958 typedef _RefFunClos<_ValArray, _Tp> _Closure;
959 return _Expr<_Closure, _Tp>(_Closure(*this, func));
962 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
963 template<typename _Tp> \
964 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
965 valarray<_Tp>::operator _Op() const \
967 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
968 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
969 return _Expr<_Closure, _Rt>(_Closure(*this)); \
972 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
973 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
974 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
975 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
977 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
979 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
980 template<class _Tp> \
981 inline valarray<_Tp>& \
982 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
984 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
988 template<class _Tp> \
989 inline valarray<_Tp>& \
990 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
992 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
993 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
994 _Array<_Tp>(__v._M_data)); \
998 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
999 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
1000 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
1001 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
1002 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
1003 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1004 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1005 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1006 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1007 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1009 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1011 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1012 template<class _Tp> template<class _Dom> \
1013 inline valarray<_Tp>& \
1014 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
1016 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
1020 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1021 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1022 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1023 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1024 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1025 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1026 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1027 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1028 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1029 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1031 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1034 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
1035 template<typename _Tp> \
1036 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
1037 typename __fun<_Name, _Tp>::result_type> \
1038 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1040 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
1041 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1042 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1043 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
1046 template<typename _Tp> \
1047 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
1048 typename __fun<_Name, _Tp>::result_type> \
1049 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
1051 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1052 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1053 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
1056 template<typename _Tp> \
1057 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
1058 typename __fun<_Name, _Tp>::result_type> \
1059 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
1061 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1062 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1063 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
1066 _DEFINE_BINARY_OPERATOR(+, __plus)
1067 _DEFINE_BINARY_OPERATOR(-, __minus)
1068 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1069 _DEFINE_BINARY_OPERATOR(/, __divides)
1070 _DEFINE_BINARY_OPERATOR(%, __modulus)
1071 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1072 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1073 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1074 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1075 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1076 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1077 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1078 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1079 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1080 _DEFINE_BINARY_OPERATOR(<, __less)
1081 _DEFINE_BINARY_OPERATOR(>, __greater)
1082 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1083 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1085 #undef _DEFINE_BINARY_OPERATOR
1087 // @} group numeric_arrays
1089 _GLIBCXX_END_NAMESPACE
1091 #endif /* _GLIBCXX_VALARRAY */