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_list.go
187 lines (155 loc) · 3.67 KB
/
edge_list.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
package gogl
import (
"gopkg.in/fatih/set.v0"
)
// Shared helper function for edge lists to enumerate vertices.
func elVertices(el interface{}, fn VertexStep) {
set := set.New(set.NonThreadSafe)
el.(EdgeEnumerator).Edges(func(e Edge) (terminate bool) {
set.Add(e.Both())
return
})
for _, v := range set.List() {
if fn(v) {
return
}
}
}
// An EdgeList is a naive GraphSource implementation that is backed only by an edge slice.
//
// EdgeLists are primarily intended for use as fixtures.
//
// It is inherently impossible for an EdgeList to represent a vertex isolate (degree 0) fully
// correctly, as vertices are only described in the context of their edges. One can be a
// little hacky, though, and represent one with a loop. As gogl expects graph implementations
// to simply discard loops if they are disallowed by the graph's constraints (i.e., in simple
// and multigraphs), they *should* be interpreted as vertex isolates.
type EdgeList []Edge
func (el EdgeList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el EdgeList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// An ArcList is a naive DigraphSource implementation that is backed only by an arc slice.
type ArcList []Arc
func (el ArcList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el ArcList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
func (el ArcList) Arcs(fn ArcStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// A WeightedEdgeList is a naive GraphSource implementation that is backed only by an edge slice.
//
// This variant is for weighted edges.
type WeightedEdgeList []WeightedEdge
func (el WeightedEdgeList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el WeightedEdgeList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// A WeightedArcList is a naive DigraphSource implementation that is backed only by an arc slice.
type WeightedArcList []Arc
func (el WeightedArcList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el WeightedArcList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
func (el WeightedArcList) Arcs(fn ArcStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// A LabeledEdgeList is a naive GraphSource implementation that is backed only by an edge slice.
//
// This variant is for labeled edges.
type LabeledEdgeList []LabeledEdge
func (el LabeledEdgeList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el LabeledEdgeList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// A LabeledArcList is a naive DigraphSource implementation that is backed only by an arc slice.
type LabeledArcList []Arc
func (el LabeledArcList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el LabeledArcList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
func (el LabeledArcList) Arcs(fn ArcStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// A DataEdgeList is a naive GraphSource implementation that is backed only by an edge slice.
//
// This variant is for labeled edges.
type DataEdgeList []DataEdge
func (el DataEdgeList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el DataEdgeList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
// A DataArcList is a naive DigraphSource implementation that is backed only by an arc slice.
type DataArcList []Arc
func (el DataArcList) Vertices(fn VertexStep) {
elVertices(el, fn)
}
func (el DataArcList) Edges(fn EdgeStep) {
for _, e := range el {
if fn(e) {
return
}
}
}
func (el DataArcList) Arcs(fn ArcStep) {
for _, e := range el {
if fn(e) {
return
}
}
}