28 #ifndef REMORA_KERNELS_DEFAULT_VECTOR_ASSIGN_HPP 29 #define REMORA_KERNELS_DEFAULT_VECTOR_ASSIGN_HPP 31 #include "../../expression_types.hpp" 33 namespace remora{
namespace bindings{
35 template<
class F,
class V>
36 void assign(vector_expression<V, cpu_tag>& v,
typename V::value_type t) {
38 typedef typename V::iterator iterator;
39 iterator end = v().end();
40 for (iterator it = v().begin(); it != end; ++it){
50 template<
class V,
class E>
52 vector_expression<V, cpu_tag>& v, vector_expression<E, cpu_tag>
const& e,
55 for(std::size_t i = 0; i != v().size(); ++i){
56 v()(i) = static_cast<typename V::value_type>(e()(i));
60 template<
class V,
class E>
62 vector_expression<V, cpu_tag>& v, vector_expression<E, cpu_tag>
const& e,
65 typedef typename E::const_iterator EIterator;
66 typedef typename V::value_type value_type;
67 EIterator eiter = e.begin();
68 EIterator eend = e.end();
75 EIterator viter = v.begin();
76 EIterator vend = v.end();
79 for(;viter.index() != eiter.index(); ++viter){
83 for(;eiter != eend; ++eiter,++viter){
87 for(;viter!= vend; ++viter){
93 template<
class V,
class E>
95 vector_expression<V, cpu_tag>& v, vector_expression<E, cpu_tag>
const& e,
96 packed_tag, packed_tag
98 typedef typename E::const_iterator EIterator;
99 EIterator eiter = e.begin();
100 EIterator eend = e.end();
107 EIterator viter = v.begin();
108 EIterator vend = v.end();
111 REMORA_SIZE_CHECK(vend-viter);
113 REMORA_SIZE_CHECK(viter.index() <= eiter.index());
114 REMORA_SIZE_CHECK(viter.index()+(vend-viter) >= eiter.index()+(eend-eiter));
117 viter += eiter.index()-viter.index();
118 for(;eiter != eend; ++eiter,++viter){
124 template<
class V,
class E>
126 vector_expression<V, cpu_tag>& v,
127 vector_expression<E, cpu_tag>
const& e,
132 typedef typename E::const_iterator iterator;
133 iterator end = e().end();
134 for(iterator it = e().begin(); it != end; ++it){
135 v()(it.index()) = *it;
139 template<
class V,
class E>
141 vector_expression<V, cpu_tag>& v,
142 vector_expression<E, cpu_tag>
const& e,
147 v().reserve(e().size());
148 typename V::iterator targetPos = v().begin();
149 REMORA_RANGE_CHECK(targetPos == v().end());
150 for(std::size_t i = 0; i != e().size(); ++i,++targetPos){
151 targetPos = v().set_element(targetPos,i,e()(i));
155 template<
class V,
class E>
157 vector_expression<V, cpu_tag>& v,
158 vector_expression<E, cpu_tag>
const& e,
163 typedef typename E::const_iterator iteratorE;
164 typename V::iterator targetPos = v().begin();
165 REMORA_RANGE_CHECK(targetPos == v().end());
166 iteratorE end = e().end();
167 for(iteratorE it = e().begin(); it != end; ++it,++targetPos){
168 targetPos = v().set_element(targetPos,it.index(),*it);
177 template<
class V,
class E,
class F>
178 void vector_assign_functor(
179 vector_expression<V, cpu_tag>& v,
180 vector_expression<E, cpu_tag>
const& e,
184 for(std::size_t i = 0; i != v().size(); ++i){
185 v()(i) = f(v()(i),e()(i));
190 template<
class V,
class E,
class F>
191 void vector_assign_functor(
192 vector_expression<V, cpu_tag>& v,
193 vector_expression<E, cpu_tag>
const& e,
195 dense_tag, packed_tag
197 typedef typename E::const_iterator EIterator;
198 typedef typename V::const_iterator VIterator;
199 typedef typename V::value_type value_type;
200 EIterator eiter = e().begin();
201 EIterator eend = e().end();
202 VIterator viter = v().begin();
203 VIterator vend = v().end();
207 for(;viter.index() != eiter.index() &&!F::right_zero_identity; ++viter){
208 *viter = f(*viter,value_type());
211 for(;eiter != eend; ++eiter,++viter){
212 *viter = f(*viter,*eiter);
216 for(;viter!= vend &&!F::right_zero_identity; ++viter){
217 *viter= value_type();
222 template<
class V,
class E,
class F>
223 void vector_assign_functor(
224 vector_expression<V, cpu_tag>& v,
225 vector_expression<E, cpu_tag>
const& e,
227 packed_tag, packed_tag
229 typedef typename E::const_iterator EIterator;
230 typedef typename V::const_iterator VIterator;
231 typedef typename V::value_type value_type;
232 EIterator eiter = e().begin();
233 EIterator eend = e().end();
234 VIterator viter = v().begin();
235 VIterator vend = v().end();
241 REMORA_SIZE_CHECK(vend-viter);
243 REMORA_SIZE_CHECK(viter.index() <= eiter.index());
244 REMORA_SIZE_CHECK(viter.index()+(vend-viter) >= eiter.index()+(eend-eiter));
247 for(;viter.index() != eiter.index() &&!F::right_zero_identity; ++viter){
248 *viter = f(*viter,value_type());
251 for(;eiter != eend; ++eiter,++viter){
252 *viter = f(*viter,*eiter);
256 for(;viter!= vend &&!F::right_zero_identity; ++viter){
257 *viter= f(*viter,value_type());
262 template<
class V,
class E,
class F>
263 void vector_assign_functor(
264 vector_expression<V, cpu_tag>& v,
265 vector_expression<E, cpu_tag>
const& e,
267 dense_tag, sparse_tag
269 typedef typename E::const_iterator iterator;
270 iterator end = e().end();
271 for(iterator it = e().begin(); it != end; ++it){
272 v()(it.index()) = f(v()(it.index()),*it);
277 template<
class V,
class E,
class F>
278 void vector_assign_functor(
279 vector_expression<V, cpu_tag>& v,
280 vector_expression<E, cpu_tag>
const& e,
282 sparse_tag
tag, dense_tag
284 typedef typename V::value_type value_type;
285 typedef typename V::size_type size_type;
286 value_type zero = value_type();
287 size_type size = e().size();
289 typename V::iterator it = v().begin();
290 for(size_type i = 0; i != size; ++i,++it){
291 if(it == v().end() || it.index() != i){
292 it = v().set_element(it,i,zero);
294 *it = f(*it, e()(i));
308 template<
class V,
class E,
class F>
310 vector_expression<V, cpu_tag>& v,
311 vector_expression<E, cpu_tag>
const& e,
314 typedef typename V::value_type value_type;
315 typedef typename V::size_type size_type;
316 value_type zero = value_type();
318 typename V::iterator it = v().begin();
319 typename E::const_iterator ite = e().begin();
320 typename E::const_iterator ite_end = e().end();
321 while(it != v().end() && ite != ite_end) {
322 size_type it_index = it.index();
323 size_type ite_index = ite.index();
324 if (it_index == ite_index) {
327 }
else if (it_index < ite_index) {
330 it = v().set_element(it,ite_index,zero);
337 for(;it != v().end();++it) {
341 for(;ite != ite_end;++it,++ite) {
342 it = v().set_element(it,ite.index(),zero);
347 template<
class V,
class E,
class F>
348 void vector_assign_functor(
349 vector_expression<V, cpu_tag>& v,
350 vector_container<E, cpu_tag>
const& e,
352 sparse_tag tag, sparse_tag
356 template<
class V,
class E,
class F>
357 void vector_assign_functor(
358 vector_container<V, cpu_tag>& v,
359 vector_expression<E, cpu_tag>
const& e,
361 sparse_tag tag, sparse_tag
363 assign_sparse(v,e,f);
365 template<
class V,
class E,
class F>
366 void vector_assign_functor(
367 vector_container<V, cpu_tag>& v,
368 vector_container<E, cpu_tag>
const& e,
370 sparse_tag tag, sparse_tag
372 assign_sparse(v,e,f);
380 template<
class V,
class E,
class F>
381 void vector_assign_functor(
382 vector_expression<V, cpu_tag>& v,
383 vector_expression<E, cpu_tag>
const& e,
385 sparse_tag tag, sparse_tag
387 typename vector_temporary<V>::type temporary(v());
388 assign_sparse(temporary,e, f);
390 bindings::vector_assign(v, temporary, tag, tag);