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