ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.12k stars 440 forks source link

Cardinalities on sets of ranges (add utilities?) #429

Open gnzlbg opened 8 years ago

gnzlbg commented 8 years ago

When writing a new view it is very common to need to compute the cardinality of a set of ranges. For example:

Do you think it is worth it to add general utilities for these? For example could these utilities be reused to compute "more complex" cardinalities easier (like view::join's cardinality?).

Are there other "common" cardinalities that one often needs on sets of ranges?

Also, for single "range" views, most of them do is_finite<Rng>::value ? finite : range_cardinality<Rng>::value. I wonder why range_cardinality<Rng>::value isn't enough here.

ericniebler commented 8 years ago

Also, for single "range" views, most of them do is_finite<Rng>::value ? finite : range_cardinality<Rng>::value. I wonder why range_cardinality<Rng>::value isn't enough here.

Some ranges know at compile time not only that they are finite but also how many elements they have (e.g., view::single). The range cardinality can be used to express that. is_finite tests for both cases: finite but unknown at compile-time (e.g. std::vector) or finite with a particular value.

EDIT: Range cardinality was a bit of an experiment. I'm not particularly happy with the amount of complexity it introduces. It's nice to tell the finite ranges apart from the infinite ranges, but even that may not be worth it.

jaredhoberock commented 7 years ago

Are there other "common" cardinalities that one often needs on sets of ranges?

I'm not sure how common this use case is, but for the domain I tend to do work in, it's advantageous to know at compile time that a range's size is bounded by some constant. Exploiting this kind of static information enables optimizations such as loop unrolling and locating data in registers. For example, these kinds of optimizations are essentially mandatory for writing competent GPU algorithm libraries. I wouldn't be surprised to find these sorts of considerations in other domains. It would be nice if there was an elegant way to reason about range cardinalities that are stronger than finite and unknown but weaker than some particular finite value.

ericniebler commented 7 years ago

Interesting! Thanks for the information. We value this kind of domain-specific experience.