Skip to content

Commit

Permalink
Fix semantics of + and += for r_vector::iterator (#231)
Browse files Browse the repository at this point in the history
* r_vector::iterator: fix + and += semantics

* add NEWS entry
  • Loading branch information
alyst authored Aug 17, 2021
1 parent 4222cec commit dd26f8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cpp11 (development version)

* Fixed `+` and `+=` operators of `r_vector::[const_]iterator` to conform the *iterators* concept:
`+=` updates the iterator, and `+` returns the updated copy, while keeping the original unchanged (@alyst, #231)
* Remove undefined behavior when constructing global `cpp11::sexp`s (#224)
* `cpp_register()` now includes `attribute_visible` in the init function, so packages compiled with `C_VISIBILITY` will find the init function.
* added `as_double()` and `as_integer()` method to coerce integers to doubles and doubles to integers to doubles (@sbearrows, #46)
Expand Down
24 changes: 15 additions & 9 deletions inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class r_vector {

const_iterator(const r_vector* data, R_xlen_t pos);

inline const_iterator& operator+(R_xlen_t pos);
inline const_iterator operator+(R_xlen_t pos);
inline ptrdiff_t operator-(const const_iterator& other) const;

inline const_iterator& operator++();
Expand Down Expand Up @@ -275,7 +275,8 @@ class r_vector : public cpp11::r_vector<T> {

using cpp11::r_vector<T>::const_iterator::operator!=;

inline iterator& operator+(R_xlen_t rhs);
inline iterator& operator+=(R_xlen_t rhs);
inline iterator operator+(R_xlen_t rhs);
};

r_vector() = default;
Expand Down Expand Up @@ -498,13 +499,11 @@ inline ptrdiff_t r_vector<T>::const_iterator::operator-(
}

template <typename T>
inline typename r_vector<T>::const_iterator& r_vector<T>::const_iterator::operator+(
inline typename r_vector<T>::const_iterator r_vector<T>::const_iterator::operator+(
R_xlen_t rhs) {
pos_ += rhs;
if (data_->is_altrep() && pos_ >= block_start_ + length_) {
fill_buf(pos_);
}
return *this;
auto it = *this;
it += rhs;
return it;
}

template <typename T>
Expand Down Expand Up @@ -623,14 +622,21 @@ inline typename r_vector<T>::iterator& r_vector<T>::iterator::operator++() {
}

template <typename T>
inline typename r_vector<T>::iterator& r_vector<T>::iterator::operator+(R_xlen_t rhs) {
inline typename r_vector<T>::iterator& r_vector<T>::iterator::operator+=(R_xlen_t rhs) {
pos_ += rhs;
if (data_.is_altrep() && pos_ >= block_start_ + length_) {
fill_buf(pos_);
}
return *this;
}

template <typename T>
inline typename r_vector<T>::iterator r_vector<T>::iterator::operator+(R_xlen_t rhs) {
auto it = *this;
it += rhs;
return it;
}

template <typename T>
inline typename r_vector<T>::iterator r_vector<T>::begin() const {
return iterator(*this, 0);
Expand Down

0 comments on commit dd26f8e

Please sign in to comment.