MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / BuildAc

Function BuildAc

strings/ahocorasick/ahocorasick.go:54–77  ·  view source on GitHub ↗

Functions that builds Aho Corasick automaton.

(p []string)

Source from the content-addressed store, hash-verified

52
53// Functions that builds Aho Corasick automaton.
54func 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}

Callers 1

AhoCorasickFunction · 0.85

Calls 5

ConstructTrieFunction · 0.85
GetParentFunction · 0.85
StateExistsFunction · 0.85
GetTransitionFunction · 0.85
ArrayUnionFunction · 0.85

Tested by

no test coverage detected