-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.h
128 lines (109 loc) · 3.15 KB
/
helper_functions.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef __HELPER_FUNCTIONS__
#define __HELPER_FUNCTIONS__
#include <iostream>
#include <vector>
#include <string>
#include <Rcpp.h>
//' Retrieves the indexes of the rows that hold a specific value
//' in a specified column
inline std::vector<int> getRowIndexesForQuery(
std::vector<std::vector<std::string>> mat,
int col_of_interest,
std::string query)
{
std::vector<int> rowIndexes;
for (int i = 0; i < mat.size(); ++i)
{
std::vector<std::string> row = mat[i];
if (row[col_of_interest] == query)
{
rowIndexes.push_back(i);
}
}
return rowIndexes;
}
//' Takes an integer vector and translates this to a Rcpp::IntegerVector
//' object so that it can be returned to R
inline Rcpp::IntegerVector getIntVecR(
std::vector<int> cppVec)
{
Rcpp::IntegerVector rVec;
for (int i = 0; i < cppVec.size(); ++i)
{
rVec.push_back(cppVec[i]);
}
return rVec;
}
//' Takes a vector of vectors object (type string) and converts this
//' into the R equivalent (Rcpp::StringMatrix)
inline Rcpp::StringMatrix getStrMatrixR(
std::vector<std::vector<std::string>> cppMat)
{
int numRows = cppMat.size();
int numCols = cppMat[0].size();
if (numRows == 0 && numCols == 0)
{
return R_NilValue;
}
Rcpp::StringMatrix rMat(numRows, numCols);
for (int i = 0; i < numRows; ++i)
{ // loop rows
for (int j = 0; j < numCols; ++j)
{
rMat(i, j) = cppMat[i][j]; // set cell of matrix to correct value
}
}
return rMat;
}
//' Takes a vector of vectors object (type double) and converts this
//' into the R equivalent (Rcpp::NumericMatrix)
inline Rcpp::NumericMatrix getNumericMatrixR(
std::vector<std::vector<double>> cppMat)
{
int numRows = cppMat.size();
int numCols = cppMat[0].size();
Rcpp::NumericMatrix rMat(numRows, numCols);
for (int i = 0; i < numRows; ++i)
{ // loop rows
for (int j = 0; j < numCols; ++j)
{
rMat(i, j) = cppMat[i][j]; // set cell of matrix to correct value
}
}
return rMat;
}
//' Takes a Rcpp::StringMatrix object and and converts this to the C++
//' equivalent (a vector of vectors of type string)
inline std::vector<std::vector<std::string>> getStrMatrixCpp(
Rcpp::StringMatrix rMat)
{
int nrow = rMat.nrow();
int ncol = rMat.ncol();
std::vector<std::vector<std::string>> cppMat(nrow, std::vector<std::string>(ncol, ""));
for (int i = 0; i < nrow; ++i)
{
for (int j = 0; j < ncol; ++j)
{
cppMat[i][j] = rMat(i, j);
}
}
return cppMat;
}
//' Takes a Rcpp::NumericMatrix object and and converts this to the C++
//' equivalent (a vector of vectors of type double)
inline std::vector<std::vector<double>> getDoubleMatrixCpp(
Rcpp::NumericMatrix rMat)
{
int nrow = rMat.nrow();
int ncol = rMat.ncol();
std::vector<std::vector<double>> cppMat(nrow, std::vector<double>(ncol, 0.0));
for (int i = 0; i < nrow; ++i)
{
for (int j = 0; j < ncol; ++j)
{
cppMat[i][j] = rMat(i, j);
}
}
return cppMat;
}
#endif // __HELPER_FUNCTIONS__