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, 2011
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 namespace std
_GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 // Helper functions on raw pointers
52 // We get memory by the old fashion way
54 __valarray_get_memory(size_t __n
)
55 { return operator new(__n
); }
57 template<typename _Tp
>
58 inline _Tp
*__restrict__
59 __valarray_get_storage(size_t __n
)
61 return static_cast<_Tp
*__restrict__
>
62 (std::__valarray_get_memory(__n
* sizeof(_Tp
)));
65 // Return memory to the system
67 __valarray_release_memory(void* __p
)
68 { operator delete(__p
); }
70 // Turn a raw-memory into an array of _Tp filled with _Tp()
71 // This is required in 'valarray<T> v(n);'
72 template<typename _Tp
, bool>
73 struct _Array_default_ctor
75 // Please note that this isn't exception safe. But
76 // valarrays aren't required to be exception safe.
78 _S_do_it(_Tp
* __b
, _Tp
* __e
)
85 template<typename _Tp
>
86 struct _Array_default_ctor
<_Tp
, true>
88 // For fundamental types, it suffices to say 'memset()'
90 _S_do_it(_Tp
* __b
, _Tp
* __e
)
91 { __builtin_memset(__b
, 0, (__e
- __b
) * sizeof(_Tp
)); }
94 template<typename _Tp
>
96 __valarray_default_construct(_Tp
* __b
, _Tp
* __e
)
98 _Array_default_ctor
<_Tp
, __is_scalar
<_Tp
>::__value
>::_S_do_it(__b
, __e
);
101 // Turn a raw-memory into an array of _Tp filled with __t
102 // This is the required in valarray<T> v(n, t). Also
103 // used in valarray<>::resize().
104 template<typename _Tp
, bool>
105 struct _Array_init_ctor
107 // Please note that this isn't exception safe. But
108 // valarrays aren't required to be exception safe.
110 _S_do_it(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
117 template<typename _Tp
>
118 struct _Array_init_ctor
<_Tp
, true>
121 _S_do_it(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
128 template<typename _Tp
>
130 __valarray_fill_construct(_Tp
* __b
, _Tp
* __e
, const _Tp __t
)
132 _Array_init_ctor
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__b
, __e
, __t
);
136 // copy-construct raw array [__o, *) from plain array [__b, __e)
137 // We can't just say 'memcpy()'
139 template<typename _Tp
, bool>
140 struct _Array_copy_ctor
142 // Please note that this isn't exception safe. But
143 // valarrays aren't required to be exception safe.
145 _S_do_it(const _Tp
* __b
, const _Tp
* __e
, _Tp
* __restrict__ __o
)
148 new(__o
++) _Tp(*__b
++);
152 template<typename _Tp
>
153 struct _Array_copy_ctor
<_Tp
, true>
156 _S_do_it(const _Tp
* __b
, const _Tp
* __e
, _Tp
* __restrict__ __o
)
157 { __builtin_memcpy(__o
, __b
, (__e
- __b
) * sizeof(_Tp
)); }
160 template<typename _Tp
>
162 __valarray_copy_construct(const _Tp
* __b
, const _Tp
* __e
,
163 _Tp
* __restrict__ __o
)
165 _Array_copy_ctor
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__b
, __e
, __o
);
168 // copy-construct raw array [__o, *) from strided array __a[<__n : __s>]
169 template<typename _Tp
>
171 __valarray_copy_construct (const _Tp
* __restrict__ __a
, size_t __n
,
172 size_t __s
, _Tp
* __restrict__ __o
)
174 if (__is_trivial(_Tp
))
183 new(__o
++) _Tp(*__a
);
188 // copy-construct raw array [__o, *) from indexed array __a[__i[<__n>]]
189 template<typename _Tp
>
191 __valarray_copy_construct (const _Tp
* __restrict__ __a
,
192 const size_t* __restrict__ __i
,
193 _Tp
* __restrict__ __o
, size_t __n
)
195 if (__is_trivial(_Tp
))
197 *__o
++ = __a
[*__i
++];
200 new (__o
++) _Tp(__a
[*__i
++]);
203 // Do the necessary cleanup when we're done with arrays.
204 template<typename _Tp
>
206 __valarray_destroy_elements(_Tp
* __b
, _Tp
* __e
)
208 if (!__is_trivial(_Tp
))
216 // Fill a plain array __a[<__n>] with __t
217 template<typename _Tp
>
219 __valarray_fill(_Tp
* __restrict__ __a
, size_t __n
, const _Tp
& __t
)
225 // fill strided array __a[<__n-1 : __s>] with __t
226 template<typename _Tp
>
228 __valarray_fill(_Tp
* __restrict__ __a
, size_t __n
,
229 size_t __s
, const _Tp
& __t
)
231 for (size_t __i
= 0; __i
< __n
; ++__i
, __a
+= __s
)
235 // fill indirect array __a[__i[<__n>]] with __i
236 template<typename _Tp
>
238 __valarray_fill(_Tp
* __restrict__ __a
, const size_t* __restrict__ __i
,
239 size_t __n
, const _Tp
& __t
)
241 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__i
)
245 // copy plain array __a[<__n>] in __b[<__n>]
246 // For non-fundamental types, it is wrong to say 'memcpy()'
247 template<typename _Tp
, bool>
251 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
258 template<typename _Tp
>
259 struct _Array_copier
<_Tp
, true>
262 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
263 { __builtin_memcpy(__b
, __a
, __n
* sizeof (_Tp
)); }
266 // Copy a plain array __a[<__n>] into a play array __b[<>]
267 template<typename _Tp
>
269 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
270 _Tp
* __restrict__ __b
)
272 _Array_copier
<_Tp
, __is_trivial(_Tp
)>::_S_do_it(__a
, __n
, __b
);
275 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
276 template<typename _Tp
>
278 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
, size_t __s
,
279 _Tp
* __restrict__ __b
)
281 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__b
, __a
+= __s
)
285 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
286 template<typename _Tp
>
288 __valarray_copy(const _Tp
* __restrict__ __a
, _Tp
* __restrict__ __b
,
289 size_t __n
, size_t __s
)
291 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__a
, __b
+= __s
)
295 // Copy strided array __src[<__n : __s1>] into another
296 // strided array __dst[< : __s2>]. Their sizes must match.
297 template<typename _Tp
>
299 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
, size_t __s1
,
300 _Tp
* __restrict__ __dst
, size_t __s2
)
302 for (size_t __i
= 0; __i
< __n
; ++__i
)
303 __dst
[__i
* __s2
] = __src
[__i
* __s1
];
306 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
307 template<typename _Tp
>
309 __valarray_copy(const _Tp
* __restrict__ __a
,
310 const size_t* __restrict__ __i
,
311 _Tp
* __restrict__ __b
, size_t __n
)
313 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__b
, ++__i
)
317 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
318 template<typename _Tp
>
320 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
321 _Tp
* __restrict__ __b
, const size_t* __restrict__ __i
)
323 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__a
, ++__i
)
327 // Copy the __n first elements of an indexed array __src[<__i>] into
328 // another indexed array __dst[<__j>].
329 template<typename _Tp
>
331 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
,
332 const size_t* __restrict__ __i
,
333 _Tp
* __restrict__ __dst
, const size_t* __restrict__ __j
)
335 for (size_t __k
= 0; __k
< __n
; ++__k
)
336 __dst
[*__j
++] = __src
[*__i
++];
340 // Compute the sum of elements in range [__f, __l)
341 // This is a naive algorithm. It suffers from cancelling.
342 // In the future try to specialize
343 // for _Tp = float, double, long double using a more accurate
346 template<typename _Tp
>
348 __valarray_sum(const _Tp
* __f
, const _Tp
* __l
)
356 // Compute the product of all elements in range [__f, __l)
357 template<typename _Tp
>
359 __valarray_product(const _Tp
* __f
, const _Tp
* __l
)
367 // Compute the min/max of an array-expression
368 template<typename _Ta
>
369 inline typename
_Ta::value_type
370 __valarray_min(const _Ta
& __a
)
372 size_t __s
= __a
.size();
373 typedef typename
_Ta::value_type _Value_type
;
374 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
375 for (size_t __i
= 1; __i
< __s
; ++__i
)
377 _Value_type __t
= __a
[__i
];
384 template<typename _Ta
>
385 inline typename
_Ta::value_type
386 __valarray_max(const _Ta
& __a
)
388 size_t __s
= __a
.size();
389 typedef typename
_Ta::value_type _Value_type
;
390 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
391 for (size_t __i
= 1; __i
< __s
; ++__i
)
393 _Value_type __t
= __a
[__i
];
401 // Helper class _Array, first layer of valarray abstraction.
402 // All operations on valarray should be forwarded to this class
403 // whenever possible. -- gdr
406 template<typename _Tp
>
409 explicit _Array(size_t);
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(size_t __n
)
505 : _M_data(__valarray_get_storage
<_Tp
>(__n
))
506 { std::__valarray_default_construct(_M_data
, _M_data
+ __n
); }
508 template<typename _Tp
>
510 _Array
<_Tp
>::_Array(_Tp
* const __restrict__ __p
)
513 template<typename _Tp
>
515 _Array
<_Tp
>::_Array(const valarray
<_Tp
>& __v
)
516 : _M_data (__v
._M_data
) {}
518 template<typename _Tp
>
520 _Array
<_Tp
>::_Array(const _Tp
* __restrict__ __b
, size_t __s
)
521 : _M_data(__valarray_get_storage
<_Tp
>(__s
))
522 { std::__valarray_copy_construct(__b
, __s
, _M_data
); }
524 template<typename _Tp
>
526 _Array
<_Tp
>::begin () const
529 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
530 template<typename _Tp> \
532 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, const _Tp& __t) \
534 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; ++__p) \
538 template<typename _Tp> \
540 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
542 _Tp* __p = __a._M_data; \
543 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; ++__p, ++__q) \
547 template<typename _Tp, class _Dom> \
549 _Array_augmented_##_Name(_Array<_Tp> __a, \
550 const _Expr<_Dom, _Tp>& __e, size_t __n) \
552 _Tp* __p(__a._M_data); \
553 for (size_t __i = 0; __i < __n; ++__i, ++__p) \
554 *__p _Op##= __e[__i]; \
557 template<typename _Tp> \
559 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, size_t __s, \
562 _Tp* __q(__b._M_data); \
563 for (_Tp* __p = __a._M_data; __p < __a._M_data + __s * __n; \
568 template<typename _Tp> \
570 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<_Tp> __b, \
571 size_t __n, size_t __s) \
573 _Tp* __q(__b._M_data); \
574 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
579 template<typename _Tp, class _Dom> \
581 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __s, \
582 const _Expr<_Dom, _Tp>& __e, size_t __n) \
584 _Tp* __p(__a._M_data); \
585 for (size_t __i = 0; __i < __n; ++__i, __p += __s) \
586 *__p _Op##= __e[__i]; \
589 template<typename _Tp> \
591 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
592 _Array<_Tp> __b, size_t __n) \
594 _Tp* __q(__b._M_data); \
595 for (size_t* __j = __i._M_data; __j < __i._M_data + __n; \
597 __a._M_data[*__j] _Op##= *__q; \
600 template<typename _Tp> \
602 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
603 _Array<_Tp> __b, _Array<size_t> __i) \
605 _Tp* __p(__a._M_data); \
606 for (size_t* __j = __i._M_data; __j<__i._M_data + __n; \
608 *__p _Op##= __b._M_data[*__j]; \
611 template<typename _Tp, class _Dom> \
613 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
614 const _Expr<_Dom, _Tp>& __e, size_t __n) \
616 size_t* __j(__i._M_data); \
617 for (size_t __k = 0; __k<__n; ++__k, ++__j) \
618 __a._M_data[*__j] _Op##= __e[__k]; \
621 template<typename _Tp> \
623 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
624 _Array<_Tp> __b, size_t __n) \
626 bool* __ok(__m._M_data); \
627 _Tp* __p(__a._M_data); \
628 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; \
629 ++__q, ++__ok, ++__p) \
640 template<typename _Tp> \
642 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
643 _Array<_Tp> __b, _Array<bool> __m) \
645 bool* __ok(__m._M_data); \
646 _Tp* __q(__b._M_data); \
647 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
648 ++__p, ++__ok, ++__q) \
659 template<typename _Tp, class _Dom> \
661 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
662 const _Expr<_Dom, _Tp>& __e, size_t __n) \
664 bool* __ok(__m._M_data); \
665 _Tp* __p(__a._M_data); \
666 for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p) \
673 *__p _Op##= __e[__i]; \
677 _DEFINE_ARRAY_FUNCTION(+, __plus
)
678 _DEFINE_ARRAY_FUNCTION(-, __minus
)
679 _DEFINE_ARRAY_FUNCTION(*, __multiplies
)
680 _DEFINE_ARRAY_FUNCTION(/, __divides
)
681 _DEFINE_ARRAY_FUNCTION(%, __modulus
)
682 _DEFINE_ARRAY_FUNCTION(^, __bitwise_xor
)
683 _DEFINE_ARRAY_FUNCTION(|, __bitwise_or
)
684 _DEFINE_ARRAY_FUNCTION(&, __bitwise_and
)
685 _DEFINE_ARRAY_FUNCTION(<<, __shift_left
)
686 _DEFINE_ARRAY_FUNCTION(>>, __shift_right
)
688 #undef _DEFINE_ARRAY_FUNCTION
690 _GLIBCXX_END_NAMESPACE_VERSION
693 # include <bits/valarray_array.tcc>
695 #endif /* _ARRAY_H */