Go: Scalable Permutations with QuickPerm
Memory efficient Go implementation of the QuickPerm algorithm.
package main
import (
"fmt"
)
func main() {
for perm := range GeneratePermutations([]int{1,2,3,4,5,6,7}){
fmt.Println(perm)
}
}
func GeneratePermutations(data []int) <-chan []int {
c := make(chan []int)
go func(c chan []int) {
defer close(c)
permutate(c, data)
}(c)
return c
}
func permutate(c chan []int, inputs []int){
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
size := len(inputs)
p := make([]int, size + 1)
for i := 0; i < size + 1; i++ {
p[i] = i;
}
for i := 1; i < size;{
p[i]--
j := 0
if i % 2 == 1 {
j = p[i]
}
tmp := inputs[j]
inputs[j] = inputs[i]
inputs[i] = tmp
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
for i = 1; p[i] == 0; i++{
p[i] = i
}
}
}