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

Enumerating permutations in-place #114

Open
gdalle opened this issue Nov 5, 2021 · 1 comment
Open

Enumerating permutations in-place #114

gdalle opened this issue Nov 5, 2021 · 1 comment

Comments

@gdalle
Copy link
Contributor

gdalle commented Nov 5, 2021

Hi there!

For one of my projects I wrote a small implementation of Heap's algorithm, which iterates through all permutations of an array by shuffling its elements in-place. It does so in a way that minimizes the number of moves.

I think this would deserve a place in Combinatorics.jl, but I can't figure out how to provide a useful generic implementation. Enumerating permutations in-place is only good if you do something at each step, so I'm not sure a standard iterator is the right choice. Right now, I'm leaning towards accepting a function as argument, which would be applied after each move. What do you think?

@mentics
Copy link

mentics commented Mar 7, 2023

I wonder if a standard iterator approach could make sense. The array is changing in place, so this likely would make sense for single threaded cases only. I think the state for the iterator could use the original array plus something to detect the last permutation. I think a for loop on that would do what was expected. There might be an issue with the standard implementation of collect, though, but that could be overridden to either throw an error, or to copy each permutation.
That said, though, I wonder if that would break some of the expectations around iterators. Maybe using foreach would fit better.

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

2 participants