This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathedge.go
185 lines (148 loc) · 4.02 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
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
package gogl
/* Edge interfaces */
// A graph's behaviors are primarily a product of the constraints and
// capabilities it places on its edges. These constraints and capabilities
// determine whether certain types of operations are possible on the graph, as
// well as the efficiencies for various operations.
// gogl aims to provide a range of graph implementations that can meet
// the varying constraints and implementation needs, but still achieve optimal
// performance given those constraints.
// Edge describes an undirected connection between two vertices.
type Edge interface {
Both() (u Vertex, v Vertex) // No order consistency is implied.
}
// Arc describes a directed connection between two vertices.
type Arc interface {
Both() (u Vertex, v Vertex) // u is tail/source, v is head/target.
Source() Vertex
Target() Vertex
}
// WeightedEdge describes an Edge that carries a numerical weight.
type WeightedEdge interface {
Edge
Weight() float64
}
// WeightedArc describes an Arc that carries a numerical weight.
type WeightedArc interface {
Arc
Weight() float64
}
// LabeledEdge describes an Edge that also has a string label.
type LabeledEdge interface {
Edge
Label() string
}
// LabeledArc describes an Arc that also has a string label.
type LabeledArc interface {
Arc
Label() string
}
// DataEdge describes an Edge that also holds arbitrary data.
type DataEdge interface {
Edge
Data() interface{}
}
// DataArc describes an Arc that also holds arbitrary data.
type DataArc interface {
Arc
Data() interface{}
}
/* Base implementations of Edge interfaces */
// BaseEdge is a struct used to represent edges and meet the Edge interface
// requirements. It uses the standard graph notation, (U,V), for its
// contained vertex pair.
type baseEdge struct {
u Vertex
v Vertex
}
func (e baseEdge) Both() (Vertex, Vertex) {
return e.u, e.v
}
// Create a new basic edge.
func NewEdge(u, v Vertex) Edge {
return baseEdge{u: u, v: v}
}
type baseArc struct {
baseEdge
}
func (e baseArc) Source() Vertex {
return e.u
}
func (e baseArc) Target() Vertex {
return e.v
}
// Create a new basic arc.
func NewArc(u, v Vertex) Arc {
return baseArc{baseEdge{u: u, v: v}}
}
// BaseWeightedEdge extends BaseEdge with weight data.
type baseWeightedEdge struct {
baseEdge
w float64
}
func (e baseWeightedEdge) Weight() float64 {
return e.w
}
// Create a new weighted edge.
func NewWeightedEdge(u, v Vertex, weight float64) WeightedEdge {
return baseWeightedEdge{baseEdge{u: u, v: v}, weight}
}
type baseWeightedArc struct {
baseArc
w float64
}
func (e baseWeightedArc) Weight() float64 {
return e.w
}
// Create a new weighted arc.
func NewWeightedArc(u, v Vertex, weight float64) WeightedArc {
return baseWeightedArc{baseArc{baseEdge{u: u, v: v}}, weight}
}
// BaseLabeledEdge extends BaseEdge with label data.
type baseLabeledEdge struct {
baseEdge
l string
}
func (e baseLabeledEdge) Label() string {
return e.l
}
// Create a new labeled edge.
func NewLabeledEdge(u, v Vertex, label string) LabeledEdge {
return baseLabeledEdge{baseEdge{u: u, v: v}, label}
}
// BaseLabeledArc extends BaseArc with label data.
type baseLabeledArc struct {
baseArc
l string
}
func (e baseLabeledArc) Label() string {
return e.l
}
// Create a new labeled arc.
func NewLabeledArc(u, v Vertex, label string) LabeledArc {
return baseLabeledArc{baseArc{baseEdge{u: u, v: v}}, label}
}
// BaseDataEdge extends BaseEdge with arbitrary data.
type baseDataEdge struct {
baseEdge
d interface{}
}
func (e baseDataEdge) Data() interface{} {
return e.d
}
// Create a new "data" edge - an edge with arbitrary embedded data.
func NewDataEdge(u, v Vertex, data interface{}) DataEdge {
return baseDataEdge{baseEdge{u: u, v: v}, data}
}
// BaseDataArc extends BaseArc with arbitrary data.
type baseDataArc struct {
baseArc
d interface{}
}
func (e baseDataArc) Data() interface{} {
return e.d
}
// Create a new "data" edge - an edge with arbitrary embedded data.
func NewDataArc(u, v Vertex, data interface{}) DataArc {
return baseDataArc{baseArc{baseEdge{u: u, v: v}}, data}
}