Skip to content

Commit

Permalink
trying to get push_back for cplx to work :\
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Dec 26, 2024
1 parent 8e044f6 commit 49ee179
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 8 additions & 5 deletions inst/include/cpp11/complexes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ typedef r_vector<r_complex> complexes;
namespace writable {

template <>
inline void r_vector<r_complex>::set_elt(
SEXP x, R_xlen_t i, typename r_vector<r_complex>::underlying_type value) {
SET_COMPLEX_ELT(x, i, static_cast<Rcomplex>(value));
inline void r_vector<r_complex>::set_elt(SEXP x, R_xlen_t i,
r_vector::underlying_type value) {
COMPLEX(x)[i] = static_cast<Rcomplex>(value);
}

typedef r_vector<r_complex> complexes;
Expand Down Expand Up @@ -98,8 +98,11 @@ class r_vector<r_complex>::proxy {
proxy(SEXP data, R_xlen_t index)
: data_(data), index_(index), buf_(nullptr), is_altrep_(false) {}

proxy(SEXP data, R_xlen_t index, Rcomplex* buf, bool is_altrep)
: data_(data), index_(index), buf_(buf), is_altrep_(is_altrep) {}
proxy(SEXP data, R_xlen_t index, r_complex* buf, bool is_altrep)
: data_(data),
index_(index),
buf_(reinterpret_cast<Rcomplex*>(buf)),
is_altrep_(is_altrep) {}

operator r_complex() const {
if (is_altrep_ && buf_ != nullptr) {
Expand Down
6 changes: 2 additions & 4 deletions inst/include/cpp11/r_complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ inline void writable::r_vector<r_complex>::push_back(r_complex value) {
this->reserve(this->capacity_ == 0 ? 1 : this->capacity_ * 2);
}

Rcomplex r_value = static_cast<Rcomplex>(value);

if (this->data_p_ != nullptr) {
this->data_p_[this->length_] = r_value;
this->data_p_[this->length_] = static_cast<Rcomplex>(value);
} else {
this->set_elt(this->data_, this->length_, r_value);
set_elt(this->data_, this->length_, value);
}

++this->length_;
Expand Down
2 changes: 2 additions & 0 deletions inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ class r_vector : public cpp11::r_vector<T> {
void push_back(T value);
/// Implemented in `strings.hpp`
void push_back(const named_arg& value);
// Implemented in `complexes.hpp`
void push_back(const std::complex<double>& value);
void pop_back();

void resize(R_xlen_t count);
Expand Down

0 comments on commit 49ee179

Please sign in to comment.