-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedge.go
125 lines (106 loc) · 2.72 KB
/
edge.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
package graff
type directedEdgeList struct {
outgoingEdges map[Node]*nodeList
incomingEdges map[Node]*nodeList
}
func newDirectedEdgeList() *directedEdgeList {
return &directedEdgeList{
outgoingEdges: make(map[Node]*nodeList),
incomingEdges: make(map[Node]*nodeList),
}
}
func (l *directedEdgeList) Copy() *directedEdgeList {
outgoingEdges := make(map[Node]*nodeList, len(l.outgoingEdges))
for node, edges := range l.outgoingEdges {
outgoingEdges[node] = edges.Copy()
}
incomingEdges := make(map[Node]*nodeList, len(l.incomingEdges))
for node, edges := range l.incomingEdges {
incomingEdges[node] = edges.Copy()
}
return &directedEdgeList{
outgoingEdges: outgoingEdges,
incomingEdges: incomingEdges,
}
}
func (l *directedEdgeList) Count() int {
return len(l.outgoingEdges)
}
func (l *directedEdgeList) HasOutgoingEdges(node Node) bool {
_, ok := l.outgoingEdges[node]
return ok
}
func (l *directedEdgeList) OutgoingEdgeCount(node Node) int {
if list := l.outgoingNodeList(node, false); list != nil {
return list.Count()
}
return 0
}
func (l *directedEdgeList) outgoingNodeList(node Node, create bool) *nodeList {
if list, ok := l.outgoingEdges[node]; ok {
return list
}
if create {
list := newNodeList()
l.outgoingEdges[node] = list
return list
}
return nil
}
func (l *directedEdgeList) OutgoingEdges(node Node) []Node {
if list := l.outgoingNodeList(node, false); list != nil {
return list.Nodes()
}
return nil
}
func (l *directedEdgeList) HasIncomingEdges(node Node) bool {
_, ok := l.incomingEdges[node]
return ok
}
func (l *directedEdgeList) IncomingEdgeCount(node Node) int {
if list := l.incomingNodeList(node, false); list != nil {
return list.Count()
}
return 0
}
func (l *directedEdgeList) incomingNodeList(node Node, create bool) *nodeList {
if list, ok := l.incomingEdges[node]; ok {
return list
}
if create {
list := newNodeList()
l.incomingEdges[node] = list
return list
}
return nil
}
func (l *directedEdgeList) IncomingEdges(node Node) []Node {
if list := l.incomingNodeList(node, false); list != nil {
return list.Nodes()
}
return nil
}
func (l *directedEdgeList) Add(from Node, to Node) {
l.outgoingNodeList(from, true).Add(to)
l.incomingNodeList(to, true).Add(from)
}
func (l *directedEdgeList) Remove(from Node, to Node) {
if list := l.outgoingNodeList(from, false); list != nil {
list.Remove(to)
if list.Count() == 0 {
delete(l.outgoingEdges, from)
}
}
if list := l.incomingNodeList(to, false); list != nil {
list.Remove(from)
if list.Count() == 0 {
delete(l.incomingEdges, to)
}
}
}
func (l *directedEdgeList) Exists(from Node, to Node) bool {
if list := l.outgoingNodeList(from, false); list != nil {
return list.Exists(to)
}
return false
}