valarray 36 KB

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