This can be done more generally. 

Following an earlier suggestion from Romain, we can use boost::tuple from the 
BH package - for a row of fixed size with general types. Then we can use a 
template to read in the data-frame and work with the set of rows.

Variadic templates would be nice here, rather than needing to enumerate for 
tuples of different lengths.

Out of interest, is this poor style for Rcpp?

Sincerely, Mark.

require(inline)
testReadDf <-
  rcpp(signature(df="data.frame"),
       includes="
#include <boost/tuple/tuple.hpp>
#include <vector>
#include <algorithm>
// general function to read a data-frame
template <class T1, class T2, class T3, class T4>
std::vector<boost::tuple<T1,T2,T3,T4> > read_df( DataFrame df ){
     typedef boost::tuple<T1,T2,T3,T4> Row;
     int n = df.nrows() ;
     std::vector<Row> rows(n) ;
     Vector<traits::r_sexptype_traits<T1>::rtype> df0 = df[0];
     Vector<traits::r_sexptype_traits<T2>::rtype> df1 = df[1];
     Vector<traits::r_sexptype_traits<T3>::rtype> df2 = df[2];
     Vector<traits::r_sexptype_traits<T4>::rtype> df3 = df[3];
     for( int i=0; i<n; i++)
         rows[i] = Row(df0[i],df1[i],df2[i],df3[i]);
     return rows ;
}
// example function
typedef boost::tuple<int,int,int,int> MyRow;
int fun(MyRow row) {
  return 
boost::get<0>(row)+2*boost::get<1>(row)+3*boost::get<2>(row)+4*boost::get<3>(row);
}
",
       body="
// read in the data-frame as a vector of rows
std::vector<MyRow> v = read_df<int,int,int,int>(df);
int n = v.size();
std::vector<int> out(n);
std::transform(v.begin(),v.end(),out.begin(),fun);
return wrap(out);
")
testReadDf(data.frame(1,2,3,4))
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to