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

Powerset count #735

Merged
merged 11 commits into from
Aug 26, 2023
Merged

Powerset count #735

merged 11 commits into from
Aug 26, 2023

Conversation

Philippe-Cholet
Copy link
Member

@Philippe-Cholet Philippe-Cholet commented Aug 22, 2023

@phimuemue After counting combinations last week, I thought a simple PR would be nice to start the week.

The first three commits are about Powerset::count while the next four commits are about Powerset::size_hint.

@Philippe-Cholet
Copy link
Member Author

Powerset::count needs n = pool.count() and self.combs.count() which also needs n. Without filling the lazy buffer, we can not compute the real n twice. That's why Combinations::n_and_count exists, to be able to re-use n.

I replaced Powerset::size_hint implementation (and removed the pos field since it is its only purpose) because it removes the fallback (when self.pos overflows, which seems unlikely to me but who knows).
Then I was unsure what to do with size_hint::pow_scalar_base: remove it or allow it to be dead code? I temporarily chose the second.
Feel free to cherry-pick the first three commits if you don't like my size_hint changes.

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.

Hi @Philippe-Cholet, thanks for this. I really appreciate the small changes.

I think the computations are correct, although non-tivial. And in some cases I wondered if powerset.count/size_hint could more clearly delegate to combinations.count/size_hint, but so far I cannot point my finger exactly where that could easily be achieved.

However, please add some tests that also test combinations for iterators with unreliable size hints (as it stands, the underlying iterator's in the tests seem to have exact size_hints, so maybe use a filtered one).

src/powerset.rs Outdated Show resolved Hide resolved
tests/test_std.rs Outdated Show resolved Hide resolved
src/powerset.rs Outdated Show resolved Hide resolved
src/powerset.rs Outdated Show resolved Hide resolved
src/size_hint.rs Outdated Show resolved Hide resolved
src/powerset.rs Outdated Show resolved Hide resolved
@Philippe-Cholet
Copy link
Member Author

Tomorrow, I'll add tests with (0..5).filter(|i| i % 2 == 0).chain(5..10).combinations(k)/powerset() which will have nice size hints. I also thought it would be interesting to test.
Otherwise, I think I answered all points.

@Philippe-Cholet
Copy link
Member Author

I can do a similar test for powerset if you want but I'm gonna wait for your feedback first.

@phimuemue
Copy link
Member

Thank you a lot!

bors r+

As for the new tests: I’m not sure if they too closely reimplement the internal logic and assert that the reimplementation matches the original.

I will have a closer look later. I think what I’d test is that the size hints do not become only more exact over time and that they are consistent with count.

@bors
Copy link
Contributor

bors bot commented Aug 26, 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 build finished

@bors bors bot merged commit f49c53b into rust-itertools:master Aug 26, 2023
@Philippe-Cholet Philippe-Cholet deleted the powerset_count branch September 6, 2023 17:25
@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.

3 participants