FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix missing value propagation in `as_integers/doubles()` by DavisVaughan · Pull Request #265 · r-lib/cpp11 · GitHub

/ cpp11 Public
3 changes: 3 additions & 0 deletions NEWS.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cpp11 (development version)

* `as_doubles()` and `as_integers()` now propagate missing values correctly
(#265).

* Fixed a performance issue related to nested `unwind_protect()` calls (#298).

* Minor performance improvements to the cpp11 protect code. (@kevinushey)
Expand Down
5 changes: 5 additions & 0 deletions cpp11test/src/test-doubles.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ context("doubles-C++") {
e.push_back("a");
e.push_back("b");
expect_error(cpp11::as_doubles(e));

cpp11::writable::integers na;
na.push_back(cpp11::na<int>());
cpp11::doubles na2(cpp11::as_doubles(na));
expect_true(cpp11::is_na(na2[0]));
}

test_that("doubles operator[] and at") {
Expand Down
9 changes: 8 additions & 1 deletion cpp11test/src/test-integers.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Rversion.h"
#include "cpp11/doubles.hpp"

#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"

#include "cpp11/doubles.hpp"
#include "cpp11/strings.hpp"

#include <testthat.h>
Expand Down Expand Up @@ -36,6 +38,11 @@ context("integers-C++") {
expect_true(t[2] == 100000);
expect_true(t[3] == 100000);
expect_true(TYPEOF(t) == INTSXP);

cpp11::writable::doubles na;
na.push_back(cpp11::na<double>());
cpp11::integers na2(cpp11::as_integers(na));
expect_true(cpp11::is_na(na2[0]));
}

test_that("integers.push_back()") {
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/as.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ using enable_if_c_string = enable_if_t<std::is_same<T, const char*>::value, R>;

// https://stackoverflow.com/a/1521682/2055486
//
inline bool is_convertable_without_loss_to_integer(double value) {
inline bool is_convertible_without_loss_to_integer(double value) {
double int_part;
return std::modf(value, &int_part) == 0.0;
}
Expand All @@ -100,7 +100,7 @@ enable_if_integral<T, T> as_cpp(SEXP from) {
return NA_INTEGER;
}
double value = REAL_ELT(from, 0);
if (is_convertable_without_loss_to_integer(value)) {
if (is_convertible_without_loss_to_integer(value)) {
return value;
}
}
Expand Down
43 changes: 27 additions & 16 deletions inst/include/cpp11/doubles.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,44 @@ typedef r_vector<double> doubles;

} // namespace writable

template <>
inline double na() {
return NA_REAL;
}

template <>
inline bool is_na(const double& x) {
return ISNA(x);
}
Comment thread
romainfrancois marked this conversation as resolved.

// forward declarations
typedef r_vector<int> integers;

template <>
int na();
Comment thread
romainfrancois marked this conversation as resolved.

template <>
int r_vector<int>::operator[](const R_xlen_t pos) const;
Comment thread
romainfrancois marked this conversation as resolved.

inline doubles as_doubles(sexp x) {
if (TYPEOF(x) == REALSXP) {
return as_cpp<doubles>(x);
}

else if (TYPEOF(x) == INTSXP) {
} else if (TYPEOF(x) == INTSXP) {
integers xn = as_cpp<integers>(x);
size_t len = xn.size();
writable::doubles ret;
for (size_t i = 0; i < len; ++i) {
ret.push_back(static_cast<double>(xn[i]));
Comment thread
romainfrancois marked this conversation as resolved.
R_xlen_t len = xn.size();
Comment thread
romainfrancois marked this conversation as resolved.
writable::doubles ret(len);
for (R_xlen_t i = 0; i < len; ++i) {
int el = xn[i];
if (is_na(el)) {
ret[i] = na<double>();
} else {
ret[i] = static_cast<double>(el);
}
}
return ret;
}

throw type_error(REALSXP, TYPEOF(x));
}

template <>
inline double na() {
return NA_REAL;
}

template <>
inline bool is_na(const double& x) {
return ISNA(x);
}
} // namespace cpp11
23 changes: 15 additions & 8 deletions inst/include/cpp11/integers.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,32 @@ inline int na() {
return NA_INTEGER;
}

// forward declaration

// forward declarations
typedef r_vector<double> doubles;

template <>
bool is_na(const double& x);
Comment thread
romainfrancois marked this conversation as resolved.

template <>
double r_vector<double>::operator[](const R_xlen_t pos) const;

inline integers as_integers(sexp x) {
if (TYPEOF(x) == INTSXP) {
return as_cpp<integers>(x);
} else if (TYPEOF(x) == REALSXP) {
doubles xn = as_cpp<doubles>(x);
size_t len = (xn.size());
writable::integers ret = writable::integers(len);
for (size_t i = 0; i < len; ++i) {
R_xlen_t len = xn.size();
writable::integers ret(len);
for (R_xlen_t i = 0; i < len; ++i) {
double el = xn[i];
if (!is_convertable_without_loss_to_integer(el)) {
if (is_na(el)) {
ret[i] = na<int>();
} else if (is_convertible_without_loss_to_integer(el)) {
ret[i] = static_cast<int>(el);
} else {
throw std::runtime_error("All elements must be integer-like");
}
ret[i] = (static_cast<int>(el));
}

return ret;
}

Expand Down

Back | FazBrowse Home | New Git URL