| 1 |
// Copyright ©2011-2012 The bíogo Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package util |
| 6 |
|
| 7 |
// Return a deBruijn sequence for a n-words with k letters. |
| 8 |
func DeBruijn(k, n byte) (s []byte) { |
| 9 |
switch k { |
| 10 |
case 0: |
| 11 |
return []byte{} |
| 12 |
case 1: |
| 13 |
return make([]byte, n) |
| 14 |
} |
| 15 |
|
| 16 |
a := make([]byte, k*n) |
| 17 |
s = make([]byte, 0, Pow(int(k), n)) |
| 18 |
|
| 19 |
var db func(byte, byte) |
| 20 |
db = func(t, p byte) { |
| 21 |
if t > n { |
| 22 |
if n%p == 0 { |
| 23 |
for j := byte(1); j <= p; j++ { |
| 24 |
s = append(s, a[j]) |
| 25 |
} |
| 26 |
} |
| 27 |
} else { |
| 28 |
a[t] = a[t-p] |
| 29 |
db(t+1, p) |
| 30 |
for j := a[t-p] + 1; j < k; j++ { |
| 31 |
a[t] = j |
| 32 |
db(t+1, t) |
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
} |
| 37 |
db(1, 1) |
| 38 |
|
| 39 |
return |
| 40 |
} |