From f6e212fad9d95afb89dea5b0b1aeeb6a533a50cf Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Fri, 9 Aug 2024 17:16:56 -0400 Subject: [PATCH] Unify `const_iterator::operator*()` --- inst/include/cpp11/list.hpp | 5 ----- inst/include/cpp11/r_vector.hpp | 6 ++++-- inst/include/cpp11/strings.hpp | 5 ----- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/inst/include/cpp11/list.hpp b/inst/include/cpp11/list.hpp index 697416d1..e5794606 100644 --- a/inst/include/cpp11/list.hpp +++ b/inst/include/cpp11/list.hpp @@ -56,11 +56,6 @@ inline bool r_vector::const_iterator::use_buf(bool is_altrep) { return false; } -template <> -inline SEXP r_vector::const_iterator::operator*() const { - return VECTOR_ELT(data_->data(), pos_); -} - typedef r_vector list; namespace writable { diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index 96c904af..48268937 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -697,10 +697,12 @@ inline typename r_vector::const_iterator r_vector::find( template inline T r_vector::const_iterator::operator*() const { - if (data_->is_altrep()) { + if (use_buf(data_->is_altrep())) { + // Use pre-loaded buffer for compatible ALTREP types return static_cast(buf_[pos_ - block_start_]); } else { - return static_cast(data_->data_p_[pos_]); + // Otherwise pass through to normal retrieval method + return data_->operator[](pos_); } } diff --git a/inst/include/cpp11/strings.hpp b/inst/include/cpp11/strings.hpp index 94ed7a43..8a33b5ab 100644 --- a/inst/include/cpp11/strings.hpp +++ b/inst/include/cpp11/strings.hpp @@ -45,11 +45,6 @@ inline bool r_vector::const_iterator::use_buf(bool is_altrep) { return false; } -template <> -inline r_string r_vector::const_iterator::operator*() const { - return STRING_ELT(data_->data(), pos_); -} - typedef r_vector strings; namespace writable {