]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/valarray_array.h
1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
3 // Copyright (C) 1997-2000 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
32 #ifndef _CPP_BITS_ARRAY_H
33 #define _CPP_BITS_ARRAY_H 1
35 #pragma GCC system_header
37 #include <bits/c++config.h>
38 #include <bits/cpp_type_traits.h>
39 #include <bits/std_cstdlib.h>
40 #include <bits/std_cstring.h>
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 (__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
* __restrict__ __b
, _Tp
* __restrict__ __e
)
77 { while (__b
!= __e
) new(__b
++) _Tp(); }
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
* __restrict__ __b
, _Tp
* __restrict__ __e
)
86 { memset(__b
, 0, (__e
- __b
)*sizeof(_Tp
)); }
89 template<typename _Tp
>
91 __valarray_default_construct(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
)
93 _Array_default_ctor
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
97 // Turn a raw-memory into an array of _Tp filled with __t
98 // This is the required in valarray<T> v(n, t). Also
99 // used in valarray<>::resize().
100 template<typename _Tp
, bool>
101 struct _Array_init_ctor
103 // Please note that this isn't exception safe. But
104 // valarrays aren't required to be exception safe.
106 _S_do_it(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
, const _Tp __t
)
107 { while (__b
!= __e
) new(__b
++) _Tp(__t
); }
110 template<typename _Tp
>
111 struct _Array_init_ctor
<_Tp
, true>
114 _S_do_it(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
, const _Tp __t
)
115 { while (__b
!= __e
) *__b
++ = __t
; }
118 template<typename _Tp
>
120 __valarray_fill_construct(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
,
123 _Array_init_ctor
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
124 _S_do_it(__b
, __e
, __t
);
128 // copy-construct raw array [__o, *) from plain array [__b, __e)
129 // We can't just say 'memcpy()'
131 template<typename _Tp
, bool>
132 struct _Array_copy_ctor
134 // Please note that this isn't exception safe. But
135 // valarrays aren't required to be exception safe.
137 _S_do_it(const _Tp
* __restrict__ __b
, const _Tp
* __restrict__ __e
,
138 _Tp
* __restrict__ __o
)
139 { while (__b
!= __e
) new(__o
++) _Tp(*__b
++); }
142 template<typename _Tp
>
143 struct _Array_copy_ctor
<_Tp
, true>
146 _S_do_it(const _Tp
* __restrict__ __b
, const _Tp
* __restrict__ __e
,
147 _Tp
* __restrict__ __o
)
148 { memcpy(__o
, __b
, (__e
- __b
)*sizeof(_Tp
)); }
151 template<typename _Tp
>
153 __valarray_copy_construct(const _Tp
* __restrict__ __b
,
154 const _Tp
* __restrict__ __e
,
155 _Tp
* __restrict__ __o
)
157 _Array_copy_ctor
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
158 _S_do_it(__b
, __e
, __o
);
161 // copy-construct raw array [__o, *) from strided array __a[<__n : __s>]
162 template<typename _Tp
>
164 __valarray_copy_construct (const _Tp
* __restrict__ __a
, size_t __n
,
165 size_t __s
, _Tp
* __restrict__ __o
)
167 if (__is_fundamental
<_Tp
>::_M_type
)
168 while (__n
--) { *__o
++ = *__a
; __a
+= __s
; }
170 while (__n
--) { new(__o
++) _Tp(*__a
); __a
+= __s
; }
173 // copy-construct raw array [__o, *) from indexed array __a[__i[<__n>]]
174 template<typename _Tp
>
176 __valarray_copy_construct (const _Tp
* __restrict__ __a
,
177 const size_t* __restrict__ __i
,
178 _Tp
* __restrict__ __o
, size_t __n
)
180 if (__is_fundamental
<_Tp
>::_M_type
)
181 while (__n
--) *__o
++ = __a
[*__i
++];
183 while (__n
--) new (__o
++) _Tp(__a
[*__i
++]);
186 // Do the necessary cleanup when we're done with arrays.
187 template<typename _Tp
>
189 __valarray_destroy_elements(_Tp
* __restrict__ __b
, _Tp
* __restrict__ __e
)
191 if (!__is_fundamental
<_Tp
>::_M_type
)
192 while (__b
!= __e
) { __b
->~_Tp(); ++__b
; }
195 // fill plain array __a[<__n>] with __t
196 template<typename _Tp
>
198 __valarray_fill (_Tp
* __restrict__ __a
, size_t __n
, const _Tp
& __t
)
199 { while (__n
--) *__a
++ = __t
; }
201 // fill strided array __a[<__n-1 : __s>] with __t
202 template<typename _Tp
>
204 __valarray_fill (_Tp
* __restrict__ __a
, size_t __n
,
205 size_t __s
, const _Tp
& __t
)
206 { for (size_t __i
=0; __i
<__n
; ++__i
, __a
+=__s
) *__a
= __t
; }
208 // fill indir ect array __a[__i[<__n>]] with __i
209 template<typename _Tp
>
211 __valarray_fill(_Tp
* __restrict__ __a
, const size_t* __restrict__ __i
,
212 size_t __n
, const _Tp
& __t
)
213 { for (size_t __j
=0; __j
<__n
; ++__j
, ++__i
) __a
[*__i
] = __t
; }
215 // copy plain array __a[<__n>] in __b[<__n>]
216 // For non-fundamental types, it is wrong to say 'memcpy()'
217 template<typename _Tp
, bool>
221 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
222 { while (__n
--) *__b
++ = *__a
++; }
225 template<typename _Tp
>
226 struct _Array_copier
<_Tp
, true>
229 _S_do_it(const _Tp
* __restrict__ __a
, size_t __n
, _Tp
* __restrict__ __b
)
230 { memcpy (__b
, __a
, __n
* sizeof (_Tp
)); }
233 template<typename _Tp
>
235 __valarray_copy (const _Tp
* __restrict__ __a
, size_t __n
,
236 _Tp
* __restrict__ __b
)
238 _Array_copier
<_Tp
, __is_fundamental
<_Tp
>::_M_type
>::
239 _S_do_it(__a
, __n
, __b
);
242 // copy strided array __a[<__n : __s>] in plain __b[<__n>]
243 template<typename _Tp
>
245 __valarray_copy (const _Tp
* __restrict__ __a
, size_t __n
, size_t __s
,
246 _Tp
* __restrict__ __b
)
247 { for (size_t __i
=0; __i
<__n
; ++__i
, ++__b
, __a
+= __s
) *__b
= *__a
; }
249 // copy plain __a[<__n>] in strided __b[<__n : __s>]
250 template<typename _Tp
>
252 __valarray_copy (const _Tp
* __restrict__ __a
, _Tp
* __restrict__ __b
,
253 size_t __n
, size_t __s
)
254 { for (size_t __i
=0; __i
<__n
; ++__i
, ++__a
, __b
+=__s
) *__b
= *__a
; }
256 // copy indexed __a[__i[<__n>]] in plain __b[<__n>]
257 template<typename _Tp
>
259 __valarray_copy (const _Tp
* __restrict__ __a
,
260 const size_t* __restrict__ __i
,
261 _Tp
* __restrict__ __b
, size_t __n
)
262 { for (size_t __j
=0; __j
<__n
; ++__j
, ++__b
, ++__i
) *__b
= __a
[*__i
]; }
264 // copy plain __a[<__n>] in indexed __b[__i[<__n>]]
265 template<typename _Tp
>
267 __valarray_copy (const _Tp
* __restrict__ __a
, size_t __n
,
268 _Tp
* __restrict__ __b
, const size_t* __restrict__ __i
)
269 { for (size_t __j
=0; __j
<__n
; ++__j
, ++__a
, ++__i
) __b
[*__i
] = *__a
; }
273 // Compute the sum of elements in range [__f, __l)
274 // This is a naive algorithm. It suffers from cancelling.
275 // In the future try to specialize
276 // for _Tp = float, double, long double using a more accurate
279 template<typename _Tp
>
281 __valarray_sum(const _Tp
* __restrict__ __f
, const _Tp
* __restrict__ __l
)
284 while (__f
!= __l
) __r
+= *__f
++;
288 // Compute the product of all elements in range [__f, __l)
289 template<typename _Tp
>
291 __valarray_product(const _Tp
* __restrict__ __f
,
292 const _Tp
* __restrict__ __l
)
295 while (__f
!= __l
) __r
= __r
* *__f
++;
299 // Compute the min/max of an array-expression
300 template<typename _Ta
>
301 inline typename
_Ta::value_type
302 __valarray_min(const _Ta
& __a
)
304 size_t __s
= __a
.size();
305 typedef typename
_Ta::value_type _Value_type
;
306 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
307 for (size_t __i
= 1; __i
< __s
; ++__i
)
309 _Value_type __t
= __a
[__i
];
316 template<typename _Ta
>
317 inline typename
_Ta::value_type
318 __valarray_max(const _Ta
& __a
)
320 size_t __s
= __a
.size();
321 typedef typename
_Ta::value_type _Value_type
;
322 _Value_type __r
= __s
== 0 ? _Value_type() : __a
[0];
323 for (size_t __i
= 1; __i
< __s
; ++__i
)
325 _Value_type __t
= __a
[__i
];
333 // Helper class _Array, first layer of valarray abstraction.
334 // All operations on valarray should be forwarded to this class
335 // whenever possible. -- gdr
338 template<typename _Tp
>
341 explicit _Array (size_t);
342 explicit _Array (_Tp
* const __restrict__
);
343 explicit _Array (const valarray
<_Tp
>&);
344 _Array (const _Tp
* __restrict__
, size_t);
348 _Tp
* const __restrict__ _M_data
;
351 template<typename _Tp
>
353 __valarray_fill (_Array
<_Tp
> __a
, size_t __n
, const _Tp
& __t
)
354 { __valarray_fill (__a
._M_data
, __n
, __t
); }
356 template<typename _Tp
>
358 __valarray_fill (_Array
<_Tp
> __a
, size_t __n
, size_t __s
, const _Tp
& __t
)
359 { __valarray_fill (__a
._M_data
, __n
, __s
, __t
); }
361 template<typename _Tp
>
363 __valarray_fill (_Array
<_Tp
> __a
, _Array
<size_t> __i
,
364 size_t __n
, const _Tp
& __t
)
365 { __valarray_fill (__a
._M_data
, __i
._M_data
, __n
, __t
); }
367 template<typename _Tp
>
369 __valarray_copy (_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
)
370 { __valarray_copy (__a
._M_data
, __n
, __b
._M_data
); }
372 template<typename _Tp
>
374 __valarray_copy (_Array
<_Tp
> __a
, size_t __n
, size_t __s
, _Array
<_Tp
> __b
)
375 { __valarray_copy(__a
._M_data
, __n
, __s
, __b
._M_data
); }
377 template<typename _Tp
>
379 __valarray_copy (_Array
<_Tp
> __a
, _Array
<_Tp
> __b
, size_t __n
, size_t __s
)
380 { __valarray_copy (__a
._M_data
, __b
._M_data
, __n
, __s
); }
382 template<typename _Tp
>
384 __valarray_copy (_Array
<_Tp
> __a
, _Array
<size_t> __i
,
385 _Array
<_Tp
> __b
, size_t __n
)
386 { __valarray_copy (__a
._M_data
, __i
._M_data
, __b
._M_data
, __n
); }
388 template<typename _Tp
>
390 __valarray_copy (_Array
<_Tp
> __a
, size_t __n
, _Array
<_Tp
> __b
,
392 { __valarray_copy (__a
._M_data
, __n
, __b
._M_data
, __i
._M_data
); }
394 template<typename _Tp
>
396 _Array
<_Tp
>::_Array (size_t __n
)
397 : _M_data(__valarray_get_storage
<_Tp
>(__n
))
398 { __valarray_default_construct(_M_data
, _M_data
+ __n
); }
400 template<typename _Tp
>
402 _Array
<_Tp
>::_Array (_Tp
* const __restrict__ __p
) : _M_data (__p
) {}
404 template<typename _Tp
>
405 inline _Array
<_Tp
>::_Array (const valarray
<_Tp
>& __v
)
406 : _M_data (__v
._M_data
) {}
408 template<typename _Tp
>
410 _Array
<_Tp
>::_Array (const _Tp
* __restrict__ __b
, size_t __s
)
411 : _M_data(__valarray_get_storage
<_Tp
>(__s
))
412 { __valarray_copy_construct(__b
, __s
, _M_data
); }
414 template<typename _Tp
>
416 _Array
<_Tp
>::begin () const
419 #define _DEFINE_ARRAY_FUNCTION(_Op, _Name) \
420 template<typename _Tp> \
422 _Array_augmented_##_Name (_Array<_Tp> __a, size_t __n, const _Tp& __t) \
424 for (_Tp* __p=__a._M_data; __p<__a._M_data+__n; ++__p) \
428 template<typename _Tp> \
430 _Array_augmented_##_Name (_Array<_Tp> __a, size_t __n, _Array<_Tp> __b) \
432 _Tp* __p = __a._M_data; \
433 for (_Tp* __q=__b._M_data; __q<__b._M_data+__n; ++__p, ++__q) \
437 template<typename _Tp, class _Dom> \
439 _Array_augmented_##_Name (_Array<_Tp> __a, \
440 const _Expr<_Dom,_Tp>& __e, size_t __n) \
442 _Tp* __p (__a._M_data); \
443 for (size_t __i=0; __i<__n; ++__i, ++__p) *__p _Op##= __e[__i]; \
446 template<typename _Tp> \
448 _Array_augmented_##_Name (_Array<_Tp> __a, size_t __n, size_t __s, \
451 _Tp* __q (__b._M_data); \
452 for (_Tp* __p=__a._M_data; __p<__a._M_data+__s*__n; __p+=__s, ++__q) \
456 template<typename _Tp> \
458 _Array_augmented_##_Name (_Array<_Tp> __a, _Array<_Tp> __b, \
459 size_t __n, size_t __s) \
461 _Tp* __q (__b._M_data); \
462 for (_Tp* __p=__a._M_data; __p<__a._M_data+__n; ++__p, __q+=__s) \
466 template<typename _Tp, class _Dom> \
468 _Array_augmented_##_Name (_Array<_Tp> __a, size_t __s, \
469 const _Expr<_Dom,_Tp>& __e, size_t __n) \
471 _Tp* __p (__a._M_data); \
472 for (size_t __i=0; __i<__n; ++__i, __p+=__s) *__p _Op##= __e[__i]; \
475 template<typename _Tp> \
477 _Array_augmented_##_Name (_Array<_Tp> __a, _Array<size_t> __i, \
478 _Array<_Tp> __b, size_t __n) \
480 _Tp* __q (__b._M_data); \
481 for (size_t* __j=__i._M_data; __j<__i._M_data+__n; ++__j, ++__q) \
482 __a._M_data[*__j] _Op##= *__q; \
485 template<typename _Tp> \
487 _Array_augmented_##_Name (_Array<_Tp> __a, size_t __n, \
488 _Array<_Tp> __b, _Array<size_t> __i) \
490 _Tp* __p (__a._M_data); \
491 for (size_t* __j=__i._M_data; __j<__i._M_data+__n; ++__j, ++__p) \
492 *__p _Op##= __b._M_data[*__j]; \
495 template<typename _Tp, class _Dom> \
497 _Array_augmented_##_Name (_Array<_Tp> __a, _Array<size_t> __i, \
498 const _Expr<_Dom, _Tp>& __e, size_t __n) \
500 size_t* __j (__i._M_data); \
501 for (size_t __k=0; __k<__n; ++__k, ++__j) \
502 __a._M_data[*__j] _Op##= __e[__k]; \
505 template<typename _Tp> \
507 _Array_augmented_##_Name (_Array<_Tp> __a, _Array<bool> __m, \
508 _Array<_Tp> __b, size_t __n) \
510 bool* ok (__m._M_data); \
511 _Tp* __p (__a._M_data); \
512 for (_Tp* __q=__b._M_data; __q<__b._M_data+__n; ++__q, ++ok, ++__p) { \
521 template<typename _Tp> \
523 _Array_augmented_##_Name (_Array<_Tp> __a, size_t __n, \
524 _Array<_Tp> __b, _Array<bool> __m) \
526 bool* ok (__m._M_data); \
527 _Tp* __q (__b._M_data); \
528 for (_Tp* __p=__a._M_data; __p<__a._M_data+__n; ++__p, ++ok, ++__q) { \
537 template<typename _Tp, class _Dom> \
539 _Array_augmented_##_Name (_Array<_Tp> __a, _Array<bool> __m, \
540 const _Expr<_Dom, _Tp>& __e, size_t __n) \
542 bool* ok(__m._M_data); \
543 _Tp* __p (__a._M_data); \
544 for (size_t __i=0; __i<__n; ++__i, ++ok, ++__p) { \
549 *__p _Op##= __e[__i]; \
553 _DEFINE_ARRAY_FUNCTION(+, plus
)
554 _DEFINE_ARRAY_FUNCTION(-, minus
)
555 _DEFINE_ARRAY_FUNCTION(*, multiplies
)
556 _DEFINE_ARRAY_FUNCTION(/, divides
)
557 _DEFINE_ARRAY_FUNCTION(%, modulus
)
558 _DEFINE_ARRAY_FUNCTION(^, xor)
559 _DEFINE_ARRAY_FUNCTION(|, or)
560 _DEFINE_ARRAY_FUNCTION(&, and)
561 _DEFINE_ARRAY_FUNCTION(<<, shift_left
)
562 _DEFINE_ARRAY_FUNCTION(>>, shift_right
)
564 #undef _DEFINE_VALARRAY_FUNCTION
568 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
570 # include <bits/valarray_array.tcc>
573 #endif /* _CPP_BITS_ARRAY_H */