Mark as release
[official-gcc.git] / libstdc++-v3 / include / bits / valarray_array.h
bloba40c880ba8d61c8e900c37e0a64b3b094c88af32
1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
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 /** @file valarray_array.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
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>
45 #include <cstdlib>
46 #include <cstring>
47 #include <new>
49 _GLIBCXX_BEGIN_NAMESPACE(std)
52 // Helper functions on raw pointers
55 // We get memory by the old fashion way
56 inline void*
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
69 inline void
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.
80 inline static void
81 _S_do_it(_Tp* __restrict__ __b, _Tp* __restrict__ __e)
83 while (__b != __e)
84 new(__b++) _Tp();
88 template<typename _Tp>
89 struct _Array_default_ctor<_Tp, true>
91 // For fundamental types, it suffices to say 'memset()'
92 inline static void
93 _S_do_it(_Tp* __restrict__ __b, _Tp* __restrict__ __e)
94 { std::memset(__b, 0, (__e - __b) * sizeof(_Tp)); }
97 template<typename _Tp>
98 inline void
99 __valarray_default_construct(_Tp* __restrict__ __b, _Tp* __restrict__ __e)
101 _Array_default_ctor<_Tp, __is_pod<_Tp>::__value>::_S_do_it(__b, __e);
104 // Turn a raw-memory into an array of _Tp filled with __t
105 // This is the required in valarray<T> v(n, t). Also
106 // used in valarray<>::resize().
107 template<typename _Tp, bool>
108 struct _Array_init_ctor
110 // Please note that this isn't exception safe. But
111 // valarrays aren't required to be exception safe.
112 inline static void
113 _S_do_it(_Tp* __restrict__ __b, _Tp* __restrict__ __e, const _Tp __t)
115 while (__b != __e)
116 new(__b++) _Tp(__t);
120 template<typename _Tp>
121 struct _Array_init_ctor<_Tp, true>
123 inline static void
124 _S_do_it(_Tp* __restrict__ __b, _Tp* __restrict__ __e, const _Tp __t)
126 while (__b != __e)
127 *__b++ = __t;
131 template<typename _Tp>
132 inline void
133 __valarray_fill_construct(_Tp* __restrict__ __b, _Tp* __restrict__ __e,
134 const _Tp __t)
136 _Array_init_ctor<_Tp, __is_pod<_Tp>::__value>::_S_do_it(__b, __e, __t);
140 // copy-construct raw array [__o, *) from plain array [__b, __e)
141 // We can't just say 'memcpy()'
143 template<typename _Tp, bool>
144 struct _Array_copy_ctor
146 // Please note that this isn't exception safe. But
147 // valarrays aren't required to be exception safe.
148 inline static void
149 _S_do_it(const _Tp* __restrict__ __b, const _Tp* __restrict__ __e,
150 _Tp* __restrict__ __o)
152 while (__b != __e)
153 new(__o++) _Tp(*__b++);
157 template<typename _Tp>
158 struct _Array_copy_ctor<_Tp, true>
160 inline static void
161 _S_do_it(const _Tp* __restrict__ __b, const _Tp* __restrict__ __e,
162 _Tp* __restrict__ __o)
163 { std::memcpy(__o, __b, (__e - __b)*sizeof(_Tp)); }
166 template<typename _Tp>
167 inline void
168 __valarray_copy_construct(const _Tp* __restrict__ __b,
169 const _Tp* __restrict__ __e,
170 _Tp* __restrict__ __o)
172 _Array_copy_ctor<_Tp, __is_pod<_Tp>::__value>::_S_do_it(__b, __e, __o);
175 // copy-construct raw array [__o, *) from strided array __a[<__n : __s>]
176 template<typename _Tp>
177 inline void
178 __valarray_copy_construct (const _Tp* __restrict__ __a, size_t __n,
179 size_t __s, _Tp* __restrict__ __o)
181 if (__is_pod<_Tp>::__value)
182 while (__n--)
184 *__o++ = *__a;
185 __a += __s;
187 else
188 while (__n--)
190 new(__o++) _Tp(*__a);
191 __a += __s;
195 // copy-construct raw array [__o, *) from indexed array __a[__i[<__n>]]
196 template<typename _Tp>
197 inline void
198 __valarray_copy_construct (const _Tp* __restrict__ __a,
199 const size_t* __restrict__ __i,
200 _Tp* __restrict__ __o, size_t __n)
202 if (__is_pod<_Tp>::__value)
203 while (__n--)
204 *__o++ = __a[*__i++];
205 else
206 while (__n--)
207 new (__o++) _Tp(__a[*__i++]);
210 // Do the necessary cleanup when we're done with arrays.
211 template<typename _Tp>
212 inline void
213 __valarray_destroy_elements(_Tp* __restrict__ __b, _Tp* __restrict__ __e)
215 if (!__is_pod<_Tp>::__value)
216 while (__b != __e)
218 __b->~_Tp();
219 ++__b;
223 // Fill a plain array __a[<__n>] with __t
224 template<typename _Tp>
225 inline void
226 __valarray_fill(_Tp* __restrict__ __a, size_t __n, const _Tp& __t)
228 while (__n--)
229 *__a++ = __t;
232 // fill strided array __a[<__n-1 : __s>] with __t
233 template<typename _Tp>
234 inline void
235 __valarray_fill(_Tp* __restrict__ __a, size_t __n,
236 size_t __s, const _Tp& __t)
238 for (size_t __i = 0; __i < __n; ++__i, __a += __s)
239 *__a = __t;
242 // fill indir ect array __a[__i[<__n>]] with __i
243 template<typename _Tp>
244 inline void
245 __valarray_fill(_Tp* __restrict__ __a, const size_t* __restrict__ __i,
246 size_t __n, const _Tp& __t)
248 for (size_t __j = 0; __j < __n; ++__j, ++__i)
249 __a[*__i] = __t;
252 // copy plain array __a[<__n>] in __b[<__n>]
253 // For non-fundamental types, it is wrong to say 'memcpy()'
254 template<typename _Tp, bool>
255 struct _Array_copier
257 inline static void
258 _S_do_it(const _Tp* __restrict__ __a, size_t __n, _Tp* __restrict__ __b)
260 while(__n--)
261 *__b++ = *__a++;
265 template<typename _Tp>
266 struct _Array_copier<_Tp, true>
268 inline static void
269 _S_do_it(const _Tp* __restrict__ __a, size_t __n, _Tp* __restrict__ __b)
270 { std::memcpy (__b, __a, __n * sizeof (_Tp)); }
273 // Copy a plain array __a[<__n>] into a play array __b[<>]
274 template<typename _Tp>
275 inline void
276 __valarray_copy(const _Tp* __restrict__ __a, size_t __n,
277 _Tp* __restrict__ __b)
279 _Array_copier<_Tp, __is_pod<_Tp>::__value>::_S_do_it(__a, __n, __b);
282 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
283 template<typename _Tp>
284 inline void
285 __valarray_copy(const _Tp* __restrict__ __a, size_t __n, size_t __s,
286 _Tp* __restrict__ __b)
288 for (size_t __i = 0; __i < __n; ++__i, ++__b, __a += __s)
289 *__b = *__a;
292 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
293 template<typename _Tp>
294 inline void
295 __valarray_copy(const _Tp* __restrict__ __a, _Tp* __restrict__ __b,
296 size_t __n, size_t __s)
298 for (size_t __i = 0; __i < __n; ++__i, ++__a, __b += __s)
299 *__b = *__a;
302 // Copy strided array __src[<__n : __s1>] into another
303 // strided array __dst[< : __s2>]. Their sizes must match.
304 template<typename _Tp>
305 inline void
306 __valarray_copy(const _Tp* __restrict__ __src, size_t __n, size_t __s1,
307 _Tp* __restrict__ __dst, size_t __s2)
309 for (size_t __i = 0; __i < __n; ++__i)
310 __dst[__i * __s2] = __src[__i * __s1];
313 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
314 template<typename _Tp>
315 inline void
316 __valarray_copy(const _Tp* __restrict__ __a,
317 const size_t* __restrict__ __i,
318 _Tp* __restrict__ __b, size_t __n)
320 for (size_t __j = 0; __j < __n; ++__j, ++__b, ++__i)
321 *__b = __a[*__i];
324 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
325 template<typename _Tp>
326 inline void
327 __valarray_copy(const _Tp* __restrict__ __a, size_t __n,
328 _Tp* __restrict__ __b, const size_t* __restrict__ __i)
330 for (size_t __j = 0; __j < __n; ++__j, ++__a, ++__i)
331 __b[*__i] = *__a;
334 // Copy the __n first elements of an indexed array __src[<__i>] into
335 // another indexed array __dst[<__j>].
336 template<typename _Tp>
337 inline void
338 __valarray_copy(const _Tp* __restrict__ __src, size_t __n,
339 const size_t* __restrict__ __i,
340 _Tp* __restrict__ __dst, const size_t* __restrict__ __j)
342 for (size_t __k = 0; __k < __n; ++__k)
343 __dst[*__j++] = __src[*__i++];
347 // Compute the sum of elements in range [__f, __l)
348 // This is a naive algorithm. It suffers from cancelling.
349 // In the future try to specialize
350 // for _Tp = float, double, long double using a more accurate
351 // algorithm.
353 template<typename _Tp>
354 inline _Tp
355 __valarray_sum(const _Tp* __restrict__ __f, const _Tp* __restrict__ __l)
357 _Tp __r = _Tp();
358 while (__f != __l)
359 __r += *__f++;
360 return __r;
363 // Compute the product of all elements in range [__f, __l)
364 template<typename _Tp>
365 inline _Tp
366 __valarray_product(const _Tp* __restrict__ __f,
367 const _Tp* __restrict__ __l)
369 _Tp __r = _Tp(1);
370 while (__f != __l)
371 __r = __r * *__f++;
372 return __r;
375 // Compute the min/max of an array-expression
376 template<typename _Ta>
377 inline typename _Ta::value_type
378 __valarray_min(const _Ta& __a)
380 size_t __s = __a.size();
381 typedef typename _Ta::value_type _Value_type;
382 _Value_type __r = __s == 0 ? _Value_type() : __a[0];
383 for (size_t __i = 1; __i < __s; ++__i)
385 _Value_type __t = __a[__i];
386 if (__t < __r)
387 __r = __t;
389 return __r;
392 template<typename _Ta>
393 inline typename _Ta::value_type
394 __valarray_max(const _Ta& __a)
396 size_t __s = __a.size();
397 typedef typename _Ta::value_type _Value_type;
398 _Value_type __r = __s == 0 ? _Value_type() : __a[0];
399 for (size_t __i = 1; __i < __s; ++__i)
401 _Value_type __t = __a[__i];
402 if (__t > __r)
403 __r = __t;
405 return __r;
409 // Helper class _Array, first layer of valarray abstraction.
410 // All operations on valarray should be forwarded to this class
411 // whenever possible. -- gdr
414 template<typename _Tp>
415 struct _Array
417 explicit _Array(size_t);
418 explicit _Array(_Tp* const __restrict__);
419 explicit _Array(const valarray<_Tp>&);
420 _Array(const _Tp* __restrict__, size_t);
422 _Tp* begin() const;
424 _Tp* const __restrict__ _M_data;
428 // Copy-construct plain array __b[<__n>] from indexed array __a[__i[<__n>]]
429 template<typename _Tp>
430 inline void
431 __valarray_copy_construct(_Array<_Tp> __a, _Array<size_t> __i,
432 _Array<_Tp> __b, size_t __n)
433 { std::__valarray_copy_construct(__a._M_data, __i._M_data,
434 __b._M_data, __n); }
436 // Copy-construct plain array __b[<__n>] from strided array __a[<__n : __s>]
437 template<typename _Tp>
438 inline void
439 __valarray_copy_construct(_Array<_Tp> __a, size_t __n, size_t __s,
440 _Array<_Tp> __b)
441 { std::__valarray_copy_construct(__a._M_data, __n, __s, __b._M_data); }
443 template<typename _Tp>
444 inline void
445 __valarray_fill (_Array<_Tp> __a, size_t __n, const _Tp& __t)
446 { std::__valarray_fill(__a._M_data, __n, __t); }
448 template<typename _Tp>
449 inline void
450 __valarray_fill(_Array<_Tp> __a, size_t __n, size_t __s, const _Tp& __t)
451 { std::__valarray_fill(__a._M_data, __n, __s, __t); }
453 template<typename _Tp>
454 inline void
455 __valarray_fill(_Array<_Tp> __a, _Array<size_t> __i,
456 size_t __n, const _Tp& __t)
457 { std::__valarray_fill(__a._M_data, __i._M_data, __n, __t); }
459 // Copy a plain array __a[<__n>] into a play array __b[<>]
460 template<typename _Tp>
461 inline void
462 __valarray_copy(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b)
463 { std::__valarray_copy(__a._M_data, __n, __b._M_data); }
465 // Copy strided array __a[<__n : __s>] in plain __b[<__n>]
466 template<typename _Tp>
467 inline void
468 __valarray_copy(_Array<_Tp> __a, size_t __n, size_t __s, _Array<_Tp> __b)
469 { std::__valarray_copy(__a._M_data, __n, __s, __b._M_data); }
471 // Copy a plain array __a[<__n>] into a strided array __b[<__n : __s>]
472 template<typename _Tp>
473 inline void
474 __valarray_copy(_Array<_Tp> __a, _Array<_Tp> __b, size_t __n, size_t __s)
475 { __valarray_copy(__a._M_data, __b._M_data, __n, __s); }
477 // Copy strided array __src[<__n : __s1>] into another
478 // strided array __dst[< : __s2>]. Their sizes must match.
479 template<typename _Tp>
480 inline void
481 __valarray_copy(_Array<_Tp> __a, size_t __n, size_t __s1,
482 _Array<_Tp> __b, size_t __s2)
483 { std::__valarray_copy(__a._M_data, __n, __s1, __b._M_data, __s2); }
485 // Copy an indexed array __a[__i[<__n>]] in plain array __b[<__n>]
486 template<typename _Tp>
487 inline void
488 __valarray_copy(_Array<_Tp> __a, _Array<size_t> __i,
489 _Array<_Tp> __b, size_t __n)
490 { std::__valarray_copy(__a._M_data, __i._M_data, __b._M_data, __n); }
492 // Copy a plain array __a[<__n>] in an indexed array __b[__i[<__n>]]
493 template<typename _Tp>
494 inline void
495 __valarray_copy(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b,
496 _Array<size_t> __i)
497 { std::__valarray_copy(__a._M_data, __n, __b._M_data, __i._M_data); }
499 // Copy the __n first elements of an indexed array __src[<__i>] into
500 // another indexed array __dst[<__j>].
501 template<typename _Tp>
502 inline void
503 __valarray_copy(_Array<_Tp> __src, size_t __n, _Array<size_t> __i,
504 _Array<_Tp> __dst, _Array<size_t> __j)
506 std::__valarray_copy(__src._M_data, __n, __i._M_data,
507 __dst._M_data, __j._M_data);
510 template<typename _Tp>
511 inline
512 _Array<_Tp>::_Array(size_t __n)
513 : _M_data(__valarray_get_storage<_Tp>(__n))
514 { std::__valarray_default_construct(_M_data, _M_data + __n); }
516 template<typename _Tp>
517 inline
518 _Array<_Tp>::_Array(_Tp* const __restrict__ __p)
519 : _M_data (__p) {}
521 template<typename _Tp>
522 inline
523 _Array<_Tp>::_Array(const valarray<_Tp>& __v)
524 : _M_data (__v._M_data) {}
526 template<typename _Tp>
527 inline
528 _Array<_Tp>::_Array(const _Tp* __restrict__ __b, size_t __s)
529 : _M_data(__valarray_get_storage<_Tp>(__s))
530 { std::__valarray_copy_construct(__b, __s, _M_data); }
532 template<typename _Tp>
533 inline _Tp*
534 _Array<_Tp>::begin () const
535 { return _M_data; }
537 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
538 template<typename _Tp> \
539 inline void \
540 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, const _Tp& __t) \
542 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; ++__p) \
543 *__p _Op##= __t; \
546 template<typename _Tp> \
547 inline void \
548 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
550 _Tp* __p = __a._M_data; \
551 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; ++__p, ++__q) \
552 *__p _Op##= *__q; \
555 template<typename _Tp, class _Dom> \
556 void \
557 _Array_augmented_##_Name(_Array<_Tp> __a, \
558 const _Expr<_Dom, _Tp>& __e, size_t __n) \
560 _Tp* __p(__a._M_data); \
561 for (size_t __i = 0; __i < __n; ++__i, ++__p) \
562 *__p _Op##= __e[__i]; \
565 template<typename _Tp> \
566 inline void \
567 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, size_t __s, \
568 _Array<_Tp> __b) \
570 _Tp* __q(__b._M_data); \
571 for (_Tp* __p = __a._M_data; __p < __a._M_data + __s * __n; \
572 __p += __s, ++__q) \
573 *__p _Op##= *__q; \
576 template<typename _Tp> \
577 inline void \
578 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<_Tp> __b, \
579 size_t __n, size_t __s) \
581 _Tp* __q(__b._M_data); \
582 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
583 ++__p, __q += __s) \
584 *__p _Op##= *__q; \
587 template<typename _Tp, class _Dom> \
588 void \
589 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __s, \
590 const _Expr<_Dom, _Tp>& __e, size_t __n) \
592 _Tp* __p(__a._M_data); \
593 for (size_t __i = 0; __i < __n; ++__i, __p += __s) \
594 *__p _Op##= __e[__i]; \
597 template<typename _Tp> \
598 inline void \
599 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
600 _Array<_Tp> __b, size_t __n) \
602 _Tp* __q(__b._M_data); \
603 for (size_t* __j = __i._M_data; __j < __i._M_data + __n; \
604 ++__j, ++__q) \
605 __a._M_data[*__j] _Op##= *__q; \
608 template<typename _Tp> \
609 inline void \
610 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
611 _Array<_Tp> __b, _Array<size_t> __i) \
613 _Tp* __p(__a._M_data); \
614 for (size_t* __j = __i._M_data; __j<__i._M_data + __n; \
615 ++__j, ++__p) \
616 *__p _Op##= __b._M_data[*__j]; \
619 template<typename _Tp, class _Dom> \
620 void \
621 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<size_t> __i, \
622 const _Expr<_Dom, _Tp>& __e, size_t __n) \
624 size_t* __j(__i._M_data); \
625 for (size_t __k = 0; __k<__n; ++__k, ++__j) \
626 __a._M_data[*__j] _Op##= __e[__k]; \
629 template<typename _Tp> \
630 void \
631 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
632 _Array<_Tp> __b, size_t __n) \
634 bool* __ok(__m._M_data); \
635 _Tp* __p(__a._M_data); \
636 for (_Tp* __q = __b._M_data; __q < __b._M_data + __n; \
637 ++__q, ++__ok, ++__p) \
639 while (! *__ok) \
641 ++__ok; \
642 ++__p; \
644 *__p _Op##= *__q; \
648 template<typename _Tp> \
649 void \
650 _Array_augmented_##_Name(_Array<_Tp> __a, size_t __n, \
651 _Array<_Tp> __b, _Array<bool> __m) \
653 bool* __ok(__m._M_data); \
654 _Tp* __q(__b._M_data); \
655 for (_Tp* __p = __a._M_data; __p < __a._M_data + __n; \
656 ++__p, ++__ok, ++__q) \
658 while (! *__ok) \
660 ++__ok; \
661 ++__q; \
663 *__p _Op##= *__q; \
667 template<typename _Tp, class _Dom> \
668 void \
669 _Array_augmented_##_Name(_Array<_Tp> __a, _Array<bool> __m, \
670 const _Expr<_Dom, _Tp>& __e, size_t __n) \
672 bool* __ok(__m._M_data); \
673 _Tp* __p(__a._M_data); \
674 for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p) \
676 while (! *__ok) \
678 ++__ok; \
679 ++__p; \
681 *__p _Op##= __e[__i]; \
685 _DEFINE_ARRAY_FUNCTION(+, __plus)
686 _DEFINE_ARRAY_FUNCTION(-, __minus)
687 _DEFINE_ARRAY_FUNCTION(*, __multiplies)
688 _DEFINE_ARRAY_FUNCTION(/, __divides)
689 _DEFINE_ARRAY_FUNCTION(%, __modulus)
690 _DEFINE_ARRAY_FUNCTION(^, __bitwise_xor)
691 _DEFINE_ARRAY_FUNCTION(|, __bitwise_or)
692 _DEFINE_ARRAY_FUNCTION(&, __bitwise_and)
693 _DEFINE_ARRAY_FUNCTION(<<, __shift_left)
694 _DEFINE_ARRAY_FUNCTION(>>, __shift_right)
696 #undef _DEFINE_ARRAY_FUNCTION
698 _GLIBCXX_END_NAMESPACE
700 #ifndef _GLIBCXX_EXPORT_TEMPLATE
701 # include <bits/valarray_array.tcc>
702 #endif
704 #endif /* _ARRAY_H */