Skip to content

Commit 9662aa7

Browse files
committed
fix cplx len
1 parent d9a03d3 commit 9662aa7

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

inst/include/cpp11/complexes.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,20 @@ inline std::complex<T>& operator+=(std::complex<T>& lhs, const cpp11::r_complex&
223223
namespace writable {
224224

225225
template <>
226-
inline r_vector<r_complex>::r_vector(std::initializer_list<r_complex> il) {
227-
R_xlen_t size = il.size();
228-
SEXP data = PROTECT(Rf_allocVector(CPLXSXP, size));
226+
inline r_vector<r_complex>::r_vector(std::initializer_list<r_complex> il)
227+
: cpp11::r_vector<r_complex>(safe[Rf_allocVector](CPLXSXP, il.size())),
228+
capacity_(il.size()) {
229229
auto it = il.begin();
230-
for (R_xlen_t i = 0; i < size; ++i, ++it) {
231-
Rcomplex r;
232-
r.r = it->real();
233-
r.i = it->imag();
234-
COMPLEX(data)[i] = r;
230+
231+
if (data_p_ != nullptr) {
232+
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
233+
data_p_[i] = static_cast<underlying_type>(*it);
234+
}
235+
} else {
236+
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
237+
set_elt(data_, i, static_cast<underlying_type>(*it));
238+
}
235239
}
236-
UNPROTECT(1);
237-
data_ = data;
238240
}
239241

240242
} // namespace writable

0 commit comments

Comments
 (0)