-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
176 lines (152 loc) · 4.46 KB
/
counter.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package combination
import (
"strconv"
"github.com/fbigand/combination/util"
)
// counter is used internally by CombinationIterator.
// At each step, it will give how many elements from which
// position in the list the combination will have.
//
// Example: first values starting with initialValue = 6
// 1: 6
// 2: 5 1
// 3: 4 2
// 4: 3 3
// 5: 2 4
// 6: 1 5
// 7: 0 6
// 8: 1 0 5
type counter struct {
initialValue uint
state []uint
endState []uint
combinationUnsetIndex int
}
// counterCombsCache record counter combinations that has already been computed
// by the method GetCombinations
var counterCombinationsCache map[string][]Combination = make(map[string][]Combination)
// newCounter creates and returns a new counter.
// See CombinationIterator documentation for more information.
func newCounter(initialValue uint, endState []uint) *counter {
c := counter{}
c.initialValue = initialValue
c.combinationUnsetIndex = int(initialValue) + 1
c.state = make([]uint, 2)
c.state[0] = initialValue
if util.MaxUint(endState...) > initialValue {
panic("newCounter: endState cannot be reached with these parameters")
}
if util.SumUint(endState...) != initialValue {
panic("newCounter: endState sum must be equal to initialValue")
}
c.endState = endState
return &c
}
func (c *counter) getStateToString() string {
strState := ""
for _, v := range c.state {
strState += strconv.FormatUint(uint64(v), 10)
}
return strState
}
// next set the counter to the next position. Undoable.
//
// Returns true if the operation was successful. Returns false
// if reached the end state
func (c *counter) next() bool {
hasNext := true
if util.EqualsSliceUint(c.state, c.endState) {
hasNext = false
} else {
c.incrementState()
}
return hasNext
}
func (c *counter) incrementState() {
if c.state[0] == 0 {
iReset := 1
for c.state[iReset] == 0 {
iReset++
}
c.state[iReset] = 0
// prevent out of range
if iReset+1 == len(c.state) {
c.state = append(c.state, 0)
// c.state = c.state[:len(c.state)+1]
}
c.state[iReset+1]++
c.state[0] = c.initialValue - util.SumUint(c.state...)
} else {
c.state[0]--
c.state[1]++
}
}
func (c *counter) getCombinations() []Combination {
var combinations []Combination
strState := c.getStateToString()
cachedCombs, combsInCache := counterCombinationsCache[strState]
if combsInCache {
combinations = cachedCombs
} else {
combinations = c.computeCounterCombinations(len(c.state) - 1)
counterCombinationsCache[strState] = combinations
}
return combinations
}
func (c *counter) computeCounterCombinations(index int) []Combination {
combinations := make([]Combination, 0)
if index != -1 {
lowerIndexCombs := c.computeCounterCombinations(index - 1)
if len(lowerIndexCombs) == 0 {
combinations = c.startCombinationsCreation(index)
} else {
combinations = c.updateLowerIndexCombinations(index, lowerIndexCombs)
}
}
return combinations
}
func (c *counter) startCombinationsCreation(index int) []Combination {
combinations := make([]Combination, 0)
binomialCombs := getBinomialCombinations(c.state[index], c.initialValue)
for _, binomialComb := range binomialCombs {
combination := make(Combination, c.initialValue)
util.InitSliceInt(combination, c.combinationUnsetIndex)
for _, v := range binomialComb {
combination[v] = index
}
combinations = append(combinations, combination)
}
return combinations
}
func (c *counter) updateLowerIndexCombinations(index int, lowerIndexCombs []Combination) []Combination {
var combinations []Combination
binomialCombs := getBinomialCombinations(c.state[index], c.initialValue)
if len(binomialCombs) == 0 {
combinations = lowerIndexCombs
} else {
combinations = make([]Combination, 0)
for _, binomialComb := range binomialCombs {
for _, lowerIndexComb := range lowerIndexCombs {
// check if values in lowerIndexComb not already taken
anySpotTaken := false
for _, v := range binomialComb {
if lowerIndexComb[v] != c.combinationUnsetIndex {
anySpotTaken = true
break
}
}
if !anySpotTaken {
// combine lower index combination with binomial combination
// eg: [7, 7, 7, 0, 0, 7] + [1, 2, 5] -> [7, 1, 1, 0, 0, 1]
combination := make(Combination, c.initialValue)
copy(combination, lowerIndexComb)
for _, v := range binomialComb {
combination[v] = index
}
combinations = append(combinations, combination)
}
}
}
}
return combinations
}