forked from shellfly/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol_graph.go
64 lines (54 loc) · 1.09 KB
/
symbol_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 algs4
import (
"strings"
"github.com/shellfly/algo/stdin"
)
// SymbolGraph ...
type SymbolGraph struct {
st ST
keys []string
g *Graph
}
// NewSymbolGraph ...
func NewSymbolGraph(filename, sp string) *SymbolGraph {
st := NewST()
in := stdin.NewInByLine(filename)
for !in.IsEmpty() {
a := strings.Split(in.ReadString(), sp)
for i := 0; i < len(a); i++ {
if !st.Contains(a[i]) {
st.Put(a[i], st.Size())
}
}
}
keys := make([]string, st.Size())
for _, name := range st.Keys() {
keys[st.Get(name)] = name
}
g := NewGraphV(st.Size())
in = stdin.NewInByLine(filename)
for !in.IsEmpty() {
a := strings.Split(in.ReadString(), sp)
v := st.Get(a[0])
for i := 1; i < len(a); i++ {
g.AddEdge(v, st.Get(a[i]))
}
}
return &SymbolGraph{st, keys, g}
}
// Contains ...
func (s *SymbolGraph) Contains(key string) bool {
return s.st.Contains(key)
}
// Index ...
func (s *SymbolGraph) Index(key string) int {
return s.st.Get(key)
}
// Name ...
func (s *SymbolGraph) Name(v int) string {
return s.keys[v]
}
// G ...
func (s *SymbolGraph) G() *Graph {
return s.g
}