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

Function ConstructTrie

strings/ahocorasick/shared.go:4–36  ·  view source on GitHub ↗

ConstructTrie Function that constructs Trie as an automaton for a set of reversed & trimmed strings.

(p []string)

Source from the content-addressed store, hash-verified

2
3// ConstructTrie Function that constructs Trie as an automaton for a set of reversed & trimmed strings.
4func ConstructTrie(p []string) (trie map[int]map[uint8]int, stateIsTerminal []bool, f map[int][]int) {
5 trie = make(map[int]map[uint8]int)
6 stateIsTerminal = make([]bool, 1)
7 f = make(map[int][]int)
8 state := 1
9 CreateNewState(0, trie)
10 for i := 0; i < len(p); i++ {
11 current := 0
12 j := 0
13 for j < len(p[i]) && GetTransition(current, p[i][j], trie) != -1 {
14 current = GetTransition(current, p[i][j], trie)
15 j++
16 }
17 for j < len(p[i]) {
18 stateIsTerminal = BoolArrayCapUp(stateIsTerminal)
19 CreateNewState(state, trie)
20 stateIsTerminal[state] = false
21 CreateTransition(current, p[i][j], state, trie)
22 current = state
23 j++
24 state++
25 }
26 if stateIsTerminal[current] {
27 newArray := IntArrayCapUp(f[current])
28 newArray[len(newArray)-1] = i
29 f[current] = newArray // F(Current) <- F(Current) union {i}
30 } else {
31 stateIsTerminal[current] = true
32 f[current] = []int{i} // F(Current) <- {i}
33 }
34 }
35 return trie, stateIsTerminal, f
36}
37
38// Contains Returns 'true' if array of int's 's' contains int 'e', 'false' otherwise.
39func Contains(s []int, e int) bool {

Callers 2

BuildAcFunction · 0.85
BuildExtendedAcFunction · 0.85

Calls 5

CreateNewStateFunction · 0.85
GetTransitionFunction · 0.85
BoolArrayCapUpFunction · 0.85
CreateTransitionFunction · 0.85
IntArrayCapUpFunction · 0.85

Tested by

no test coverage detected