1 // cpp11 version: 0.3.1.1
2 // vendored on: 2021-08-11
5 #include <algorithm> // for min
6 #include <array> // for array
7 #include <cstdint> // for uint8_t
8 #include <initializer_list> // for initializer_list
10 #include "cpp11/R.hpp" // for RAW, SEXP, SEXPREC, Rf_allocVector
11 #include "cpp11/attribute_proxy.hpp" // for attribute_proxy
12 #include "cpp11/named_arg.hpp" // for named_arg
13 #include "cpp11/protect.hpp" // for preserved
14 #include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
15 #include "cpp11/sexp.hpp" // for sexp
17 // Specializations for raws
22 inline SEXP r_vector<uint8_t>::valid_type(SEXP data) {
23 if (TYPEOF(data) != RAWSXP) {
24 throw type_error(RAWSXP, TYPEOF(data));
30 inline uint8_t r_vector<uint8_t>::operator[](const R_xlen_t pos) const {
31 // NOPROTECT: likely too costly to unwind protect every elt
32 return is_altrep_ ? RAW_ELT(data_, pos) : data_p_[pos];
36 inline uint8_t* r_vector<uint8_t>::get_p(bool is_altrep, SEXP data) {
40 return reinterpret_cast<uint8_t*>(RAW(data));
45 inline void r_vector<uint8_t>::const_iterator::fill_buf(R_xlen_t pos) {
46 using namespace cpp11::literals;
47 length_ = std::min(64_xl, data_->size() - pos);
49 [&] { RAW_GET_REGION(data_->data_, pos, length_, (uint8_t*)buf_.data()); });
53 typedef r_vector<uint8_t> raws;
58 inline typename r_vector<uint8_t>::proxy& r_vector<uint8_t>::proxy::operator=(
61 // NOPROTECT: likely too costly to unwind protect every set elt
62 RAW(data_)[index_] = rhs;
70 inline r_vector<uint8_t>::proxy::operator uint8_t() const {
72 // NOPROTECT: likely too costly to unwind protect every elt
73 return RAW(data_)[index_];
80 inline r_vector<uint8_t>::r_vector(std::initializer_list<uint8_t> il)
81 : cpp11::r_vector<uint8_t>(safe[Rf_allocVector](RAWSXP, il.size())),
82 capacity_(il.size()) {
83 protect_ = preserved.insert(data_);
85 for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
91 inline r_vector<uint8_t>::r_vector(std::initializer_list<named_arg> il)
92 : cpp11::r_vector<uint8_t>(safe[Rf_allocVector](RAWSXP, il.size())),
93 capacity_(il.size()) {
94 protect_ = preserved.insert(data_);
99 Rf_setAttrib(data_, R_NamesSymbol, Rf_allocVector(STRSXP, capacity_));
100 SEXP names = PROTECT(Rf_getAttrib(data_, R_NamesSymbol));
103 auto it = il.begin();
104 for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
105 data_p_[i] = RAW_ELT(it->value(), 0);
106 SET_STRING_ELT(names, i, Rf_mkCharCE(it->name(), CE_UTF8));
108 UNPROTECT(n_protected);
110 } catch (const unwind_exception& e) {
111 preserved.release(protect_);
112 UNPROTECT(n_protected);
118 inline void r_vector<uint8_t>::reserve(R_xlen_t new_capacity) {
119 data_ = data_ == R_NilValue ? safe[Rf_allocVector](RAWSXP, new_capacity)
120 : safe[Rf_xlengthgets](data_, new_capacity);
122 SEXP old_protect = protect_;
123 protect_ = preserved.insert(data_);
124 preserved.release(old_protect);
126 data_p_ = reinterpret_cast<uint8_t*>(RAW(data_));
127 capacity_ = new_capacity;
131 inline void r_vector<uint8_t>::push_back(uint8_t value) {
132 while (length_ >= capacity_) {
133 reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
136 // NOPROTECT: likely too costly to unwind protect every elt
137 RAW(data_)[length_] = value;
139 data_p_[length_] = value;
144 typedef r_vector<uint8_t> raws;
146 } // namespace writable