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

[alg.random.shuffle] ranges::shuffle - "exactly" or "at most" for number of swaps? #5636

Open
JoeLoser opened this issue Jul 22, 2022 · 5 comments

Comments

@JoeLoser
Copy link

JoeLoser commented Jul 22, 2022

In [alg.random.shuffle] section, for ranges::shuffle, the standard states that the complexity is exactly (last - first) - 1) swaps. In some other algorithms, the wording of at most for the number of swaps is used.

Should we change the wording for ranges::shuffle to say at most instead of exactly for the number of swaps? It would be nice for a standard library implementation to perform fewer swaps than required where possible and still be conforming. If not, can someone provide clarity on why there must be exactly this number of swaps in all cases?

This came up during discussion in the libc++ implementation of ranges::shuffle here

@frederick-vs-ja
Copy link
Contributor

I think this issue fits in the scope of LWG issues rather than editorial ones.

@timsong-cpp
Copy link
Contributor

The two have the same effect per [structure.specifications]/7:

Complexity requirements specified in the library clauses are upper bounds, and implementations that provide better complexity guarantees meet the requirements.

@CaseyCarter
Copy link
Contributor

The two have the same effect per [structure.specifications]/7:

Perhaps this should be repurposed as an editorial issue asking why we sometimes say "at most" and sometimes "exactly" instead of picking a single form to use consistently?

@JoeLoser
Copy link
Author

The two have the same effect per [structure.specifications]/7:

Perhaps this should be repurposed as an editorial issue asking why we sometimes say "at most" and sometimes "exactly" instead of picking a single form to use consistently?

Thanks for the clarification, @timsong-cpp. As Casey said, I'd like to understand if there's a good reason for the difference in the use of "at most" versus "exactly" instead of one consistent form throughout.

@Eric41293
Copy link
Contributor

"Exactly" is clearly erroneous. For example, if the range has size 2, then it would require the algorithm to always swap them or to swap an element with itself. More generally, requiring a specific number of swaps excludes half the possible shuffles, except by using a self-swap.

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

No branches or pull requests

5 participants