28#ifndef REMORA_KERNELS_DEFAULT_VECTOR_ASSIGN_HPP
29#define REMORA_KERNELS_DEFAULT_VECTOR_ASSIGN_HPP
31#include "../../detail/traits.hpp"
33namespace remora{
namespace bindings{
35template<
class F,
class V>
36void apply(vector_expression<V, cpu_tag>& v, F
const& f) {
37 typedef typename V::iterator iterator;
38 iterator end = v().end();
39 for (iterator it = v().begin(); it != end; ++it){
44template<
class F,
class V>
45void assign(vector_expression<V, cpu_tag>& v,
typename V::value_type t) {
47 apply(v, [=](
typename V::value_type x){
return f(x,t);});
55template<
class V,
class E>
57 vector_expression<V, cpu_tag>& v, vector_expression<E, cpu_tag>
const& e,
60 for(std::size_t i = 0; i != v().size(); ++i){
61 v()(i) =
static_cast<typename V::value_type
>(e()(i));
65template<
class V,
class E>
67 vector_expression<V, cpu_tag>& v, vector_expression<E, cpu_tag>
const& e,
70 typedef typename E::const_iterator EIterator;
71 typedef typename V::value_type value_type;
72 EIterator eiter = e.begin();
73 EIterator eend = e.end();
80 EIterator viter = v.begin();
81 EIterator vend = v.end();
84 for(;viter.index() != eiter.index(); ++viter){
88 for(;eiter != eend; ++eiter,++viter){
92 for(;viter!= vend; ++viter){
98template<
class V,
class E>
100 vector_expression<V, cpu_tag>& v, vector_expression<E, cpu_tag>
const& e,
101 packed_tag, packed_tag
103 typedef typename E::const_iterator EIterator;
104 EIterator eiter = e.begin();
105 EIterator eend = e.end();
112 EIterator viter = v.begin();
113 EIterator vend = v.end();
116 REMORA_SIZE_CHECK(vend-viter);
118 REMORA_SIZE_CHECK(viter.index() <= eiter.index());
119 REMORA_SIZE_CHECK(viter.index()+(vend-viter) >= eiter.index()+(eend-eiter));
122 viter += eiter.index()-viter.index();
123 for(;eiter != eend; ++eiter,++viter){
129template<
class V,
class E>
131 vector_expression<V, cpu_tag>& v,
132 vector_expression<E, cpu_tag>
const& e,
137 typedef typename E::const_iterator iterator;
138 iterator end = e().end();
139 for(iterator it = e().begin(); it != end; ++it){
140 v()(it.index()) = *it;
144template<
class V,
class E>
146 vector_expression<V, cpu_tag>& v,
147 vector_expression<E, cpu_tag>
const& e,
152 v().reserve(e().size());
153 typename V::iterator targetPos = v().begin();
154 REMORA_RANGE_CHECK(targetPos == v().end());
155 for(std::size_t i = 0; i != e().size(); ++i){
156 targetPos = v().set_element(targetPos,i,e()(i));
160template<
class V,
class E>
162 vector_expression<V, cpu_tag>& v,
163 vector_expression<E, cpu_tag>
const& e,
168 typedef typename E::const_iterator iteratorE;
169 typename V::iterator targetPos = v().begin();
170 REMORA_RANGE_CHECK(targetPos == v().end());
171 iteratorE end = e().end();
172 for(iteratorE it = e().begin(); it != end; ++it){
173 targetPos = v().set_element(targetPos,it.index(),*it);
182template<
class V,
class E,
class F>
183void vector_assign_functor(
184 vector_expression<V, cpu_tag>& v,
185 vector_expression<E, cpu_tag>
const& e,
189 for(std::size_t i = 0; i != v().size(); ++i){
190 v()(i) = f(v()(i),e()(i));
195template<
class V,
class E,
class F>
196void vector_assign_functor(
197 vector_expression<V, cpu_tag>& v,
198 vector_expression<E, cpu_tag>
const& e,
200 dense_tag, packed_tag
202 typedef typename E::const_iterator EIterator;
203 typedef typename V::const_iterator VIterator;
204 typedef typename V::value_type value_type;
205 EIterator eiter = e().begin();
206 EIterator eend = e().end();
207 VIterator viter = v().begin();
208 VIterator vend = v().end();
212 for(;viter.index() != eiter.index() &&!F::right_zero_identity; ++viter){
213 *viter = f(*viter,value_type());
216 for(;eiter != eend; ++eiter,++viter){
217 *viter = f(*viter,*eiter);
221 for(;viter!= vend &&!F::right_zero_identity; ++viter){
222 *viter= value_type();
227template<
class V,
class E,
class F>
228void vector_assign_functor(
229 vector_expression<V, cpu_tag>& v,
230 vector_expression<E, cpu_tag>
const& e,
232 packed_tag, packed_tag
234 typedef typename E::const_iterator EIterator;
235 typedef typename V::const_iterator VIterator;
236 typedef typename V::value_type value_type;
237 EIterator eiter = e().begin();
238 EIterator eend = e().end();
239 VIterator viter = v().begin();
240 VIterator vend = v().end();
246 REMORA_SIZE_CHECK(vend-viter);
248 REMORA_SIZE_CHECK(viter.index() <= eiter.index());
249 REMORA_SIZE_CHECK(viter.index()+(vend-viter) >= eiter.index()+(eend-eiter));
252 for(;viter.index() != eiter.index() &&!F::right_zero_identity; ++viter){
253 *viter = f(*viter,value_type());
256 for(;eiter != eend; ++eiter,++viter){
257 *viter = f(*viter,*eiter);
261 for(;viter!= vend &&!F::right_zero_identity; ++viter){
262 *viter= f(*viter,value_type());
267template<
class V,
class E,
class F>
268void vector_assign_functor(
269 vector_expression<V, cpu_tag>& v,
270 vector_expression<E, cpu_tag>
const& e,
272 dense_tag, sparse_tag
274 typedef typename E::const_iterator iterator;
275 iterator end = e().end();
276 for(iterator it = e().begin(); it != end; ++it){
277 v()(it.index()) = f(v()(it.index()),*it);
282template<
class V,
class E,
class F>
283void vector_assign_functor(
284 vector_expression<V, cpu_tag>& v,
285 vector_expression<E, cpu_tag>
const& e,
287 sparse_tag
tag, dense_tag
289 typedef typename V::size_type size_type;
290 size_type size = e().size();
291 auto it = v().begin();
292 for(size_type i = 0; i != size; ++i){
294 if(it == v().end() || it.index() != i){
295 it = v().set_element(it,i,val);
303template<
class V,
class E,
class F>
304void vector_assign_functor(
305 vector_expression<V, cpu_tag>& v,
306 vector_expression<E, cpu_tag>
const& e,
308 sparse_tag
tag, sparse_tag
310 typedef typename V::value_type value_type;
311 typedef typename V::size_type size_type;
312 value_type zero = value_type();
314 typename V::iterator it = v().begin();
315 typename E::const_iterator ite = e().begin();
316 typename E::const_iterator ite_end = e().end();
317 while(it != v().end() && ite != ite_end) {
318 size_type it_index = it.index();
319 size_type ite_index = ite.index();
320 if (it_index == ite_index) {
324 }
else if (it_index < ite_index) {
328 it = v().set_element(it,ite_index,f(zero, *ite));
334 for(;it != v().end();++it) {
338 for(;ite != ite_end;++it,++ite) {
339 it = v().set_element(it,ite.index(),zero);