-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpptest.cpp
More file actions
83 lines (72 loc) · 1.96 KB
/
cpptest.cpp
File metadata and controls
83 lines (72 loc) · 1.96 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "cpptest.h"
#include "read_array.h"
typedef boost::multi_array<double, 3> array_type;
// [[Rcpp::export]]
double add(double a, double b) {
return a + b;
}
// [[Rcpp::export]]
double do_double(double x) {
doubler y = doubler(x);
return y.run();
}
// [[Rcpp::export]]
std::vector<double> modify_std_vector(std::vector<double> vector) {
vector[2] = 25;
return vector;
}
// // [[Rcpp::export]]
// int computeGCD(int a, int b) {
// return boost::integer::gcd(a, b);
// }
// boost::multi_array<double, 3> modify_3d_array(boost::multi_array<double, 3> array) {
// std::list< std::vector<double> >::iterator it = list.begin();
// std::advance(it, 2);
// it = vec;
// return list;
// }
// [[Rcpp::export]]
void get_array() {
array_type A(boost::extents[3][4][2]);
A[0][0][0] = 1;
A[0][0][1] = 2;
A[0][1][0] = 3;
A[0][1][1] = 4;
A[0][2][0] = 5;
A[0][2][1] = 6;
A[0][3][0] = 7;
A[0][3][1] = 8;
A[1][0][0] = 9;
Rcpp::Rcout << "hi" << A.origin() << "\n";
}
// [[Rcpp::export]]
std::vector<double> push_array(std::vector<double> arr) {
array_3d A = read_3d_array(arr, 3, 4, 2);
Rcpp::Rcout << "hi" << A.origin() << "\n";
std::vector<double> ret(3 * 4 * 2);
// std::copy would be nicer but the iterators don't want to work for us.
double * data = A.data();
for (size_t i = 0; i < A.num_elements(); i++) {
ret[i] = data[i];
}
ret[12] = 55;
return ret;
}
// [[Rcpp::export]]
std::list< std::vector<double> > push_list_arrays(std::list< std::vector<double> > lst) {
std::list<array_3d> arrays = read_arrays_list(lst);
// return back
std::list< std::vector<double> > ret;
std::vector<double> ret_array(3 * 4 * 2);
for (std::list<array_3d>::iterator it = arrays.begin(); it != arrays.end(); it++) {
double * data = (*it).data();
for (size_t j = 0; j < (*it).num_elements(); j++) {
ret_array[j] = data[j];
if (j == 12) {
ret_array[j] = 55;
}
}
ret.push_back(ret_array);
}
return ret;
}