Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composing ranges::view::cycle with ranges::view::slice #778

Closed
tremmelg opened this issue Feb 5, 2018 · 1 comment · Fixed by #913
Closed

Composing ranges::view::cycle with ranges::view::slice #778

tremmelg opened this issue Feb 5, 2018 · 1 comment · Fixed by #913
Labels

Comments

@tremmelg
Copy link

tremmelg commented Feb 5, 2018

Composing ranges::view::cycle with ranges::view::slice results in a view that returns begin and endpoints that compare equal. On the other hand, ranges::distance returns the expected 512. Also, see the example below.

#include <iostream>
#include <iterator>
#include <vector>

#include <range/v3/all.hpp>

int main() {
  const auto length = 512;
  const auto k = 16;

  std::vector<int> input(length);

  auto output = ranges::view::cycle(input)
              | ranges::view::slice(length + k, 2 * length + k);

  if (ranges::begin(output) == ranges::end(output)) {
    std::cout << "End equals begin!\n";
    std::cout << "Size: " << std::size(output) << "\n";
    std::cout << "Distance: " << ranges::distance(output) << "\n";
    return 1;
  }
}

Compiling the above example against 2ff4cf2 and running it gives:

% ./cycle_slice
End equals begin!
Size: 512
Distance: 512
@CaseyCarter CaseyCarter added the bug label Feb 5, 2018
@CaseyCarter
Copy link
Collaborator

CaseyCarter commented Feb 5, 2018

The bug here is that view::slice incorrectly believes that the bounded range [it, it + count) is equivalent to the counted range [it, count) when it is a RandomAccessIterator (See https://github.com/ericniebler/range-v3/blob/master/include/range/v3/view/slice.hpp#L142). I suggest replacing view::slice(first, last) with view::drop(first) | view::take(last - first) as a workaround until we get this fixed (a la https://wandbox.org/permlink/TbXQsmx0YfvQcRKj).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants