xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/bits/valarray_array.tcc (revision 07ece4eabb6d327c320416d49d51617a7c0fb3be)
1 // The template and inlines for the -*- C++ -*- internal _Array helper class.
2 
3 // Copyright (C) 1997, 1998, 1999, 2003, 2005, 2009 Free Software Foundation, Inc.
4 //
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 3, or (at your option)
9 // any later version.
10 
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.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file valarray_array.tcc
26  *  This is an internal header file, included by other library headers.
27  *  You should not attempt to use it directly.
28  */
29 
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31 
32 #ifndef _VALARRAY_ARRAY_TCC
33 #define _VALARRAY_ARRAY_TCC 1
34 
35 _GLIBCXX_BEGIN_NAMESPACE(std)
36 
37   template<typename _Tp>
38     void
39     __valarray_fill(_Array<_Tp> __a, size_t __n, _Array<bool> __m,
40 		    const _Tp& __t)
41     {
42       _Tp* __p = __a._M_data;
43       bool* __ok (__m._M_data);
44       for (size_t __i=0; __i < __n; ++__i, ++__ok, ++__p)
45 	{
46 	  while (!*__ok)
47 	  {
48 	    ++__ok;
49 	    ++__p;
50 	  }
51 	  *__p = __t;
52 	}
53     }
54 
55   // Copy n elements of a into consecutive elements of b.  When m is
56   // false, the corresponding element of a is skipped.  m must contain
57   // at least n true elements.  a must contain at least n elements and
58   // enough elements to match up with m through the nth true element
59   // of m.  I.e.  if n is 10, m has 15 elements with 5 false followed
60   // by 10 true, a must have 15 elements.
61   template<typename _Tp>
62     void
63     __valarray_copy(_Array<_Tp> __a, _Array<bool> __m, _Array<_Tp> __b,
64 		    size_t __n)
65     {
66       _Tp* __p (__a._M_data);
67       bool* __ok (__m._M_data);
68       for (_Tp* __q = __b._M_data; __q < __b._M_data + __n;
69 	   ++__q, ++__ok, ++__p)
70 	{
71 	  while (! *__ok)
72 	    {
73 	      ++__ok;
74 	      ++__p;
75 	    }
76 	  *__q = *__p;
77 	}
78     }
79 
80   // Copy n consecutive elements from a into elements of b.  Elements
81   // of b are skipped if the corresponding element of m is false.  m
82   // must contain at least n true elements.  b must have at least as
83   // many elements as the index of the nth true element of m.  I.e. if
84   // m has 15 elements with 5 false followed by 10 true, b must have
85   // at least 15 elements.
86   template<typename _Tp>
87     void
88     __valarray_copy(_Array<_Tp> __a, size_t __n, _Array<_Tp> __b,
89 		    _Array<bool> __m)
90     {
91       _Tp* __q (__b._M_data);
92       bool* __ok (__m._M_data);
93       for (_Tp* __p = __a._M_data; __p < __a._M_data+__n;
94 	   ++__p, ++__ok, ++__q)
95 	{
96 	  while (! *__ok)
97 	    {
98 	      ++__ok;
99 	      ++__q;
100 	    }
101 	  *__q = *__p;
102 	}
103     }
104 
105   // Copy n elements from a into elements of b.  Elements of a are
106   // skipped if the corresponding element of m is false.  Elements of
107   // b are skipped if the corresponding element of k is false.  m and
108   // k must contain at least n true elements.  a and b must have at
109   // least as many elements as the index of the nth true element of m.
110   template<typename _Tp>
111     void
112     __valarray_copy(_Array<_Tp> __a, _Array<bool> __m, size_t __n,
113 		    _Array<_Tp> __b, _Array<bool> __k)
114     {
115       _Tp* __p (__a._M_data);
116       _Tp* __q (__b._M_data);
117       bool* __srcok (__m._M_data);
118       bool* __dstok (__k._M_data);
119       for (size_t __i = 0; __i < __n;
120 	   ++__srcok, ++__p, ++__dstok, ++__q, ++__i)
121 	{
122 	  while (! *__srcok)
123 	    {
124 	      ++__srcok;
125 	      ++__p;
126 	    }
127 	  while (! *__dstok)
128 	    {
129 	      ++__dstok;
130 	      ++__q;
131 	    }
132 	  *__q = *__p;
133 	}
134     }
135 
136   // Copy n consecutive elements of e into consecutive elements of a.
137   // I.e. a[i] = e[i].
138   template<typename _Tp, class _Dom>
139     void
140     __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n, _Array<_Tp> __a)
141     {
142       _Tp* __p (__a._M_data);
143       for (size_t __i = 0; __i < __n; ++__i, ++__p)
144 	*__p = __e[__i];
145     }
146 
147   // Copy n consecutive elements of e into elements of a using stride
148   // s.  I.e., a[0] = e[0], a[s] = e[1], a[2*s] = e[2].
149   template<typename _Tp, class _Dom>
150     void
151     __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n,
152 		     _Array<_Tp> __a, size_t __s)
153     {
154       _Tp* __p (__a._M_data);
155       for (size_t __i = 0; __i < __n; ++__i, __p += __s)
156 	*__p = __e[__i];
157     }
158 
159   // Copy n consecutive elements of e into elements of a indexed by
160   // contents of i.  I.e., a[i[0]] = e[0].
161   template<typename _Tp, class _Dom>
162     void
163     __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n,
164 		    _Array<_Tp> __a, _Array<size_t> __i)
165     {
166       size_t* __j (__i._M_data);
167       for (size_t __k = 0; __k < __n; ++__k, ++__j)
168 	__a._M_data[*__j] = __e[__k];
169     }
170 
171   // Copy n elements of e indexed by contents of f into elements of a
172   // indexed by contents of i.  I.e., a[i[0]] = e[f[0]].
173   template<typename _Tp>
174     void
175     __valarray_copy(_Array<_Tp> __e, _Array<size_t> __f,
176 		    size_t __n,
177 		    _Array<_Tp> __a, _Array<size_t> __i)
178     {
179       size_t* __g (__f._M_data);
180       size_t* __j (__i._M_data);
181       for (size_t __k = 0; __k < __n; ++__k, ++__j, ++__g)
182 	__a._M_data[*__j] = __e._M_data[*__g];
183     }
184 
185   // Copy n consecutive elements of e into elements of a.  Elements of
186   // a are skipped if the corresponding element of m is false.  m must
187   // have at least n true elements and a must have at least as many
188   // elements as the index of the nth true element of m.  I.e. if m
189   // has 5 false followed by 10 true elements and n == 10, a must have
190   // at least 15 elements.
191   template<typename _Tp, class _Dom>
192     void
193     __valarray_copy(const _Expr<_Dom, _Tp>& __e, size_t __n,
194 		    _Array<_Tp> __a, _Array<bool> __m)
195     {
196       bool* __ok (__m._M_data);
197       _Tp* __p (__a._M_data);
198       for (size_t __i = 0; __i < __n; ++__i, ++__ok, ++__p)
199 	{
200 	  while (! *__ok)
201 	    {
202 	      ++__ok;
203 	      ++__p;
204 	    }
205 	  *__p = __e[__i];
206 	}
207     }
208 
209 
210   template<typename _Tp, class _Dom>
211     void
212     __valarray_copy_construct(const _Expr<_Dom, _Tp>& __e, size_t __n,
213 			      _Array<_Tp> __a)
214     {
215       _Tp* __p (__a._M_data);
216       for (size_t __i = 0; __i < __n; ++__i, ++__p)
217 	new (__p) _Tp(__e[__i]);
218     }
219 
220 
221   template<typename _Tp>
222     void
223     __valarray_copy_construct(_Array<_Tp> __a, _Array<bool> __m,
224 			      _Array<_Tp> __b, size_t __n)
225     {
226       _Tp* __p (__a._M_data);
227       bool* __ok (__m._M_data);
228       for (_Tp* __q = __b._M_data; __q < __b._M_data+__n; ++__q, ++__ok, ++__p)
229 	{
230 	  while (! *__ok)
231 	    {
232 	      ++__ok;
233 	      ++__p;
234 	    }
235 	  new (__q) _Tp(*__p);
236 	}
237     }
238 
239 _GLIBCXX_END_NAMESPACE
240 
241 #endif /* _VALARRAY_ARRAY_TCC */
242