Skip to content

Commit

Permalink
Fix iterators when advancing to negative numbers
Browse files Browse the repository at this point in the history
in this case both n and diff are already negative, so we want to use +=,
not -=
  • Loading branch information
jimhester committed Nov 9, 2021
1 parent 8ef5c86 commit 67f3eba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/delimited_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ class delimited_index : public index,
return i_ == static_cast<const column_iterator*>(&it)->i_;
}
ptrdiff_t distance_to(const base_iterator& it) const override {
return (static_cast<ptrdiff_t>(
static_cast<const column_iterator*>(&it)->i_) -
static_cast<ptrdiff_t>(i_)) /
ptrdiff_t(idx_->columns_);
ptrdiff_t i = i_;
ptrdiff_t j = static_cast<const column_iterator*>(&it)->i_;
ptrdiff_t columns = idx_->columns_;
return (j - i) / columns;
}

string value() const override {
return idx_->get_trimmed_val(i_, is_first_, is_last_);
}
Expand Down
8 changes: 4 additions & 4 deletions src/index_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ void index_collection::full_iterator::advance(ptrdiff_t n) {
}
if (n < 0) {
while (n < 0) {
auto diff = it_start_ - it_;
auto diff = -(it_ - it_start_);
if (n > diff) {
it_ -= n;
it_ += n;
return;
}
it_ -= (diff + 1);
n += diff;
it_ += (diff + 1);
n -= diff;
prev();
}
return;
Expand Down

0 comments on commit 67f3eba

Please sign in to comment.