valarray 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. // The template and inlines for the -*- C++ -*- valarray class.
  2. // Copyright (C) 1997-2015 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. template<typename _Tp>
  472. inline const _Tp&
  473. valarray<_Tp>::operator[](size_t __i) const
  474. {
  475. __glibcxx_requires_subscript(__i);
  476. return _M_data[__i];
  477. }
  478. template<typename _Tp>
  479. inline _Tp&
  480. valarray<_Tp>::operator[](size_t __i)
  481. {
  482. __glibcxx_requires_subscript(__i);
  483. return _M_data[__i];
  484. }
  485. // @} group numeric_arrays
  486. _GLIBCXX_END_NAMESPACE_VERSION
  487. } // namespace
  488. #include <bits/valarray_after.h>
  489. #include <bits/slice_array.h>
  490. #include <bits/gslice.h>
  491. #include <bits/gslice_array.h>
  492. #include <bits/mask_array.h>
  493. #include <bits/indirect_array.h>
  494. namespace std _GLIBCXX_VISIBILITY(default)
  495. {
  496. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  497. /**
  498. * @addtogroup numeric_arrays
  499. * @{
  500. */
  501. template<typename _Tp>
  502. inline
  503. valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
  504. template<typename _Tp>
  505. inline
  506. valarray<_Tp>::valarray(size_t __n)
  507. : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
  508. { std::__valarray_default_construct(_M_data, _M_data + __n); }
  509. template<typename _Tp>
  510. inline
  511. valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
  512. : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
  513. { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
  514. template<typename _Tp>
  515. inline
  516. valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
  517. : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
  518. {
  519. _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
  520. std::__valarray_copy_construct(__p, __p + __n, _M_data);
  521. }
  522. template<typename _Tp>
  523. inline
  524. valarray<_Tp>::valarray(const valarray<_Tp>& __v)
  525. : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
  526. { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
  527. _M_data); }
  528. #if __cplusplus >= 201103L
  529. template<typename _Tp>
  530. inline
  531. valarray<_Tp>::valarray(valarray<_Tp>&& __v) noexcept
  532. : _M_size(__v._M_size), _M_data(__v._M_data)
  533. {
  534. __v._M_size = 0;
  535. __v._M_data = 0;
  536. }
  537. #endif
  538. template<typename _Tp>
  539. inline
  540. valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
  541. : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
  542. {
  543. std::__valarray_copy_construct
  544. (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
  545. }
  546. template<typename _Tp>
  547. inline
  548. valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
  549. : _M_size(__ga._M_index.size()),
  550. _M_data(__valarray_get_storage<_Tp>(_M_size))
  551. {
  552. std::__valarray_copy_construct
  553. (__ga._M_array, _Array<size_t>(__ga._M_index),
  554. _Array<_Tp>(_M_data), _M_size);
  555. }
  556. template<typename _Tp>
  557. inline
  558. valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
  559. : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
  560. {
  561. std::__valarray_copy_construct
  562. (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
  563. }
  564. template<typename _Tp>
  565. inline
  566. valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
  567. : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
  568. {
  569. std::__valarray_copy_construct
  570. (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
  571. }
  572. #if __cplusplus >= 201103L
  573. template<typename _Tp>
  574. inline
  575. valarray<_Tp>::valarray(initializer_list<_Tp> __l)
  576. : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
  577. { std::__valarray_copy_construct(__l.begin(), __l.end(), _M_data); }
  578. #endif
  579. template<typename _Tp> template<class _Dom>
  580. inline
  581. valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
  582. : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
  583. { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
  584. template<typename _Tp>
  585. inline
  586. valarray<_Tp>::~valarray() _GLIBCXX_NOEXCEPT
  587. {
  588. std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
  589. std::__valarray_release_memory(_M_data);
  590. }
  591. template<typename _Tp>
  592. inline valarray<_Tp>&
  593. valarray<_Tp>::operator=(const valarray<_Tp>& __v)
  594. {
  595. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  596. // 630. arrays of valarray.
  597. if (_M_size == __v._M_size)
  598. std::__valarray_copy(__v._M_data, _M_size, _M_data);
  599. else
  600. {
  601. if (_M_data)
  602. {
  603. std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
  604. std::__valarray_release_memory(_M_data);
  605. }
  606. _M_size = __v._M_size;
  607. _M_data = __valarray_get_storage<_Tp>(_M_size);
  608. std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
  609. _M_data);
  610. }
  611. return *this;
  612. }
  613. #if __cplusplus >= 201103L
  614. template<typename _Tp>
  615. inline valarray<_Tp>&
  616. valarray<_Tp>::operator=(valarray<_Tp>&& __v) noexcept
  617. {
  618. if (_M_data)
  619. {
  620. std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
  621. std::__valarray_release_memory(_M_data);
  622. }
  623. _M_size = __v._M_size;
  624. _M_data = __v._M_data;
  625. __v._M_size = 0;
  626. __v._M_data = 0;
  627. return *this;
  628. }
  629. template<typename _Tp>
  630. inline valarray<_Tp>&
  631. valarray<_Tp>::operator=(initializer_list<_Tp> __l)
  632. {
  633. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  634. // 630. arrays of valarray.
  635. if (_M_size == __l.size())
  636. std::__valarray_copy(__l.begin(), __l.size(), _M_data);
  637. else
  638. {
  639. if (_M_data)
  640. {
  641. std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
  642. std::__valarray_release_memory(_M_data);
  643. }
  644. _M_size = __l.size();
  645. _M_data = __valarray_get_storage<_Tp>(_M_size);
  646. std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size,
  647. _M_data);
  648. }
  649. return *this;
  650. }
  651. #endif
  652. template<typename _Tp>
  653. inline valarray<_Tp>&
  654. valarray<_Tp>::operator=(const _Tp& __t)
  655. {
  656. std::__valarray_fill(_M_data, _M_size, __t);
  657. return *this;
  658. }
  659. template<typename _Tp>
  660. inline valarray<_Tp>&
  661. valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
  662. {
  663. _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
  664. std::__valarray_copy(__sa._M_array, __sa._M_sz,
  665. __sa._M_stride, _Array<_Tp>(_M_data));
  666. return *this;
  667. }
  668. template<typename _Tp>
  669. inline valarray<_Tp>&
  670. valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
  671. {
  672. _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
  673. std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
  674. _Array<_Tp>(_M_data), _M_size);
  675. return *this;
  676. }
  677. template<typename _Tp>
  678. inline valarray<_Tp>&
  679. valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
  680. {
  681. _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
  682. std::__valarray_copy(__ma._M_array, __ma._M_mask,
  683. _Array<_Tp>(_M_data), _M_size);
  684. return *this;
  685. }
  686. template<typename _Tp>
  687. inline valarray<_Tp>&
  688. valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
  689. {
  690. _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
  691. std::__valarray_copy(__ia._M_array, __ia._M_index,
  692. _Array<_Tp>(_M_data), _M_size);
  693. return *this;
  694. }
  695. template<typename _Tp> template<class _Dom>
  696. inline valarray<_Tp>&
  697. valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
  698. {
  699. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  700. // 630. arrays of valarray.
  701. if (_M_size == __e.size())
  702. std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
  703. else
  704. {
  705. if (_M_data)
  706. {
  707. std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
  708. std::__valarray_release_memory(_M_data);
  709. }
  710. _M_size = __e.size();
  711. _M_data = __valarray_get_storage<_Tp>(_M_size);
  712. std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data));
  713. }
  714. return *this;
  715. }
  716. template<typename _Tp>
  717. inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
  718. valarray<_Tp>::operator[](slice __s) const
  719. {
  720. typedef _SClos<_ValArray,_Tp> _Closure;
  721. return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
  722. }
  723. template<typename _Tp>
  724. inline slice_array<_Tp>
  725. valarray<_Tp>::operator[](slice __s)
  726. { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
  727. template<typename _Tp>
  728. inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
  729. valarray<_Tp>::operator[](const gslice& __gs) const
  730. {
  731. typedef _GClos<_ValArray,_Tp> _Closure;
  732. return _Expr<_Closure, _Tp>
  733. (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
  734. }
  735. template<typename _Tp>
  736. inline gslice_array<_Tp>
  737. valarray<_Tp>::operator[](const gslice& __gs)
  738. {
  739. return gslice_array<_Tp>
  740. (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
  741. }
  742. template<typename _Tp>
  743. inline valarray<_Tp>
  744. valarray<_Tp>::operator[](const valarray<bool>& __m) const
  745. {
  746. size_t __s = 0;
  747. size_t __e = __m.size();
  748. for (size_t __i=0; __i<__e; ++__i)
  749. if (__m[__i]) ++__s;
  750. return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
  751. _Array<bool> (__m)));
  752. }
  753. template<typename _Tp>
  754. inline mask_array<_Tp>
  755. valarray<_Tp>::operator[](const valarray<bool>& __m)
  756. {
  757. size_t __s = 0;
  758. size_t __e = __m.size();
  759. for (size_t __i=0; __i<__e; ++__i)
  760. if (__m[__i]) ++__s;
  761. return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
  762. }
  763. template<typename _Tp>
  764. inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
  765. valarray<_Tp>::operator[](const valarray<size_t>& __i) const
  766. {
  767. typedef _IClos<_ValArray,_Tp> _Closure;
  768. return _Expr<_Closure, _Tp>(_Closure(*this, __i));
  769. }
  770. template<typename _Tp>
  771. inline indirect_array<_Tp>
  772. valarray<_Tp>::operator[](const valarray<size_t>& __i)
  773. {
  774. return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
  775. _Array<size_t>(__i));
  776. }
  777. #if __cplusplus >= 201103L
  778. template<class _Tp>
  779. inline void
  780. valarray<_Tp>::swap(valarray<_Tp>& __v) noexcept
  781. {
  782. std::swap(_M_size, __v._M_size);
  783. std::swap(_M_data, __v._M_data);
  784. }
  785. #endif
  786. template<class _Tp>
  787. inline size_t
  788. valarray<_Tp>::size() const
  789. { return _M_size; }
  790. template<class _Tp>
  791. inline _Tp
  792. valarray<_Tp>::sum() const
  793. {
  794. _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
  795. return std::__valarray_sum(_M_data, _M_data + _M_size);
  796. }
  797. template<class _Tp>
  798. inline valarray<_Tp>
  799. valarray<_Tp>::shift(int __n) const
  800. {
  801. valarray<_Tp> __ret;
  802. if (_M_size == 0)
  803. return __ret;
  804. _Tp* __restrict__ __tmp_M_data =
  805. std::__valarray_get_storage<_Tp>(_M_size);
  806. if (__n == 0)
  807. std::__valarray_copy_construct(_M_data,
  808. _M_data + _M_size, __tmp_M_data);
  809. else if (__n > 0) // shift left
  810. {
  811. if (size_t(__n) > _M_size)
  812. __n = int(_M_size);
  813. std::__valarray_copy_construct(_M_data + __n,
  814. _M_data + _M_size, __tmp_M_data);
  815. std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
  816. __tmp_M_data + _M_size);
  817. }
  818. else // shift right
  819. {
  820. if (-size_t(__n) > _M_size)
  821. __n = -int(_M_size);
  822. std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
  823. __tmp_M_data - __n);
  824. std::__valarray_default_construct(__tmp_M_data,
  825. __tmp_M_data - __n);
  826. }
  827. __ret._M_size = _M_size;
  828. __ret._M_data = __tmp_M_data;
  829. return __ret;
  830. }
  831. template<class _Tp>
  832. inline valarray<_Tp>
  833. valarray<_Tp>::cshift(int __n) const
  834. {
  835. valarray<_Tp> __ret;
  836. if (_M_size == 0)
  837. return __ret;
  838. _Tp* __restrict__ __tmp_M_data =
  839. std::__valarray_get_storage<_Tp>(_M_size);
  840. if (__n == 0)
  841. std::__valarray_copy_construct(_M_data,
  842. _M_data + _M_size, __tmp_M_data);
  843. else if (__n > 0) // cshift left
  844. {
  845. if (size_t(__n) > _M_size)
  846. __n = int(__n % _M_size);
  847. std::__valarray_copy_construct(_M_data, _M_data + __n,
  848. __tmp_M_data + _M_size - __n);
  849. std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
  850. __tmp_M_data);
  851. }
  852. else // cshift right
  853. {
  854. if (-size_t(__n) > _M_size)
  855. __n = -int(-size_t(__n) % _M_size);
  856. std::__valarray_copy_construct(_M_data + _M_size + __n,
  857. _M_data + _M_size, __tmp_M_data);
  858. std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
  859. __tmp_M_data - __n);
  860. }
  861. __ret._M_size = _M_size;
  862. __ret._M_data = __tmp_M_data;
  863. return __ret;
  864. }
  865. template<class _Tp>
  866. inline void
  867. valarray<_Tp>::resize(size_t __n, _Tp __c)
  868. {
  869. // This complication is so to make valarray<valarray<T> > work
  870. // even though it is not required by the standard. Nobody should
  871. // be saying valarray<valarray<T> > anyway. See the specs.
  872. std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
  873. if (_M_size != __n)
  874. {
  875. std::__valarray_release_memory(_M_data);
  876. _M_size = __n;
  877. _M_data = __valarray_get_storage<_Tp>(__n);
  878. }
  879. std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
  880. }
  881. template<typename _Tp>
  882. inline _Tp
  883. valarray<_Tp>::min() const
  884. {
  885. _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
  886. return *std::min_element(_M_data, _M_data + _M_size);
  887. }
  888. template<typename _Tp>
  889. inline _Tp
  890. valarray<_Tp>::max() const
  891. {
  892. _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
  893. return *std::max_element(_M_data, _M_data + _M_size);
  894. }
  895. template<class _Tp>
  896. inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
  897. valarray<_Tp>::apply(_Tp func(_Tp)) const
  898. {
  899. typedef _ValFunClos<_ValArray, _Tp> _Closure;
  900. return _Expr<_Closure, _Tp>(_Closure(*this, func));
  901. }
  902. template<class _Tp>
  903. inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
  904. valarray<_Tp>::apply(_Tp func(const _Tp &)) const
  905. {
  906. typedef _RefFunClos<_ValArray, _Tp> _Closure;
  907. return _Expr<_Closure, _Tp>(_Closure(*this, func));
  908. }
  909. #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
  910. template<typename _Tp> \
  911. inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
  912. valarray<_Tp>::operator _Op() const \
  913. { \
  914. typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
  915. typedef typename __fun<_Name, _Tp>::result_type _Rt; \
  916. return _Expr<_Closure, _Rt>(_Closure(*this)); \
  917. }
  918. _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
  919. _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
  920. _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
  921. _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
  922. #undef _DEFINE_VALARRAY_UNARY_OPERATOR
  923. #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
  924. template<class _Tp> \
  925. inline valarray<_Tp>& \
  926. valarray<_Tp>::operator _Op##=(const _Tp &__t) \
  927. { \
  928. _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
  929. return *this; \
  930. } \
  931. \
  932. template<class _Tp> \
  933. inline valarray<_Tp>& \
  934. valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
  935. { \
  936. _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
  937. _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
  938. _Array<_Tp>(__v._M_data)); \
  939. return *this; \
  940. }
  941. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
  942. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
  943. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
  944. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
  945. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
  946. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
  947. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
  948. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
  949. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
  950. _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
  951. #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
  952. #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
  953. template<class _Tp> template<class _Dom> \
  954. inline valarray<_Tp>& \
  955. valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
  956. { \
  957. _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
  958. return *this; \
  959. }
  960. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
  961. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
  962. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
  963. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
  964. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
  965. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
  966. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
  967. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
  968. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
  969. _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
  970. #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
  971. #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
  972. template<typename _Tp> \
  973. inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
  974. typename __fun<_Name, _Tp>::result_type> \
  975. operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
  976. { \
  977. _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
  978. typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
  979. typedef typename __fun<_Name, _Tp>::result_type _Rt; \
  980. return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
  981. } \
  982. \
  983. template<typename _Tp> \
  984. inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
  985. typename __fun<_Name, _Tp>::result_type> \
  986. operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
  987. { \
  988. typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
  989. typedef typename __fun<_Name, _Tp>::result_type _Rt; \
  990. return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
  991. } \
  992. \
  993. template<typename _Tp> \
  994. inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
  995. typename __fun<_Name, _Tp>::result_type> \
  996. operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
  997. { \
  998. typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
  999. typedef typename __fun<_Name, _Tp>::result_type _Rt; \
  1000. return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
  1001. }
  1002. _DEFINE_BINARY_OPERATOR(+, __plus)
  1003. _DEFINE_BINARY_OPERATOR(-, __minus)
  1004. _DEFINE_BINARY_OPERATOR(*, __multiplies)
  1005. _DEFINE_BINARY_OPERATOR(/, __divides)
  1006. _DEFINE_BINARY_OPERATOR(%, __modulus)
  1007. _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
  1008. _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
  1009. _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
  1010. _DEFINE_BINARY_OPERATOR(<<, __shift_left)
  1011. _DEFINE_BINARY_OPERATOR(>>, __shift_right)
  1012. _DEFINE_BINARY_OPERATOR(&&, __logical_and)
  1013. _DEFINE_BINARY_OPERATOR(||, __logical_or)
  1014. _DEFINE_BINARY_OPERATOR(==, __equal_to)
  1015. _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
  1016. _DEFINE_BINARY_OPERATOR(<, __less)
  1017. _DEFINE_BINARY_OPERATOR(>, __greater)
  1018. _DEFINE_BINARY_OPERATOR(<=, __less_equal)
  1019. _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
  1020. #undef _DEFINE_BINARY_OPERATOR
  1021. #if __cplusplus >= 201103L
  1022. /**
  1023. * @brief Return an iterator pointing to the first element of
  1024. * the valarray.
  1025. * @param __va valarray.
  1026. */
  1027. template<class _Tp>
  1028. inline _Tp*
  1029. begin(valarray<_Tp>& __va)
  1030. { return std::__addressof(__va[0]); }
  1031. /**
  1032. * @brief Return an iterator pointing to the first element of
  1033. * the const valarray.
  1034. * @param __va valarray.
  1035. */
  1036. template<class _Tp>
  1037. inline const _Tp*
  1038. begin(const valarray<_Tp>& __va)
  1039. { return std::__addressof(__va[0]); }
  1040. /**
  1041. * @brief Return an iterator pointing to one past the last element of
  1042. * the valarray.
  1043. * @param __va valarray.
  1044. */
  1045. template<class _Tp>
  1046. inline _Tp*
  1047. end(valarray<_Tp>& __va)
  1048. { return std::__addressof(__va[0]) + __va.size(); }
  1049. /**
  1050. * @brief Return an iterator pointing to one past the last element of
  1051. * the const valarray.
  1052. * @param __va valarray.
  1053. */
  1054. template<class _Tp>
  1055. inline const _Tp*
  1056. end(const valarray<_Tp>& __va)
  1057. { return std::__addressof(__va[0]) + __va.size(); }
  1058. #endif // C++11
  1059. // @} group numeric_arrays
  1060. _GLIBCXX_END_NAMESPACE_VERSION
  1061. } // namespace
  1062. #endif /* _GLIBCXX_VALARRAY */