-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.go
64 lines (57 loc) · 1.48 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package gmars
import "slices"
// buildReferenceGraph takes a map of expresions and builds a map representing
// a graph of references where each key has a slice of symbols referenced by
// that symbol's tokens
func buildReferenceGraph(values map[string][]token) map[string][]string {
graph := make(map[string][]string)
for key, tokens := range values {
if len(tokens) == 0 {
continue
}
keyRefs := make([]string, 0)
for _, tok := range tokens {
if tok.typ != tokText {
continue
}
_, ok := values[tok.val]
if ok {
if slices.Contains(keyRefs, tok.val) {
continue
}
keyRefs = append(keyRefs, tok.val)
}
}
graph[key] = keyRefs
}
return graph
}
// nodeContainsCycle checks for a cycle in graph by performing a depth first traversal
// recursively, starting from node, and passing the visited nodes to stop if a cycle
// is found
func nodeContainsCycle(node string, graph map[string][]string, visited []string) (bool, string) {
visited = append(visited, node)
symRefs, ok := graph[node]
if !ok {
return false, ""
}
for _, ref := range symRefs {
if slices.Contains(visited, ref) {
return true, ref
}
subCycle, key := nodeContainsCycle(ref, graph, visited)
if subCycle {
return true, key
}
}
return false, ""
}
func graphContainsCycle(graph map[string][]string) (bool, string) {
for key := range graph {
nodeCycle, cycleKey := nodeContainsCycle(key, graph, []string{})
if nodeCycle {
return true, cycleKey
}
}
return false, ""
}