1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file bits/valarray_array.h
28 * This is an internal header file, included by other library headers.
29 * Do not attempt to use it directly. @headername{valarray}
32 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
34 #ifndef _VALARRAY_ARRAY_H
35 #define _VALARRAY_ARRAY_H 1
37 #pragma GCC system_header
39 #include <bits/c++config.h>
40 #include <bits/cpp_type_traits.h>
44 _GLIBCXX_BEGIN_NAMESPACE(std
)
47 // Helper functions on raw pointers
50 // We get memory by the old fashion way
52 __valarray_get_memory(size_t __n
)
53 { return operator new(__n
); }
55 template<typename _Tp
>
56 inline _Tp
*__restrict__
57 __valarray_get_storage(size_t __n
)
59 return static_cast<_Tp
*__restrict__
>
60 (std::__valarray_get_memory(__n
* sizeof(_Tp
)));
63 // Return memory to the system
65 __valarray_release_memory(void* __p
)
66 { operator delete(__p
); }
68 // Turn a raw-memory into an array of _Tp filled with _Tp()
69 // This is required in 'valarray<T> v(n);'
70 template<typename _Tp
, bool>
71 struct _Array_default_ctor
73 // Please note that this isn't exception safe. But
74 // valarrays aren't required to be exception safe.
76 _S_do_it(_Tp
* __b
, _Tp
* __e
)
83 template<typename _Tp
>
84 struct _Array_default_ctor
<_Tp
, true>
86 // For fundamental types, it suffices to say 'memset()'
88 _S_do_it(_Tp
* __b
, _Tp
* __e
)
89 { __builtin_memset(__b
, 0, (__e
- __b
) * sizeof(_Tp
)); }
92 template<typename _Tp
>
94 __valarray_default_construct(_Tp
* __b
, _Tp
* __e
)
96 _Array_default_ctor
<_Tp
, __is_scalar
<_Tp
>::__value
>::_S_do_it(__b
, __e
);
99 // Turn a raw-memory into an array of _Tp filled with __t
100 // This is the required in valarray<T> v(n, t). Also
101 // used in valarray<>::resize().
102 template<typename _Tp
, bool>
103 struct _Array_init_ctor
105 // Please note that this isn't exception safe. But
106 // valarrays aren't required to be exception safe.
108 _S_do_it(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
115 template<typename _Tp
>
116 struct _Array_init_ctor
<_Tp
, true>
119 _S_do_it(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
126 template<typename _Tp
>
128 __valarray_fill_construct(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
130 _Array_init_ctor
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__b
, __e
, __t
);
134 // copy-construct raw array [__o, *) from plain array [__b, __e)
135 // We can't just say 'memcpy()'
137 template<typename _Tp
, bool>
138 struct _Array_copy_ctor
140 // Please note that this isn't exception safe. But
141 // valarrays aren't required to be exception safe.
143 _S_do_it(const _Tp
* __b
, const _Tp
* __e
, _Tp
* __restrict__ __o
)
146 new(__o
++) _Tp(*__b
++);
150 template<typename _Tp
>
151 struct _Array_copy_ctor
<_Tp
, true>
154 _S_do_it(const _Tp
* __b
, const _Tp
* __e
, _Tp
* __restrict__ __o
)
155 { __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
)
261 { __builtin_memcpy(__b
, __a
, __n
* sizeof (_Tp
)); }
264 // Copy a plain array __a[<__n>] into a play array __b[<>]
265 template<typename _Tp
>
267 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
268 _Tp
* __restrict__ __b
)
270 _Array_copier
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__a
, __n
, __b
);
273 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
274 template<typename _Tp
>
276 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
, size_t __s
,
277 _Tp
* __restrict__ __b
)
279 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__b
, __a
+= __s
)
283 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
284 template<typename _Tp
>
286 __valarray_copy(const _Tp
* __restrict__ __a
, _Tp
* __restrict__ __b
,
287 size_t __n
, size_t __s
)
289 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__a
, __b
+= __s
)
293 // Copy strided array __src[<__n : __s1>] into another
294 // strided array __dst[< : __s2>]. Their sizes must match.
295 template<typename _Tp
>
297 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
, size_t __s1
,
298 _Tp
* __restrict__ __dst
, size_t __s2
)
300 for (size_t __i
= 0; __i
< __n
; ++__i
)
301 __dst
[__i
* __s2
] = __src
[__i
* __s1
];
304 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
305 template<typename _Tp
>
307 __valarray_copy(const _Tp
* __restrict__ __a
,
308 const size_t* __restrict__ __i
,
309 _Tp
* __restrict__ __b
, size_t __n
)
311 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__b
, ++__i
)
315 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
316 template<typename _Tp
>
318 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
319 _Tp
* __restrict__ __b
, const size_t* __restrict__ __i
)
321 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__a
, ++__i
)
325 // Copy the __n first elements of an indexed array __src[<__i>] into
326 // another indexed array __dst[<__j>].
327 template<typename _Tp
>
329 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
,
330 const size_t* __restrict__ __i
,
331 _Tp
* __restrict__ __dst
, const size_t* __restrict__ __j
)
333 for (size_t __k
= 0; __k
< __n
; ++__k
)
334 __dst
[*__j
++] = __src
[*__i
++];
338 // Compute the sum of elements in range [__f, __l)
339 // This is a naive algorithm. It suffers from cancelling.
340 // In the future try to specialize
341 // for _Tp = float, double, long double using a more accurate
344 template<typename _Tp
>
346 __valarray_sum(const _Tp
* __f
, const _Tp
* __l
)
354 // Compute the product of all elements in range [__f, __l)
355 template<typename _Tp
>
357 __valarray_product(const _Tp
* __f
, const _Tp
* __l
)
365 // Compute the min/max of an array-expression
366 template<typename _Ta
>
367 inline typename
_Ta::value_type
368 __valarray_min(const _Ta
& __a
)
370 size_t __s
= __a
.size();
371 typedef typename
_Ta::value_type _Value_type
;
372 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
373 for (size_t __i
= 1; __i
< __s
; ++__i
)
375 _Value_type __t
= __a
[__i
];
382 template<typename _Ta
>
383 inline typename
_Ta::value_type
384 __valarray_max(const _Ta
& __a
)
386 size_t __s
= __a
.size();
387 typedef typename
_Ta::value_type _Value_type
;
388 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
389 for (size_t __i
= 1; __i
< __s
; ++__i
)
391 _Value_type __t
= __a
[__i
];
399 // Helper class _Array, first layer of valarray abstraction.
400 // All operations on valarray should be forwarded to this class
401 // whenever possible. -- gdr
404 template<typename _Tp
>
407 explicit _Array(size_t);
408 explicit _Array(_Tp
* const __restrict__
);
409 explicit _Array(const valarray
<_Tp
>&);
410 _Array(const _Tp
* __restrict__
, size_t);
414 _Tp
* const __restrict__ _M_data
;
418 // Copy-construct plain array __b[<__n>] from indexed array __a[__i[<__n>]]
419 template<typename _Tp
>
421 __valarray_copy_construct(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
422 _Array
<_Tp
> __b
, size_t __n
)
423 { std::__valarray_copy_construct(__a
._M_data
, __i
._M_data
,
426 // Copy-construct plain array __b[<__n>] from strided array __a[<__n : __s>]
427 template<typename _Tp
>
429 __valarray_copy_construct(_Array
<_Tp
> __a
, size_t __n
, size_t __s
,
431 { std::__valarray_copy_construct(__a
._M_data
, __n
, __s
, __b
._M_data
); }
433 template<typename _Tp
>
435 __valarray_fill (_Array
<_Tp
> __a
, size_t __n
, const _Tp
& __t
)
436 { std::__valarray_fill(__a
._M_data
, __n
, __t
); }
438 template<typename _Tp
>
440 __valarray_fill(_Array
<_Tp
> __a
, size_t __n
, size_t __s
, const _Tp
& __t
)
441 { std::__valarray_fill(__a
._M_data
, __n
, __s
, __t
); }
443 template<typename _Tp
>
445 __valarray_fill(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
446 size_t __n
, const _Tp
& __t
)
447 { std::__valarray_fill(__a
._M_data
, __i
._M_data
, __n
, __t
); }
449 // Copy a plain array __a[<__n>] into a play array __b[<>]
450 template<typename _Tp
>
452 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
)
453 { std::__valarray_copy(__a
._M_data
, __n
, __b
._M_data
); }
455 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
456 template<typename _Tp
>
458 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, size_t __s
, _Array
<_Tp
> __b
)
459 { std::__valarray_copy(__a
._M_data
, __n
, __s
, __b
._M_data
); }
461 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
462 template<typename _Tp
>
464 __valarray_copy(_Array
<_Tp
> __a
, _Array
<_Tp
> __b
, size_t __n
, size_t __s
)
465 { __valarray_copy(__a
._M_data
, __b
._M_data
, __n
, __s
); }
467 // Copy strided array __src[<__n : __s1>] into another
468 // strided array __dst[< : __s2>]. Their sizes must match.
469 template<typename _Tp
>
471 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, size_t __s1
,
472 _Array
<_Tp
> __b
, size_t __s2
)
473 { std::__valarray_copy(__a
._M_data
, __n
, __s1
, __b
._M_data
, __s2
); }
475 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
476 template<typename _Tp
>
478 __valarray_copy(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
479 _Array
<_Tp
> __b
, size_t __n
)
480 { std::__valarray_copy(__a
._M_data
, __i
._M_data
, __b
._M_data
, __n
); }
482 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
483 template<typename _Tp
>
485 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
,
487 { std::__valarray_copy(__a
._M_data
, __n
, __b
._M_data
, __i
._M_data
); }
489 // Copy the __n first elements of an indexed array __src[<__i>] into
490 // another indexed array __dst[<__j>].
491 template<typename _Tp
>
493 __valarray_copy(_Array
<_Tp
> __src
, size_t __n
, _Array
<size_t> __i
,
494 _Array
<_Tp
> __dst
, _Array
<size_t> __j
)
496 std::__valarray_copy(__src
._M_data
, __n
, __i
._M_data
,
497 __dst
._M_data
, __j
._M_data
);
500 template<typename _Tp
>
502 _Array
<_Tp
>::_Array(size_t __n
)
503 : _M_data(__valarray_get_storage
<_Tp
>(__n
))
504 { std::__valarray_default_construct(_M_data
, _M_data
+ __n
); }
506 template<typename _Tp
>
508 _Array
<_Tp
>::_Array(_Tp
* const __restrict__ __p
)
511 template<typename _Tp
>
513 _Array
<_Tp
>::_Array(const valarray
<_Tp
>& __v
)
514 : _M_data (__v
._M_data
) {}
516 template<typename _Tp
>
518 _Array
<_Tp
>::_Array(const _Tp
* __restrict__ __b
, size_t __s
)
519 : _M_data(__valarray_get_storage
<_Tp
>(__s
))
520 { std::__valarray_copy_construct(__b
, __s
, _M_data
); }
522 template<typename _Tp
>
524 _Array
<_Tp
>::begin () const
527 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
528 template<typename _Tp> \
530 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, const _Tp& __t) \
532 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; ++__p) \
536 template<typename _Tp> \
538 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
540 _Tp* __p = __a._M_data; \
541 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; ++__p, ++__q) \
545 template<typename _Tp, class _Dom> \
547 _Array_augmented_##_Name(_Array<_Tp> __a, \
548 const _Expr<_Dom, _Tp>& __e, size_t __n) \
550 _Tp* __p(__a._M_data); \
551 for (size_t __i = 0; __i < __n; ++__i, ++__p) \
552 *__p _Op##= __e[__i]; \
555 template<typename _Tp> \
557 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, size_t __s, \
560 _Tp* __q(__b._M_data); \
561 for (_Tp* __p = __a._M_data; __p < __a._M_data + __s * __n; \
566 template<typename _Tp> \
568 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<_Tp> __b, \
569 size_t __n, size_t __s) \
571 _Tp* __q(__b._M_data); \
572 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
577 template<typename _Tp, class _Dom> \
579 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __s, \
580 const _Expr<_Dom, _Tp>& __e, size_t __n) \
582 _Tp* __p(__a._M_data); \
583 for (size_t __i = 0; __i < __n; ++__i, __p += __s) \
584 *__p _Op##= __e[__i]; \
587 template<typename _Tp> \
589 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
590 _Array<_Tp> __b, size_t __n) \
592 _Tp* __q(__b._M_data); \
593 for (size_t* __j = __i._M_data; __j < __i._M_data + __n; \
595 __a._M_data[*__j] _Op##= *__q; \
598 template<typename _Tp> \
600 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
601 _Array<_Tp> __b, _Array<size_t> __i) \
603 _Tp* __p(__a._M_data); \
604 for (size_t* __j = __i._M_data; __j<__i._M_data + __n; \
606 *__p _Op##= __b._M_data[*__j]; \
609 template<typename _Tp, class _Dom> \
611 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
612 const _Expr<_Dom, _Tp>& __e, size_t __n) \
614 size_t* __j(__i._M_data); \
615 for (size_t __k = 0; __k<__n; ++__k, ++__j) \
616 __a._M_data[*__j] _Op##= __e[__k]; \
619 template<typename _Tp> \
621 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
622 _Array<_Tp> __b, size_t __n) \
624 bool* __ok(__m._M_data); \
625 _Tp* __p(__a._M_data); \
626 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; \
627 ++__q, ++__ok, ++__p) \
638 template<typename _Tp> \
640 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
641 _Array<_Tp> __b, _Array<bool> __m) \
643 bool* __ok(__m._M_data); \
644 _Tp* __q(__b._M_data); \
645 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
646 ++__p, ++__ok, ++__q) \
657 template<typename _Tp, class _Dom> \
659 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
660 const _Expr<_Dom, _Tp>& __e, size_t __n) \
662 bool* __ok(__m._M_data); \
663 _Tp* __p(__a._M_data); \
664 for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p) \
671 *__p _Op##= __e[__i]; \
675 _DEFINE_ARRAY_FUNCTION(+, __plus
)
676 _DEFINE_ARRAY_FUNCTION(-, __minus
)
677 _DEFINE_ARRAY_FUNCTION(*, __multiplies
)
678 _DEFINE_ARRAY_FUNCTION(/, __divides
)
679 _DEFINE_ARRAY_FUNCTION(%, __modulus
)
680 _DEFINE_ARRAY_FUNCTION(^, __bitwise_xor
)
681 _DEFINE_ARRAY_FUNCTION(|, __bitwise_or
)
682 _DEFINE_ARRAY_FUNCTION(&, __bitwise_and
)
683 _DEFINE_ARRAY_FUNCTION(<<, __shift_left
)
684 _DEFINE_ARRAY_FUNCTION(>>, __shift_right
)
686 #undef _DEFINE_ARRAY_FUNCTION
688 _GLIBCXX_END_NAMESPACE
690 #ifndef _GLIBCXX_EXPORT_TEMPLATE
691 # include <bits/valarray_array.tcc>
694 #endif /* _ARRAY_H */