rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.76k stars 309 forks source link

Const generic permutations #954

Closed FlareFlo closed 5 months ago

FlareFlo commented 5 months ago

In an application of mine i use a fixed size of permutations, which would cut down on some very wasteful temporary allocations. Therefore I am proposing a const-generic counterpart to the regular permutations.

This drafts purpose is to get some initial feedback on the idea. Do not expect feature completeness or sanity, it is 3AM. Though I do intend to properly implement this if interest exists.

Philippe-Cholet commented 5 months ago

Unless the lexicographic order of the permutations is important for you, the permutohedron crate should plainly satify you (created by bluss who first wrote itertools). It should even be faster than a const-generics version of our permutations, due to the algorithm.

Now, some context... Right now, our MSRV is 1.43.1 while const-generics require 1.51.0. We will move to it in a near future though. But core::array::from_fn requires 1.63.0 and core::array::try_from_fn is still nightly, same for Iterator::next_chunk. So we are missing some helpful bricks at the moment.

This will eventually be done along with combinations & Co. but not immediately. #560 would be a more intermediary step.

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 0% with 97 lines in your changes are missing coverage. Please review.

Project coverage is 93.23%. Comparing base (6814180) to head (80fc48d). Report is 99 commits behind head on master.

Files Patch % Lines
src/permutations_const.rs 0.00% 90 Missing :warning:
src/lib.rs 0.00% 7 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #954 +/- ## ========================================== - Coverage 94.38% 93.23% -1.16% ========================================== Files 48 50 +2 Lines 6665 7143 +478 ========================================== + Hits 6291 6660 +369 - Misses 374 483 +109 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

FlareFlo commented 5 months ago

Unless the lexicographic order of the permutations is important for you, the permutohedron crate should plainly satify you (created by bluss who first wrote itertools). It should even be faster than a const-generics version of our permutations, due to the algorithm.

Now, some context... Right now, our MSRV is 1.43.1 while const-generics require 1.51.0. We will move to it in a near future though. But core::array::from_fn requires 1.63.0 and core::array::try_from_fn is still nightly, same for Iterator::next_chunk. So we are missing some helpful bricks at the moment.

This will eventually be done along with combinations & Co. but not immediately. #560 would be a more intermediary step.

permutohedron covers my case, thanks!

Seeing that the MSRV is restricted like that, should I close this?

Philippe-Cholet commented 5 months ago

If you want to revisit this when a more appriopriate time come then we can re-open it.