valarray 39 KB

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