Rcpp Version 0.9.10
convolve9_cpp.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 
00003 // this version expands convolve8_cpp by making Vec mimic the structure of 
00004 // NumericVector. It peforms well, so this is is not the structure of 
00005 // NumericVector that is the problem. So what is it then ?
00006 //
00007 // could it be because NumericVector is in a different library than 
00008 // this code, so that operator[] is not inlined ?
00009 // 
00010 // clues: 
00011 // - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3538.html
00012 
00013 #include <Rcpp.h>
00014 
00015 class Cache{
00016 public:
00017     typedef double& proxy ;
00018     typedef double* iterator ;
00019     
00020     Cache( iterator data_) : data(data_){}
00021     
00022     inline proxy ref(int i){ return data[i] ; }
00023     inline proxy ref(int i) const { return data[i] ; }
00024             
00025 private:    
00026     iterator data ;
00027 } ;
00028 
00029 class Vec {
00030 public:
00031     typedef double& proxy ;
00032     
00033     Vec( double* data_ ) : cache(data_){}
00034     inline proxy operator[]( int i){ return cache.ref(i) ; }
00035     inline proxy operator[]( int i) const { return cache.ref(i) ; }
00036     
00037 private:
00038     Cache cache ;
00039 } ;
00040 
00041 
00042 RcppExport SEXP convolve9cpp(SEXP a, SEXP b){
00043     Rcpp::NumericVector xa(a);
00044     Rcpp::NumericVector xb(b);
00045     int n_xa = xa.size() ;
00046     int n_xb = xb.size() ;
00047     int nab = n_xa + n_xb - 1;
00048     Rcpp::NumericVector xab(nab);
00049     
00050     Vec vab(xab.begin()), va(xa.begin()), vb(xb.begin()) ;
00051     
00052     for (int i = 0; i < n_xa; i++)
00053         for (int j = 0; j < n_xb; j++) 
00054             vab[i + j] += va[i] * vb[j];
00055 
00056     return xab ;
00057 }
00058 
00059 #include "loopmacro.h"
00060 LOOPMACRO_CPP(convolve9cpp)
00061 
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines