-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermutation.go
99 lines (91 loc) · 1.9 KB
/
permutation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package next
// Returns a channel of permutations of n element from base w/o repetition
func Permutation(base []interface{}, n int, repeat bool) <-chan []interface{} {
if n < 0 {
n = 0
}
if repeat {
return repeatPermutation(base).of(n)
} else {
return permutation(base).of(n)
}
}
// A combination of elements.
type permutation []interface{}
// Returns a channel of possible combinations of l elements.
func (p permutation) of(r int) <-chan []interface{} {
res := make(chan []interface{})
go p.results(r, res)
return res
}
func (p permutation) results(r int, ch chan<- []interface{}) {
defer close(ch)
n := len(p)
if r > n {
return
}
idxs := make([]int, n)
for i := range idxs {
idxs[i] = i
}
cycles := make([]int, r)
for i := range cycles {
cycles[i] = n - i
}
cmb := make([]interface{}, r)
res := make([]interface{}, r)
for i, el := range idxs[:r] {
cmb[i] = p[el]
}
copy(res, cmb)
ch <- res
for n > 0 {
i := r - 1
for ; i >= 0; i -= 1 {
cycles[i] -= 1
if cycles[i] == 0 {
index := idxs[i]
for j := i; j < n-1; j += 1 {
idxs[j] = idxs[j+1]
}
idxs[n-1] = index
cycles[i] = n - i
} else {
j := cycles[i]
idxs[i], idxs[n-j] = idxs[n-j], idxs[i]
for k := i; k < r; k += 1 {
cmb[k] = p[idxs[k]]
}
res := make([]interface{}, r)
copy(res, cmb)
ch <- res
break
}
}
if i < 0 {
return
}
}
}
// A combination of elements.
type repeatPermutation []interface{}
// Returns a channel of possible combinations of l elements.
func (p repeatPermutation) of(r int) <-chan []interface{} {
res := make(chan []interface{})
go p.results(r, res)
return res
}
func (p repeatPermutation) results(r int, ch chan<- []interface{}) {
defer close(ch)
n, t := len(p), count(p, r)
for i := 0; i < t; i++ {
v := make([]interface{}, r)
j := i
for k := 0; k < r; k++ {
x := j % n
j = int(j / n)
v[k] = p[x]
}
ch <- v
}
}