1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2003, 2004
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
33 /** @file valarray_array.h
34 * This is an internal header file, included by other library headers.
35 * You should not attempt to use it directly.
38 #ifndef _VALARRAY_ARRAY_H
39 #define _VALARRAY_ARRAY_H 1
41 #pragma GCC system_header
43 #include <bits/c++config.h>
44 #include <bits/cpp_type_traits.h>
52 // Helper functions on raw pointers
55 // We get memory by the old fashion way
57 __valarray_get_memory(size_t __n
)
58 { return operator new(__n
); }
60 template<typename _Tp
>
61 inline _Tp
*__restrict__
62 __valarray_get_storage(size_t __n
)
64 return static_cast<_Tp
*__restrict__
>
65 (std::__valarray_get_memory(__n
* sizeof(_Tp
)));
68 // Return memory to the system
70 __valarray_release_memory(void* __p
)
71 { operator delete(__p
); }
73 // Turn a raw-memory into an array of _Tp filled with _Tp()
74 // This is required in 'valarray<T> v(n);'
75 template<typename _Tp
, bool>
76 struct _Array_default_ctor
78 // Please note that this isn't exception safe. But
79 // valarrays aren't required to be exception safe.
81 _S_do_it(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
)
88 template<typename _Tp
>
89 struct _Array_default_ctor
<_Tp
, true>
91 // For fundamental types, it suffices to say 'memset()'
93 _S_do_it(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
)
94 { std::memset(__b
, 0, (__e
- __b
) * sizeof(_Tp
)); }
97 template<typename _Tp
>
99 __valarray_default_construct(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
)
101 _Array_default_ctor
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
105 // Turn a raw-memory into an array of _Tp filled with __t
106 // This is the required in valarray<T> v(n, t). Also
107 // used in valarray<>::resize().
108 template<typename _Tp
, bool>
109 struct _Array_init_ctor
111 // Please note that this isn't exception safe. But
112 // valarrays aren't required to be exception safe.
114 _S_do_it(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
, const _Tp __t
)
121 template<typename _Tp
>
122 struct _Array_init_ctor
<_Tp
, true>
125 _S_do_it(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
, const _Tp __t
)
132 template<typename _Tp
>
134 __valarray_fill_construct(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
,
137 _Array_init_ctor
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
138 _S_do_it(__b
, __e
, __t
);
142 // copy-construct raw array [__o, *) from plain array [__b, __e)
143 // We can't just say 'memcpy()'
145 template<typename _Tp
, bool>
146 struct _Array_copy_ctor
148 // Please note that this isn't exception safe. But
149 // valarrays aren't required to be exception safe.
151 _S_do_it(const _Tp
* __restrict__ __b
, const _Tp
* __restrict__ __e
,
152 _Tp
* __restrict__ __o
)
155 new(__o
++) _Tp(*__b
++);
159 template<typename _Tp
>
160 struct _Array_copy_ctor
<_Tp
, true>
163 _S_do_it(const _Tp
* __restrict__ __b
, const _Tp
* __restrict__ __e
,
164 _Tp
* __restrict__ __o
)
165 { std::memcpy(__o
, __b
, (__e
- __b
)*sizeof(_Tp
)); }
168 template<typename _Tp
>
170 __valarray_copy_construct(const _Tp
* __restrict__ __b
,
171 const _Tp
* __restrict__ __e
,
172 _Tp
* __restrict__ __o
)
174 _Array_copy_ctor
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
175 _S_do_it(__b
, __e
, __o
);
178 // copy-construct raw array [__o, *) from strided array __a[<__n : __s>]
179 template<typename _Tp
>
181 __valarray_copy_construct (const _Tp
* __restrict__ __a
, size_t __n
,
182 size_t __s
, _Tp
* __restrict__ __o
)
184 if (__is_fundamental
<_Tp
>::_M_type
)
193 new(__o
++) _Tp(*__a
);
198 // copy-construct raw array [__o, *) from indexed array __a[__i[<__n>]]
199 template<typename _Tp
>
201 __valarray_copy_construct (const _Tp
* __restrict__ __a
,
202 const size_t* __restrict__ __i
,
203 _Tp
* __restrict__ __o
, size_t __n
)
205 if (__is_fundamental
<_Tp
>::_M_type
)
207 *__o
++ = __a
[*__i
++];
210 new (__o
++) _Tp(__a
[*__i
++]);
213 // Do the necessary cleanup when we're done with arrays.
214 template<typename _Tp
>
216 __valarray_destroy_elements(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
)
218 if (!__is_fundamental
<_Tp
>::_M_type
)
226 // Fill a plain array __a[<__n>] with __t
227 template<typename _Tp
>
229 __valarray_fill(_Tp
* __restrict__ __a
, size_t __n
, const _Tp
& __t
)
235 // fill strided array __a[<__n-1 : __s>] with __t
236 template<typename _Tp
>
238 __valarray_fill(_Tp
* __restrict__ __a
, size_t __n
,
239 size_t __s
, const _Tp
& __t
)
241 for (size_t __i
= 0; __i
< __n
; ++__i
, __a
+= __s
)
245 // fill indir ect array __a[__i[<__n>]] with __i
246 template<typename _Tp
>
248 __valarray_fill(_Tp
* __restrict__ __a
, const size_t* __restrict__ __i
,
249 size_t __n
, const _Tp
& __t
)
251 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__i
)
255 // copy plain array __a[<__n>] in __b[<__n>]
256 // For non-fundamental types, it is wrong to say 'memcpy()'
257 template<typename _Tp
, bool>
261 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
268 template<typename _Tp
>
269 struct _Array_copier
<_Tp
, true>
272 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
273 { std::memcpy (__b
, __a
, __n
* sizeof (_Tp
)); }
276 // Copy a plain array __a[<__n>] into a play array __b[<>]
277 template<typename _Tp
>
279 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
280 _Tp
* __restrict__ __b
)
282 _Array_copier
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
283 _S_do_it(__a
, __n
, __b
);
286 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
287 template<typename _Tp
>
289 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
, size_t __s
,
290 _Tp
* __restrict__ __b
)
292 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__b
, __a
+= __s
)
296 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
297 template<typename _Tp
>
299 __valarray_copy(const _Tp
* __restrict__ __a
, _Tp
* __restrict__ __b
,
300 size_t __n
, size_t __s
)
302 for (size_t __i
= 0; __i
< __n
; ++__i
, ++__a
, __b
+= __s
)
306 // Copy strided array __src[<__n : __s1>] into another
307 // strided array __dst[< : __s2>]. Their sizes must match.
308 template<typename _Tp
>
310 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
, size_t __s1
,
311 _Tp
* __restrict__ __dst
, size_t __s2
)
313 for (size_t __i
= 0; __i
< __n
; ++__i
)
314 __dst
[__i
* __s2
] = __src
[__i
* __s1
];
317 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
318 template<typename _Tp
>
320 __valarray_copy(const _Tp
* __restrict__ __a
,
321 const size_t* __restrict__ __i
,
322 _Tp
* __restrict__ __b
, size_t __n
)
324 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__b
, ++__i
)
328 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
329 template<typename _Tp
>
331 __valarray_copy(const _Tp
* __restrict__ __a
, size_t __n
,
332 _Tp
* __restrict__ __b
, const size_t* __restrict__ __i
)
334 for (size_t __j
= 0; __j
< __n
; ++__j
, ++__a
, ++__i
)
338 // Copy the __n first elements of an indexed array __src[<__i>] into
339 // another indexed array __dst[<__j>].
340 template<typename _Tp
>
342 __valarray_copy(const _Tp
* __restrict__ __src
, size_t __n
,
343 const size_t* __restrict__ __i
,
344 _Tp
* __restrict__ __dst
, const size_t* __restrict__ __j
)
346 for (size_t __k
= 0; __k
< __n
; ++__k
)
347 __dst
[*__j
++] = __src
[*__i
++];
351 // Compute the sum of elements in range [__f, __l)
352 // This is a naive algorithm. It suffers from cancelling.
353 // In the future try to specialize
354 // for _Tp = float, double, long double using a more accurate
357 template<typename _Tp
>
359 __valarray_sum(const _Tp
* __restrict__ __f
, const _Tp
* __restrict__ __l
)
367 // Compute the product of all elements in range [__f, __l)
368 template<typename _Tp
>
370 __valarray_product(const _Tp
* __restrict__ __f
,
371 const _Tp
* __restrict__ __l
)
379 // Compute the min/max of an array-expression
380 template<typename _Ta
>
381 inline typename
_Ta::value_type
382 __valarray_min(const _Ta
& __a
)
384 size_t __s
= __a
.size();
385 typedef typename
_Ta::value_type _Value_type
;
386 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
387 for (size_t __i
= 1; __i
< __s
; ++__i
)
389 _Value_type __t
= __a
[__i
];
396 template<typename _Ta
>
397 inline typename
_Ta::value_type
398 __valarray_max(const _Ta
& __a
)
400 size_t __s
= __a
.size();
401 typedef typename
_Ta::value_type _Value_type
;
402 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
403 for (size_t __i
= 1; __i
< __s
; ++__i
)
405 _Value_type __t
= __a
[__i
];
413 // Helper class _Array, first layer of valarray abstraction.
414 // All operations on valarray should be forwarded to this class
415 // whenever possible. -- gdr
418 template<typename _Tp
>
421 explicit _Array(size_t);
422 explicit _Array(_Tp
* const __restrict__
);
423 explicit _Array(const valarray
<_Tp
>&);
424 _Array(const _Tp
* __restrict__
, size_t);
428 _Tp
* const __restrict__ _M_data
;
431 template<typename _Tp
>
433 __valarray_fill (_Array
<_Tp
> __a
, size_t __n
, const _Tp
& __t
)
434 { std::__valarray_fill(__a
._M_data
, __n
, __t
); }
436 template<typename _Tp
>
438 __valarray_fill(_Array
<_Tp
> __a
, size_t __n
, size_t __s
, const _Tp
& __t
)
439 { std::__valarray_fill(__a
._M_data
, __n
, __s
, __t
); }
441 template<typename _Tp
>
443 __valarray_fill(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
444 size_t __n
, const _Tp
& __t
)
445 { std::__valarray_fill(__a
._M_data
, __i
._M_data
, __n
, __t
); }
447 // Copy a plain array __a[<__n>] into a play array __b[<>]
448 template<typename _Tp
>
450 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
)
451 { std::__valarray_copy(__a
._M_data
, __n
, __b
._M_data
); }
453 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
454 template<typename _Tp
>
456 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, size_t __s
, _Array
<_Tp
> __b
)
457 { std::__valarray_copy(__a
._M_data
, __n
, __s
, __b
._M_data
); }
459 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
460 template<typename _Tp
>
462 __valarray_copy(_Array
<_Tp
> __a
, _Array
<_Tp
> __b
, size_t __n
, size_t __s
)
463 { __valarray_copy(__a
._M_data
, __b
._M_data
, __n
, __s
); }
465 // Copy strided array __src[<__n : __s1>] into another
466 // strided array __dst[< : __s2>]. Their sizes must match.
467 template<typename _Tp
>
469 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, size_t __s1
,
470 _Array
<_Tp
> __b
, size_t __s2
)
471 { std::__valarray_copy(__a
._M_data
, __n
, __s1
, __b
._M_data
, __s2
); }
473 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
474 template<typename _Tp
>
476 __valarray_copy(_Array
<_Tp
> __a
, _Array
<size_t> __i
,
477 _Array
<_Tp
> __b
, size_t __n
)
478 { std::__valarray_copy(__a
._M_data
, __i
._M_data
, __b
._M_data
, __n
); }
480 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
481 template<typename _Tp
>
483 __valarray_copy(_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
,
485 { std::__valarray_copy(__a
._M_data
, __n
, __b
._M_data
, __i
._M_data
); }
487 // Copy the __n first elements of an indexed array __src[<__i>] into
488 // another indexed array __dst[<__j>].
489 template<typename _Tp
>
491 __valarray_copy(_Array
<_Tp
> __src
, size_t __n
, _Array
<size_t> __i
,
492 _Array
<_Tp
> __dst
, _Array
<size_t> __j
)
494 std::__valarray_copy(__src
._M_data
, __n
, __i
._M_data
,
495 __dst
._M_data
, __j
._M_data
);
498 template<typename _Tp
>
500 _Array
<_Tp
>::_Array(size_t __n
)
501 : _M_data(__valarray_get_storage
<_Tp
>(__n
))
502 { std::__valarray_default_construct(_M_data
, _M_data
+ __n
); }
504 template<typename _Tp
>
506 _Array
<_Tp
>::_Array(_Tp
* const __restrict__ __p
)
509 template<typename _Tp
>
511 _Array
<_Tp
>::_Array(const valarray
<_Tp
>& __v
)
512 : _M_data (__v
._M_data
) {}
514 template<typename _Tp
>
516 _Array
<_Tp
>::_Array(const _Tp
* __restrict__ __b
, size_t __s
)
517 : _M_data(__valarray_get_storage
<_Tp
>(__s
))
518 { std::__valarray_copy_construct(__b
, __s
, _M_data
); }
520 template<typename _Tp
>
522 _Array
<_Tp
>::begin () const
525 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
526 template<typename _Tp> \
528 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, const _Tp& __t) \
530 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; ++__p) \
534 template<typename _Tp> \
536 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
538 _Tp* __p = __a._M_data; \
539 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; ++__p, ++__q) \
543 template<typename _Tp, class _Dom> \
545 _Array_augmented_##_Name(_Array<_Tp> __a, \
546 const _Expr<_Dom, _Tp>& __e, size_t __n) \
548 _Tp* __p(__a._M_data); \
549 for (size_t __i = 0; __i < __n; ++__i, ++__p) \
550 *__p _Op##= __e[__i]; \
553 template<typename _Tp> \
555 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, size_t __s, \
558 _Tp* __q(__b._M_data); \
559 for (_Tp* __p = __a._M_data; __p < __a._M_data + __s * __n; \
564 template<typename _Tp> \
566 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<_Tp> __b, \
567 size_t __n, size_t __s) \
569 _Tp* __q(__b._M_data); \
570 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
575 template<typename _Tp, class _Dom> \
577 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __s, \
578 const _Expr<_Dom, _Tp>& __e, size_t __n) \
580 _Tp* __p(__a._M_data); \
581 for (size_t __i = 0; __i < __n; ++__i, __p += __s) \
582 *__p _Op##= __e[__i]; \
585 template<typename _Tp> \
587 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
588 _Array<_Tp> __b, size_t __n) \
590 _Tp* __q(__b._M_data); \
591 for (size_t* __j = __i._M_data; __j < __i._M_data + __n; \
593 __a._M_data[*__j] _Op##= *__q; \
596 template<typename _Tp> \
598 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
599 _Array<_Tp> __b, _Array<size_t> __i) \
601 _Tp* __p(__a._M_data); \
602 for (size_t* __j = __i._M_data; __j<__i._M_data + __n; \
604 *__p _Op##= __b._M_data[*__j]; \
607 template<typename _Tp, class _Dom> \
609 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
610 const _Expr<_Dom, _Tp>& __e, size_t __n) \
612 size_t* __j(__i._M_data); \
613 for (size_t __k = 0; __k<__n; ++__k, ++__j) \
614 __a._M_data[*__j] _Op##= __e[__k]; \
617 template<typename _Tp> \
619 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
620 _Array<_Tp> __b, size_t __n) \
622 bool* __ok(__m._M_data); \
623 _Tp* __p(__a._M_data); \
624 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; \
625 ++__q, ++__ok, ++__p) \
636 template<typename _Tp> \
638 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
639 _Array<_Tp> __b, _Array<bool> __m) \
641 bool* __ok(__m._M_data); \
642 _Tp* __q(__b._M_data); \
643 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
644 ++__p, ++__ok, ++__q) \
655 template<typename _Tp, class _Dom> \
657 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
658 const _Expr<_Dom, _Tp>& __e, size_t __n) \
660 bool* __ok(__m._M_data); \
661 _Tp* __p(__a._M_data); \
662 for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p) \
669 *__p _Op##= __e[__i]; \
673 _DEFINE_ARRAY_FUNCTION(+, __plus
)
674 _DEFINE_ARRAY_FUNCTION(-, __minus
)
675 _DEFINE_ARRAY_FUNCTION(*, __multiplies
)
676 _DEFINE_ARRAY_FUNCTION(/, __divides
)
677 _DEFINE_ARRAY_FUNCTION(%, __modulus
)
678 _DEFINE_ARRAY_FUNCTION(^, __bitwise_xor
)
679 _DEFINE_ARRAY_FUNCTION(|, __bitwise_or
)
680 _DEFINE_ARRAY_FUNCTION(&, __bitwise_and
)
681 _DEFINE_ARRAY_FUNCTION(<<, __shift_left
)
682 _DEFINE_ARRAY_FUNCTION(>>, __shift_right
)
684 #undef _DEFINE_VALARRAY_FUNCTION
687 #ifndef _GLIBCXX_EXPORT_TEMPLATE
688 # include <bits/valarray_array.tcc>
691 #endif /* _ARRAY_H */