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