1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
3 // Copyright (C) 1997-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
32 #ifndef _VALARRAY_ARRAY_H
33 #define _VALARRAY_ARRAY_H 1
35 #pragma GCC system_header
37 #include <bits/c++config.h>
38 #include <bits/cpp_type_traits.h>
42 namespace std
_GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 // Helper functions on raw pointers
50 // We get memory the old fashioned way
51 template<typename _Tp
>
53 __valarray_get_storage(size_t) __attribute__((__malloc__
));
55 template<typename _Tp
>
57 __valarray_get_storage(size_t __n
)
58 { return static_cast<_Tp
*>(operator new(__n
* sizeof(_Tp
))); }
60 // Return memory to the system
62 __valarray_release_memory(void* __p
)
63 { operator delete(__p
); }
65 // Turn a raw-memory into an array of _Tp filled with _Tp()
66 // This is required in 'valarray<T> v(n);'
67 template<typename _Tp
, bool>
68 struct _Array_default_ctor
70 // Please note that this isn't exception safe. But
71 // valarrays aren't required to be exception safe.
73 _S_do_it(_Tp
* __b
, _Tp
* __e
)
80 template<typename _Tp
>
81 struct _Array_default_ctor
<_Tp
, true>
83 // For fundamental types, it suffices to say 'memset()'
85 _S_do_it(_Tp
* __b
, _Tp
* __e
)
86 { __builtin_memset(__b
, 0, (__e
- __b
) * sizeof(_Tp
)); }
89 template<typename _Tp
>
91 __valarray_default_construct(_Tp
* __b
, _Tp
* __e
)
93 _Array_default_ctor
<_Tp
, __is_scalar
<_Tp
>::__value
>::_S_do_it(__b
, __e
);
96 // Turn a raw-memory into an array of _Tp filled with __t
97 // This is the required in valarray<T> v(n, t). Also
98 // used in valarray<>::resize().
99 template<typename _Tp
, bool>
100 struct _Array_init_ctor
102 // Please note that this isn't exception safe. But
103 // valarrays aren't required to be exception safe.
105 _S_do_it(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
112 template<typename _Tp
>
113 struct _Array_init_ctor
<_Tp
, true>
116 _S_do_it(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
123 template<typename _Tp
>
125 __valarray_fill_construct(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
127 _Array_init_ctor
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__b
, __e
, __t
);
131 // copy-construct raw array [__o, *) from plain array [__b, __e)
132 // We can't just say 'memcpy()'
134 template<typename _Tp
, bool>
135 struct _Array_copy_ctor
137 // Please note that this isn't exception safe. But
138 // valarrays aren't required to be exception safe.
140 _S_do_it(const _Tp
* __b
, const _Tp
* __e
, _Tp
* __restrict__ __o
)
143 new(__o
++) _Tp(*__b
++);
147 template<typename _Tp
>
148 struct _Array_copy_ctor
<_Tp
, true>
151 _S_do_it(const _Tp
* __b
, const _Tp
* __e
, _Tp
* __restrict__ __o
)
154 __builtin_memcpy(__o
, __b
, (__e
- __b
) * sizeof(_Tp
));
158 template<typename _Tp
>
160 __valarray_copy_construct(const _Tp
* __b
, const _Tp
* __e
,
161 _Tp
* __restrict__ __o
)
163 _Array_copy_ctor
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__b
, __e
, __o
);
166 // copy-construct raw array [__o, *) from strided array __a[<__n : __s>]
167 template<typename _Tp
>
169 __valarray_copy_construct (const _Tp
* __restrict__ __a
, size_t __n
,
170 size_t __s
, _Tp
* __restrict__ __o
)
172 if (__is_trivial(_Tp
))
181 new(__o
++) _Tp(*__a
);
186 // copy-construct raw array [__o, *) from indexed array __a[__i[<__n>]]
187 template<typename _Tp
>
189 __valarray_copy_construct (const _Tp
* __restrict__ __a
,
190 const size_t* __restrict__ __i
,
191 _Tp
* __restrict__ __o
, size_t __n
)
193 if (__is_trivial(_Tp
))
195 *__o
++ = __a
[*__i
++];
198 new (__o
++) _Tp(__a
[*__i
++]);
201 // Do the necessary cleanup when we're done with arrays.
202 template<typename _Tp
>
204 __valarray_destroy_elements(_Tp
* __b
, _Tp
* __e
)
206 if (!__is_trivial(_Tp
))
214 // Fill a plain array __a[<__n>] with __t
215 template<typename _Tp
>
217 __valarray_fill(_Tp
* __restrict__ __a
, size_t __n
, const _Tp
& __t
)
223 // fill strided array __a[<__n-1 : __s>] with __t
224 template<typename _Tp
>
226 __valarray_fill(_Tp
* __restrict__ __a
, size_t __n
,
227 size_t __s
, const _Tp
& __t
)
229 for (size_t __i
= 0; __i
< __n
; ++__i
, __a
+= __s
)
233 // fill indirect array __a[__i[<__n>]] with __i
234 template<typename _Tp
>
236 __valarray_fill(_Tp
* __restrict__ __a
, const size_t* __restrict__ __i
,
237 size_t __n
, const _Tp
& __t
)
239 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__i
)
243 // copy plain array __a[<__n>] in __b[<__n>]
244 // For non-fundamental types, it is wrong to say 'memcpy()'
245 template<typename _Tp
, bool>
249 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
256 template<typename _Tp
>
257 struct _Array_copier
<_Tp
, true>
260 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
263 __builtin_memcpy(__b
, __a
, __n
* sizeof (_Tp
));
267 // Copy a plain array __a[<__n>] into a play array __b[<>]
268 template<typename _Tp
>
270 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
271 _Tp
* __restrict__ __b
)
273 _Array_copier
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__a
, __n
, __b
);
276 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
277 template<typename _Tp
>
279 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
, size_t __s
,
280 _Tp
* __restrict__ __b
)
282 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__b
, __a
+= __s
)
286 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
287 template<typename _Tp
>
289 __valarray_copy(const _Tp
* __restrict__ __a
, _Tp
* __restrict__ __b
,
290 size_t __n
, size_t __s
)
292 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__a
, __b
+= __s
)
296 // Copy strided array __src[<__n : __s1>] into another
297 // strided array __dst[< : __s2>]. Their sizes must match.
298 template<typename _Tp
>
300 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
, size_t __s1
,
301 _Tp
* __restrict__ __dst
, size_t __s2
)
303 for (size_t __i
= 0; __i
< __n
; ++__i
)
304 __dst
[__i
* __s2
] = __src
[__i
* __s1
];
307 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
308 template<typename _Tp
>
310 __valarray_copy(const _Tp
* __restrict__ __a
,
311 const size_t* __restrict__ __i
,
312 _Tp
* __restrict__ __b
, size_t __n
)
314 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__b
, ++__i
)
318 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
319 template<typename _Tp
>
321 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
322 _Tp
* __restrict__ __b
, const size_t* __restrict__ __i
)
324 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__a
, ++__i
)
328 // Copy the __n first elements of an indexed array __src[<__i>] into
329 // another indexed array __dst[<__j>].
330 template<typename _Tp
>
332 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
,
333 const size_t* __restrict__ __i
,
334 _Tp
* __restrict__ __dst
, const size_t* __restrict__ __j
)
336 for (size_t __k
= 0; __k
< __n
; ++__k
)
337 __dst
[*__j
++] = __src
[*__i
++];
341 // Compute the sum of elements in range [__f, __l)
342 // This is a naive algorithm. It suffers from cancelling.
343 // In the future try to specialize
344 // for _Tp = float, double, long double using a more accurate
347 template<typename _Tp
>
349 __valarray_sum(const _Tp
* __f
, const _Tp
* __l
)
357 // Compute the product of all elements in range [__f, __l)
358 template<typename _Tp
>
360 __valarray_product(const _Tp
* __f
, const _Tp
* __l
)
368 // Compute the min/max of an array-expression
369 template<typename _Ta
>
370 inline typename
_Ta::value_type
371 __valarray_min(const _Ta
& __a
)
373 size_t __s
= __a
.size();
374 typedef typename
_Ta::value_type _Value_type
;
375 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
376 for (size_t __i
= 1; __i
< __s
; ++__i
)
378 _Value_type __t
= __a
[__i
];
385 template<typename _Ta
>
386 inline typename
_Ta::value_type
387 __valarray_max(const _Ta
& __a
)
389 size_t __s
= __a
.size();
390 typedef typename
_Ta::value_type _Value_type
;
391 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
392 for (size_t __i
= 1; __i
< __s
; ++__i
)
394 _Value_type __t
= __a
[__i
];
402 // Helper class _Array, first layer of valarray abstraction.
403 // All operations on valarray should be forwarded to this class
404 // whenever possible. -- gdr
407 template<typename _Tp
>
410 explicit _Array(_Tp
* const __restrict__
);
411 explicit _Array(const valarray
<_Tp
>&);
412 _Array(const _Tp
* __restrict__
, size_t);
416 _Tp
* const __restrict__ _M_data
;
420 // Copy-construct plain array __b[<__n>] from indexed array __a[__i[<__n>]]
421 template<typename _Tp
>
423 __valarray_copy_construct(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
424 _Array
<_Tp
> __b
, size_t __n
)
425 { std::__valarray_copy_construct(__a
._M_data
, __i
._M_data
,
428 // Copy-construct plain array __b[<__n>] from strided array __a[<__n : __s>]
429 template<typename _Tp
>
431 __valarray_copy_construct(_Array
<_Tp
> __a
, size_t __n
, size_t __s
,
433 { std::__valarray_copy_construct(__a
._M_data
, __n
, __s
, __b
._M_data
); }
435 template<typename _Tp
>
437 __valarray_fill (_Array
<_Tp
> __a
, size_t __n
, const _Tp
& __t
)
438 { std::__valarray_fill(__a
._M_data
, __n
, __t
); }
440 template<typename _Tp
>
442 __valarray_fill(_Array
<_Tp
> __a
, size_t __n
, size_t __s
, const _Tp
& __t
)
443 { std::__valarray_fill(__a
._M_data
, __n
, __s
, __t
); }
445 template<typename _Tp
>
447 __valarray_fill(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
448 size_t __n
, const _Tp
& __t
)
449 { std::__valarray_fill(__a
._M_data
, __i
._M_data
, __n
, __t
); }
451 // Copy a plain array __a[<__n>] into a play array __b[<>]
452 template<typename _Tp
>
454 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
)
455 { std::__valarray_copy(__a
._M_data
, __n
, __b
._M_data
); }
457 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
458 template<typename _Tp
>
460 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, size_t __s
, _Array
<_Tp
> __b
)
461 { std::__valarray_copy(__a
._M_data
, __n
, __s
, __b
._M_data
); }
463 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
464 template<typename _Tp
>
466 __valarray_copy(_Array
<_Tp
> __a
, _Array
<_Tp
> __b
, size_t __n
, size_t __s
)
467 { __valarray_copy(__a
._M_data
, __b
._M_data
, __n
, __s
); }
469 // Copy strided array __src[<__n : __s1>] into another
470 // strided array __dst[< : __s2>]. Their sizes must match.
471 template<typename _Tp
>
473 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, size_t __s1
,
474 _Array
<_Tp
> __b
, size_t __s2
)
475 { std::__valarray_copy(__a
._M_data
, __n
, __s1
, __b
._M_data
, __s2
); }
477 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
478 template<typename _Tp
>
480 __valarray_copy(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
481 _Array
<_Tp
> __b
, size_t __n
)
482 { std::__valarray_copy(__a
._M_data
, __i
._M_data
, __b
._M_data
, __n
); }
484 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
485 template<typename _Tp
>
487 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
,
489 { std::__valarray_copy(__a
._M_data
, __n
, __b
._M_data
, __i
._M_data
); }
491 // Copy the __n first elements of an indexed array __src[<__i>] into
492 // another indexed array __dst[<__j>].
493 template<typename _Tp
>
495 __valarray_copy(_Array
<_Tp
> __src
, size_t __n
, _Array
<size_t> __i
,
496 _Array
<_Tp
> __dst
, _Array
<size_t> __j
)
498 std::__valarray_copy(__src
._M_data
, __n
, __i
._M_data
,
499 __dst
._M_data
, __j
._M_data
);
502 template<typename _Tp
>
504 _Array
<_Tp
>::_Array(_Tp
* const __restrict__ __p
)
507 template<typename _Tp
>
509 _Array
<_Tp
>::_Array(const valarray
<_Tp
>& __v
)
510 : _M_data (__v
._M_data
) {}
512 template<typename _Tp
>
514 _Array
<_Tp
>::_Array(const _Tp
* __restrict__ __b
, size_t __s
)
515 : _M_data(__valarray_get_storage
<_Tp
>(__s
))
516 { std::__valarray_copy_construct(__b
, __s
, _M_data
); }
518 template<typename _Tp
>
520 _Array
<_Tp
>::begin () const
523 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
524 template<typename _Tp> \
526 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, const _Tp& __t) \
528 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; ++__p) \
532 template<typename _Tp> \
534 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
536 _Tp* __p = __a._M_data; \
537 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; ++__p, ++__q) \
541 template<typename _Tp, class _Dom> \
543 _Array_augmented_##_Name(_Array<_Tp> __a, \
544 const _Expr<_Dom, _Tp>& __e, size_t __n) \
546 _Tp* __p(__a._M_data); \
547 for (size_t __i = 0; __i < __n; ++__i, ++__p) \
548 *__p _Op##= __e[__i]; \
551 template<typename _Tp> \
553 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, size_t __s, \
556 _Tp* __q(__b._M_data); \
557 for (_Tp* __p = __a._M_data; __p < __a._M_data + __s * __n; \
562 template<typename _Tp> \
564 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<_Tp> __b, \
565 size_t __n, size_t __s) \
567 _Tp* __q(__b._M_data); \
568 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
573 template<typename _Tp, class _Dom> \
575 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __s, \
576 const _Expr<_Dom, _Tp>& __e, size_t __n) \
578 _Tp* __p(__a._M_data); \
579 for (size_t __i = 0; __i < __n; ++__i, __p += __s) \
580 *__p _Op##= __e[__i]; \
583 template<typename _Tp> \
585 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
586 _Array<_Tp> __b, size_t __n) \
588 _Tp* __q(__b._M_data); \
589 for (size_t* __j = __i._M_data; __j < __i._M_data + __n; \
591 __a._M_data[*__j] _Op##= *__q; \
594 template<typename _Tp> \
596 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
597 _Array<_Tp> __b, _Array<size_t> __i) \
599 _Tp* __p(__a._M_data); \
600 for (size_t* __j = __i._M_data; __j<__i._M_data + __n; \
602 *__p _Op##= __b._M_data[*__j]; \
605 template<typename _Tp, class _Dom> \
607 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
608 const _Expr<_Dom, _Tp>& __e, size_t __n) \
610 size_t* __j(__i._M_data); \
611 for (size_t __k = 0; __k<__n; ++__k, ++__j) \
612 __a._M_data[*__j] _Op##= __e[__k]; \
615 template<typename _Tp> \
617 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
618 _Array<_Tp> __b, size_t __n) \
620 bool* __ok(__m._M_data); \
621 _Tp* __p(__a._M_data); \
622 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; \
623 ++__q, ++__ok, ++__p) \
634 template<typename _Tp> \
636 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
637 _Array<_Tp> __b, _Array<bool> __m) \
639 bool* __ok(__m._M_data); \
640 _Tp* __q(__b._M_data); \
641 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
642 ++__p, ++__ok, ++__q) \
653 template<typename _Tp, class _Dom> \
655 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
656 const _Expr<_Dom, _Tp>& __e, size_t __n) \
658 bool* __ok(__m._M_data); \
659 _Tp* __p(__a._M_data); \
660 for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p) \
667 *__p _Op##= __e[__i]; \
671 _DEFINE_ARRAY_FUNCTION(+, __plus
)
672 _DEFINE_ARRAY_FUNCTION(-, __minus
)
673 _DEFINE_ARRAY_FUNCTION(*, __multiplies
)
674 _DEFINE_ARRAY_FUNCTION(/, __divides
)
675 _DEFINE_ARRAY_FUNCTION(%, __modulus
)
676 _DEFINE_ARRAY_FUNCTION(^, __bitwise_xor
)
677 _DEFINE_ARRAY_FUNCTION(|, __bitwise_or
)
678 _DEFINE_ARRAY_FUNCTION(&, __bitwise_and
)
679 _DEFINE_ARRAY_FUNCTION(<<, __shift_left
)
680 _DEFINE_ARRAY_FUNCTION(>>, __shift_right
)
682 #undef _DEFINE_ARRAY_FUNCTION
684 _GLIBCXX_END_NAMESPACE_VERSION
687 # include <bits/valarray_array.tcc>
689 #endif /* _ARRAY_H */