Skip to content

Commit

Permalink
Avoid using SETLENGTH()
Browse files Browse the repository at this point in the history
Not part of the R API.
  • Loading branch information
gaborcsardi committed Apr 24, 2024
1 parent a9b414a commit f14dcf8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/CollectorList.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@
class CollectorList {
SEXP data_;
R_xlen_t n_;
bool free_;

public:
CollectorList(R_xlen_t size = 1) : n_(0) {
data_ = Rf_allocVector(VECSXP, size);
R_PreserveObject(data_);
free_ = true;
}

void push_back(SEXP x) {
if (Rf_xlength(data_) == n_) {
R_ReleaseObject(data_);
free_ = false;
data_ = Rf_lengthgets(data_, n_ * 2);
R_PreserveObject(data_);
free_ = true;
}
SET_VECTOR_ELT(data_, n_++, x);
}

operator SEXP() {
if (Rf_xlength(data_) != n_) {
SETLENGTH(data_, n_);
R_ReleaseObject(data_);
free_ = false;
data_ = Rf_lengthgets(data_, n_);
R_PreserveObject(data_);
free_ = true;
}
return data_;
}

~CollectorList() { R_ReleaseObject(data_); }
~CollectorList() { if (free_) R_ReleaseObject(data_); }
};

0 comments on commit f14dcf8

Please sign in to comment.