valarray_array.tcc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // The template and inlines for the -*- C++ -*- internal _Array helper class.
  2. // Copyright (C) 1997-2022 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 bits/valarray_array.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{valarray}
  23. */
  24. // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
  25. #ifndef _VALARRAY_ARRAY_TCC
  26. #define _VALARRAY_ARRAY_TCC 1
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. template<typename _Tp>
  31. void
  32. __valarray_fill(_Array<_Tp> __a, size_t __n, _Array<bool> __m,
  33. const _Tp& __t)
  34. {
  35. _Tp* __p = __a._M_data;
  36. bool* __ok (__m._M_data);
  37. for (size_t __i=0; __i < __n; ++__i, ++__ok, ++__p)
  38. {
  39. while (!*__ok)
  40. {
  41. ++__ok;
  42. ++__p;
  43. }
  44. *__p = __t;
  45. }
  46. }
  47. // Copy n elements of a into consecutive elements of b. When m is
  48. // false, the corresponding element of a is skipped. m must contain
  49. // at least n true elements. a must contain at least n elements and
  50. // enough elements to match up with m through the nth true element
  51. // of m. I.e. if n is 10, m has 15 elements with 5 false followed
  52. // by 10 true, a must have 15 elements.
  53. template<typename _Tp>
  54. void
  55. __valarray_copy(_Array<_Tp> __a, _Array<bool> __m, _Array<_Tp> __b,
  56. size_t __n)
  57. {
  58. _Tp* __p (__a._M_data);
  59. bool* __ok (__m._M_data);
  60. for (_Tp* __q = __b._M_data; __q < __b._M_data + __n;
  61. ++__q, ++__ok, ++__p)
  62. {
  63. while (! *__ok)
  64. {
  65. ++__ok;
  66. ++__p;
  67. }
  68. *__q = *__p;
  69. }
  70. }
  71. // Copy n consecutive elements from a into elements of b. Elements
  72. // of b are skipped if the corresponding element of m is false. m
  73. // must contain at least n true elements. b must have at least as
  74. // many elements as the index of the nth true element of m. I.e. if
  75. // m has 15 elements with 5 false followed by 10 true, b must have
  76. // at least 15 elements.
  77. template<typename _Tp>
  78. void
  79. __valarray_copy(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b,
  80. _Array<bool> __m)
  81. {
  82. _Tp* __q (__b._M_data);
  83. bool* __ok (__m._M_data);
  84. for (_Tp* __p = __a._M_data; __p < __a._M_data+__n;
  85. ++__p, ++__ok, ++__q)
  86. {
  87. while (! *__ok)
  88. {
  89. ++__ok;
  90. ++__q;
  91. }
  92. *__q = *__p;
  93. }
  94. }
  95. // Copy n elements from a into elements of b. Elements of a are
  96. // skipped if the corresponding element of m is false. Elements of
  97. // b are skipped if the corresponding element of k is false. m and
  98. // k must contain at least n true elements. a and b must have at
  99. // least as many elements as the index of the nth true element of m.
  100. template<typename _Tp>
  101. void
  102. __valarray_copy(_Array<_Tp> __a, _Array<bool> __m, size_t __n,
  103. _Array<_Tp> __b, _Array<bool> __k)
  104. {
  105. _Tp* __p (__a._M_data);
  106. _Tp* __q (__b._M_data);
  107. bool* __srcok (__m._M_data);
  108. bool* __dstok (__k._M_data);
  109. for (size_t __i = 0; __i < __n;
  110. ++__srcok, ++__p, ++__dstok, ++__q, ++__i)
  111. {
  112. while (! *__srcok)
  113. {
  114. ++__srcok;
  115. ++__p;
  116. }
  117. while (! *__dstok)
  118. {
  119. ++__dstok;
  120. ++__q;
  121. }
  122. *__q = *__p;
  123. }
  124. }
  125. // Copy n consecutive elements of e into consecutive elements of a.
  126. // I.e. a[i] = e[i].
  127. template<typename _Tp, class _Dom>
  128. void
  129. __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n, _Array<_Tp> __a)
  130. {
  131. _Tp* __p (__a._M_data);
  132. for (size_t __i = 0; __i < __n; ++__i, ++__p)
  133. *__p = __e[__i];
  134. }
  135. // Copy n consecutive elements of e into elements of a using stride
  136. // s. I.e., a[0] = e[0], a[s] = e[1], a[2*s] = e[2].
  137. template<typename _Tp, class _Dom>
  138. void
  139. __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n,
  140. _Array<_Tp> __a, size_t __s)
  141. {
  142. _Tp* __p (__a._M_data);
  143. for (size_t __i = 0; __i < __n; ++__i, __p += __s)
  144. *__p = __e[__i];
  145. }
  146. // Copy n consecutive elements of e into elements of a indexed by
  147. // contents of i. I.e., a[i[0]] = e[0].
  148. template<typename _Tp, class _Dom>
  149. void
  150. __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n,
  151. _Array<_Tp> __a, _Array<size_t> __i)
  152. {
  153. size_t* __j (__i._M_data);
  154. for (size_t __k = 0; __k < __n; ++__k, ++__j)
  155. __a._M_data[*__j] = __e[__k];
  156. }
  157. // Copy n elements of e indexed by contents of f into elements of a
  158. // indexed by contents of i. I.e., a[i[0]] = e[f[0]].
  159. template<typename _Tp>
  160. void
  161. __valarray_copy(_Array<_Tp> __e, _Array<size_t> __f,
  162. size_t __n,
  163. _Array<_Tp> __a, _Array<size_t> __i)
  164. {
  165. size_t* __g (__f._M_data);
  166. size_t* __j (__i._M_data);
  167. for (size_t __k = 0; __k < __n; ++__k, ++__j, ++__g)
  168. __a._M_data[*__j] = __e._M_data[*__g];
  169. }
  170. // Copy n consecutive elements of e into elements of a. Elements of
  171. // a are skipped if the corresponding element of m is false. m must
  172. // have at least n true elements and a must have at least as many
  173. // elements as the index of the nth true element of m. I.e. if m
  174. // has 5 false followed by 10 true elements and n == 10, a must have
  175. // at least 15 elements.
  176. template<typename _Tp, class _Dom>
  177. void
  178. __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n,
  179. _Array<_Tp> __a, _Array<bool> __m)
  180. {
  181. bool* __ok (__m._M_data);
  182. _Tp* __p (__a._M_data);
  183. for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p)
  184. {
  185. while (! *__ok)
  186. {
  187. ++__ok;
  188. ++__p;
  189. }
  190. *__p = __e[__i];
  191. }
  192. }
  193. template<typename _Tp, class _Dom>
  194. void
  195. __valarray_copy_construct(const _Expr<_Dom, _Tp>& __e, size_t __n,
  196. _Array<_Tp> __a)
  197. {
  198. _Tp* __p (__a._M_data);
  199. for (size_t __i = 0; __i < __n; ++__i, ++__p)
  200. new (__p) _Tp(__e[__i]);
  201. }
  202. template<typename _Tp>
  203. void
  204. __valarray_copy_construct(_Array<_Tp> __a, _Array<bool> __m,
  205. _Array<_Tp> __b, size_t __n)
  206. {
  207. _Tp* __p (__a._M_data);
  208. bool* __ok (__m._M_data);
  209. for (_Tp* __q = __b._M_data; __q < __b._M_data+__n; ++__q, ++__ok, ++__p)
  210. {
  211. while (! *__ok)
  212. {
  213. ++__ok;
  214. ++__p;
  215. }
  216. new (__q) _Tp(*__p);
  217. }
  218. }
  219. _GLIBCXX_END_NAMESPACE_VERSION
  220. } // namespace
  221. #endif /* _VALARRAY_ARRAY_TCC */