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

Complete Permutations::size_hint #739

Merged
merged 9 commits into from Sep 6, 2023

Conversation

Philippe-Cholet
Copy link
Member

@phimuemue
This series of size hint/count improvements ends where it started: with a TODO I wanted to fix.
The end user will mostly enjoy that (0..n).permutations(k).map(...).collect_vec() will now allocate to the resulting vector in one go (PermutationState::StartUnknownLen case).

In the first commit, you probably will want me to unwrap but panic!(message) is more similar to expect than unwrap. Which is why I previously used expect too.
The 2nd commit (about enough_vals) is off topic but I really don't see why it would not be a (minor) improvement.
I have a test permutations_inexact_size_hints ready if you want (similar to combinations_inexact_size_hints).

Known/Overflow variants have the advantage of the readability but it's less handy than options. A doc comment seems enough to me to say that None means it would overflow.
Clearer.
And `prefill` extending the buffer seems better performance wise than push items to the buffer one by one with `get_next`.
We are at the very beginning. We prefilled `k` values, but did not generate any item yet. There are `n!/(n-k)!` items to come (safely done in remaining). But `n` might be unknown.
We generated `prev_iteration_count` items (`n` still unknown).
So the same as `PermutationState::StartUnknownLen` minus `prev_iteration_count`.
Copy link
Member

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this!

tests/test_std.rs Show resolved Hide resolved
src/permutations.rs Outdated Show resolved Hide resolved
Copy link
Member

@phimuemue phimuemue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/permutations.rs Show resolved Hide resolved
src/permutations.rs Outdated Show resolved Hide resolved
Ideally, the lower bound of the size hint would remain `usize::MAX` but I don't see how we could make this happen.
Small bugfix: `n+1` might overflow.
@jswrenn
Copy link
Member

jswrenn commented Sep 6, 2023

bors r+

@bors
Copy link
Contributor

bors bot commented Sep 6, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 31d598f into rust-itertools:master Sep 6, 2023
10 checks passed
@Philippe-Cholet Philippe-Cholet deleted the permutations-size-hint branch September 6, 2023 15:26
@jswrenn jswrenn added this to the next milestone Nov 14, 2023
@jswrenn jswrenn mentioned this pull request Nov 14, 2023
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

Successfully merging this pull request may close these issues.

None yet

3 participants