File: convolve9_cpp.cpp

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-

// this version expands convolve8_cpp by making Vec mimic the structure of
// NumericVector. It peforms well, so this is is not the structure of
// NumericVector that is the problem. So what is it then ?
//
// could it be because NumericVector is in a different library than
// this code, so that operator[] is not inlined ?
//
// clues:
// - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3538.html

#include <Rcpp.h>

class Cache{
public:
    typedef double& proxy ;
    typedef double* iterator ;

    Cache( iterator data_) : data(data_){}

    inline proxy ref(int i){ return data[i] ; }
    inline proxy ref(int i) const { return data[i] ; }

private:
    iterator data ;
} ;

class Vec {
public:
    typedef double& proxy ;

    Vec( double* data_ ) : cache(data_){}
    inline proxy operator[]( int i){ return cache.ref(i) ; }
    inline proxy operator[]( int i) const { return cache.ref(i) ; }

private:
    Cache cache ;
} ;


RcppExport SEXP convolve9cpp(SEXP a, SEXP b){
    Rcpp::NumericVector xa(a);
    Rcpp::NumericVector xb(b);
    int n_xa = xa.size() ;
    int n_xb = xb.size() ;
    int nab = n_xa + n_xb - 1;
    Rcpp::NumericVector xab(nab);

    Vec vab(xab.begin()), va(xa.begin()), vb(xb.begin()) ;

    for (int i = 0; i < n_xa; i++)
        for (int j = 0; j < n_xb; j++)
            vab[i + j] += va[i] * vb[j];

    return xab ;
}

#include "loopmacro.h"
LOOPMACRO_CPP(convolve9cpp)