-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.cpp
138 lines (132 loc) · 4.18 KB
/
utils.cpp
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
129
130
131
132
133
134
135
136
137
138
#include "utils.h"
#include <Rcpp.h>
ampl::Tuple list2tuple(Rcpp::List list) {
int p = 0;
ampl::Variant arguments[list.size()];
for(Rcpp::List::iterator it = list.begin(); it != list.end(); it++) {
switch(TYPEOF(*it)) {
case REALSXP:
arguments[p++] = ampl::Variant(Rcpp::as<double>(*it));
break;
case INTSXP:
arguments[p++] = ampl::Variant(Rcpp::as<int>(*it));
break;
case STRSXP:
arguments[p++] = ampl::Variant(Rcpp::as<std::string>(*it));
break;
default:
Rcpp::stop("only accepts lists containing numbers and strings");
}
}
return ampl::Tuple(arguments, list.size());
}
Rcpp::List tuple2list(const ampl::TupleRef &tuple) {
Rcpp::List list(tuple.size());
for(std::size_t i = 0; i < tuple.size(); i++) {
const ampl::VariantRef &e = tuple[i];
if(e.type() == ampl::NUMERIC) {
list[i] = e.dbl();
} else {
list[i] = e.str();
}
}
return list;
}
ampl::DataFrame rdf2df(Rcpp::DataFrame rdf, int numberOfIndexColumns){
int nrows = rdf.nrows();
int ncols = rdf.length();
const char *names[ncols];
Rcpp::CharacterVector colnames = rdf.names();
for(int i = 0; i < colnames.size(); i++){
names[i] = Rcpp::as<const char *>(colnames[i]);
}
if(numberOfIndexColumns == -1){
numberOfIndexColumns = ncols - 1;
}
ampl::DataFrame df(numberOfIndexColumns, ampl::StringArgs(names, ncols));
int p = 0;
for(Rcpp::DataFrame::iterator it = rdf.begin(); it != rdf.end(); it++){
switch(TYPEOF(*it)) {
case REALSXP:
df.setColumn(names[p++], Rcpp::as<std::vector<double> >(*it).data(), nrows);
break;
case INTSXP:
if(::Rf_isFactor(*it) == false) {
Rcpp::IntegerVector iv = *it;
std::vector<double> dbl_column(iv.size());
for(int i = 0; i < iv.size(); i++) {
dbl_column[i] = iv[i];
}
df.setColumn(names[p++], dbl_column.data(), dbl_column.size());
} else{
Rcpp::IntegerVector iv = *it;
std::vector<const char *> str_column(iv.size());
std::vector<std::string > levels = Rcpp::as<std::vector<std::string> >(iv.attr("levels"));
for(int i = 0; i < iv.size(); i++) {
str_column[i] = levels[iv[i]-1].c_str();
}
df.setColumn(names[p++], str_column.data(), str_column.size());
}
break;
case STRSXP: {
Rcpp::StringVector iv = *it;
std::vector<const char *> str_column(iv.size());
for(int i = 0; i < iv.size(); i++) {
str_column[i] = iv[i];
}
df.setColumn(names[p++], str_column.data(), str_column.size());
}
break;
default:
Rcpp::stop("invalid type");
}
}
return df;
}
Rcpp::DataFrame df2rdf(const ampl::DataFrame &df){
Rcpp::List tmp;
int ncols = df.getNumCols();
ampl::StringArray headers = df.getHeaders();
for(int i = 0; i < ncols; i++){
ampl::DataFrame::Column col = df.getColumn(headers[i]);
bool numeric = true;
for(ampl::DataFrame::Column::iterator it = col.begin(); it != col.end(); it++){
if(it->type() != ampl::NUMERIC) {
numeric = false;
break;
}
}
if(numeric) {
std::vector<double> dbl_column;
for(ampl::DataFrame::Column::iterator it = col.begin(); it != col.end(); it++){
dbl_column.push_back(it->dbl());
}
tmp[headers[i]] = dbl_column;
} else {
std::vector<std::string> str_column;
for(ampl::DataFrame::Column::iterator it = col.begin(); it != col.end(); it++){
str_column.push_back(it->str());
}
tmp[headers[i]] = str_column;
}
}
return Rcpp::DataFrame(tmp);
}
SEXP variant2sexp(const ampl::VariantRef &value) {
if(value.type() == ampl::NUMERIC) {
return Rcpp::wrap(value.dbl());
} else if(value.type() == ampl::STRING) {
return Rcpp::wrap(value.str());
} else {
return Rcpp::wrap(Rcpp::String(NA_STRING));
}
}
SEXP variant2sexp(const ampl::Variant &value) {
if(value.type() == ampl::NUMERIC) {
return Rcpp::wrap(value.dbl());
} else if(value.type() == ampl::STRING) {
return Rcpp::wrap(value.str());
} else {
return Rcpp::wrap(Rcpp::String(NA_STRING));
}
}