libstdc++
|
00001 // The template and inlines for the -*- C++ -*- valarray class. 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 00004 // 2006, 2007, 2008, 2009, 2010 00005 // Free Software Foundation, Inc. 00006 // 00007 // This file is part of the GNU ISO C++ Library. This library is free 00008 // software; you can redistribute it and/or modify it under the 00009 // terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 3, or (at your option) 00011 // any later version. 00012 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 00018 // Under Section 7 of GPL version 3, you are granted additional 00019 // permissions described in the GCC Runtime Library Exception, version 00020 // 3.1, as published by the Free Software Foundation. 00021 00022 // You should have received a copy of the GNU General Public License and 00023 // a copy of the GCC Runtime Library Exception along with this program; 00024 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00025 // <http://www.gnu.org/licenses/>. 00026 00027 /** @file include/valarray 00028 * This is a Standard C++ Library header. 00029 */ 00030 00031 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> 00032 00033 #ifndef _GLIBCXX_VALARRAY 00034 #define _GLIBCXX_VALARRAY 1 00035 00036 #pragma GCC system_header 00037 00038 #include <bits/c++config.h> 00039 #include <cmath> 00040 #include <algorithm> 00041 #include <debug/debug.h> 00042 #include <initializer_list> 00043 00044 namespace std _GLIBCXX_VISIBILITY(default) 00045 { 00046 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00047 00048 template<class _Clos, typename _Tp> 00049 class _Expr; 00050 00051 template<typename _Tp1, typename _Tp2> 00052 class _ValArray; 00053 00054 template<class _Oper, template<class, class> class _Meta, class _Dom> 00055 struct _UnClos; 00056 00057 template<class _Oper, 00058 template<class, class> class _Meta1, 00059 template<class, class> class _Meta2, 00060 class _Dom1, class _Dom2> 00061 class _BinClos; 00062 00063 template<template<class, class> class _Meta, class _Dom> 00064 class _SClos; 00065 00066 template<template<class, class> class _Meta, class _Dom> 00067 class _GClos; 00068 00069 template<template<class, class> class _Meta, class _Dom> 00070 class _IClos; 00071 00072 template<template<class, class> class _Meta, class _Dom> 00073 class _ValFunClos; 00074 00075 template<template<class, class> class _Meta, class _Dom> 00076 class _RefFunClos; 00077 00078 template<class _Tp> class valarray; // An array of type _Tp 00079 class slice; // BLAS-like slice out of an array 00080 template<class _Tp> class slice_array; 00081 class gslice; // generalized slice out of an array 00082 template<class _Tp> class gslice_array; 00083 template<class _Tp> class mask_array; // masked array 00084 template<class _Tp> class indirect_array; // indirected array 00085 00086 _GLIBCXX_END_NAMESPACE_VERSION 00087 } // namespace 00088 00089 #include <bits/valarray_array.h> 00090 #include <bits/valarray_before.h> 00091 00092 namespace std _GLIBCXX_VISIBILITY(default) 00093 { 00094 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00095 00096 /** 00097 * @defgroup numeric_arrays Numeric Arrays 00098 * @ingroup numerics 00099 * 00100 * Classes and functions for representing and manipulating arrays of elements. 00101 * @{ 00102 */ 00103 00104 /** 00105 * @brief Smart array designed to support numeric processing. 00106 * 00107 * A valarray is an array that provides constraints intended to allow for 00108 * effective optimization of numeric array processing by reducing the 00109 * aliasing that can result from pointer representations. It represents a 00110 * one-dimensional array from which different multidimensional subsets can 00111 * be accessed and modified. 00112 * 00113 * @param Tp Type of object in the array. 00114 */ 00115 template<class _Tp> 00116 class valarray 00117 { 00118 template<class _Op> 00119 struct _UnaryOp 00120 { 00121 typedef typename __fun<_Op, _Tp>::result_type __rt; 00122 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt; 00123 }; 00124 public: 00125 typedef _Tp value_type; 00126 00127 // _lib.valarray.cons_ construct/destroy: 00128 /// Construct an empty array. 00129 valarray(); 00130 00131 /// Construct an array with @a n elements. 00132 explicit valarray(size_t); 00133 00134 /// Construct an array with @a n elements initialized to @a t. 00135 valarray(const _Tp&, size_t); 00136 00137 /// Construct an array initialized to the first @a n elements of @a t. 00138 valarray(const _Tp* __restrict__, size_t); 00139 00140 /// Copy constructor. 00141 valarray(const valarray&); 00142 00143 /// Construct an array with the same size and values in @a sa. 00144 valarray(const slice_array<_Tp>&); 00145 00146 /// Construct an array with the same size and values in @a ga. 00147 valarray(const gslice_array<_Tp>&); 00148 00149 /// Construct an array with the same size and values in @a ma. 00150 valarray(const mask_array<_Tp>&); 00151 00152 /// Construct an array with the same size and values in @a ia. 00153 valarray(const indirect_array<_Tp>&); 00154 00155 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00156 /// Construct an array with an initializer_list of values. 00157 valarray(initializer_list<_Tp>); 00158 #endif 00159 00160 template<class _Dom> 00161 valarray(const _Expr<_Dom, _Tp>& __e); 00162 00163 ~valarray(); 00164 00165 // _lib.valarray.assign_ assignment: 00166 /** 00167 * @brief Assign elements to an array. 00168 * 00169 * Assign elements of array to values in @a v. Results are undefined 00170 * if @a v does not have the same size as this array. 00171 * 00172 * @param v Valarray to get values from. 00173 */ 00174 valarray<_Tp>& operator=(const valarray<_Tp>&); 00175 00176 /** 00177 * @brief Assign elements to a value. 00178 * 00179 * Assign all elements of array to @a t. 00180 * 00181 * @param t Value for elements. 00182 */ 00183 valarray<_Tp>& operator=(const _Tp&); 00184 00185 /** 00186 * @brief Assign elements to an array subset. 00187 * 00188 * Assign elements of array to values in @a sa. Results are undefined 00189 * if @a sa does not have the same size as this array. 00190 * 00191 * @param sa Array slice to get values from. 00192 */ 00193 valarray<_Tp>& operator=(const slice_array<_Tp>&); 00194 00195 /** 00196 * @brief Assign elements to an array subset. 00197 * 00198 * Assign elements of array to values in @a ga. Results are undefined 00199 * if @a ga does not have the same size as this array. 00200 * 00201 * @param ga Array slice to get values from. 00202 */ 00203 valarray<_Tp>& operator=(const gslice_array<_Tp>&); 00204 00205 /** 00206 * @brief Assign elements to an array subset. 00207 * 00208 * Assign elements of array to values in @a ma. Results are undefined 00209 * if @a ma does not have the same size as this array. 00210 * 00211 * @param ma Array slice to get values from. 00212 */ 00213 valarray<_Tp>& operator=(const mask_array<_Tp>&); 00214 00215 /** 00216 * @brief Assign elements to an array subset. 00217 * 00218 * Assign elements of array to values in @a ia. Results are undefined 00219 * if @a ia does not have the same size as this array. 00220 * 00221 * @param ia Array slice to get values from. 00222 */ 00223 valarray<_Tp>& operator=(const indirect_array<_Tp>&); 00224 00225 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00226 /** 00227 * @brief Assign elements to an initializer_list. 00228 * 00229 * Assign elements of array to values in @a l. Results are undefined 00230 * if @a l does not have the same size as this array. 00231 * 00232 * @param l initializer_list to get values from. 00233 */ 00234 valarray& operator=(initializer_list<_Tp>); 00235 #endif 00236 00237 template<class _Dom> valarray<_Tp>& 00238 operator= (const _Expr<_Dom, _Tp>&); 00239 00240 // _lib.valarray.access_ element access: 00241 /** 00242 * Return a reference to the i'th array element. 00243 * 00244 * @param i Index of element to return. 00245 * @return Reference to the i'th element. 00246 */ 00247 _Tp& operator[](size_t); 00248 00249 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00250 // 389. Const overload of valarray::operator[] returns by value. 00251 const _Tp& operator[](size_t) const; 00252 00253 // _lib.valarray.sub_ subset operations: 00254 /** 00255 * @brief Return an array subset. 00256 * 00257 * Returns a new valarray containing the elements of the array 00258 * indicated by the slice argument. The new valarray has the same size 00259 * as the input slice. @see slice. 00260 * 00261 * @param s The source slice. 00262 * @return New valarray containing elements in @a s. 00263 */ 00264 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const; 00265 00266 /** 00267 * @brief Return a reference to an array subset. 00268 * 00269 * Returns a new valarray containing the elements of the array 00270 * indicated by the slice argument. The new valarray has the same size 00271 * as the input slice. @see slice. 00272 * 00273 * @param s The source slice. 00274 * @return New valarray containing elements in @a s. 00275 */ 00276 slice_array<_Tp> operator[](slice); 00277 00278 /** 00279 * @brief Return an array subset. 00280 * 00281 * Returns a slice_array referencing the elements of the array 00282 * indicated by the slice argument. @see gslice. 00283 * 00284 * @param s The source slice. 00285 * @return Slice_array referencing elements indicated by @a s. 00286 */ 00287 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const; 00288 00289 /** 00290 * @brief Return a reference to an array subset. 00291 * 00292 * Returns a new valarray containing the elements of the array 00293 * indicated by the gslice argument. The new valarray has 00294 * the same size as the input gslice. @see gslice. 00295 * 00296 * @param s The source gslice. 00297 * @return New valarray containing elements in @a s. 00298 */ 00299 gslice_array<_Tp> operator[](const gslice&); 00300 00301 /** 00302 * @brief Return an array subset. 00303 * 00304 * Returns a new valarray containing the elements of the array 00305 * indicated by the argument. The input is a valarray of bool which 00306 * represents a bitmask indicating which elements should be copied into 00307 * the new valarray. Each element of the array is added to the return 00308 * valarray if the corresponding element of the argument is true. 00309 * 00310 * @param m The valarray bitmask. 00311 * @return New valarray containing elements indicated by @a m. 00312 */ 00313 valarray<_Tp> operator[](const valarray<bool>&) const; 00314 00315 /** 00316 * @brief Return a reference to an array subset. 00317 * 00318 * Returns a new mask_array referencing the elements of the array 00319 * indicated by the argument. The input is a valarray of bool which 00320 * represents a bitmask indicating which elements are part of the 00321 * subset. Elements of the array are part of the subset if the 00322 * corresponding element of the argument is true. 00323 * 00324 * @param m The valarray bitmask. 00325 * @return New valarray containing elements indicated by @a m. 00326 */ 00327 mask_array<_Tp> operator[](const valarray<bool>&); 00328 00329 /** 00330 * @brief Return an array subset. 00331 * 00332 * Returns a new valarray containing the elements of the array 00333 * indicated by the argument. The elements in the argument are 00334 * interpreted as the indices of elements of this valarray to copy to 00335 * the return valarray. 00336 * 00337 * @param i The valarray element index list. 00338 * @return New valarray containing elements in @a s. 00339 */ 00340 _Expr<_IClos<_ValArray, _Tp>, _Tp> 00341 operator[](const valarray<size_t>&) const; 00342 00343 /** 00344 * @brief Return a reference to an array subset. 00345 * 00346 * Returns an indirect_array referencing the elements of the array 00347 * indicated by the argument. The elements in the argument are 00348 * interpreted as the indices of elements of this valarray to include 00349 * in the subset. The returned indirect_array refers to these 00350 * elements. 00351 * 00352 * @param i The valarray element index list. 00353 * @return Indirect_array referencing elements in @a i. 00354 */ 00355 indirect_array<_Tp> operator[](const valarray<size_t>&); 00356 00357 // _lib.valarray.unary_ unary operators: 00358 /// Return a new valarray by applying unary + to each element. 00359 typename _UnaryOp<__unary_plus>::_Rt operator+() const; 00360 00361 /// Return a new valarray by applying unary - to each element. 00362 typename _UnaryOp<__negate>::_Rt operator-() const; 00363 00364 /// Return a new valarray by applying unary ~ to each element. 00365 typename _UnaryOp<__bitwise_not>::_Rt operator~() const; 00366 00367 /// Return a new valarray by applying unary ! to each element. 00368 typename _UnaryOp<__logical_not>::_Rt operator!() const; 00369 00370 // _lib.valarray.cassign_ computed assignment: 00371 /// Multiply each element of array by @a t. 00372 valarray<_Tp>& operator*=(const _Tp&); 00373 00374 /// Divide each element of array by @a t. 00375 valarray<_Tp>& operator/=(const _Tp&); 00376 00377 /// Set each element e of array to e % @a t. 00378 valarray<_Tp>& operator%=(const _Tp&); 00379 00380 /// Add @a t to each element of array. 00381 valarray<_Tp>& operator+=(const _Tp&); 00382 00383 /// Subtract @a t to each element of array. 00384 valarray<_Tp>& operator-=(const _Tp&); 00385 00386 /// Set each element e of array to e ^ @a t. 00387 valarray<_Tp>& operator^=(const _Tp&); 00388 00389 /// Set each element e of array to e & @a t. 00390 valarray<_Tp>& operator&=(const _Tp&); 00391 00392 /// Set each element e of array to e | @a t. 00393 valarray<_Tp>& operator|=(const _Tp&); 00394 00395 /// Left shift each element e of array by @a t bits. 00396 valarray<_Tp>& operator<<=(const _Tp&); 00397 00398 /// Right shift each element e of array by @a t bits. 00399 valarray<_Tp>& operator>>=(const _Tp&); 00400 00401 /// Multiply elements of array by corresponding elements of @a v. 00402 valarray<_Tp>& operator*=(const valarray<_Tp>&); 00403 00404 /// Divide elements of array by corresponding elements of @a v. 00405 valarray<_Tp>& operator/=(const valarray<_Tp>&); 00406 00407 /// Modulo elements of array by corresponding elements of @a v. 00408 valarray<_Tp>& operator%=(const valarray<_Tp>&); 00409 00410 /// Add corresponding elements of @a v to elements of array. 00411 valarray<_Tp>& operator+=(const valarray<_Tp>&); 00412 00413 /// Subtract corresponding elements of @a v from elements of array. 00414 valarray<_Tp>& operator-=(const valarray<_Tp>&); 00415 00416 /// Logical xor corresponding elements of @a v with elements of array. 00417 valarray<_Tp>& operator^=(const valarray<_Tp>&); 00418 00419 /// Logical or corresponding elements of @a v with elements of array. 00420 valarray<_Tp>& operator|=(const valarray<_Tp>&); 00421 00422 /// Logical and corresponding elements of @a v with elements of array. 00423 valarray<_Tp>& operator&=(const valarray<_Tp>&); 00424 00425 /// Left shift elements of array by corresponding elements of @a v. 00426 valarray<_Tp>& operator<<=(const valarray<_Tp>&); 00427 00428 /// Right shift elements of array by corresponding elements of @a v. 00429 valarray<_Tp>& operator>>=(const valarray<_Tp>&); 00430 00431 template<class _Dom> 00432 valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&); 00433 template<class _Dom> 00434 valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&); 00435 template<class _Dom> 00436 valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&); 00437 template<class _Dom> 00438 valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&); 00439 template<class _Dom> 00440 valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&); 00441 template<class _Dom> 00442 valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&); 00443 template<class _Dom> 00444 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&); 00445 template<class _Dom> 00446 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&); 00447 template<class _Dom> 00448 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&); 00449 template<class _Dom> 00450 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&); 00451 00452 // _lib.valarray.members_ member functions: 00453 /// Return the number of elements in array. 00454 size_t size() const; 00455 00456 /** 00457 * @brief Return the sum of all elements in the array. 00458 * 00459 * Accumulates the sum of all elements into a Tp using +=. The order 00460 * of adding the elements is unspecified. 00461 */ 00462 _Tp sum() const; 00463 00464 /// Return the minimum element using operator<(). 00465 _Tp min() const; 00466 00467 /// Return the maximum element using operator<(). 00468 _Tp max() const; 00469 00470 /** 00471 * @brief Return a shifted array. 00472 * 00473 * A new valarray is constructed as a copy of this array with elements 00474 * in shifted positions. For an element with index i, the new position 00475 * is i - n. The new valarray has the same size as the current one. 00476 * New elements without a value are set to 0. Elements whose new 00477 * position is outside the bounds of the array are discarded. 00478 * 00479 * Positive arguments shift toward index 0, discarding elements [0, n). 00480 * Negative arguments discard elements from the top of the array. 00481 * 00482 * @param n Number of element positions to shift. 00483 * @return New valarray with elements in shifted positions. 00484 */ 00485 valarray<_Tp> shift (int) const; 00486 00487 /** 00488 * @brief Return a rotated array. 00489 * 00490 * A new valarray is constructed as a copy of this array with elements 00491 * in shifted positions. For an element with index i, the new position 00492 * is (i - n) % size(). The new valarray has the same size as the 00493 * current one. Elements that are shifted beyond the array bounds are 00494 * shifted into the other end of the array. No elements are lost. 00495 * 00496 * Positive arguments shift toward index 0, wrapping around the top. 00497 * Negative arguments shift towards the top, wrapping around to 0. 00498 * 00499 * @param n Number of element positions to rotate. 00500 * @return New valarray with elements in shifted positions. 00501 */ 00502 valarray<_Tp> cshift(int) const; 00503 00504 /** 00505 * @brief Apply a function to the array. 00506 * 00507 * Returns a new valarray with elements assigned to the result of 00508 * applying func to the corresponding element of this array. The new 00509 * array has the same size as this one. 00510 * 00511 * @param func Function of Tp returning Tp to apply. 00512 * @return New valarray with transformed elements. 00513 */ 00514 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const; 00515 00516 /** 00517 * @brief Apply a function to the array. 00518 * 00519 * Returns a new valarray with elements assigned to the result of 00520 * applying func to the corresponding element of this array. The new 00521 * array has the same size as this one. 00522 * 00523 * @param func Function of const Tp& returning Tp to apply. 00524 * @return New valarray with transformed elements. 00525 */ 00526 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const; 00527 00528 /** 00529 * @brief Resize array. 00530 * 00531 * Resize this array to @a size and set all elements to @a c. All 00532 * references and iterators are invalidated. 00533 * 00534 * @param size New array size. 00535 * @param c New value for all elements. 00536 */ 00537 void resize(size_t __size, _Tp __c = _Tp()); 00538 00539 private: 00540 size_t _M_size; 00541 _Tp* __restrict__ _M_data; 00542 00543 friend class _Array<_Tp>; 00544 }; 00545 00546 template<typename _Tp> 00547 inline const _Tp& 00548 valarray<_Tp>::operator[](size_t __i) const 00549 { 00550 __glibcxx_requires_subscript(__i); 00551 return _M_data[__i]; 00552 } 00553 00554 template<typename _Tp> 00555 inline _Tp& 00556 valarray<_Tp>::operator[](size_t __i) 00557 { 00558 __glibcxx_requires_subscript(__i); 00559 return _M_data[__i]; 00560 } 00561 00562 // @} group numeric_arrays 00563 00564 _GLIBCXX_END_NAMESPACE_VERSION 00565 } // namespace 00566 00567 #include <bits/valarray_after.h> 00568 #include <bits/slice_array.h> 00569 #include <bits/gslice.h> 00570 #include <bits/gslice_array.h> 00571 #include <bits/mask_array.h> 00572 #include <bits/indirect_array.h> 00573 00574 namespace std _GLIBCXX_VISIBILITY(default) 00575 { 00576 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00577 00578 /** 00579 * @addtogroup numeric_arrays 00580 * @{ 00581 */ 00582 00583 template<typename _Tp> 00584 inline 00585 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {} 00586 00587 template<typename _Tp> 00588 inline 00589 valarray<_Tp>::valarray(size_t __n) 00590 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) 00591 { std::__valarray_default_construct(_M_data, _M_data + __n); } 00592 00593 template<typename _Tp> 00594 inline 00595 valarray<_Tp>::valarray(const _Tp& __t, size_t __n) 00596 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) 00597 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); } 00598 00599 template<typename _Tp> 00600 inline 00601 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n) 00602 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) 00603 { 00604 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0); 00605 std::__valarray_copy_construct(__p, __p + __n, _M_data); 00606 } 00607 00608 template<typename _Tp> 00609 inline 00610 valarray<_Tp>::valarray(const valarray<_Tp>& __v) 00611 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size)) 00612 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, 00613 _M_data); } 00614 00615 template<typename _Tp> 00616 inline 00617 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa) 00618 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz)) 00619 { 00620 std::__valarray_copy_construct 00621 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data)); 00622 } 00623 00624 template<typename _Tp> 00625 inline 00626 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga) 00627 : _M_size(__ga._M_index.size()), 00628 _M_data(__valarray_get_storage<_Tp>(_M_size)) 00629 { 00630 std::__valarray_copy_construct 00631 (__ga._M_array, _Array<size_t>(__ga._M_index), 00632 _Array<_Tp>(_M_data), _M_size); 00633 } 00634 00635 template<typename _Tp> 00636 inline 00637 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma) 00638 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz)) 00639 { 00640 std::__valarray_copy_construct 00641 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size); 00642 } 00643 00644 template<typename _Tp> 00645 inline 00646 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia) 00647 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz)) 00648 { 00649 std::__valarray_copy_construct 00650 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size); 00651 } 00652 00653 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00654 template<typename _Tp> 00655 inline 00656 valarray<_Tp>::valarray(initializer_list<_Tp> __l) 00657 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size())) 00658 { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); } 00659 #endif 00660 00661 template<typename _Tp> template<class _Dom> 00662 inline 00663 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e) 00664 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size)) 00665 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); } 00666 00667 template<typename _Tp> 00668 inline 00669 valarray<_Tp>::~valarray() 00670 { 00671 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 00672 std::__valarray_release_memory(_M_data); 00673 } 00674 00675 template<typename _Tp> 00676 inline valarray<_Tp>& 00677 valarray<_Tp>::operator=(const valarray<_Tp>& __v) 00678 { 00679 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00680 // 630. arrays of valarray. 00681 if (_M_size == __v._M_size) 00682 std::__valarray_copy(__v._M_data, _M_size, _M_data); 00683 else 00684 { 00685 if (_M_data) 00686 { 00687 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 00688 std::__valarray_release_memory(_M_data); 00689 } 00690 _M_size = __v._M_size; 00691 _M_data = __valarray_get_storage<_Tp>(_M_size); 00692 std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, 00693 _M_data); 00694 } 00695 return *this; 00696 } 00697 00698 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00699 template<typename _Tp> 00700 inline valarray<_Tp>& 00701 valarray<_Tp>::operator=(initializer_list<_Tp> __l) 00702 { 00703 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00704 // 630. arrays of valarray. 00705 if (_M_size == __l.size()) 00706 std::__valarray_copy(__l.begin(), __l.size(), _M_data); 00707 else 00708 { 00709 if (_M_data) 00710 { 00711 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 00712 std::__valarray_release_memory(_M_data); 00713 } 00714 _M_size = __l.size(); 00715 _M_data = __valarray_get_storage<_Tp>(_M_size); 00716 std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size, 00717 _M_data); 00718 } 00719 return *this; 00720 } 00721 #endif 00722 00723 template<typename _Tp> 00724 inline valarray<_Tp>& 00725 valarray<_Tp>::operator=(const _Tp& __t) 00726 { 00727 std::__valarray_fill(_M_data, _M_size, __t); 00728 return *this; 00729 } 00730 00731 template<typename _Tp> 00732 inline valarray<_Tp>& 00733 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa) 00734 { 00735 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz); 00736 std::__valarray_copy(__sa._M_array, __sa._M_sz, 00737 __sa._M_stride, _Array<_Tp>(_M_data)); 00738 return *this; 00739 } 00740 00741 template<typename _Tp> 00742 inline valarray<_Tp>& 00743 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga) 00744 { 00745 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size()); 00746 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index), 00747 _Array<_Tp>(_M_data), _M_size); 00748 return *this; 00749 } 00750 00751 template<typename _Tp> 00752 inline valarray<_Tp>& 00753 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma) 00754 { 00755 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz); 00756 std::__valarray_copy(__ma._M_array, __ma._M_mask, 00757 _Array<_Tp>(_M_data), _M_size); 00758 return *this; 00759 } 00760 00761 template<typename _Tp> 00762 inline valarray<_Tp>& 00763 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia) 00764 { 00765 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz); 00766 std::__valarray_copy(__ia._M_array, __ia._M_index, 00767 _Array<_Tp>(_M_data), _M_size); 00768 return *this; 00769 } 00770 00771 template<typename _Tp> template<class _Dom> 00772 inline valarray<_Tp>& 00773 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) 00774 { 00775 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size()); 00776 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); 00777 return *this; 00778 } 00779 00780 template<typename _Tp> 00781 inline _Expr<_SClos<_ValArray,_Tp>, _Tp> 00782 valarray<_Tp>::operator[](slice __s) const 00783 { 00784 typedef _SClos<_ValArray,_Tp> _Closure; 00785 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s)); 00786 } 00787 00788 template<typename _Tp> 00789 inline slice_array<_Tp> 00790 valarray<_Tp>::operator[](slice __s) 00791 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); } 00792 00793 template<typename _Tp> 00794 inline _Expr<_GClos<_ValArray,_Tp>, _Tp> 00795 valarray<_Tp>::operator[](const gslice& __gs) const 00796 { 00797 typedef _GClos<_ValArray,_Tp> _Closure; 00798 return _Expr<_Closure, _Tp> 00799 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index)); 00800 } 00801 00802 template<typename _Tp> 00803 inline gslice_array<_Tp> 00804 valarray<_Tp>::operator[](const gslice& __gs) 00805 { 00806 return gslice_array<_Tp> 00807 (_Array<_Tp>(_M_data), __gs._M_index->_M_index); 00808 } 00809 00810 template<typename _Tp> 00811 inline valarray<_Tp> 00812 valarray<_Tp>::operator[](const valarray<bool>& __m) const 00813 { 00814 size_t __s = 0; 00815 size_t __e = __m.size(); 00816 for (size_t __i=0; __i<__e; ++__i) 00817 if (__m[__i]) ++__s; 00818 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s, 00819 _Array<bool> (__m))); 00820 } 00821 00822 template<typename _Tp> 00823 inline mask_array<_Tp> 00824 valarray<_Tp>::operator[](const valarray<bool>& __m) 00825 { 00826 size_t __s = 0; 00827 size_t __e = __m.size(); 00828 for (size_t __i=0; __i<__e; ++__i) 00829 if (__m[__i]) ++__s; 00830 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m)); 00831 } 00832 00833 template<typename _Tp> 00834 inline _Expr<_IClos<_ValArray,_Tp>, _Tp> 00835 valarray<_Tp>::operator[](const valarray<size_t>& __i) const 00836 { 00837 typedef _IClos<_ValArray,_Tp> _Closure; 00838 return _Expr<_Closure, _Tp>(_Closure(*this, __i)); 00839 } 00840 00841 template<typename _Tp> 00842 inline indirect_array<_Tp> 00843 valarray<_Tp>::operator[](const valarray<size_t>& __i) 00844 { 00845 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(), 00846 _Array<size_t>(__i)); 00847 } 00848 00849 template<class _Tp> 00850 inline size_t 00851 valarray<_Tp>::size() const 00852 { return _M_size; } 00853 00854 template<class _Tp> 00855 inline _Tp 00856 valarray<_Tp>::sum() const 00857 { 00858 _GLIBCXX_DEBUG_ASSERT(_M_size > 0); 00859 return std::__valarray_sum(_M_data, _M_data + _M_size); 00860 } 00861 00862 template<class _Tp> 00863 inline valarray<_Tp> 00864 valarray<_Tp>::shift(int __n) const 00865 { 00866 valarray<_Tp> __ret; 00867 00868 if (_M_size == 0) 00869 return __ret; 00870 00871 _Tp* __restrict__ __tmp_M_data = 00872 std::__valarray_get_storage<_Tp>(_M_size); 00873 00874 if (__n == 0) 00875 std::__valarray_copy_construct(_M_data, 00876 _M_data + _M_size, __tmp_M_data); 00877 else if (__n > 0) // shift left 00878 { 00879 if (size_t(__n) > _M_size) 00880 __n = int(_M_size); 00881 00882 std::__valarray_copy_construct(_M_data + __n, 00883 _M_data + _M_size, __tmp_M_data); 00884 std::__valarray_default_construct(__tmp_M_data + _M_size - __n, 00885 __tmp_M_data + _M_size); 00886 } 00887 else // shift right 00888 { 00889 if (-size_t(__n) > _M_size) 00890 __n = -int(_M_size); 00891 00892 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n, 00893 __tmp_M_data - __n); 00894 std::__valarray_default_construct(__tmp_M_data, 00895 __tmp_M_data - __n); 00896 } 00897 00898 __ret._M_size = _M_size; 00899 __ret._M_data = __tmp_M_data; 00900 return __ret; 00901 } 00902 00903 template<class _Tp> 00904 inline valarray<_Tp> 00905 valarray<_Tp>::cshift(int __n) const 00906 { 00907 valarray<_Tp> __ret; 00908 00909 if (_M_size == 0) 00910 return __ret; 00911 00912 _Tp* __restrict__ __tmp_M_data = 00913 std::__valarray_get_storage<_Tp>(_M_size); 00914 00915 if (__n == 0) 00916 std::__valarray_copy_construct(_M_data, 00917 _M_data + _M_size, __tmp_M_data); 00918 else if (__n > 0) // cshift left 00919 { 00920 if (size_t(__n) > _M_size) 00921 __n = int(__n % _M_size); 00922 00923 std::__valarray_copy_construct(_M_data, _M_data + __n, 00924 __tmp_M_data + _M_size - __n); 00925 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size, 00926 __tmp_M_data); 00927 } 00928 else // cshift right 00929 { 00930 if (-size_t(__n) > _M_size) 00931 __n = -int(-size_t(__n) % _M_size); 00932 00933 std::__valarray_copy_construct(_M_data + _M_size + __n, 00934 _M_data + _M_size, __tmp_M_data); 00935 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n, 00936 __tmp_M_data - __n); 00937 } 00938 00939 __ret._M_size = _M_size; 00940 __ret._M_data = __tmp_M_data; 00941 return __ret; 00942 } 00943 00944 template<class _Tp> 00945 inline void 00946 valarray<_Tp>::resize(size_t __n, _Tp __c) 00947 { 00948 // This complication is so to make valarray<valarray<T> > work 00949 // even though it is not required by the standard. Nobody should 00950 // be saying valarray<valarray<T> > anyway. See the specs. 00951 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 00952 if (_M_size != __n) 00953 { 00954 std::__valarray_release_memory(_M_data); 00955 _M_size = __n; 00956 _M_data = __valarray_get_storage<_Tp>(__n); 00957 } 00958 std::__valarray_fill_construct(_M_data, _M_data + __n, __c); 00959 } 00960 00961 template<typename _Tp> 00962 inline _Tp 00963 valarray<_Tp>::min() const 00964 { 00965 _GLIBCXX_DEBUG_ASSERT(_M_size > 0); 00966 return *std::min_element(_M_data, _M_data + _M_size); 00967 } 00968 00969 template<typename _Tp> 00970 inline _Tp 00971 valarray<_Tp>::max() const 00972 { 00973 _GLIBCXX_DEBUG_ASSERT(_M_size > 0); 00974 return *std::max_element(_M_data, _M_data + _M_size); 00975 } 00976 00977 template<class _Tp> 00978 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> 00979 valarray<_Tp>::apply(_Tp func(_Tp)) const 00980 { 00981 typedef _ValFunClos<_ValArray, _Tp> _Closure; 00982 return _Expr<_Closure, _Tp>(_Closure(*this, func)); 00983 } 00984 00985 template<class _Tp> 00986 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> 00987 valarray<_Tp>::apply(_Tp func(const _Tp &)) const 00988 { 00989 typedef _RefFunClos<_ValArray, _Tp> _Closure; 00990 return _Expr<_Closure, _Tp>(_Closure(*this, func)); 00991 } 00992 00993 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \ 00994 template<typename _Tp> \ 00995 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \ 00996 valarray<_Tp>::operator _Op() const \ 00997 { \ 00998 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \ 00999 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 01000 return _Expr<_Closure, _Rt>(_Closure(*this)); \ 01001 } 01002 01003 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus) 01004 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate) 01005 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not) 01006 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not) 01007 01008 #undef _DEFINE_VALARRAY_UNARY_OPERATOR 01009 01010 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \ 01011 template<class _Tp> \ 01012 inline valarray<_Tp>& \ 01013 valarray<_Tp>::operator _Op##=(const _Tp &__t) \ 01014 { \ 01015 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \ 01016 return *this; \ 01017 } \ 01018 \ 01019 template<class _Tp> \ 01020 inline valarray<_Tp>& \ 01021 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \ 01022 { \ 01023 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \ 01024 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \ 01025 _Array<_Tp>(__v._M_data)); \ 01026 return *this; \ 01027 } 01028 01029 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus) 01030 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus) 01031 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies) 01032 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides) 01033 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus) 01034 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) 01035 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and) 01036 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or) 01037 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left) 01038 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right) 01039 01040 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT 01041 01042 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \ 01043 template<class _Tp> template<class _Dom> \ 01044 inline valarray<_Tp>& \ 01045 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \ 01046 { \ 01047 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \ 01048 return *this; \ 01049 } 01050 01051 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus) 01052 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus) 01053 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies) 01054 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides) 01055 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus) 01056 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) 01057 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and) 01058 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or) 01059 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left) 01060 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right) 01061 01062 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT 01063 01064 01065 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \ 01066 template<typename _Tp> \ 01067 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \ 01068 typename __fun<_Name, _Tp>::result_type> \ 01069 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \ 01070 { \ 01071 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \ 01072 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \ 01073 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 01074 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \ 01075 } \ 01076 \ 01077 template<typename _Tp> \ 01078 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \ 01079 typename __fun<_Name, _Tp>::result_type> \ 01080 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \ 01081 { \ 01082 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \ 01083 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 01084 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \ 01085 } \ 01086 \ 01087 template<typename _Tp> \ 01088 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \ 01089 typename __fun<_Name, _Tp>::result_type> \ 01090 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \ 01091 { \ 01092 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \ 01093 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 01094 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \ 01095 } 01096 01097 _DEFINE_BINARY_OPERATOR(+, __plus) 01098 _DEFINE_BINARY_OPERATOR(-, __minus) 01099 _DEFINE_BINARY_OPERATOR(*, __multiplies) 01100 _DEFINE_BINARY_OPERATOR(/, __divides) 01101 _DEFINE_BINARY_OPERATOR(%, __modulus) 01102 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor) 01103 _DEFINE_BINARY_OPERATOR(&, __bitwise_and) 01104 _DEFINE_BINARY_OPERATOR(|, __bitwise_or) 01105 _DEFINE_BINARY_OPERATOR(<<, __shift_left) 01106 _DEFINE_BINARY_OPERATOR(>>, __shift_right) 01107 _DEFINE_BINARY_OPERATOR(&&, __logical_and) 01108 _DEFINE_BINARY_OPERATOR(||, __logical_or) 01109 _DEFINE_BINARY_OPERATOR(==, __equal_to) 01110 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to) 01111 _DEFINE_BINARY_OPERATOR(<, __less) 01112 _DEFINE_BINARY_OPERATOR(>, __greater) 01113 _DEFINE_BINARY_OPERATOR(<=, __less_equal) 01114 _DEFINE_BINARY_OPERATOR(>=, __greater_equal) 01115 01116 #undef _DEFINE_BINARY_OPERATOR 01117 01118 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01119 /** 01120 * @brief Return an iterator pointing to the first element of 01121 * the valarray. 01122 * @param va valarray. 01123 */ 01124 template<class _Tp> 01125 inline _Tp* 01126 begin(valarray<_Tp>& __va) 01127 { return std::__addressof(__va[0]); } 01128 01129 /** 01130 * @brief Return an iterator pointing to the first element of 01131 * the const valarray. 01132 * @param va valarray. 01133 */ 01134 template<class _Tp> 01135 inline const _Tp* 01136 begin(const valarray<_Tp>& __va) 01137 { return std::__addressof(__va[0]); } 01138 01139 /** 01140 * @brief Return an iterator pointing to one past the last element of 01141 * the valarray. 01142 * @param va valarray. 01143 */ 01144 template<class _Tp> 01145 inline _Tp* 01146 end(valarray<_Tp>& __va) 01147 { return std::__addressof(__va[0]) + __va.size(); } 01148 01149 /** 01150 * @brief Return an iterator pointing to one past the last element of 01151 * the const valarray. 01152 * @param va valarray. 01153 */ 01154 template<class _Tp> 01155 inline const _Tp* 01156 end(const valarray<_Tp>& __va) 01157 { return std::__addressof(__va[0]) + __va.size(); } 01158 #endif // __GXX_EXPERIMENTAL_CXX0X__ 01159 01160 // @} group numeric_arrays 01161 01162 _GLIBCXX_END_NAMESPACE_VERSION 01163 } // namespace 01164 01165 #endif /* _GLIBCXX_VALARRAY */