odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.13k stars 551 forks source link

Add in-place permutation iterator to `core:slice` #3716

Closed Feoramund closed 3 weeks ago

Feoramund commented 3 weeks ago

I evaluated the Steinhaus-Johnston-Trotter algorithm (with Shimon Even's improvement), Heap's algorithm, and the Alternate Ive's algorithm and found this one (Heap's) to be the simplest to implement and the fastest (same conclusion as Robert Sedgewick in 1977).

Note that the iterator only returns a boolean. I figured it would make the most sense not to return the slice over again, to lessen any confusion about this being an in-place iterator versus a version that duplicates every permutation (which would be an easy thing to layer on top of this, if someone wanted).

Kelimion commented 3 weeks ago

Useful and elegant. Thanks.