Skip to content

Commit 08e8ee8

Browse files
move as_doubles() and as_integers() to as_vector.hpp
1 parent 39b5ee2 commit 08e8ee8

File tree

4 files changed

+54
-45
lines changed

4 files changed

+54
-45
lines changed

cpp11test/src/test-doubles.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "cpp11/integers.hpp"
55
#include "cpp11/sexp.hpp"
66
#include "cpp11/strings.hpp"
7+
#include "cpp11/as_vector.hpp"
78

89
#include <testthat.h>
910

inst/include/cpp11/as_vector.hpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
#include "R_ext/Arith.h" // for ISNA
4+
#include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_allocVector, REAL
5+
6+
#include "cpp11/doubles.hpp"
7+
#include "cpp11/integers.hpp"
8+
9+
namespace cpp11 {
10+
11+
inline SEXP as_doubles(SEXP x) {
12+
if (TYPEOF(x) == REALSXP) {
13+
return x;
14+
}
15+
16+
if (TYPEOF(x) == INTSXP) {
17+
integers xn(x);
18+
auto len = XLENGTH(x);
19+
SEXP ret = PROTECT(Rf_allocVector(REALSXP, len));
20+
double* p_ret = REAL(ret);
21+
for (decltype(len) i = 0; i < len; ++i, ++p_ret) {
22+
int el = xn[i];
23+
*p_ret = (el == NA_INTEGER) ? NA_REAL : static_cast<double>(el);
24+
}
25+
UNPROTECT(1);
26+
return ret;
27+
}
28+
29+
throw type_error(REALSXP, TYPEOF(x));
30+
}
31+
32+
inline integers as_integers(sexp x) {
33+
if (TYPEOF(x) == INTSXP) {
34+
return as_cpp<integers>(x);
35+
} else if (TYPEOF(x) == REALSXP) {
36+
doubles xn = as_cpp<doubles>(x);
37+
size_t len = (xn.size());
38+
writable::integers ret = writable::integers(len);
39+
for (size_t i = 0; i < len; ++i) {
40+
double el = xn[i];
41+
if (!is_convertible_without_loss_to_integer(el)) {
42+
throw std::runtime_error("All elements must be integer-like");
43+
}
44+
ret[i] = (static_cast<int>(el));
45+
}
46+
47+
return ret;
48+
}
49+
50+
throw type_error(INTSXP, TYPEOF(x));
51+
}
52+
53+
}

inst/include/cpp11/doubles.hpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,6 @@ typedef r_vector<double> doubles;
135135

136136
} // namespace writable
137137

138-
typedef r_vector<int> integers;
139-
140-
inline doubles as_doubles(sexp x) {
141-
if (TYPEOF(x) == REALSXP) {
142-
return as_cpp<doubles>(x);
143-
}
144-
145-
else if (TYPEOF(x) == INTSXP) {
146-
integers xn = as_cpp<integers>(x);
147-
size_t len = xn.size();
148-
writable::doubles ret;
149-
for (size_t i = 0; i < len; ++i) {
150-
ret.push_back(static_cast<double>(xn[i]));
151-
}
152-
return ret;
153-
}
154-
155-
throw type_error(REALSXP, TYPEOF(x));
156-
}
157-
158138
template <>
159139
inline double na() {
160140
return NA_REAL;

inst/include/cpp11/integers.hpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,29 +145,4 @@ inline int na() {
145145
return NA_INTEGER;
146146
}
147147

148-
// forward declaration
149-
150-
typedef r_vector<double> doubles;
151-
152-
inline integers as_integers(sexp x) {
153-
if (TYPEOF(x) == INTSXP) {
154-
return as_cpp<integers>(x);
155-
} else if (TYPEOF(x) == REALSXP) {
156-
doubles xn = as_cpp<doubles>(x);
157-
size_t len = (xn.size());
158-
writable::integers ret = writable::integers(len);
159-
for (size_t i = 0; i < len; ++i) {
160-
double el = xn[i];
161-
if (!is_convertible_without_loss_to_integer(el)) {
162-
throw std::runtime_error("All elements must be integer-like");
163-
}
164-
ret[i] = (static_cast<int>(el));
165-
}
166-
167-
return ret;
168-
}
169-
170-
throw type_error(INTSXP, TYPEOF(x));
171-
}
172-
173148
} // namespace cpp11

0 commit comments

Comments
 (0)