Functions that builds Aho Corasick automaton.
(p []string)
| 52 | |
| 53 | // Functions that builds Aho Corasick automaton. |
| 54 | func BuildAc(p []string) (acToReturn map[int]map[uint8]int, f map[int][]int, s []int) { |
| 55 | acTrie, stateIsTerminal, f := ConstructTrie(p) |
| 56 | s = make([]int, len(stateIsTerminal)) //supply function |
| 57 | i := 0 //root of acTrie |
| 58 | acToReturn = acTrie |
| 59 | s[i] = -1 |
| 60 | for current := 1; current < len(stateIsTerminal); current++ { |
| 61 | o, parent := GetParent(current, acTrie) |
| 62 | down := s[parent] |
| 63 | for StateExists(down, acToReturn) && GetTransition(down, o, acToReturn) == -1 { |
| 64 | down = s[down] |
| 65 | } |
| 66 | if StateExists(down, acToReturn) { |
| 67 | s[current] = GetTransition(down, o, acToReturn) |
| 68 | if stateIsTerminal[s[current]] { |
| 69 | stateIsTerminal[current] = true |
| 70 | f[current] = ArrayUnion(f[current], f[s[current]]) //F(Current) <- F(Current) union F(S(Current)) |
| 71 | } |
| 72 | } else { |
| 73 | s[current] = i //initial state? |
| 74 | } |
| 75 | } |
| 76 | return acToReturn, f, s |
| 77 | } |
no test coverage detected