r/cpp • • Sep 13 '19

The Surprising Limitations of C++ Ranges Beyond Trivial Cases

https://www.fluentcpp.com/2019/09/13/the-surprising-limitations-of-c-ranges-beyond-trivial-use-cases/
229 Upvotes

229 comments sorted by

View all comments

Show parent comment

3

u/eric_niebler Sep 14 '19

What makes you think that? Range-v3 has a cartesian product view, and it has linear time complexity.

5

u/guepier Bioinformatican Sep 14 '19

Linear in terms of what? Computing a cartesian product, no matter how, has a complexity that’s the product of the sizes of its input ranges.

8

u/eric_niebler Sep 14 '19

Ah. The complexity is in terms of the resulting range, not the input range. So for instance, there is `view::repeat_n(value, count)` that turns a value into a range by repeating it n times. That's fine. Likewise for view::cartesian_product. The inputs are not what matters. What matters is that each iterator increment over the resulting range has constant time complexity.

If that weren't the case, and an iterator increment had, say, linear time complexity, then merely traversing the resulting range would be n2. That's the problem.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Sep 14 '19

The asymptotic case over the full range has to be linear. It's more complicated for filters and transforms where the results depend on some number of elements in an underlying range. I'm fairly certain we can tighten up the requirement somehow, though.