ericniebler / range-v3

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

drop_while view and sorted range : possible optimization? #1746

Open ghost opened 1 year ago

ghost commented 1 year ago

Hi, Reading drop_while page I think it would be handy to be able to tell that we are iterating an already sorted range, when applicable.

Right now, it seems to iterate every elements, whereas it could use a binary search to go faster.

JohelEGP commented 1 year ago

So you want an optimized drop_while where the range is_partitioned on the predicate.

ghost commented 1 year ago

Actually my request holds for take_while and filter as well.

Range may be partitioned or not. But always sorted in regard to the criteria.

beojan commented 1 year ago

Do you mean you would pass an argument telling it if the range is sorted? Otherwise, determining if the range is sorted would require iterating over every element anyway.

ghost commented 1 year ago

Yes a boolean, Or a ::ranges::views::already_sorted flag.. like so

auto v = my_container | ::ranges::views::already_sorted | the rest

At this point, the dev takes the responsibility of this criteria and that the following drop_while/take_while/filter uses are coherent with this flag. otherwise, don't use this flag, and do just like right now.

It is just a speed optimization opportunity that may be used or not.