Skip to content

Commit

Permalink
locator: token_metadata: remove _ring_pos from tokens_iterator_impl
Browse files Browse the repository at this point in the history
_ring_pos is slightly confusing. I thought at first that it doesn't do anything
since operator== doesn't use it.
This cosmetic patch tries to improve the readability, and also removes
operator!= which is generated automatically in C++20.
  • Loading branch information
michoecho committed Jun 7, 2021
1 parent 30e5290 commit baaac5b
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions locator/token_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -888,49 +888,34 @@ class tokens_iterator_impl {
using difference_type = std::ptrdiff_t;
using pointer = token*;
using reference = token&;
private:
tokens_iterator_impl(std::vector<token>::const_iterator it, size_t pos)
: _cur_it(it), _ring_pos(pos) {}

public:
tokens_iterator_impl() = default;
tokens_iterator_impl(const token& start, const token_metadata_impl* token_metadata)
: _token_metadata(token_metadata) {
_cur_it = _token_metadata->sorted_tokens().begin() + _token_metadata->first_token_index(start);
_remaining = _token_metadata->sorted_tokens().size();
}

bool operator==(const tokens_iterator_impl& it) const {
return _cur_it == it._cur_it;
}

bool operator!=(const tokens_iterator_impl& it) const {
return _cur_it != it._cur_it;
return _remaining == it._remaining;
}

const token& operator*() {
return *_cur_it;
}

tokens_iterator_impl& operator++() {
if (_ring_pos >= _token_metadata->sorted_tokens().size()) {
_cur_it = _token_metadata->sorted_tokens().end();
} else {
++_cur_it;
++_ring_pos;

if (_cur_it == _token_metadata->sorted_tokens().end()) {
_cur_it = _token_metadata->sorted_tokens().begin();
}
++_cur_it;
if (_cur_it == _token_metadata->sorted_tokens().end()) {
_cur_it = _token_metadata->sorted_tokens().begin();
}
--_remaining;
return *this;
}

private:
std::vector<token>::const_iterator _cur_it;
//
// position on the token ring starting from token corresponding to
// "start"
//
size_t _ring_pos = 0;
size_t _remaining = 0;
const token_metadata_impl* _token_metadata = nullptr;

friend class token_metadata_impl;
Expand All @@ -940,7 +925,7 @@ inline
boost::iterator_range<token_metadata_impl::tokens_iterator>
token_metadata_impl::ring_range(const token& start) const {
auto begin = tokens_iterator(start, this);
auto end = tokens_iterator(sorted_tokens().end(), sorted_tokens().size());
auto end = tokens_iterator();
return boost::make_iterator_range(begin, end);
}

Expand Down

0 comments on commit baaac5b

Please sign in to comment.