Skip to content

Commit 2df5613

Browse files
Revert "Fix missing value propagation in as_integers/doubles() (#265)"
This reverts commit 3c87798.
1 parent 3c87798 commit 2df5613

File tree

6 files changed

+27
-60
lines changed

6 files changed

+27
-60
lines changed

NEWS.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# cpp11 (development version)
22

3-
* `as_doubles()` and `as_integers()` now propagate missing values correctly
4-
(#265).
5-
63
* Fixed a performance issue related to nested `unwind_protect()` calls (#298).
74

85
* Minor performance improvements to the cpp11 protect code. (@kevinushey)

cpp11test/src/test-doubles.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,6 @@ context("doubles-C++") {
393393
e.push_back("a");
394394
e.push_back("b");
395395
expect_error(cpp11::as_doubles(e));
396-
397-
cpp11::writable::integers na;
398-
na.push_back(cpp11::na<int>());
399-
cpp11::doubles na2(cpp11::as_doubles(na));
400-
expect_true(cpp11::is_na(na2[0]));
401396
}
402397

403398
test_that("doubles operator[] and at") {

cpp11test/src/test-integers.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include "Rversion.h"
2-
2+
#include "cpp11/doubles.hpp"
33
#include "cpp11/function.hpp"
44
#include "cpp11/integers.hpp"
5-
6-
#include "cpp11/doubles.hpp"
75
#include "cpp11/strings.hpp"
86

97
#include <testthat.h>
@@ -38,11 +36,6 @@ context("integers-C++") {
3836
expect_true(t[2] == 100000);
3937
expect_true(t[3] == 100000);
4038
expect_true(TYPEOF(t) == INTSXP);
41-
42-
cpp11::writable::doubles na;
43-
na.push_back(cpp11::na<double>());
44-
cpp11::integers na2(cpp11::as_integers(na));
45-
expect_true(cpp11::is_na(na2[0]));
4639
}
4740

4841
test_that("integers.push_back()") {

inst/include/cpp11/as.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ using enable_if_c_string = enable_if_t<std::is_same<T, const char*>::value, R>;
7373

7474
// https://stackoverflow.com/a/1521682/2055486
7575
//
76-
inline bool is_convertible_without_loss_to_integer(double value) {
76+
inline bool is_convertable_without_loss_to_integer(double value) {
7777
double int_part;
7878
return std::modf(value, &int_part) == 0.0;
7979
}
@@ -100,7 +100,7 @@ enable_if_integral<T, T> as_cpp(SEXP from) {
100100
return NA_INTEGER;
101101
}
102102
double value = REAL_ELT(from, 0);
103-
if (is_convertible_without_loss_to_integer(value)) {
103+
if (is_convertable_without_loss_to_integer(value)) {
104104
return value;
105105
}
106106
}

inst/include/cpp11/doubles.hpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,44 +135,33 @@ typedef r_vector<double> doubles;
135135

136136
} // namespace writable
137137

138-
template <>
139-
inline double na() {
140-
return NA_REAL;
141-
}
142-
143-
template <>
144-
inline bool is_na(const double& x) {
145-
return ISNA(x);
146-
}
147-
148-
// forward declarations
149138
typedef r_vector<int> integers;
150139

151-
template <>
152-
int na();
153-
154-
template <>
155-
int r_vector<int>::operator[](const R_xlen_t pos) const;
156-
157140
inline doubles as_doubles(sexp x) {
158141
if (TYPEOF(x) == REALSXP) {
159142
return as_cpp<doubles>(x);
160-
} else if (TYPEOF(x) == INTSXP) {
143+
}
144+
145+
else if (TYPEOF(x) == INTSXP) {
161146
integers xn = as_cpp<integers>(x);
162-
R_xlen_t len = xn.size();
163-
writable::doubles ret(len);
164-
for (R_xlen_t i = 0; i < len; ++i) {
165-
int el = xn[i];
166-
if (is_na(el)) {
167-
ret[i] = na<double>();
168-
} else {
169-
ret[i] = static_cast<double>(el);
170-
}
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]));
171151
}
172152
return ret;
173153
}
174154

175155
throw type_error(REALSXP, TYPEOF(x));
176156
}
177157

158+
template <>
159+
inline double na() {
160+
return NA_REAL;
161+
}
162+
163+
template <>
164+
inline bool is_na(const double& x) {
165+
return ISNA(x);
166+
}
178167
} // namespace cpp11

inst/include/cpp11/integers.hpp

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

148-
// forward declarations
149-
typedef r_vector<double> doubles;
150-
151-
template <>
152-
bool is_na(const double& x);
148+
// forward declaration
153149

154-
template <>
155-
double r_vector<double>::operator[](const R_xlen_t pos) const;
150+
typedef r_vector<double> doubles;
156151

157152
inline integers as_integers(sexp x) {
158153
if (TYPEOF(x) == INTSXP) {
159154
return as_cpp<integers>(x);
160155
} else if (TYPEOF(x) == REALSXP) {
161156
doubles xn = as_cpp<doubles>(x);
162-
R_xlen_t len = xn.size();
163-
writable::integers ret(len);
164-
for (R_xlen_t i = 0; i < len; ++i) {
157+
size_t len = (xn.size());
158+
writable::integers ret = writable::integers(len);
159+
for (size_t i = 0; i < len; ++i) {
165160
double el = xn[i];
166-
if (is_na(el)) {
167-
ret[i] = na<int>();
168-
} else if (is_convertible_without_loss_to_integer(el)) {
169-
ret[i] = static_cast<int>(el);
170-
} else {
161+
if (!is_convertable_without_loss_to_integer(el)) {
171162
throw std::runtime_error("All elements must be integer-like");
172163
}
164+
ret[i] = (static_cast<int>(el));
173165
}
166+
174167
return ret;
175168
}
176169

0 commit comments

Comments
 (0)