Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as_integers and as_doubles now understand logical inputs #426

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cpp11test/src/test-doubles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/logicals.hpp"
#include "cpp11/sexp.hpp"
#include "cpp11/strings.hpp"

Expand Down Expand Up @@ -454,6 +455,22 @@ context("doubles-C++") {
expect_true(!cpp11::is_na(na2[1]));
}

test_that("as_doubles(logicals)") {
cpp11::writable::logicals y;

for (int i = 0; i < 4; i++) {
y.push_back(i % 2 == 0);
}

cpp11::doubles i(cpp11::as_doubles(y));

expect_true(i[0] == 1.0);
expect_true(i[1] == 0.0);
expect_true(i[2] == 1.0);
expect_true(i[3] == 0.0);
expect_true(cpp11::detail::r_typeof(i) == REALSXP);
}

test_that("doubles operator[] and at") {
cpp11::doubles x(Rf_allocVector(REALSXP, 2));
REAL(x)[0] = 1;
Expand Down
17 changes: 17 additions & 0 deletions cpp11test/src/test-integers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/logicals.hpp"
#include "cpp11/strings.hpp"

#include <testthat.h>
Expand Down Expand Up @@ -43,6 +44,22 @@ context("integers-C++") {
expect_true(!cpp11::is_na(na2[1]));
}

test_that("as_integers(logicals)") {
cpp11::writable::logicals y;

for (int i = 0; i < 4; i++) {
y.push_back(i % 2 == 0);
}

cpp11::integers i(cpp11::as_integers(y));

expect_true(i[0] == 1);
expect_true(i[1] == 0);
expect_true(i[2] == 1);
expect_true(i[3] == 0);
expect_true(cpp11::detail::r_typeof(i) == INTSXP);
}

test_that("integers.push_back()") {
cpp11::writable::integers x;
x.push_back(1);
Expand Down
14 changes: 11 additions & 3 deletions inst/include/cpp11/doubles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_allocVector, REAL
#include "cpp11/as.hpp" // for as_sexp
#include "cpp11/protect.hpp" // for safe
#include "cpp11/r_bool.hpp" // for r_bool
#include "cpp11/r_vector.hpp" // for vector, vector<>::proxy, vector<>::...
#include "cpp11/sexp.hpp" // for sexp

Expand Down Expand Up @@ -71,20 +72,27 @@ typedef r_vector<double> doubles;
} // namespace writable

typedef r_vector<int> integers;
typedef r_vector<r_bool> logicals;

inline doubles as_doubles(SEXP x) {
if (detail::r_typeof(x) == REALSXP) {
return doubles(x);
}

else if (detail::r_typeof(x) == INTSXP) {
} else if (detail::r_typeof(x) == INTSXP) {
integers xn(x);
size_t len = xn.size();
writable::doubles ret(len);
std::transform(xn.begin(), xn.end(), ret.begin(), [](int value) {
return value == NA_INTEGER ? NA_REAL : static_cast<double>(value);
});
return ret;
} else if (detail::r_typeof(x) == LGLSXP) {
logicals xn(x);
size_t len = xn.size();
writable::doubles ret(len);
std::transform(xn.begin(), xn.end(), ret.begin(), [](bool value) {
return value == NA_LOGICAL ? NA_REAL : static_cast<double>(value);
});
return ret;
}

throw type_error(REALSXP, detail::r_typeof(x));
Expand Down
10 changes: 10 additions & 0 deletions inst/include/cpp11/integers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cpp11/as.hpp" // for as_sexp
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
#include "cpp11/protect.hpp" // for safe
#include "cpp11/r_bool.hpp" // for r_bool
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
#include "cpp11/sexp.hpp" // for sexp

Expand Down Expand Up @@ -79,6 +80,7 @@ inline int na() {
// forward declaration

typedef r_vector<double> doubles;
typedef r_vector<r_bool> logicals;

inline integers as_integers(SEXP x) {
if (detail::r_typeof(x) == INTSXP) {
Expand All @@ -96,6 +98,14 @@ inline integers as_integers(SEXP x) {
return static_cast<int>(value);
});
return ret;
} else if (detail::r_typeof(x) == LGLSXP) {
logicals xn(x);
size_t len = xn.size();
writable::integers ret(len);
std::transform(xn.begin(), xn.end(), ret.begin(), [](bool value) {
return value == NA_LOGICAL ? NA_INTEGER : static_cast<int>(value);
});
return ret;
}

throw type_error(INTSXP, detail::r_typeof(x));
Expand Down
Loading