1
- // cpp11 version: 0.4.7
2
- // vendored on: 2024-06-26
1
+ // cpp11 version: 0.5.0
2
+ // vendored on: 2024-09-24
3
3
#pragma once
4
4
5
5
#include < algorithm> // for min, tranform
6
6
#include < array> // for array
7
7
#include < initializer_list> // for initializer_list
8
8
9
- #include " R_ext/Arith.h" // for ISNA
10
- #include " cpp11/R.hpp" // for SEXP, SEXPREC, Rf_allocVector, REAL
11
- #include " cpp11/as.hpp" // for as_sexp
12
- #include " cpp11/named_arg.hpp" // for named_arg
13
- #include " cpp11/protect.hpp" // for SEXP, SEXPREC, REAL_ELT, R_Preserve...
14
- #include " cpp11/r_vector.hpp" // for vector, vector<>::proxy, vector<>::...
15
- #include " cpp11/sexp.hpp" // for sexp
9
+ #include " R_ext/Arith.h" // for ISNA
10
+ #include " cpp11/R.hpp" // for SEXP, SEXPREC, Rf_allocVector, REAL
11
+ #include " cpp11/as.hpp" // for as_sexp
12
+ #include " cpp11/protect.hpp" // for safe
13
+ #include " cpp11/r_vector.hpp" // for vector, vector<>::proxy, vector<>::...
14
+ #include " cpp11/sexp.hpp" // for sexp
16
15
17
16
// Specializations for doubles
18
17
19
18
namespace cpp11 {
20
19
21
20
template <>
22
- inline SEXP r_vector<double >::valid_type(SEXP data) {
23
- if (data == nullptr ) {
24
- throw type_error (REALSXP, NILSXP);
25
- }
26
- if (TYPEOF (data) != REALSXP) {
27
- throw type_error (REALSXP, TYPEOF (data));
28
- }
29
- return data;
21
+ inline SEXPTYPE r_vector<double >::get_sexptype() {
22
+ return REALSXP;
30
23
}
31
24
32
25
template <>
33
- inline double r_vector<double >::operator [](const R_xlen_t pos) const {
26
+ inline typename r_vector<double >::underlying_type r_vector<double >::get_elt(SEXP x,
27
+ R_xlen_t i) {
34
28
// NOPROTECT: likely too costly to unwind protect every elt
35
- return is_altrep_ ? REAL_ELT (data_, pos) : data_p_[pos] ;
29
+ return REAL_ELT (x, i) ;
36
30
}
37
31
38
32
template <>
@@ -46,91 +40,32 @@ inline typename r_vector<double>::underlying_type* r_vector<double>::get_p(bool
46
40
}
47
41
48
42
template <>
49
- inline void r_vector<double >::const_iterator::fill_buf(R_xlen_t pos) {
50
- length_ = std::min (64_xl, data_->size () - pos);
51
- REAL_GET_REGION (data_->data_ , pos, length_, buf_.data ());
52
- block_start_ = pos;
43
+ inline typename r_vector<double >::underlying_type const * r_vector<double >::get_const_p(
44
+ bool is_altrep, SEXP data) {
45
+ return REAL_OR_NULL (data);
53
46
}
54
47
55
- typedef r_vector<double > doubles;
56
-
57
- namespace writable {
58
-
59
48
template <>
60
- inline typename r_vector<double >::proxy& r_vector<double >::proxy::operator =(
61
- const double & rhs) {
62
- if (is_altrep_) {
63
- // NOPROTECT: likely too costly to unwind protect every set elt
64
- SET_REAL_ELT (data_, index_, rhs);
65
- } else {
66
- *p_ = rhs;
67
- }
68
- return *this ;
69
- }
49
+ inline void r_vector<double >::get_region(SEXP x, R_xlen_t i, R_xlen_t n,
50
+ typename r_vector::underlying_type* buf) {
51
+ // NOPROTECT: likely too costly to unwind protect here
52
+ REAL_GET_REGION (x, i, n, buf);
53
+ };
70
54
71
55
template <>
72
- inline r_vector<double >::proxy::operator double () const {
73
- if (p_ == nullptr ) {
74
- // NOPROTECT: likely too costly to unwind protect every elt
75
- return REAL_ELT (data_, index_);
76
- } else {
77
- return *p_;
78
- }
56
+ inline bool r_vector<double >::const_iterator::use_buf(bool is_altrep) {
57
+ return is_altrep;
79
58
}
80
59
81
- template <>
82
- inline r_vector<double >::r_vector(std::initializer_list<double > il)
83
- : cpp11::r_vector<double >(as_sexp(il)), capacity_(il.size()) {}
84
-
85
- template <>
86
- inline r_vector<double >::r_vector(std::initializer_list<named_arg> il)
87
- : cpp11::r_vector<double >(safe[Rf_allocVector](REALSXP, il.size())),
88
- capacity_ (il.size()) {
89
- protect_ = preserved.insert (data_);
90
- int n_protected = 0 ;
91
-
92
- try {
93
- unwind_protect ([&] {
94
- Rf_setAttrib (data_, R_NamesSymbol, Rf_allocVector (STRSXP, capacity_));
95
- SEXP names = PROTECT (Rf_getAttrib (data_, R_NamesSymbol));
96
- ++n_protected;
97
- auto it = il.begin ();
98
- for (R_xlen_t i = 0 ; i < capacity_; ++i, ++it) {
99
- data_p_[i] = REAL_ELT (it->value (), 0 );
100
- SET_STRING_ELT (names, i, Rf_mkCharCE (it->name (), CE_UTF8));
101
- }
102
- UNPROTECT (n_protected);
103
- });
104
- } catch (const unwind_exception& e) {
105
- preserved.release (protect_);
106
- UNPROTECT (n_protected);
107
- throw e;
108
- }
109
- }
60
+ typedef r_vector<double > doubles;
110
61
111
- template <>
112
- inline void r_vector<double >::reserve(R_xlen_t new_capacity) {
113
- data_ = data_ == R_NilValue ? safe[Rf_allocVector](REALSXP, new_capacity)
114
- : safe[Rf_xlengthgets](data_, new_capacity);
115
- SEXP old_protect = protect_;
116
- protect_ = preserved.insert (data_);
117
- preserved.release (old_protect);
118
-
119
- data_p_ = REAL (data_);
120
- capacity_ = new_capacity;
121
- }
62
+ namespace writable {
122
63
123
64
template <>
124
- inline void r_vector<double >::push_back(double value) {
125
- while (length_ >= capacity_) {
126
- reserve (capacity_ == 0 ? 1 : capacity_ *= 2 );
127
- }
128
- if (is_altrep_) {
129
- SET_REAL_ELT (data_, length_, value);
130
- } else {
131
- data_p_[length_] = value;
132
- }
133
- ++length_;
65
+ inline void r_vector<double >::set_elt(SEXP x, R_xlen_t i,
66
+ typename r_vector::underlying_type value) {
67
+ // NOPROTECT: Likely too costly to unwind protect every set elt
68
+ SET_REAL_ELT (x, i, value);
134
69
}
135
70
136
71
typedef r_vector<double > doubles;
@@ -140,11 +75,11 @@ typedef r_vector<double> doubles;
140
75
typedef r_vector<int > integers;
141
76
142
77
inline doubles as_doubles (SEXP x) {
143
- if (TYPEOF (x) == REALSXP) {
78
+ if (detail::r_typeof (x) == REALSXP) {
144
79
return doubles (x);
145
80
}
146
81
147
- else if (TYPEOF (x) == INTSXP) {
82
+ else if (detail::r_typeof (x) == INTSXP) {
148
83
integers xn (x);
149
84
size_t len = xn.size ();
150
85
writable::doubles ret (len);
@@ -154,7 +89,7 @@ inline doubles as_doubles(SEXP x) {
154
89
return ret;
155
90
}
156
91
157
- throw type_error (REALSXP, TYPEOF (x));
92
+ throw type_error (REALSXP, detail::r_typeof (x));
158
93
}
159
94
160
95
template <>
0 commit comments