]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/r/inst/include/cpp11/raws.hpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / r / inst / include / cpp11 / raws.hpp
1 // cpp11 version: 0.3.1.1
2 // vendored on: 2021-08-11
3 #pragma once
4
5 #include <algorithm> // for min
6 #include <array> // for array
7 #include <cstdint> // for uint8_t
8 #include <initializer_list> // for initializer_list
9
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
16
17 // Specializations for raws
18
19 namespace cpp11 {
20
21 template <>
22 inline SEXP r_vector<uint8_t>::valid_type(SEXP data) {
23 if (TYPEOF(data) != RAWSXP) {
24 throw type_error(RAWSXP, TYPEOF(data));
25 }
26 return data;
27 }
28
29 template <>
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];
33 }
34
35 template <>
36 inline uint8_t* r_vector<uint8_t>::get_p(bool is_altrep, SEXP data) {
37 if (is_altrep) {
38 return nullptr;
39 } else {
40 return reinterpret_cast<uint8_t*>(RAW(data));
41 }
42 }
43
44 template <>
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);
48 unwind_protect(
49 [&] { RAW_GET_REGION(data_->data_, pos, length_, (uint8_t*)buf_.data()); });
50 block_start_ = pos;
51 }
52
53 typedef r_vector<uint8_t> raws;
54
55 namespace writable {
56
57 template <>
58 inline typename r_vector<uint8_t>::proxy& r_vector<uint8_t>::proxy::operator=(
59 const uint8_t& rhs) {
60 if (is_altrep_) {
61 // NOPROTECT: likely too costly to unwind protect every set elt
62 RAW(data_)[index_] = rhs;
63 } else {
64 *p_ = rhs;
65 }
66 return *this;
67 }
68
69 template <>
70 inline r_vector<uint8_t>::proxy::operator uint8_t() const {
71 if (p_ == nullptr) {
72 // NOPROTECT: likely too costly to unwind protect every elt
73 return RAW(data_)[index_];
74 } else {
75 return *p_;
76 }
77 }
78
79 template <>
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_);
84 auto it = il.begin();
85 for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
86 data_p_[i] = *it;
87 }
88 }
89
90 template <>
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_);
95 int n_protected = 0;
96
97 try {
98 unwind_protect([&] {
99 Rf_setAttrib(data_, R_NamesSymbol, Rf_allocVector(STRSXP, capacity_));
100 SEXP names = PROTECT(Rf_getAttrib(data_, R_NamesSymbol));
101 ++n_protected;
102
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));
107 }
108 UNPROTECT(n_protected);
109 });
110 } catch (const unwind_exception& e) {
111 preserved.release(protect_);
112 UNPROTECT(n_protected);
113 throw e;
114 }
115 }
116
117 template <>
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);
121
122 SEXP old_protect = protect_;
123 protect_ = preserved.insert(data_);
124 preserved.release(old_protect);
125
126 data_p_ = reinterpret_cast<uint8_t*>(RAW(data_));
127 capacity_ = new_capacity;
128 }
129
130 template <>
131 inline void r_vector<uint8_t>::push_back(uint8_t value) {
132 while (length_ >= capacity_) {
133 reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
134 }
135 if (is_altrep_) {
136 // NOPROTECT: likely too costly to unwind protect every elt
137 RAW(data_)[length_] = value;
138 } else {
139 data_p_[length_] = value;
140 }
141 ++length_;
142 }
143
144 typedef r_vector<uint8_t> raws;
145
146 } // namespace writable
147
148 } // namespace cpp11