Skip to content

Commit c54765c

Browse files
committed
Unify push_back() implementations
1 parent 888afdb commit c54765c

File tree

7 files changed

+23
-77
lines changed

7 files changed

+23
-77
lines changed

inst/include/cpp11/doubles.hpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ namespace writable {
5757
template <>
5858
inline void r_vector<double>::set_elt(SEXP x, R_xlen_t i,
5959
typename r_vector::underlying_type value) {
60+
// NOPROTECT: Likely too costly to unwind protect every set elt
6061
SET_REAL_ELT(x, i, value);
6162
}
6263

@@ -84,19 +85,6 @@ inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
8485
}
8586
}
8687

87-
template <>
88-
inline void r_vector<double>::push_back(double value) {
89-
while (length_ >= capacity_) {
90-
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
91-
}
92-
if (is_altrep_) {
93-
SET_REAL_ELT(data_, length_, value);
94-
} else {
95-
data_p_[length_] = value;
96-
}
97-
++length_;
98-
}
99-
10088
typedef r_vector<double> doubles;
10189

10290
} // namespace writable

inst/include/cpp11/integers.hpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace writable {
5858
template <>
5959
inline void r_vector<int>::set_elt(SEXP x, R_xlen_t i,
6060
typename r_vector::underlying_type value) {
61+
// NOPROTECT: Likely too costly to unwind protect every set elt
6162
SET_INTEGER_ELT(x, i, value);
6263
}
6364

@@ -85,20 +86,6 @@ inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
8586
}
8687
}
8788

88-
template <>
89-
inline void r_vector<int>::push_back(int value) {
90-
while (length_ >= capacity_) {
91-
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
92-
}
93-
if (is_altrep_) {
94-
// NOPROTECT: likely too costly to unwind protect every elt
95-
SET_INTEGER_ELT(data_, length_, value);
96-
} else {
97-
data_p_[length_] = value;
98-
}
99-
++length_;
100-
}
101-
10289
typedef r_vector<int> integers;
10390

10491
} // namespace writable

inst/include/cpp11/list.hpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace writable {
6868
template <>
6969
inline void r_vector<SEXP>::set_elt(SEXP x, R_xlen_t i,
7070
typename r_vector::underlying_type value) {
71+
// NOPROTECT: Likely too costly to unwind protect every set elt
7172
SET_VECTOR_ELT(x, i, value);
7273
}
7374

@@ -95,15 +96,6 @@ inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
9596
}
9697
}
9798

98-
template <>
99-
inline void r_vector<SEXP>::push_back(SEXP value) {
100-
while (length_ >= capacity_) {
101-
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
102-
}
103-
SET_VECTOR_ELT(data_, length_, value);
104-
++length_;
105-
}
106-
10799
typedef r_vector<SEXP> list;
108100

109101
} // namespace writable

inst/include/cpp11/logicals.hpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ namespace writable {
5757
template <>
5858
inline void r_vector<r_bool>::set_elt(SEXP x, R_xlen_t i,
5959
typename r_vector::underlying_type value) {
60+
// NOPROTECT: Likely too costly to unwind protect every set elt
6061
SET_LOGICAL_ELT(x, i, value);
6162
}
6263

@@ -99,19 +100,6 @@ inline r_vector<r_bool>::r_vector(std::initializer_list<named_arg> il)
99100
}
100101
}
101102

102-
template <>
103-
inline void r_vector<r_bool>::push_back(r_bool value) {
104-
while (length_ >= capacity_) {
105-
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
106-
}
107-
if (is_altrep_) {
108-
SET_LOGICAL_ELT(data_, length_, value);
109-
} else {
110-
data_p_[length_] = value;
111-
}
112-
++length_;
113-
}
114-
115103
typedef r_vector<r_bool> logicals;
116104

117105
} // namespace writable

inst/include/cpp11/r_vector.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ class r_vector : public cpp11::r_vector<T> {
225225
proxy at(const size_type pos) const;
226226
proxy at(const r_string& name) const;
227227

228-
/// Implemented in specialization
229228
void push_back(T value);
230229
/// Implemented in `strings.hpp`
231230
void push_back(const named_arg& value);
@@ -942,6 +941,21 @@ inline typename r_vector<T>::proxy r_vector<T>::at(const r_string& name) const {
942941
return operator[](name);
943942
}
944943

944+
template <typename T>
945+
inline void r_vector<T>::push_back(T value) {
946+
while (length_ >= capacity_) {
947+
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
948+
}
949+
950+
if (data_p_ != nullptr) {
951+
data_p_[length_] = static_cast<underlying_type>(value);
952+
} else {
953+
set_elt(data_, length_, static_cast<underlying_type>(value));
954+
}
955+
956+
++length_;
957+
}
958+
945959
template <typename T>
946960
inline void r_vector<T>::pop_back() {
947961
--length_;
@@ -1063,9 +1077,8 @@ inline typename r_vector<T>::proxy& r_vector<T>::proxy::operator=(const T& rhs)
10631077
if (p_ != nullptr) {
10641078
*p_ = elt;
10651079
} else {
1066-
// Handles ALTREP, VECSXP, and STRSXP cases.
1067-
// NOPROTECT: Likely too costly to unwind protect every set elt.
1068-
r_vector<T>::set_elt(data_, index_, elt);
1080+
// Handles ALTREP, VECSXP, and STRSXP cases
1081+
set_elt(data_, index_, elt);
10691082
}
10701083

10711084
return *this;

inst/include/cpp11/raws.hpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace writable {
6565
template <>
6666
inline void r_vector<uint8_t>::set_elt(SEXP x, R_xlen_t i,
6767
typename r_vector::underlying_type value) {
68+
// NOPROTECT: Likely too costly to unwind protect every set elt
6869
#if R_VERSION >= R_Version(4, 2, 0)
6970
SET_RAW_ELT(x, i, value);
7071
#else
@@ -97,20 +98,6 @@ inline r_vector<uint8_t>::r_vector(std::initializer_list<named_arg> il)
9798
}
9899
}
99100

100-
template <>
101-
inline void r_vector<uint8_t>::push_back(uint8_t value) {
102-
while (length_ >= capacity_) {
103-
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
104-
}
105-
if (is_altrep_) {
106-
// NOPROTECT: likely too costly to unwind protect every elt
107-
RAW(data_)[length_] = value;
108-
} else {
109-
data_p_[length_] = value;
110-
}
111-
++length_;
112-
}
113-
114101
typedef r_vector<uint8_t> raws;
115102

116103
} // namespace writable

inst/include/cpp11/strings.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ namespace writable {
5757
template <>
5858
inline void r_vector<r_string>::set_elt(SEXP x, R_xlen_t i,
5959
typename r_vector::underlying_type value) {
60+
// NOPROTECT: Likely too costly to unwind protect every set elt
6061
SET_STRING_ELT(x, i, value);
6162
}
6263

@@ -147,16 +148,6 @@ inline r_vector<r_string>::r_vector(std::initializer_list<named_arg> il)
147148
}
148149
}
149150

150-
template <>
151-
inline void r_vector<r_string>::push_back(r_string value) {
152-
while (length_ >= capacity_) {
153-
reserve(capacity_ == 0 ? 1 : capacity_ *= 2);
154-
}
155-
// NOPROTECT: likely too costly to unwind protect every elt
156-
SET_STRING_ELT(data_, length_, value);
157-
++length_;
158-
}
159-
160151
typedef r_vector<r_string> strings;
161152

162153
template <typename T>

0 commit comments

Comments
 (0)