]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix ranges::shuffle for non-sized range [PR121917]
authorPatrick Palka <ppalka@redhat.com>
Sat, 13 Sep 2025 14:44:12 +0000 (10:44 -0400)
committerPatrick Palka <ppalka@redhat.com>
Sat, 13 Sep 2025 14:44:12 +0000 (10:44 -0400)
commit349affa42c9fa47a12eb7f5f97f5650a77bbf014
tree9b87146b71a448d8b29c750095826b0ce9b3c764
parentf8b6f1d83ba8fd4186100e1e357ba7eb8124bdb7
libstdc++: Fix ranges::shuffle for non-sized range [PR121917]

ranges::shuffle has a two-at-a-time PRNG optimization (copied from
std::shuffle) that considers the PRNG width vs the size of the range.
But in C++20 a random access sentinel isn't always sized so we can't
unconditionally do __last - __first to obtain the size in constant
time.

We could instead use ranges::distance, but that'd take linear time for a
non-sized sentinel which makes the optimization less clear of a win.  So
this patch instead makes us only consider this optimization for sized
ranges.

PR libstdc++/121917

libstdc++-v3/ChangeLog:

* include/bits/ranges_algo.h (__shuffle_fn::operator()): Only
consider the two-at-a-time PRNG optimization if the range is
sized.
* testsuite/25_algorithms/shuffle/constrained.cc (test03): New
test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/include/bits/ranges_algo.h
libstdc++-v3/testsuite/25_algorithms/shuffle/constrained.cc