libstdc++
valarray_array.h
Go to the documentation of this file.
1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
2 
3 // Copyright (C) 1997-2026 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/valarray_array.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{valarray}
28  */
29 
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31 
32 #ifndef _VALARRAY_ARRAY_H
33 #define _VALARRAY_ARRAY_H 1
34 
35 #ifdef _GLIBCXX_SYSHDR
36 #pragma GCC system_header
37 #endif
38 
39 #include <bits/c++config.h>
40 #include <bits/cpp_type_traits.h>
41 #include <bits/new_allocator.h>
42 #include <cstdlib>
43 #include <new>
44 
45 namespace std _GLIBCXX_VISIBILITY(default)
46 {
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 
49  //
50  // Helper functions on raw pointers
51  //
52 
53  // We get memory the old fashioned way
54  template<typename _Tp>
55  _Tp*
56  __valarray_get_storage(size_t) __attribute__((__malloc__));
57 
58  template<typename _Tp>
59  inline _Tp*
60  __valarray_get_storage(size_t __n)
61  { return std::__new_allocator<_Tp>().allocate(__n); }
62 
63  // Return memory to the system
64  template<typename _Tp>
65  inline void
66  __valarray_release_memory(_Tp* __p, size_t __n)
67  { std::__new_allocator<_Tp>().deallocate(__p, __n); }
68 
69 #pragma GCC diagnostic push
70 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
71 
72  // Turn raw-memory into an array of _Tp filled with _Tp().
73  // This is used in `valarray<T> v(n);` and in `valarray<T>::shift(n)`.
74  template<typename _Tp>
75  inline void
76  __valarray_default_construct(_Tp* __b, _Tp* __e)
77  {
78  if _GLIBCXX_CONSTEXPR (__is_trivial(_Tp))
79  __builtin_memset(__b, 0, (__e - __b) * sizeof(_Tp));
80  else
81  while (__b != __e)
82  ::new(static_cast<void*>(__b++)) _Tp();
83  }
84 
85  // Turn a raw-memory into an array of _Tp filled with __t
86  // This is the required in valarray<T> v(n, t). Also
87  // used in valarray<>::resize().
88  template<typename _Tp>
89  inline void
90  __valarray_fill_construct(_Tp* __b, _Tp* __e, const _Tp __t)
91  {
92  while (__b != __e)
93  ::new(static_cast<void*>(__b++)) _Tp(__t);
94  }
95 
96  // copy-construct raw array [__o, *) from plain array [__b, __e)
97  template<typename _Tp>
98  inline void
99  __valarray_copy_construct(const _Tp* __b, const _Tp* __e,
100  _Tp* __restrict__ __o)
101  {
102  if _GLIBCXX_CONSTEXPR (__is_trivial(_Tp))
103  {
104  if (__b)
105  __builtin_memcpy(__o, __b, (__e - __b) * sizeof(_Tp));
106  }
107  else
108  while (__b != __e)
109  ::new(static_cast<void*>(__o++)) _Tp(*__b++);
110  }
111 
112  // copy-construct raw array [__o, *) from strided array __a[<__n : __s>]
113  template<typename _Tp>
114  inline void
115  __valarray_copy_construct (const _Tp* __restrict__ __a, size_t __n,
116  size_t __s, _Tp* __restrict__ __o)
117  {
118  if _GLIBCXX_CONSTEXPR (__is_trivial(_Tp))
119  while (__n--)
120  {
121  *__o++ = *__a;
122  __a += __s;
123  }
124  else
125  while (__n--)
126  {
127  new(__o++) _Tp(*__a);
128  __a += __s;
129  }
130  }
131 
132  // copy-construct raw array [__o, *) from indexed array __a[__i[<__n>]]
133  template<typename _Tp>
134  inline void
135  __valarray_copy_construct (const _Tp* __restrict__ __a,
136  const size_t* __restrict__ __i,
137  _Tp* __restrict__ __o, size_t __n)
138  {
139  if _GLIBCXX_CONSTEXPR (__is_trivial(_Tp))
140  while (__n--)
141  *__o++ = __a[*__i++];
142  else
143  while (__n--)
144  new (__o++) _Tp(__a[*__i++]);
145  }
146 
147  // Do the necessary cleanup when we're done with arrays.
148  template<typename _Tp>
149  inline void
150  __valarray_destroy_elements(_Tp* __b, _Tp* __e)
151  {
152  if _GLIBCXX_CONSTEXPR (!__is_trivial(_Tp))
153  while (__b != __e)
154  {
155  __b->~_Tp();
156  ++__b;
157  }
158  }
159 
160 #pragma GCC diagnostic pop
161 
162  // Fill a plain array __a[<__n>] with __t
163  template<typename _Tp>
164  inline void
165  __valarray_fill(_Tp* __restrict__ __a, size_t __n, const _Tp& __t)
166  {
167  while (__n--)
168  *__a++ = __t;
169  }
170 
171  // fill strided array __a[<__n-1 : __s>] with __t
172  template<typename _Tp>
173  inline void
174  __valarray_fill(_Tp* __restrict__ __a, size_t __n,
175  size_t __s, const _Tp& __t)
176  {
177  for (size_t __i = 0; __i < __n; ++__i, __a += __s)
178  *__a = __t;
179  }
180 
181  // fill indirect array __a[__i[<__n>]] with __i
182  template<typename _Tp>
183  inline void
184  __valarray_fill(_Tp* __restrict__ __a, const size_t* __restrict__ __i,
185  size_t __n, const _Tp& __t)
186  {
187  for (size_t __j = 0; __j < __n; ++__j, ++__i)
188  __a[*__i] = __t;
189  }
190 
191  // copy plain array __a[<__n>] in __b[<__n>]
192  // For non-fundamental types, it is wrong to say 'memcpy()'
193  template<typename _Tp, bool>
194  struct _Array_copier
195  {
196  inline static void
197  _S_do_it(const _Tp* __restrict__ __a, size_t __n, _Tp* __restrict__ __b)
198  {
199  while(__n--)
200  *__b++ = *__a++;
201  }
202  };
203 
204  template<typename _Tp>
205  struct _Array_copier<_Tp, true>
206  {
207  inline static void
208  _S_do_it(const _Tp* __restrict__ __a, size_t __n, _Tp* __restrict__ __b)
209  {
210  if (__n != 0)
211  __builtin_memcpy(__b, __a, __n * sizeof (_Tp));
212  }
213  };
214 
215  // Copy a plain array __a[<__n>] into a play array __b[<>]
216  template<typename _Tp>
217  inline void
218  __valarray_copy(const _Tp* __restrict__ __a, size_t __n,
219  _Tp* __restrict__ __b)
220  {
221  _Array_copier<_Tp, __is_trivial(_Tp)>::_S_do_it(__a, __n, __b);
222  }
223 
224  // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
225  template<typename _Tp>
226  inline void
227  __valarray_copy(const _Tp* __restrict__ __a, size_t __n, size_t __s,
228  _Tp* __restrict__ __b)
229  {
230  for (size_t __i = 0; __i < __n; ++__i, ++__b, __a += __s)
231  *__b = *__a;
232  }
233 
234  // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
235  template<typename _Tp>
236  inline void
237  __valarray_copy(const _Tp* __restrict__ __a, _Tp* __restrict__ __b,
238  size_t __n, size_t __s)
239  {
240  for (size_t __i = 0; __i < __n; ++__i, ++__a, __b += __s)
241  *__b = *__a;
242  }
243 
244  // Copy strided array __src[<__n : __s1>] into another
245  // strided array __dst[< : __s2>]. Their sizes must match.
246  template<typename _Tp>
247  inline void
248  __valarray_copy(const _Tp* __restrict__ __src, size_t __n, size_t __s1,
249  _Tp* __restrict__ __dst, size_t __s2)
250  {
251  for (size_t __i = 0; __i < __n; ++__i)
252  __dst[__i * __s2] = __src[__i * __s1];
253  }
254 
255  // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
256  template<typename _Tp>
257  inline void
258  __valarray_copy(const _Tp* __restrict__ __a,
259  const size_t* __restrict__ __i,
260  _Tp* __restrict__ __b, size_t __n)
261  {
262  for (size_t __j = 0; __j < __n; ++__j, ++__b, ++__i)
263  *__b = __a[*__i];
264  }
265 
266  // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
267  template<typename _Tp>
268  inline void
269  __valarray_copy(const _Tp* __restrict__ __a, size_t __n,
270  _Tp* __restrict__ __b, const size_t* __restrict__ __i)
271  {
272  for (size_t __j = 0; __j < __n; ++__j, ++__a, ++__i)
273  __b[*__i] = *__a;
274  }
275 
276  // Copy the __n first elements of an indexed array __src[<__i>] into
277  // another indexed array __dst[<__j>].
278  template<typename _Tp>
279  inline void
280  __valarray_copy(const _Tp* __restrict__ __src, size_t __n,
281  const size_t* __restrict__ __i,
282  _Tp* __restrict__ __dst, const size_t* __restrict__ __j)
283  {
284  for (size_t __k = 0; __k < __n; ++__k)
285  __dst[*__j++] = __src[*__i++];
286  }
287 
288  //
289  // Compute the sum of elements in range [__f, __l) which must not be empty.
290  // This is a naive algorithm. It suffers from cancelling.
291  // In the future try to specialize for _Tp = float, double, long double
292  // using a more accurate algorithm.
293  //
294  template<typename _Tp>
295  inline _Tp
296  __valarray_sum(const _Tp* __f, const _Tp* __l)
297  {
298  _Tp __r = *__f++;
299  while (__f != __l)
300  __r += *__f++;
301  return __r;
302  }
303 
304  // Compute the min/max of an array-expression
305  template<typename _Ta>
306  inline typename _Ta::value_type
307  __valarray_min(const _Ta& __a)
308  {
309  size_t __s = __a.size();
310  typedef typename _Ta::value_type _Value_type;
311  _Value_type __r = __s == 0 ? _Value_type() : __a[0];
312  for (size_t __i = 1; __i < __s; ++__i)
313  {
314  _Value_type __t = __a[__i];
315  if (__t < __r)
316  __r = __t;
317  }
318  return __r;
319  }
320 
321  template<typename _Ta>
322  inline typename _Ta::value_type
323  __valarray_max(const _Ta& __a)
324  {
325  size_t __s = __a.size();
326  typedef typename _Ta::value_type _Value_type;
327  _Value_type __r = __s == 0 ? _Value_type() : __a[0];
328  for (size_t __i = 1; __i < __s; ++__i)
329  {
330  _Value_type __t = __a[__i];
331  if (__t > __r)
332  __r = __t;
333  }
334  return __r;
335  }
336 
337  //
338  // Helper class _Array, first layer of valarray abstraction.
339  // All operations on valarray should be forwarded to this class
340  // whenever possible. -- gdr
341  //
342 
343  template<typename _Tp>
344  struct _Array
345  {
346  explicit _Array(_Tp* const __restrict__);
347  explicit _Array(const valarray<_Tp>&);
348  _Array(const _Tp* __restrict__, size_t);
349 
350  _Tp* begin() const;
351 
352  _Tp* const __restrict__ _M_data;
353  };
354 
355 
356  // Copy-construct plain array __b[<__n>] from indexed array __a[__i[<__n>]]
357  template<typename _Tp>
358  inline void
359  __valarray_copy_construct(_Array<_Tp> __a, _Array<size_t> __i,
360  _Array<_Tp> __b, size_t __n)
361  { std::__valarray_copy_construct(__a._M_data, __i._M_data,
362  __b._M_data, __n); }
363 
364  // Copy-construct plain array __b[<__n>] from strided array __a[<__n : __s>]
365  template<typename _Tp>
366  inline void
367  __valarray_copy_construct(_Array<_Tp> __a, size_t __n, size_t __s,
368  _Array<_Tp> __b)
369  { std::__valarray_copy_construct(__a._M_data, __n, __s, __b._M_data); }
370 
371  template<typename _Tp>
372  inline void
373  __valarray_fill (_Array<_Tp> __a, size_t __n, const _Tp& __t)
374  { std::__valarray_fill(__a._M_data, __n, __t); }
375 
376  template<typename _Tp>
377  inline void
378  __valarray_fill(_Array<_Tp> __a, size_t __n, size_t __s, const _Tp& __t)
379  { std::__valarray_fill(__a._M_data, __n, __s, __t); }
380 
381  template<typename _Tp>
382  inline void
383  __valarray_fill(_Array<_Tp> __a, _Array<size_t> __i,
384  size_t __n, const _Tp& __t)
385  { std::__valarray_fill(__a._M_data, __i._M_data, __n, __t); }
386 
387  // Copy a plain array __a[<__n>] into a play array __b[<>]
388  template<typename _Tp>
389  inline void
390  __valarray_copy(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b)
391  { std::__valarray_copy(__a._M_data, __n, __b._M_data); }
392 
393  // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
394  template<typename _Tp>
395  inline void
396  __valarray_copy(_Array<_Tp> __a, size_t __n, size_t __s, _Array<_Tp> __b)
397  { std::__valarray_copy(__a._M_data, __n, __s, __b._M_data); }
398 
399  // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
400  template<typename _Tp>
401  inline void
402  __valarray_copy(_Array<_Tp> __a, _Array<_Tp> __b, size_t __n, size_t __s)
403  { __valarray_copy(__a._M_data, __b._M_data, __n, __s); }
404 
405  // Copy strided array __src[<__n : __s1>] into another
406  // strided array __dst[< : __s2>]. Their sizes must match.
407  template<typename _Tp>
408  inline void
409  __valarray_copy(_Array<_Tp> __a, size_t __n, size_t __s1,
410  _Array<_Tp> __b, size_t __s2)
411  { std::__valarray_copy(__a._M_data, __n, __s1, __b._M_data, __s2); }
412 
413  // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
414  template<typename _Tp>
415  inline void
416  __valarray_copy(_Array<_Tp> __a, _Array<size_t> __i,
417  _Array<_Tp> __b, size_t __n)
418  { std::__valarray_copy(__a._M_data, __i._M_data, __b._M_data, __n); }
419 
420  // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
421  template<typename _Tp>
422  inline void
423  __valarray_copy(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b,
424  _Array<size_t> __i)
425  { std::__valarray_copy(__a._M_data, __n, __b._M_data, __i._M_data); }
426 
427  // Copy the __n first elements of an indexed array __src[<__i>] into
428  // another indexed array __dst[<__j>].
429  template<typename _Tp>
430  inline void
431  __valarray_copy(_Array<_Tp> __src, size_t __n, _Array<size_t> __i,
432  _Array<_Tp> __dst, _Array<size_t> __j)
433  {
434  std::__valarray_copy(__src._M_data, __n, __i._M_data,
435  __dst._M_data, __j._M_data);
436  }
437 
438  template<typename _Tp>
439  inline
440  _Array<_Tp>::_Array(_Tp* const __restrict__ __p)
441  : _M_data (__p) {}
442 
443  template<typename _Tp>
444  inline
445  _Array<_Tp>::_Array(const valarray<_Tp>& __v)
446  : _M_data (__v._M_data) {}
447 
448  template<typename _Tp>
449  inline
450  _Array<_Tp>::_Array(const _Tp* __restrict__ __b, size_t __s)
451  : _M_data(__valarray_get_storage<_Tp>(__s))
452  { std::__valarray_copy_construct(__b, __s, _M_data); }
453 
454  template<typename _Tp>
455  inline _Tp*
456  _Array<_Tp>::begin () const
457  { return _M_data; }
458 
459 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
460  template<typename _Tp> \
461  inline void \
462  _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, const _Tp& __t) \
463  { \
464  for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; ++__p) \
465  *__p _Op##= __t; \
466  } \
467  \
468  template<typename _Tp> \
469  inline void \
470  _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
471  { \
472  _Tp* __p = __a._M_data; \
473  for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; ++__p, ++__q) \
474  *__p _Op##= *__q; \
475  } \
476  \
477  template<typename _Tp, class _Dom> \
478  void \
479  _Array_augmented_##_Name(_Array<_Tp> __a, \
480  const _Expr<_Dom, _Tp>& __e, size_t __n) \
481  { \
482  _Tp* __p(__a._M_data); \
483  for (size_t __i = 0; __i < __n; ++__i, ++__p) \
484  *__p _Op##= __e[__i]; \
485  } \
486  \
487  template<typename _Tp> \
488  inline void \
489  _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, size_t __s, \
490  _Array<_Tp> __b) \
491  { \
492  _Tp* __q(__b._M_data); \
493  for (_Tp* __p = __a._M_data; __p < __a._M_data + __s * __n; \
494  __p += __s, ++__q) \
495  *__p _Op##= *__q; \
496  } \
497  \
498  template<typename _Tp> \
499  inline void \
500  _Array_augmented_##_Name(_Array<_Tp> __a, _Array<_Tp> __b, \
501  size_t __n, size_t __s) \
502  { \
503  _Tp* __q(__b._M_data); \
504  for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
505  ++__p, __q += __s) \
506  *__p _Op##= *__q; \
507  } \
508  \
509  template<typename _Tp, class _Dom> \
510  void \
511  _Array_augmented_##_Name(_Array<_Tp> __a, size_t __s, \
512  const _Expr<_Dom, _Tp>& __e, size_t __n) \
513  { \
514  _Tp* __p(__a._M_data); \
515  for (size_t __i = 0; __i < __n; ++__i, __p += __s) \
516  *__p _Op##= __e[__i]; \
517  } \
518  \
519  template<typename _Tp> \
520  inline void \
521  _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
522  _Array<_Tp> __b, size_t __n) \
523  { \
524  _Tp* __q(__b._M_data); \
525  for (size_t* __j = __i._M_data; __j < __i._M_data + __n; \
526  ++__j, ++__q) \
527  __a._M_data[*__j] _Op##= *__q; \
528  } \
529  \
530  template<typename _Tp> \
531  inline void \
532  _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
533  _Array<_Tp> __b, _Array<size_t> __i) \
534  { \
535  _Tp* __p(__a._M_data); \
536  for (size_t* __j = __i._M_data; __j<__i._M_data + __n; \
537  ++__j, ++__p) \
538  *__p _Op##= __b._M_data[*__j]; \
539  } \
540  \
541  template<typename _Tp, class _Dom> \
542  void \
543  _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
544  const _Expr<_Dom, _Tp>& __e, size_t __n) \
545  { \
546  size_t* __j(__i._M_data); \
547  for (size_t __k = 0; __k<__n; ++__k, ++__j) \
548  __a._M_data[*__j] _Op##= __e[__k]; \
549  } \
550  \
551  template<typename _Tp> \
552  void \
553  _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
554  _Array<_Tp> __b, size_t __n) \
555  { \
556  bool* __ok(__m._M_data); \
557  _Tp* __p(__a._M_data); \
558  for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; \
559  ++__q, ++__ok, ++__p) \
560  { \
561  while (! *__ok) \
562  { \
563  ++__ok; \
564  ++__p; \
565  } \
566  *__p _Op##= *__q; \
567  } \
568  } \
569  \
570  template<typename _Tp> \
571  void \
572  _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
573  _Array<_Tp> __b, _Array<bool> __m) \
574  { \
575  bool* __ok(__m._M_data); \
576  _Tp* __q(__b._M_data); \
577  for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
578  ++__p, ++__ok, ++__q) \
579  { \
580  while (! *__ok) \
581  { \
582  ++__ok; \
583  ++__q; \
584  } \
585  *__p _Op##= *__q; \
586  } \
587  } \
588  \
589  template<typename _Tp, class _Dom> \
590  void \
591  _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
592  const _Expr<_Dom, _Tp>& __e, size_t __n) \
593  { \
594  bool* __ok(__m._M_data); \
595  _Tp* __p(__a._M_data); \
596  for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p) \
597  { \
598  while (! *__ok) \
599  { \
600  ++__ok; \
601  ++__p; \
602  } \
603  *__p _Op##= __e[__i]; \
604  } \
605  }
606 
607  _DEFINE_ARRAY_FUNCTION(+, __plus)
608  _DEFINE_ARRAY_FUNCTION(-, __minus)
609  _DEFINE_ARRAY_FUNCTION(*, __multiplies)
610  _DEFINE_ARRAY_FUNCTION(/, __divides)
611  _DEFINE_ARRAY_FUNCTION(%, __modulus)
612  _DEFINE_ARRAY_FUNCTION(^, __bitwise_xor)
613  _DEFINE_ARRAY_FUNCTION(|, __bitwise_or)
614  _DEFINE_ARRAY_FUNCTION(&, __bitwise_and)
615  _DEFINE_ARRAY_FUNCTION(<<, __shift_left)
616  _DEFINE_ARRAY_FUNCTION(>>, __shift_right)
617 
618 #undef _DEFINE_ARRAY_FUNCTION
619 
620 _GLIBCXX_END_NAMESPACE_VERSION
621 } // namespace
622 
623 # include <bits/valarray_array.tcc>
624 
625 #endif /* _ARRAY_H */
cpp_type_traits.h
cstdlib
std
ISO C++ entities toplevel namespace is std.
std::begin
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1229
c++config.h
new_allocator.h
std::__new_allocator
An allocator that uses global new, as per C++03 [20.4.1].
Definition: bits/new_allocator.h:63
valarray_array.tcc
new