MCPcopy Create free account
hub / github.com/egonelbre/exp / CodeShuffle

Function CodeShuffle

permutation/code.go:150–189  ·  view source on GitHub ↗
(perm [base]byte)

Source from the content-addressed store, hash-verified

148}
149
150func CodeShuffle(perm [base]byte) int {
151 var state = [base]byte{
152 0, 1, 2, 3,
153 4, 5, 6, 7,
154 8, 9, 10, 11,
155 }
156 var inverse = [base]byte{
157 0, 1, 2, 3,
158 4, 5, 6, 7,
159 8, 9, 10, 11,
160 }
161 for i := range perm {
162 /*
163 * this cryptic looking code is an optimized version of
164 * this pseudocode:
165 *
166 * j = inverse[perm[i]]
167 * swap entries state[i] and state[j] in inverse
168 * swap entries i and j in state
169 * perm[i] = j - i
170 *
171 * two optimizations are performed:
172 * - inverse[state[k]] == k and state[inverse[k]] == k
173 * - after iteration i, inverse[perm[i]] and state[i]
174 * are never read again, so we can omit assigning to
175 * them.
176 */
177 j := inverse[perm[i]]
178 inverse[state[i]] = j
179 state[j] = state[i]
180 perm[i] = j - byte(i)
181 }
182
183 total := 0
184 for i, v := range perm {
185 total += int(v) * fact[i]
186 }
187
188 return total
189}
190
191func CodeShuffleUnroll(perm [base]byte) int {
192 var state = [base]byte{

Callers 2

TestShuffleRandomFunction · 0.85
BenchmarkShuffleFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestShuffleRandomFunction · 0.68
BenchmarkShuffleFunction · 0.68