Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/bits/valarray_before.h
1 // The template and inlines for the -*- C++ -*- internal _Meta class. 2 3 // Copyright (C) 1997-2025 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 bits/valarray_before.h 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. @headername{valarray} 28 */ 29 30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr> 31 32 #ifndef _VALARRAY_BEFORE_H 33 #define _VALARRAY_BEFORE_H 1 34 35 #ifdef _GLIBCXX_SYSHDR 36 #pragma GCC system_header 37 #endif 38 39 #include <bits/slice_array.h> 40 41 namespace std _GLIBCXX_VISIBILITY(default) 42 { 43 _GLIBCXX_BEGIN_NAMESPACE_VERSION 44 45 // 46 // Implementing a loosened valarray return value is tricky. 47 // First we need to meet 26.3.1/3: we should not add more than 48 // two levels of template nesting. Therefore we resort to template 49 // template to "flatten" loosened return value types. 50 // At some point we use partial specialization to remove one level 51 // template nesting due to _Expr<> 52 // 53 54 // This class is NOT defined. It doesn't need to. 55 template<typename _Tp1, typename _Tp2> class _Constant; 56 57 // Implementations of unary functions applied to valarray<>s. 58 // I use hard-coded object functions here instead of a generic 59 // approach like pointers to function: 60 // 1) correctness: some functions take references, others values. 61 // we can't deduce the correct type afterwards. 62 // 2) efficiency -- object functions can be easily inlined 63 // 3) be Koenig-lookup-friendly 64 65 struct _Abs 66 { 67 template<typename _Tp> 68 _Tp operator()(const _Tp& __t) const 69 { return abs(__t); } 70 }; 71 72 struct _Cos 73 { 74 template<typename _Tp> 75 _Tp operator()(const _Tp& __t) const 76 { return cos(__t); } 77 }; 78 79 struct _Acos 80 { 81 template<typename _Tp> 82 _Tp operator()(const _Tp& __t) const 83 { return acos(__t); } 84 }; 85 86 struct _Cosh 87 { 88 template<typename _Tp> 89 _Tp operator()(const _Tp& __t) const 90 { return cosh(__t); } 91 }; 92 93 struct _Sin 94 { 95 template<typename _Tp> 96 _Tp operator()(const _Tp& __t) const 97 { return sin(__t); } 98 }; 99 100 struct _Asin 101 { 102 template<typename _Tp> 103 _Tp operator()(const _Tp& __t) const 104 { return asin(__t); } 105 }; 106 107 struct _Sinh 108 { 109 template<typename _Tp> 110 _Tp operator()(const _Tp& __t) const 111 { return sinh(__t); } 112 }; 113 114 struct _Tan 115 { 116 template<typename _Tp> 117 _Tp operator()(const _Tp& __t) const 118 { return tan(__t); } 119 }; 120 121 struct _Atan 122 { 123 template<typename _Tp> 124 _Tp operator()(const _Tp& __t) const 125 { return atan(__t); } 126 }; 127 128 struct _Tanh 129 { 130 template<typename _Tp> 131 _Tp operator()(const _Tp& __t) const 132 { return tanh(__t); } 133 }; 134 135 struct _Exp 136 { 137 template<typename _Tp> 138 _Tp operator()(const _Tp& __t) const 139 { return exp(__t); } 140 }; 141 142 struct _Log 143 { 144 template<typename _Tp> 145 _Tp operator()(const _Tp& __t) const 146 { return log(__t); } 147 }; 148 149 struct _Log10 150 { 151 template<typename _Tp> 152 _Tp operator()(const _Tp& __t) const 153 { return log10(__t); } 154 }; 155 156 struct _Sqrt 157 { 158 template<typename _Tp> 159 _Tp operator()(const _Tp& __t) const 160 { return sqrt(__t); } 161 }; 162 163 // In the past, we used to tailor operator applications semantics 164 // to the specialization of standard function objects (i.e. plus<>, etc.) 165 // That is incorrect. Therefore we provide our own surrogates. 166 167 struct __unary_plus 168 { 169 template<typename _Tp> 170 _Tp operator()(const _Tp& __t) const 171 { return +__t; } 172 }; 173 174 struct __negate 175 { 176 template<typename _Tp> 177 _Tp operator()(const _Tp& __t) const 178 { return -__t; } 179 }; 180 181 struct __bitwise_not 182 { 183 template<typename _Tp> 184 _Tp operator()(const _Tp& __t) const 185 { return ~__t; } 186 }; 187 188 struct __plus 189 { 190 template<typename _Tp> 191 _Tp operator()(const _Tp& __x, const _Tp& __y) const 192 { return __x + __y; } 193 }; 194 195 struct __minus 196 { 197 template<typename _Tp> 198 _Tp operator()(const _Tp& __x, const _Tp& __y) const 199 { return __x - __y; } 200 }; 201 202 struct __multiplies 203 { 204 template<typename _Tp> 205 _Tp operator()(const _Tp& __x, const _Tp& __y) const 206 { return __x * __y; } 207 }; 208 209 struct __divides 210 { 211 template<typename _Tp> 212 _Tp operator()(const _Tp& __x, const _Tp& __y) const 213 { return __x / __y; } 214 }; 215 216 struct __modulus 217 { 218 template<typename _Tp> 219 _Tp operator()(const _Tp& __x, const _Tp& __y) const 220 { return __x % __y; } 221 }; 222 223 struct __bitwise_xor 224 { 225 template<typename _Tp> 226 _Tp operator()(const _Tp& __x, const _Tp& __y) const 227 { return __x ^ __y; } 228 }; 229 230 struct __bitwise_and 231 { 232 template<typename _Tp> 233 _Tp operator()(const _Tp& __x, const _Tp& __y) const 234 { return __x & __y; } 235 }; 236 237 struct __bitwise_or 238 { 239 template<typename _Tp> 240 _Tp operator()(const _Tp& __x, const _Tp& __y) const 241 { return __x | __y; } 242 }; 243 244 struct __shift_left 245 { 246 template<typename _Tp> 247 _Tp operator()(const _Tp& __x, const _Tp& __y) const 248 { return __x << __y; } 249 }; 250 251 struct __shift_right 252 { 253 template<typename _Tp> 254 _Tp operator()(const _Tp& __x, const _Tp& __y) const 255 { return __x >> __y; } 256 }; 257 258 struct __logical_and 259 { 260 template<typename _Tp> 261 bool operator()(const _Tp& __x, const _Tp& __y) const 262 { return __x && __y; } 263 }; 264 265 struct __logical_or 266 { 267 template<typename _Tp> 268 bool operator()(const _Tp& __x, const _Tp& __y) const 269 { return __x || __y; } 270 }; 271 272 struct __logical_not 273 { 274 template<typename _Tp> 275 bool operator()(const _Tp& __x) const 276 { return !__x; } 277 }; 278 279 struct __equal_to 280 { 281 template<typename _Tp> 282 bool operator()(const _Tp& __x, const _Tp& __y) const 283 { return __x == __y; } 284 }; 285 286 struct __not_equal_to 287 { 288 template<typename _Tp> 289 bool operator()(const _Tp& __x, const _Tp& __y) const 290 { return __x != __y; } 291 }; 292 293 struct __less 294 { 295 template<typename _Tp> 296 bool operator()(const _Tp& __x, const _Tp& __y) const 297 { return __x < __y; } 298 }; 299 300 struct __greater 301 { 302 template<typename _Tp> 303 bool operator()(const _Tp& __x, const _Tp& __y) const 304 { return __x > __y; } 305 }; 306 307 struct __less_equal 308 { 309 template<typename _Tp> 310 bool operator()(const _Tp& __x, const _Tp& __y) const 311 { return __x <= __y; } 312 }; 313 314 struct __greater_equal 315 { 316 template<typename _Tp> 317 bool operator()(const _Tp& __x, const _Tp& __y) const 318 { return __x >= __y; } 319 }; 320 321 // The few binary functions we miss. 322 struct _Atan2 323 { 324 template<typename _Tp> 325 _Tp operator()(const _Tp& __x, const _Tp& __y) const 326 { return atan2(__x, __y); } 327 }; 328 329 struct _Pow 330 { 331 template<typename _Tp> 332 _Tp operator()(const _Tp& __x, const _Tp& __y) const 333 { return pow(__x, __y); } 334 }; 335 336 template<typename _Tp, bool _IsValidValarrayValue = !__is_abstract(_Tp)> 337 struct __fun_with_valarray 338 { 339 typedef _Tp result_type; 340 }; 341 342 template<typename _Tp> 343 struct __fun_with_valarray<_Tp, false> 344 { 345 // No result type defined for invalid value types. 346 }; 347 348 // We need these bits in order to recover the return type of 349 // some functions/operators now that we're no longer using 350 // function templates. 351 template<typename, typename _Tp> 352 struct __fun : __fun_with_valarray<_Tp> 353 { 354 }; 355 356 // several specializations for relational operators. 357 template<typename _Tp> 358 struct __fun<__logical_not, _Tp> 359 { 360 typedef bool result_type; 361 }; 362 363 template<typename _Tp> 364 struct __fun<__logical_and, _Tp> 365 { 366 typedef bool result_type; 367 }; 368 369 template<typename _Tp> 370 struct __fun<__logical_or, _Tp> 371 { 372 typedef bool result_type; 373 }; 374 375 template<typename _Tp> 376 struct __fun<__less, _Tp> 377 { 378 typedef bool result_type; 379 }; 380 381 template<typename _Tp> 382 struct __fun<__greater, _Tp> 383 { 384 typedef bool result_type; 385 }; 386 387 template<typename _Tp> 388 struct __fun<__less_equal, _Tp> 389 { 390 typedef bool result_type; 391 }; 392 393 template<typename _Tp> 394 struct __fun<__greater_equal, _Tp> 395 { 396 typedef bool result_type; 397 }; 398 399 template<typename _Tp> 400 struct __fun<__equal_to, _Tp> 401 { 402 typedef bool result_type; 403 }; 404 405 template<typename _Tp> 406 struct __fun<__not_equal_to, _Tp> 407 { 408 typedef bool result_type; 409 }; 410 411 namespace __detail 412 { 413 // Closure types already have reference semantics and are often short-lived, 414 // so store them by value to avoid (some cases of) dangling references to 415 // out-of-scope temporaries. 416 template<typename _Tp> 417 struct _ValArrayRef 418 { typedef const _Tp __type; }; 419 420 // Use real references for std::valarray objects. 421 template<typename _Tp> 422 struct _ValArrayRef< valarray<_Tp> > 423 { typedef const valarray<_Tp>& __type; }; 424 425 // 426 // Apply function taking a value/const reference closure 427 // 428 429 template<typename _Dom, typename _Arg> 430 class _FunBase 431 { 432 public: 433 typedef typename _Dom::value_type value_type; 434 435 _FunBase(const _Dom& __e, value_type __f(_Arg)) 436 : _M_expr(__e), _M_func(__f) {} 437 438 value_type operator[](size_t __i) const 439 { return _M_func (_M_expr[__i]); } 440 441 size_t size() const { return _M_expr.size ();} 442 443 private: 444 typename _ValArrayRef<_Dom>::__type _M_expr; 445 value_type (*_M_func)(_Arg); 446 }; 447 448 template<class _Dom> 449 struct _ValFunClos<_Expr,_Dom> : _FunBase<_Dom, typename _Dom::value_type> 450 { 451 typedef _FunBase<_Dom, typename _Dom::value_type> _Base; 452 typedef typename _Base::value_type value_type; 453 typedef value_type _Tp; 454 455 _ValFunClos(const _Dom& __e, _Tp __f(_Tp)) : _Base(__e, __f) {} 456 }; 457 458 template<typename _Tp> 459 struct _ValFunClos<_ValArray,_Tp> : _FunBase<valarray<_Tp>, _Tp> 460 { 461 typedef _FunBase<valarray<_Tp>, _Tp> _Base; 462 typedef _Tp value_type; 463 464 _ValFunClos(const valarray<_Tp>& __v, _Tp __f(_Tp)) : _Base(__v, __f) {} 465 }; 466 467 template<class _Dom> 468 struct _RefFunClos<_Expr, _Dom> 469 : _FunBase<_Dom, const typename _Dom::value_type&> 470 { 471 typedef _FunBase<_Dom, const typename _Dom::value_type&> _Base; 472 typedef typename _Base::value_type value_type; 473 typedef value_type _Tp; 474 475 _RefFunClos(const _Dom& __e, _Tp __f(const _Tp&)) 476 : _Base(__e, __f) {} 477 }; 478 479 template<typename _Tp> 480 struct _RefFunClos<_ValArray, _Tp> 481 : _FunBase<valarray<_Tp>, const _Tp&> 482 { 483 typedef _FunBase<valarray<_Tp>, const _Tp&> _Base; 484 typedef _Tp value_type; 485 486 _RefFunClos(const valarray<_Tp>& __v, _Tp __f(const _Tp&)) 487 : _Base(__v, __f) {} 488 }; 489 490 // 491 // Unary expression closure. 492 // 493 494 template<class _Oper, class _Arg> 495 class _UnBase 496 { 497 public: 498 typedef typename _Arg::value_type _Vt; 499 typedef typename __fun<_Oper, _Vt>::result_type value_type; 500 501 _UnBase(const _Arg& __e) : _M_expr(__e) {} 502 503 value_type operator[](size_t __i) const 504 { return _Oper()(_M_expr[__i]); } 505 506 size_t size() const { return _M_expr.size(); } 507 508 private: 509 typename _ValArrayRef<_Arg>::__type _M_expr; 510 }; 511 512 template<class _Oper, class _Dom> 513 struct _UnClos<_Oper, _Expr, _Dom> 514 : _UnBase<_Oper, _Dom> 515 { 516 typedef _Dom _Arg; 517 typedef _UnBase<_Oper, _Dom> _Base; 518 typedef typename _Base::value_type value_type; 519 520 _UnClos(const _Arg& __e) : _Base(__e) {} 521 }; 522 523 template<class _Oper, typename _Tp> 524 struct _UnClos<_Oper, _ValArray, _Tp> 525 : _UnBase<_Oper, valarray<_Tp> > 526 { 527 typedef valarray<_Tp> _Arg; 528 typedef _UnBase<_Oper, valarray<_Tp> > _Base; 529 typedef typename _Base::value_type value_type; 530 531 _UnClos(const _Arg& __e) : _Base(__e) {} 532 }; 533 534 535 // 536 // Binary expression closure. 537 // 538 539 template<class _Oper, class _FirstArg, class _SecondArg> 540 class _BinBase 541 { 542 public: 543 typedef typename _FirstArg::value_type _Vt; 544 typedef typename __fun<_Oper, _Vt>::result_type value_type; 545 546 _BinBase(const _FirstArg& __e1, const _SecondArg& __e2) 547 : _M_expr1(__e1), _M_expr2(__e2) {} 548 549 value_type operator[](size_t __i) const 550 { return _Oper()(_M_expr1[__i], _M_expr2[__i]); } 551 552 size_t size() const { return _M_expr1.size(); } 553 554 private: 555 typename _ValArrayRef<_FirstArg>::__type _M_expr1; 556 typename _ValArrayRef<_SecondArg>::__type _M_expr2; 557 }; 558 559 560 template<class _Oper, class _Clos> 561 class _BinBase2 562 { 563 public: 564 typedef typename _Clos::value_type _Vt; 565 typedef typename __fun<_Oper, _Vt>::result_type value_type; 566 567 _BinBase2(const _Clos& __e, const _Vt& __t) 568 : _M_expr1(__e), _M_expr2(__t) {} 569 570 value_type operator[](size_t __i) const 571 { return _Oper()(_M_expr1[__i], _M_expr2); } 572 573 size_t size() const { return _M_expr1.size(); } 574 575 private: 576 typename _ValArrayRef<_Clos>::__type _M_expr1; 577 _Vt _M_expr2; 578 }; 579 580 template<class _Oper, class _Clos> 581 class _BinBase1 582 { 583 public: 584 typedef typename _Clos::value_type _Vt; 585 typedef typename __fun<_Oper, _Vt>::result_type value_type; 586 587 _BinBase1(const _Vt& __t, const _Clos& __e) 588 : _M_expr1(__t), _M_expr2(__e) {} 589 590 value_type operator[](size_t __i) const 591 { return _Oper()(_M_expr1, _M_expr2[__i]); } 592 593 size_t size() const { return _M_expr2.size(); } 594 595 private: 596 _Vt _M_expr1; 597 typename _ValArrayRef<_Clos>::__type _M_expr2; 598 }; 599 600 template<class _Oper, class _Dom1, class _Dom2> 601 struct _BinClos<_Oper, _Expr, _Expr, _Dom1, _Dom2> 602 : _BinBase<_Oper, _Dom1, _Dom2> 603 { 604 typedef _BinBase<_Oper, _Dom1, _Dom2> _Base; 605 typedef typename _Base::value_type value_type; 606 607 _BinClos(const _Dom1& __e1, const _Dom2& __e2) : _Base(__e1, __e2) {} 608 }; 609 610 template<class _Oper, typename _Tp> 611 struct _BinClos<_Oper, _ValArray, _ValArray, _Tp, _Tp> 612 : _BinBase<_Oper, valarray<_Tp>, valarray<_Tp> > 613 { 614 typedef _BinBase<_Oper, valarray<_Tp>, valarray<_Tp> > _Base; 615 typedef typename _Base::value_type value_type; 616 617 _BinClos(const valarray<_Tp>& __v, const valarray<_Tp>& __w) 618 : _Base(__v, __w) {} 619 }; 620 621 template<class _Oper, class _Dom> 622 struct _BinClos<_Oper, _Expr, _ValArray, _Dom, typename _Dom::value_type> 623 : _BinBase<_Oper, _Dom, valarray<typename _Dom::value_type> > 624 { 625 typedef typename _Dom::value_type _Tp; 626 typedef _BinBase<_Oper,_Dom,valarray<_Tp> > _Base; 627 typedef typename _Base::value_type value_type; 628 629 _BinClos(const _Dom& __e1, const valarray<_Tp>& __e2) 630 : _Base(__e1, __e2) {} 631 }; 632 633 template<class _Oper, class _Dom> 634 struct _BinClos<_Oper, _ValArray, _Expr, typename _Dom::value_type, _Dom> 635 : _BinBase<_Oper, valarray<typename _Dom::value_type>,_Dom> 636 { 637 typedef typename _Dom::value_type _Tp; 638 typedef _BinBase<_Oper, valarray<_Tp>, _Dom> _Base; 639 typedef typename _Base::value_type value_type; 640 641 _BinClos(const valarray<_Tp>& __e1, const _Dom& __e2) 642 : _Base(__e1, __e2) {} 643 }; 644 645 template<class _Oper, class _Dom> 646 struct _BinClos<_Oper, _Expr, _Constant, _Dom, typename _Dom::value_type> 647 : _BinBase2<_Oper, _Dom> 648 { 649 typedef typename _Dom::value_type _Tp; 650 typedef _BinBase2<_Oper,_Dom> _Base; 651 typedef typename _Base::value_type value_type; 652 653 _BinClos(const _Dom& __e1, const _Tp& __e2) : _Base(__e1, __e2) {} 654 }; 655 656 template<class _Oper, class _Dom> 657 struct _BinClos<_Oper, _Constant, _Expr, typename _Dom::value_type, _Dom> 658 : _BinBase1<_Oper, _Dom> 659 { 660 typedef typename _Dom::value_type _Tp; 661 typedef _BinBase1<_Oper, _Dom> _Base; 662 typedef typename _Base::value_type value_type; 663 664 _BinClos(const _Tp& __e1, const _Dom& __e2) : _Base(__e1, __e2) {} 665 }; 666 667 template<class _Oper, typename _Tp> 668 struct _BinClos<_Oper, _ValArray, _Constant, _Tp, _Tp> 669 : _BinBase2<_Oper, valarray<_Tp> > 670 { 671 typedef _BinBase2<_Oper,valarray<_Tp> > _Base; 672 typedef typename _Base::value_type value_type; 673 674 _BinClos(const valarray<_Tp>& __v, const _Tp& __t) : _Base(__v, __t) {} 675 }; 676 677 template<class _Oper, typename _Tp> 678 struct _BinClos<_Oper, _Constant, _ValArray, _Tp, _Tp> 679 : _BinBase1<_Oper, valarray<_Tp> > 680 { 681 typedef _BinBase1<_Oper, valarray<_Tp> > _Base; 682 typedef typename _Base::value_type value_type; 683 684 _BinClos(const _Tp& __t, const valarray<_Tp>& __v) : _Base(__t, __v) {} 685 }; 686 687 // 688 // slice_array closure. 689 // 690 template<typename _Dom> 691 class _SBase 692 { 693 public: 694 typedef typename _Dom::value_type value_type; 695 696 _SBase (const _Dom& __e, const slice& __s) 697 : _M_expr (__e), _M_slice (__s) {} 698 699 value_type 700 operator[] (size_t __i) const 701 { return _M_expr[_M_slice.start () + __i * _M_slice.stride ()]; } 702 703 size_t 704 size() const 705 { return _M_slice.size (); } 706 707 private: 708 typename _ValArrayRef<_Dom>::__type _M_expr; 709 const slice& _M_slice; 710 }; 711 712 template<typename _Tp> 713 class _SBase<_Array<_Tp> > 714 { 715 public: 716 typedef _Tp value_type; 717 718 _SBase (_Array<_Tp> __a, const slice& __s) 719 : _M_array (__a._M_data+__s.start()), _M_size (__s.size()), 720 _M_stride (__s.stride()) {} 721 722 value_type 723 operator[] (size_t __i) const 724 { return _M_array._M_data[__i * _M_stride]; } 725 726 size_t 727 size() const 728 { return _M_size; } 729 730 private: 731 const _Array<_Tp> _M_array; 732 const size_t _M_size; 733 const size_t _M_stride; 734 }; 735 736 template<class _Dom> 737 struct _SClos<_Expr, _Dom> 738 : _SBase<_Dom> 739 { 740 typedef _SBase<_Dom> _Base; 741 typedef typename _Base::value_type value_type; 742 743 _SClos (const _Dom& __e, const slice& __s) : _Base (__e, __s) {} 744 }; 745 746 template<typename _Tp> 747 struct _SClos<_ValArray, _Tp> 748 : _SBase<_Array<_Tp> > 749 { 750 typedef _SBase<_Array<_Tp> > _Base; 751 typedef _Tp value_type; 752 753 _SClos (_Array<_Tp> __a, const slice& __s) : _Base (__a, __s) {} 754 }; 755 } // namespace __detail 756 757 _GLIBCXX_END_NAMESPACE_VERSION 758 } // namespace 759 760 #endif /* _CPP_VALARRAY_BEFORE_H */