-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.go
202 lines (168 loc) · 4.15 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package graph
import (
"fmt"
"sort"
)
// Graph represents a graph
type Graph struct {
// Nodes contains all the nodes for the graph
Nodes map[interface{}]*Node
// NodeStringer is a function for stringifying nodes
NodeStringer func(interface{}) string
// OnNodeCreated is called when a node is created
OnNodeCreated func(*Node)
// OnEdgeCreated is called when an edge is created
OnEdgeCreated func(*Edge)
// OnSameNodeEdge is called when an edge is created that has same source and destination
OnSameNodeEdge func(*Node)
// OnDuplicateEdge is called when trying to create an edge that already exists
OnDuplicateEdge func(*Edge)
// Regions is used to group nodes into regions
Regions map[interface{}][]*Node
}
// NewGraph returns a new graph
func NewGraph() *Graph {
return &Graph{
Nodes: make(map[interface{}]*Node),
Regions: make(map[interface{}][]*Node),
}
}
// Stringify stringifies the graph
func (g *Graph) Stringify() {
for _, n := range g.Nodes {
for _, e := range n.Edges {
if e.Source != n {
continue
}
fmt.Printf("[%v] %v -(%v)> [%v] %v\n", n, g.NodeStringer(n.Data), e.Data, e.Destination, g.NodeStringer(e.Destination.Data))
}
}
}
func (g *Graph) NumberOfEdges() int {
c := 0
for _, n := range g.Nodes {
for _, e := range n.Edges {
if e.Source != n {
continue
}
c++
}
}
return c
}
func (g *Graph) PrintNodes() {
for _, node := range g.Nodes {
if node.Data != nil {
fmt.Printf("[%v]: %v(%T)\n", node, g.NodeStringer(node.Data), node.Data)
} else {
fmt.Printf("[%v]: NIL(%T)\n", node, node.Data)
}
}
}
func (g *Graph) Print() {
edges := make(map[*Edge]struct{ id, num int })
id := 0
edgeCounter := 0
ids := make([]int, len(g.Nodes))
i := 0
for _, n := range g.Nodes {
ids[i] = int(n.ID)
i++
}
sort.Ints(ids)
//for _, node := range g.Nodes {
for _, i := range ids {
node := g.FindById(uint32(i))
if node.Data != nil {
if g.NodeStringer != nil {
fmt.Printf("[%v]: %v(%T)\n", node, g.NodeStringer(node.Data), node.Data)
} else {
fmt.Printf("[%v]: %v(%T)\n", node, node.Data, node.Data)
}
} else {
fmt.Printf("[%v]: NIL(%T)\n", node, node.Data)
}
for i, edge := range node.Edges {
edgeCounter++
value, hasEdge := edges[edge]
if !hasEdge {
value = struct{ id, num int }{id, 1}
edges[edge] = value
id++
} else {
value.num++
edges[edge] = value
}
inbound := edge.Destination == node
if inbound {
fmt.Printf(" [%v] %v <- %v (%p) [%v/%v]\n", i, edge.Destination.ID, edge.Source.ID, edge, value.id, value.num)
} else {
fmt.Printf(" [%v] %v -> %v (%p) [%v/%v]\n", i, edge.Source.ID, edge.Destination.ID, edge, value.id, value.num)
}
}
}
fmt.Printf("Nodes: %v, Unique edges: %v, total edges: %v\n", len(g.Nodes), len(edges), edgeCounter)
}
// NewNode will add a new node to the graph.
// If the node exists, it will return that node.
func (g *Graph) NewNode(data interface{}) (node *Node) {
node = g.Find(data)
if node == nil {
node = newNode(data)
node.ID = uint32(len(g.Nodes))
node.graph = g
g.Nodes[data] = node
if g.OnNodeCreated != nil {
g.OnNodeCreated(node)
}
}
return
}
func (g *Graph) addNode(node *Node) {
if node == nil {
return
}
g.Nodes[node.Data] = node
}
// Find will find a graph node in the graph, given the ast node.
// If the ast node is not in the graph, nil is returned.
func (g *Graph) Find(data interface{}) *Node {
node, hasNode := g.Nodes[data]
if hasNode {
return node
} else {
return nil
}
}
// FindById will find a node by its given id
func (g *Graph) FindById(i uint32) *Node {
for _, n := range g.Nodes {
if n.ID == i {
return n
}
}
return nil
}
// Size returns the number of nodes in the graph.
func (g *Graph) Size() int {
return len(g.Nodes)
}
func (g *Graph) SizeEdges() int {
edges := make(map[*Edge]bool)
for _, n := range g.Nodes {
for _, e := range n.Edges {
edges[e] = true
}
}
return len(edges)
}
// HasCyclicDependencies returns true if the graph has cyclic dependencies.
func (g *Graph) HasCyclicDependencies() bool {
for _, n := range g.Nodes {
deps := []*Node{}
if n.hasCyclicDependency(deps) {
return true
}
}
return false
}