1 // The template and inlines for the -*- C++ -*- valarray class. 2 3 // Copyright (C) 1997-2022 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file include/valarray 26 * This is a Standard C++ Library header. 27 */ 28 29 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis (a] DPTMaths.ENS-Cachan.Fr> 30 31 #ifndef _GLIBCXX_VALARRAY 32 #define _GLIBCXX_VALARRAY 1 33 34 #pragma GCC system_header 35 36 #include <bits/c++config.h> 37 #include <cmath> 38 #include <algorithm> 39 #include <debug/debug.h> 40 #if __cplusplus >= 201103L 41 #include <initializer_list> 42 #endif 43 44 namespace std _GLIBCXX_VISIBILITY(default) 45 { 46 _GLIBCXX_BEGIN_NAMESPACE_VERSION 47 48 template<class _Clos, typename _Tp> 49 class _Expr; 50 51 template<typename _Tp1, typename _Tp2> 52 class _ValArray; 53 54 namespace __detail 55 { 56 template<class _Oper, template<class, class> class _Meta, class _Dom> 57 struct _UnClos; 58 59 template<class _Oper, 60 template<class, class> class _Meta1, 61 template<class, class> class _Meta2, 62 class _Dom1, class _Dom2> 63 struct _BinClos; 64 65 template<template<class, class> class _Meta, class _Dom> 66 struct _SClos; 67 68 template<template<class, class> class _Meta, class _Dom> 69 struct _GClos; 70 71 template<template<class, class> class _Meta, class _Dom> 72 struct _IClos; 73 74 template<template<class, class> class _Meta, class _Dom> 75 struct _ValFunClos; 76 77 template<template<class, class> class _Meta, class _Dom> 78 struct _RefFunClos; 79 } // namespace __detail 80 81 using __detail::_UnClos; 82 using __detail::_BinClos; 83 using __detail::_SClos; 84 using __detail::_GClos; 85 using __detail::_IClos; 86 using __detail::_ValFunClos; 87 using __detail::_RefFunClos; 88 89 template<class _Tp> class valarray; // An array of type _Tp 90 class slice; // BLAS-like slice out of an array 91 template<class _Tp> class slice_array; 92 class gslice; // generalized slice out of an array 93 template<class _Tp> class gslice_array; 94 template<class _Tp> class mask_array; // masked array 95 template<class _Tp> class indirect_array; // indirected array 96 97 _GLIBCXX_END_NAMESPACE_VERSION 98 } // namespace 99 100 #include <bits/valarray_array.h> 101 #include <bits/valarray_before.h> 102 103 namespace std _GLIBCXX_VISIBILITY(default) 104 { 105 _GLIBCXX_BEGIN_NAMESPACE_VERSION 106 107 /** 108 * @defgroup numeric_arrays Numeric Arrays 109 * @ingroup numerics 110 * 111 * Classes and functions for representing and manipulating arrays of elements. 112 * @{ 113 */ 114 115 /** 116 * @brief Smart array designed to support numeric processing. 117 * 118 * A valarray is an array that provides constraints intended to allow for 119 * effective optimization of numeric array processing by reducing the 120 * aliasing that can result from pointer representations. It represents a 121 * one-dimensional array from which different multidimensional subsets can 122 * be accessed and modified. 123 * 124 * @tparam _Tp Type of object in the array. 125 */ 126 template<class _Tp> 127 class valarray 128 { 129 template<class _Op> 130 struct _UnaryOp 131 { 132 typedef typename __fun<_Op, _Tp>::result_type __rt; 133 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt; 134 }; 135 public: 136 typedef _Tp value_type; 137 138 // _lib.valarray.cons_ construct/destroy: 139 /// Construct an empty array. 140 valarray() _GLIBCXX_NOTHROW; 141 142 /// Construct an array with @a n elements. 143 explicit valarray(size_t); 144 145 /// Construct an array with @a n elements initialized to @a t. 146 valarray(const _Tp&, size_t); 147 148 /// Construct an array initialized to the first @a n elements of @a t. 149 valarray(const _Tp* __restrict__, size_t); 150 151 /// Copy constructor. 152 valarray(const valarray&); 153 154 #if __cplusplus >= 201103L 155 /// Move constructor. 156 valarray(valarray&&) noexcept; 157 #endif 158 159 /// Construct an array with the same size and values in @a sa. 160 valarray(const slice_array<_Tp>&); 161 162 /// Construct an array with the same size and values in @a ga. 163 valarray(const gslice_array<_Tp>&); 164 165 /// Construct an array with the same size and values in @a ma. 166 valarray(const mask_array<_Tp>&); 167 168 /// Construct an array with the same size and values in @a ia. 169 valarray(const indirect_array<_Tp>&); 170 171 #if __cplusplus >= 201103L 172 /// Construct an array with an initializer_list of values. 173 valarray(initializer_list<_Tp>); 174 #endif 175 176 template<class _Dom> 177 valarray(const _Expr<_Dom, _Tp>& __e); 178 179 ~valarray() _GLIBCXX_NOEXCEPT; 180 181 // _lib.valarray.assign_ assignment: 182 /** 183 * @brief Assign elements to an array. 184 * 185 * Assign elements of array to values in @a v. 186 * 187 * @param __v Valarray to get values from. 188 */ 189 valarray<_Tp>& operator=(const valarray<_Tp>& __v); 190 191 #if __cplusplus >= 201103L 192 /** 193 * @brief Move assign elements to an array. 194 * 195 * Move assign elements of array to values in @a v. 196 * 197 * @param __v Valarray to get values from. 198 */ 199 valarray<_Tp>& operator=(valarray<_Tp>&& __v) noexcept; 200 #endif 201 202 /** 203 * @brief Assign elements to a value. 204 * 205 * Assign all elements of array to @a t. 206 * 207 * @param __t Value for elements. 208 */ 209 valarray<_Tp>& operator=(const _Tp& __t); 210 211 /** 212 * @brief Assign elements to an array subset. 213 * 214 * Assign elements of array to values in @a sa. Results are undefined 215 * if @a sa does not have the same size as this array. 216 * 217 * @param __sa Array slice to get values from. 218 */ 219 valarray<_Tp>& operator=(const slice_array<_Tp>& __sa); 220 221 /** 222 * @brief Assign elements to an array subset. 223 * 224 * Assign elements of array to values in @a ga. Results are undefined 225 * if @a ga does not have the same size as this array. 226 * 227 * @param __ga Array slice to get values from. 228 */ 229 valarray<_Tp>& operator=(const gslice_array<_Tp>& __ga); 230 231 /** 232 * @brief Assign elements to an array subset. 233 * 234 * Assign elements of array to values in @a ma. Results are undefined 235 * if @a ma does not have the same size as this array. 236 * 237 * @param __ma Array slice to get values from. 238 */ 239 valarray<_Tp>& operator=(const mask_array<_Tp>& __ma); 240 241 /** 242 * @brief Assign elements to an array subset. 243 * 244 * Assign elements of array to values in @a ia. Results are undefined 245 * if @a ia does not have the same size as this array. 246 * 247 * @param __ia Array slice to get values from. 248 */ 249 valarray<_Tp>& operator=(const indirect_array<_Tp>& __ia); 250 251 #if __cplusplus >= 201103L 252 /** 253 * @brief Assign elements to an initializer_list. 254 * 255 * Assign elements of array to values in @a __l. Results are undefined 256 * if @a __l does not have the same size as this array. 257 * 258 * @param __l initializer_list to get values from. 259 */ 260 valarray& operator=(initializer_list<_Tp> __l); 261 #endif 262 263 template<class _Dom> valarray<_Tp>& 264 operator= (const _Expr<_Dom, _Tp>&); 265 266 // _lib.valarray.access_ element access: 267 /** 268 * Return a reference to the i'th array element. 269 * 270 * @param __i Index of element to return. 271 * @return Reference to the i'th element. 272 */ 273 _Tp& operator[](size_t __i) _GLIBCXX_NOTHROW; 274 275 // _GLIBCXX_RESOLVE_LIB_DEFECTS 276 // 389. Const overload of valarray::operator[] returns by value. 277 const _Tp& operator[](size_t) const _GLIBCXX_NOTHROW; 278 279 // _lib.valarray.sub_ subset operations: 280 /** 281 * @brief Return an array subset. 282 * 283 * Returns a new valarray containing the elements of the array 284 * indicated by the slice argument. The new valarray has the same size 285 * as the input slice. @see slice. 286 * 287 * @param __s The source slice. 288 * @return New valarray containing elements in @a __s. 289 */ 290 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice __s) const; 291 292 /** 293 * @brief Return a reference to an array subset. 294 * 295 * Returns a new valarray containing the elements of the array 296 * indicated by the slice argument. The new valarray has the same size 297 * as the input slice. @see slice. 298 * 299 * @param __s The source slice. 300 * @return New valarray containing elements in @a __s. 301 */ 302 slice_array<_Tp> operator[](slice __s); 303 304 /** 305 * @brief Return an array subset. 306 * 307 * Returns a slice_array referencing the elements of the array 308 * indicated by the slice argument. @see gslice. 309 * 310 * @param __s The source slice. 311 * @return Slice_array referencing elements indicated by @a __s. 312 */ 313 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice& __s) const; 314 315 /** 316 * @brief Return a reference to an array subset. 317 * 318 * Returns a new valarray containing the elements of the array 319 * indicated by the gslice argument. The new valarray has 320 * the same size as the input gslice. @see gslice. 321 * 322 * @param __s The source gslice. 323 * @return New valarray containing elements in @a __s. 324 */ 325 gslice_array<_Tp> operator[](const gslice& __s); 326 327 /** 328 * @brief Return an array subset. 329 * 330 * Returns a new valarray containing the elements of the array 331 * indicated by the argument. The input is a valarray of bool which 332 * represents a bitmask indicating which elements should be copied into 333 * the new valarray. Each element of the array is added to the return 334 * valarray if the corresponding element of the argument is true. 335 * 336 * @param __m The valarray bitmask. 337 * @return New valarray containing elements indicated by @a __m. 338 */ 339 valarray<_Tp> operator[](const valarray<bool>& __m) const; 340 341 /** 342 * @brief Return a reference to an array subset. 343 * 344 * Returns a new mask_array referencing the elements of the array 345 * indicated by the argument. The input is a valarray of bool which 346 * represents a bitmask indicating which elements are part of the 347 * subset. Elements of the array are part of the subset if the 348 * corresponding element of the argument is true. 349 * 350 * @param __m The valarray bitmask. 351 * @return New valarray containing elements indicated by @a __m. 352 */ 353 mask_array<_Tp> operator[](const valarray<bool>& __m); 354 355 /** 356 * @brief Return an array subset. 357 * 358 * Returns a new valarray containing the elements of the array 359 * indicated by the argument. The elements in the argument are 360 * interpreted as the indices of elements of this valarray to copy to 361 * the return valarray. 362 * 363 * @param __i The valarray element index list. 364 * @return New valarray containing elements in @a __s. 365 */ 366 _Expr<_IClos<_ValArray, _Tp>, _Tp> 367 operator[](const valarray<size_t>& __i) const; 368 369 /** 370 * @brief Return a reference to an array subset. 371 * 372 * Returns an indirect_array referencing the elements of the array 373 * indicated by the argument. The elements in the argument are 374 * interpreted as the indices of elements of this valarray to include 375 * in the subset. The returned indirect_array refers to these 376 * elements. 377 * 378 * @param __i The valarray element index list. 379 * @return Indirect_array referencing elements in @a __i. 380 */ 381 indirect_array<_Tp> operator[](const valarray<size_t>& __i); 382 383 // _lib.valarray.unary_ unary operators: 384 /// Return a new valarray by applying unary + to each element. 385 typename _UnaryOp<__unary_plus>::_Rt operator+() const; 386 387 /// Return a new valarray by applying unary - to each element. 388 typename _UnaryOp<__negate>::_Rt operator-() const; 389 390 /// Return a new valarray by applying unary ~ to each element. 391 typename _UnaryOp<__bitwise_not>::_Rt operator~() const; 392 393 /// Return a new valarray by applying unary ! to each element. 394 typename _UnaryOp<__logical_not>::_Rt operator!() const; 395 396 // _lib.valarray.cassign_ computed assignment: 397 /// Multiply each element of array by @a t. 398 valarray<_Tp>& operator*=(const _Tp&); 399 400 /// Divide each element of array by @a t. 401 valarray<_Tp>& operator/=(const _Tp&); 402 403 /// Set each element e of array to e % @a t. 404 valarray<_Tp>& operator%=(const _Tp&); 405 406 /// Add @a t to each element of array. 407 valarray<_Tp>& operator+=(const _Tp&); 408 409 /// Subtract @a t to each element of array. 410 valarray<_Tp>& operator-=(const _Tp&); 411 412 /// Set each element e of array to e ^ @a t. 413 valarray<_Tp>& operator^=(const _Tp&); 414 415 /// Set each element e of array to e & @a t. 416 valarray<_Tp>& operator&=(const _Tp&); 417 418 /// Set each element e of array to e | @a t. 419 valarray<_Tp>& operator|=(const _Tp&); 420 421 /// Left shift each element e of array by @a t bits. 422 valarray<_Tp>& operator<<=(const _Tp&); 423 424 /// Right shift each element e of array by @a t bits. 425 valarray<_Tp>& operator>>=(const _Tp&); 426 427 /// Multiply elements of array by corresponding elements of @a v. 428 valarray<_Tp>& operator*=(const valarray<_Tp>&); 429 430 /// Divide elements of array by corresponding elements of @a v. 431 valarray<_Tp>& operator/=(const valarray<_Tp>&); 432 433 /// Modulo elements of array by corresponding elements of @a v. 434 valarray<_Tp>& operator%=(const valarray<_Tp>&); 435 436 /// Add corresponding elements of @a v to elements of array. 437 valarray<_Tp>& operator+=(const valarray<_Tp>&); 438 439 /// Subtract corresponding elements of @a v from elements of array. 440 valarray<_Tp>& operator-=(const valarray<_Tp>&); 441 442 /// Logical xor corresponding elements of @a v with elements of array. 443 valarray<_Tp>& operator^=(const valarray<_Tp>&); 444 445 /// Logical or corresponding elements of @a v with elements of array. 446 valarray<_Tp>& operator|=(const valarray<_Tp>&); 447 448 /// Logical and corresponding elements of @a v with elements of array. 449 valarray<_Tp>& operator&=(const valarray<_Tp>&); 450 451 /// Left shift elements of array by corresponding elements of @a v. 452 valarray<_Tp>& operator<<=(const valarray<_Tp>&); 453 454 /// Right shift elements of array by corresponding elements of @a v. 455 valarray<_Tp>& operator>>=(const valarray<_Tp>&); 456 457 template<class _Dom> 458 valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&); 459 template<class _Dom> 460 valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&); 461 template<class _Dom> 462 valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&); 463 template<class _Dom> 464 valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&); 465 template<class _Dom> 466 valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&); 467 template<class _Dom> 468 valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&); 469 template<class _Dom> 470 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&); 471 template<class _Dom> 472 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&); 473 template<class _Dom> 474 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&); 475 template<class _Dom> 476 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&); 477 478 // _lib.valarray.members_ member functions: 479 #if __cplusplus >= 201103L 480 /// Swap. 481 void swap(valarray<_Tp>& __v) noexcept; 482 #endif 483 484 /// Return the number of elements in array. 485 size_t size() const; 486 487 /** 488 * @brief Return the sum of all elements in the array. 489 * 490 * Accumulates the sum of all elements into a Tp using +=. The order 491 * of adding the elements is unspecified. 492 */ 493 _Tp sum() const; 494 495 /// Return the minimum element using operator<(). 496 _Tp min() const; 497 498 /// Return the maximum element using operator<(). 499 _Tp max() const; 500 501 /** 502 * @brief Return a shifted array. 503 * 504 * A new valarray is constructed as a copy of this array with elements 505 * in shifted positions. For an element with index i, the new position 506 * is i - n. The new valarray has the same size as the current one. 507 * New elements without a value are set to 0. Elements whose new 508 * position is outside the bounds of the array are discarded. 509 * 510 * Positive arguments shift toward index 0, discarding elements [0, n). 511 * Negative arguments discard elements from the top of the array. 512 * 513 * @param __n Number of element positions to shift. 514 * @return New valarray with elements in shifted positions. 515 */ 516 valarray<_Tp> shift (int __n) const; 517 518 /** 519 * @brief Return a rotated array. 520 * 521 * A new valarray is constructed as a copy of this array with elements 522 * in shifted positions. For an element with index i, the new position 523 * is (i - n) % size(). The new valarray has the same size as the 524 * current one. Elements that are shifted beyond the array bounds are 525 * shifted into the other end of the array. No elements are lost. 526 * 527 * Positive arguments shift toward index 0, wrapping around the top. 528 * Negative arguments shift towards the top, wrapping around to 0. 529 * 530 * @param __n Number of element positions to rotate. 531 * @return New valarray with elements in shifted positions. 532 */ 533 valarray<_Tp> cshift(int __n) const; 534 535 /** 536 * @brief Apply a function to the array. 537 * 538 * Returns a new valarray with elements assigned to the result of 539 * applying __func to the corresponding element of this array. The new 540 * array has the same size as this one. 541 * 542 * @param __func Function of Tp returning Tp to apply. 543 * @return New valarray with transformed elements. 544 */ 545 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp __func(_Tp)) const; 546 547 /** 548 * @brief Apply a function to the array. 549 * 550 * Returns a new valarray with elements assigned to the result of 551 * applying __func to the corresponding element of this array. The new 552 * array has the same size as this one. 553 * 554 * @param __func Function of const Tp& returning Tp to apply. 555 * @return New valarray with transformed elements. 556 */ 557 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp __func(const _Tp&)) const; 558 559 /** 560 * @brief Resize array. 561 * 562 * Resize this array to @a size and set all elements to @a c. All 563 * references and iterators are invalidated. 564 * 565 * @param __size New array size. 566 * @param __c New value for all elements. 567 */ 568 void resize(size_t __size, _Tp __c = _Tp()); 569 570 private: 571 size_t _M_size; 572 _Tp* __restrict__ _M_data; 573 574 friend struct _Array<_Tp>; 575 }; 576 577 #if __cpp_deduction_guides >= 201606 578 template<typename _Tp, size_t _Nm> 579 valarray(const _Tp(&)[_Nm], size_t) -> valarray<_Tp>; 580 #endif 581 582 template<typename _Tp> 583 inline const _Tp& 584 valarray<_Tp>::operator[](size_t __i) const _GLIBCXX_NOTHROW 585 { 586 __glibcxx_requires_subscript(__i); 587 return _M_data[__i]; 588 } 589 590 template<typename _Tp> 591 inline _Tp& 592 valarray<_Tp>::operator[](size_t __i) _GLIBCXX_NOTHROW 593 { 594 __glibcxx_requires_subscript(__i); 595 return _M_data[__i]; 596 } 597 598 /// @} group numeric_arrays 599 600 _GLIBCXX_END_NAMESPACE_VERSION 601 } // namespace 602 603 #include <bits/valarray_after.h> 604 #include <bits/slice_array.h> 605 #include <bits/gslice.h> 606 #include <bits/gslice_array.h> 607 #include <bits/mask_array.h> 608 #include <bits/indirect_array.h> 609 610 namespace std _GLIBCXX_VISIBILITY(default) 611 { 612 _GLIBCXX_BEGIN_NAMESPACE_VERSION 613 614 /** 615 * @addtogroup numeric_arrays 616 * @{ 617 */ 618 619 template<typename _Tp> 620 inline 621 valarray<_Tp>::valarray() _GLIBCXX_NOTHROW : _M_size(0), _M_data(0) {} 622 623 template<typename _Tp> 624 inline 625 valarray<_Tp>::valarray(size_t __n) 626 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) 627 { std::__valarray_default_construct(_M_data, _M_data + __n); } 628 629 template<typename _Tp> 630 inline 631 valarray<_Tp>::valarray(const _Tp& __t, size_t __n) 632 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) 633 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); } 634 635 template<typename _Tp> 636 inline 637 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n) 638 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) 639 { 640 __glibcxx_assert(__p != 0 || __n == 0); 641 std::__valarray_copy_construct(__p, __p + __n, _M_data); 642 } 643 644 template<typename _Tp> 645 inline 646 valarray<_Tp>::valarray(const valarray<_Tp>& __v) 647 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size)) 648 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, 649 _M_data); } 650 651 #if __cplusplus >= 201103L 652 template<typename _Tp> 653 inline 654 valarray<_Tp>::valarray(valarray<_Tp>&& __v) noexcept 655 : _M_size(__v._M_size), _M_data(__v._M_data) 656 { 657 __v._M_size = 0; 658 __v._M_data = 0; 659 } 660 #endif 661 662 template<typename _Tp> 663 inline 664 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa) 665 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz)) 666 { 667 std::__valarray_copy_construct 668 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data)); 669 } 670 671 template<typename _Tp> 672 inline 673 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga) 674 : _M_size(__ga._M_index.size()), 675 _M_data(__valarray_get_storage<_Tp>(_M_size)) 676 { 677 std::__valarray_copy_construct 678 (__ga._M_array, _Array<size_t>(__ga._M_index), 679 _Array<_Tp>(_M_data), _M_size); 680 } 681 682 template<typename _Tp> 683 inline 684 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma) 685 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz)) 686 { 687 std::__valarray_copy_construct 688 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size); 689 } 690 691 template<typename _Tp> 692 inline 693 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia) 694 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz)) 695 { 696 std::__valarray_copy_construct 697 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size); 698 } 699 700 #if __cplusplus >= 201103L 701 template<typename _Tp> 702 inline 703 valarray<_Tp>::valarray(initializer_list<_Tp> __l) 704 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size())) 705 { std::__valarray_copy_construct(__l.begin(), __l.end(), _M_data); } 706 #endif 707 708 template<typename _Tp> template<class _Dom> 709 inline 710 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e) 711 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size)) 712 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); } 713 714 template<typename _Tp> 715 inline 716 valarray<_Tp>::~valarray() _GLIBCXX_NOEXCEPT 717 { 718 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 719 std::__valarray_release_memory(_M_data); 720 } 721 722 template<typename _Tp> 723 inline valarray<_Tp>& 724 valarray<_Tp>::operator=(const valarray<_Tp>& __v) 725 { 726 // _GLIBCXX_RESOLVE_LIB_DEFECTS 727 // 630. arrays of valarray. 728 if (_M_size == __v._M_size) 729 std::__valarray_copy(__v._M_data, _M_size, _M_data); 730 else 731 { 732 if (_M_data) 733 { 734 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 735 std::__valarray_release_memory(_M_data); 736 } 737 _M_size = __v._M_size; 738 _M_data = __valarray_get_storage<_Tp>(_M_size); 739 std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, 740 _M_data); 741 } 742 return *this; 743 } 744 745 #if __cplusplus >= 201103L 746 template<typename _Tp> 747 inline valarray<_Tp>& 748 valarray<_Tp>::operator=(valarray<_Tp>&& __v) noexcept 749 { 750 if (_M_data) 751 { 752 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 753 std::__valarray_release_memory(_M_data); 754 } 755 _M_size = __v._M_size; 756 _M_data = __v._M_data; 757 __v._M_size = 0; 758 __v._M_data = 0; 759 return *this; 760 } 761 762 template<typename _Tp> 763 inline valarray<_Tp>& 764 valarray<_Tp>::operator=(initializer_list<_Tp> __l) 765 { 766 // _GLIBCXX_RESOLVE_LIB_DEFECTS 767 // 630. arrays of valarray. 768 if (_M_size == __l.size()) 769 std::__valarray_copy(__l.begin(), __l.size(), _M_data); 770 else 771 { 772 if (_M_data) 773 { 774 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 775 std::__valarray_release_memory(_M_data); 776 } 777 _M_size = __l.size(); 778 _M_data = __valarray_get_storage<_Tp>(_M_size); 779 std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size, 780 _M_data); 781 } 782 return *this; 783 } 784 #endif 785 786 template<typename _Tp> 787 inline valarray<_Tp>& 788 valarray<_Tp>::operator=(const _Tp& __t) 789 { 790 std::__valarray_fill(_M_data, _M_size, __t); 791 return *this; 792 } 793 794 template<typename _Tp> 795 inline valarray<_Tp>& 796 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa) 797 { 798 __glibcxx_assert(_M_size == __sa._M_sz); 799 std::__valarray_copy(__sa._M_array, __sa._M_sz, 800 __sa._M_stride, _Array<_Tp>(_M_data)); 801 return *this; 802 } 803 804 template<typename _Tp> 805 inline valarray<_Tp>& 806 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga) 807 { 808 __glibcxx_assert(_M_size == __ga._M_index.size()); 809 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index), 810 _Array<_Tp>(_M_data), _M_size); 811 return *this; 812 } 813 814 template<typename _Tp> 815 inline valarray<_Tp>& 816 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma) 817 { 818 __glibcxx_assert(_M_size == __ma._M_sz); 819 std::__valarray_copy(__ma._M_array, __ma._M_mask, 820 _Array<_Tp>(_M_data), _M_size); 821 return *this; 822 } 823 824 template<typename _Tp> 825 inline valarray<_Tp>& 826 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia) 827 { 828 __glibcxx_assert(_M_size == __ia._M_sz); 829 std::__valarray_copy(__ia._M_array, __ia._M_index, 830 _Array<_Tp>(_M_data), _M_size); 831 return *this; 832 } 833 834 template<typename _Tp> template<class _Dom> 835 inline valarray<_Tp>& 836 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) 837 { 838 // _GLIBCXX_RESOLVE_LIB_DEFECTS 839 // 630. arrays of valarray. 840 if (_M_size == __e.size()) 841 { 842 // Copy manually instead of using __valarray_copy, because __e might 843 // alias _M_data and the _Array param type of __valarray_copy uses 844 // restrict which doesn't allow aliasing. 845 for (size_t __i = 0; __i < _M_size; ++__i) 846 _M_data[__i] = __e[__i]; 847 } 848 else 849 { 850 if (_M_data) 851 { 852 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 853 std::__valarray_release_memory(_M_data); 854 } 855 _M_size = __e.size(); 856 _M_data = __valarray_get_storage<_Tp>(_M_size); 857 std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); 858 } 859 return *this; 860 } 861 862 template<typename _Tp> 863 inline _Expr<_SClos<_ValArray,_Tp>, _Tp> 864 valarray<_Tp>::operator[](slice __s) const 865 { 866 typedef _SClos<_ValArray,_Tp> _Closure; 867 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s)); 868 } 869 870 template<typename _Tp> 871 inline slice_array<_Tp> 872 valarray<_Tp>::operator[](slice __s) 873 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); } 874 875 template<typename _Tp> 876 inline _Expr<_GClos<_ValArray,_Tp>, _Tp> 877 valarray<_Tp>::operator[](const gslice& __gs) const 878 { 879 typedef _GClos<_ValArray,_Tp> _Closure; 880 return _Expr<_Closure, _Tp> 881 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index)); 882 } 883 884 template<typename _Tp> 885 inline gslice_array<_Tp> 886 valarray<_Tp>::operator[](const gslice& __gs) 887 { 888 return gslice_array<_Tp> 889 (_Array<_Tp>(_M_data), __gs._M_index->_M_index); 890 } 891 892 template<typename _Tp> 893 inline valarray<_Tp> 894 valarray<_Tp>::operator[](const valarray<bool>& __m) const 895 { 896 size_t __s = 0; 897 size_t __e = __m.size(); 898 for (size_t __i=0; __i<__e; ++__i) 899 if (__m[__i]) ++__s; 900 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s, 901 _Array<bool> (__m))); 902 } 903 904 template<typename _Tp> 905 inline mask_array<_Tp> 906 valarray<_Tp>::operator[](const valarray<bool>& __m) 907 { 908 size_t __s = 0; 909 size_t __e = __m.size(); 910 for (size_t __i=0; __i<__e; ++__i) 911 if (__m[__i]) ++__s; 912 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m)); 913 } 914 915 template<typename _Tp> 916 inline _Expr<_IClos<_ValArray,_Tp>, _Tp> 917 valarray<_Tp>::operator[](const valarray<size_t>& __i) const 918 { 919 typedef _IClos<_ValArray,_Tp> _Closure; 920 return _Expr<_Closure, _Tp>(_Closure(*this, __i)); 921 } 922 923 template<typename _Tp> 924 inline indirect_array<_Tp> 925 valarray<_Tp>::operator[](const valarray<size_t>& __i) 926 { 927 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(), 928 _Array<size_t>(__i)); 929 } 930 931 #if __cplusplus >= 201103L 932 template<class _Tp> 933 inline void 934 valarray<_Tp>::swap(valarray<_Tp>& __v) noexcept 935 { 936 std::swap(_M_size, __v._M_size); 937 std::swap(_M_data, __v._M_data); 938 } 939 #endif 940 941 template<class _Tp> 942 inline size_t 943 valarray<_Tp>::size() const 944 { return _M_size; } 945 946 template<class _Tp> 947 inline _Tp 948 valarray<_Tp>::sum() const 949 { 950 __glibcxx_assert(_M_size > 0); 951 return std::__valarray_sum(_M_data, _M_data + _M_size); 952 } 953 954 template<class _Tp> 955 inline valarray<_Tp> 956 valarray<_Tp>::shift(int __n) const 957 { 958 valarray<_Tp> __ret; 959 960 if (_M_size == 0) 961 return __ret; 962 963 _Tp* __restrict__ __tmp_M_data = 964 std::__valarray_get_storage<_Tp>(_M_size); 965 966 if (__n == 0) 967 std::__valarray_copy_construct(_M_data, 968 _M_data + _M_size, __tmp_M_data); 969 else if (__n > 0) // shift left 970 { 971 if (size_t(__n) > _M_size) 972 __n = int(_M_size); 973 974 std::__valarray_copy_construct(_M_data + __n, 975 _M_data + _M_size, __tmp_M_data); 976 std::__valarray_default_construct(__tmp_M_data + _M_size - __n, 977 __tmp_M_data + _M_size); 978 } 979 else // shift right 980 { 981 if (-size_t(__n) > _M_size) 982 __n = -int(_M_size); 983 984 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n, 985 __tmp_M_data - __n); 986 std::__valarray_default_construct(__tmp_M_data, 987 __tmp_M_data - __n); 988 } 989 990 __ret._M_size = _M_size; 991 __ret._M_data = __tmp_M_data; 992 return __ret; 993 } 994 995 template<class _Tp> 996 inline valarray<_Tp> 997 valarray<_Tp>::cshift(int __n) const 998 { 999 valarray<_Tp> __ret; 1000 1001 if (_M_size == 0) 1002 return __ret; 1003 1004 _Tp* __restrict__ __tmp_M_data = 1005 std::__valarray_get_storage<_Tp>(_M_size); 1006 1007 if (__n == 0) 1008 std::__valarray_copy_construct(_M_data, 1009 _M_data + _M_size, __tmp_M_data); 1010 else if (__n > 0) // cshift left 1011 { 1012 if (size_t(__n) > _M_size) 1013 __n = int(__n % _M_size); 1014 1015 std::__valarray_copy_construct(_M_data, _M_data + __n, 1016 __tmp_M_data + _M_size - __n); 1017 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size, 1018 __tmp_M_data); 1019 } 1020 else // cshift right 1021 { 1022 if (-size_t(__n) > _M_size) 1023 __n = -int(-size_t(__n) % _M_size); 1024 1025 std::__valarray_copy_construct(_M_data + _M_size + __n, 1026 _M_data + _M_size, __tmp_M_data); 1027 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n, 1028 __tmp_M_data - __n); 1029 } 1030 1031 __ret._M_size = _M_size; 1032 __ret._M_data = __tmp_M_data; 1033 return __ret; 1034 } 1035 1036 template<class _Tp> 1037 inline void 1038 valarray<_Tp>::resize(size_t __n, _Tp __c) 1039 { 1040 // This complication is so to make valarray<valarray<T> > work 1041 // even though it is not required by the standard. Nobody should 1042 // be saying valarray<valarray<T> > anyway. See the specs. 1043 std::__valarray_destroy_elements(_M_data, _M_data + _M_size); 1044 if (_M_size != __n) 1045 { 1046 std::__valarray_release_memory(_M_data); 1047 _M_size = __n; 1048 _M_data = __valarray_get_storage<_Tp>(__n); 1049 } 1050 std::__valarray_fill_construct(_M_data, _M_data + __n, __c); 1051 } 1052 1053 template<typename _Tp> 1054 inline _Tp 1055 valarray<_Tp>::min() const 1056 { 1057 __glibcxx_assert(_M_size > 0); 1058 return *std::min_element(_M_data, _M_data + _M_size); 1059 } 1060 1061 template<typename _Tp> 1062 inline _Tp 1063 valarray<_Tp>::max() const 1064 { 1065 __glibcxx_assert(_M_size > 0); 1066 return *std::max_element(_M_data, _M_data + _M_size); 1067 } 1068 1069 template<class _Tp> 1070 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> 1071 valarray<_Tp>::apply(_Tp __func(_Tp)) const 1072 { 1073 typedef _ValFunClos<_ValArray, _Tp> _Closure; 1074 return _Expr<_Closure, _Tp>(_Closure(*this, __func)); 1075 } 1076 1077 template<class _Tp> 1078 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> 1079 valarray<_Tp>::apply(_Tp __func(const _Tp &)) const 1080 { 1081 typedef _RefFunClos<_ValArray, _Tp> _Closure; 1082 return _Expr<_Closure, _Tp>(_Closure(*this, __func)); 1083 } 1084 1085 /// @cond undocumented 1086 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \ 1087 template<typename _Tp> \ 1088 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \ 1089 valarray<_Tp>::operator _Op() const \ 1090 { \ 1091 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \ 1092 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 1093 return _Expr<_Closure, _Rt>(_Closure(*this)); \ 1094 } 1095 1096 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus) 1097 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate) 1098 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not) 1099 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not) 1100 1101 #undef _DEFINE_VALARRAY_UNARY_OPERATOR 1102 1103 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \ 1104 template<class _Tp> \ 1105 inline valarray<_Tp>& \ 1106 valarray<_Tp>::operator _Op##=(const _Tp &__t) \ 1107 { \ 1108 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \ 1109 return *this; \ 1110 } \ 1111 \ 1112 template<class _Tp> \ 1113 inline valarray<_Tp>& \ 1114 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \ 1115 { \ 1116 __glibcxx_assert(_M_size == __v._M_size); \ 1117 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \ 1118 _Array<_Tp>(__v._M_data)); \ 1119 return *this; \ 1120 } 1121 1122 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus) 1123 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus) 1124 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies) 1125 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides) 1126 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus) 1127 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) 1128 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and) 1129 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or) 1130 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left) 1131 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right) 1132 1133 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT 1134 1135 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \ 1136 template<class _Tp> template<class _Dom> \ 1137 inline valarray<_Tp>& \ 1138 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \ 1139 { \ 1140 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \ 1141 return *this; \ 1142 } 1143 1144 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus) 1145 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus) 1146 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies) 1147 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides) 1148 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus) 1149 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) 1150 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and) 1151 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or) 1152 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left) 1153 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right) 1154 1155 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT 1156 1157 1158 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \ 1159 template<typename _Tp> \ 1160 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \ 1161 typename __fun<_Name, _Tp>::result_type> \ 1162 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \ 1163 { \ 1164 __glibcxx_assert(__v.size() == __w.size()); \ 1165 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \ 1166 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 1167 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \ 1168 } \ 1169 \ 1170 template<typename _Tp> \ 1171 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \ 1172 typename __fun<_Name, _Tp>::result_type> \ 1173 operator _Op(const valarray<_Tp>& __v, \ 1174 const typename valarray<_Tp>::value_type& __t) \ 1175 { \ 1176 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \ 1177 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 1178 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \ 1179 } \ 1180 \ 1181 template<typename _Tp> \ 1182 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \ 1183 typename __fun<_Name, _Tp>::result_type> \ 1184 operator _Op(const typename valarray<_Tp>::value_type& __t, \ 1185 const valarray<_Tp>& __v) \ 1186 { \ 1187 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \ 1188 typedef typename __fun<_Name, _Tp>::result_type _Rt; \ 1189 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \ 1190 } 1191 1192 _DEFINE_BINARY_OPERATOR(+, __plus) 1193 _DEFINE_BINARY_OPERATOR(-, __minus) 1194 _DEFINE_BINARY_OPERATOR(*, __multiplies) 1195 _DEFINE_BINARY_OPERATOR(/, __divides) 1196 _DEFINE_BINARY_OPERATOR(%, __modulus) 1197 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor) 1198 _DEFINE_BINARY_OPERATOR(&, __bitwise_and) 1199 _DEFINE_BINARY_OPERATOR(|, __bitwise_or) 1200 _DEFINE_BINARY_OPERATOR(<<, __shift_left) 1201 _DEFINE_BINARY_OPERATOR(>>, __shift_right) 1202 _DEFINE_BINARY_OPERATOR(&&, __logical_and) 1203 _DEFINE_BINARY_OPERATOR(||, __logical_or) 1204 _DEFINE_BINARY_OPERATOR(==, __equal_to) 1205 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to) 1206 _DEFINE_BINARY_OPERATOR(<, __less) 1207 _DEFINE_BINARY_OPERATOR(>, __greater) 1208 _DEFINE_BINARY_OPERATOR(<=, __less_equal) 1209 _DEFINE_BINARY_OPERATOR(>=, __greater_equal) 1210 1211 #undef _DEFINE_BINARY_OPERATOR 1212 /// @endcond 1213 1214 #if __cplusplus >= 201103L 1215 /** 1216 * @brief Return an iterator pointing to the first element of 1217 * the valarray. 1218 * @param __va valarray. 1219 */ 1220 template<class _Tp> 1221 [[__nodiscard__]] 1222 inline _Tp* 1223 begin(valarray<_Tp>& __va) noexcept 1224 { return __va.size() ? std::__addressof(__va[0]) : nullptr; } 1225 1226 /** 1227 * @brief Return an iterator pointing to the first element of 1228 * the const valarray. 1229 * @param __va valarray. 1230 */ 1231 template<class _Tp> 1232 [[__nodiscard__]] 1233 inline const _Tp* 1234 begin(const valarray<_Tp>& __va) noexcept 1235 { return __va.size() ? std::__addressof(__va[0]) : nullptr; } 1236 1237 /** 1238 * @brief Return an iterator pointing to one past the last element of 1239 * the valarray. 1240 * @param __va valarray. 1241 */ 1242 template<class _Tp> 1243 [[__nodiscard__]] 1244 inline _Tp* 1245 end(valarray<_Tp>& __va) noexcept 1246 { 1247 if (auto __n = __va.size()) 1248 return std::__addressof(__va[0]) + __n; 1249 else 1250 return nullptr; 1251 } 1252 1253 /** 1254 * @brief Return an iterator pointing to one past the last element of 1255 * the const valarray. 1256 * @param __va valarray. 1257 */ 1258 template<class _Tp> 1259 [[__nodiscard__]] 1260 inline const _Tp* 1261 end(const valarray<_Tp>& __va) noexcept 1262 { 1263 if (auto __n = __va.size()) 1264 return std::__addressof(__va[0]) + __n; 1265 else 1266 return nullptr; 1267 } 1268 #endif // C++11 1269 1270 /// @} group numeric_arrays 1271 1272 _GLIBCXX_END_NAMESPACE_VERSION 1273 } // namespace 1274 1275 #endif /* _GLIBCXX_VALARRAY */ 1276