Skip to content

Commit 05fad3c

Browse files
committed
Introduce Arc and interfaces.
Still lots of conversion to do.
1 parent 6244354 commit 05fad3c

File tree

3 files changed

+135
-17
lines changed

3 files changed

+135
-17
lines changed

edge.go

Lines changed: 89 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,54 @@ package gogl
1111
// the varying constraints and implementation needs, but still achieve optimal
1212
// performance given those constraints.
1313

14-
// The Edge interface describes a connection between two vertices.
15-
//
16-
// Edge does not have an intrinsic opinion about directionality; gogl treats
17-
// that as a property of the overall Graph object in which the Edge appears
18-
// rather than a property of any individual Edge.
14+
// Edge describes an undirected connection between two vertices.
1915
type Edge interface {
16+
Both() (u Vertex, v Vertex) // No order consistency is implied.
17+
}
18+
19+
// Arc describes a directed connection between two vertices.
20+
type Arc interface {
21+
Both() (u Vertex, v Vertex) // u is tail/source, v is head/target.
2022
Source() Vertex
2123
Target() Vertex
22-
Both() (Vertex, Vertex)
2324
}
2425

25-
// A WeightedEdge is an Edge that also has a numerical weight.
26+
// WeightedEdge describes an Edge that carries a numerical weight.
2627
type WeightedEdge interface {
2728
Edge
2829
Weight() float64
2930
}
3031

31-
// A LabeledEdge is an Edge that also has a string label.
32+
// WeightedArc describes an Arc that carries a numerical weight.
33+
type WeightedArc interface {
34+
Arc
35+
Weight() float64
36+
}
37+
38+
// LabeledEdge describes an Edge that also has a string label.
3239
type LabeledEdge interface {
3340
Edge
3441
Label() string
3542
}
3643

37-
// A DataEdge is an Edge that also holds arbitrary data.
44+
// LabeledArc describes an Arc that also has a string label.
45+
type LabeledArc interface {
46+
Arc
47+
Label() string
48+
}
49+
50+
// DataEdge describes an Edge that also holds arbitrary data.
3851
type DataEdge interface {
3952
Edge
4053
Data() interface{}
4154
}
4255

56+
// DataArc describes an Arc that also holds arbitrary data.
57+
type DataArc interface {
58+
Arc
59+
Data() interface{}
60+
}
61+
4362
/* Base implementations of Edge interfaces */
4463

4564
// BaseEdge is a struct used to represent edges and meet the Edge interface
@@ -50,14 +69,6 @@ type baseEdge struct {
5069
v Vertex
5170
}
5271

53-
func (e baseEdge) Source() Vertex {
54-
return e.u
55-
}
56-
57-
func (e baseEdge) Target() Vertex {
58-
return e.v
59-
}
60-
6172
func (e baseEdge) Both() (Vertex, Vertex) {
6273
return e.u, e.v
6374
}
@@ -67,6 +78,23 @@ func NewEdge(u, v Vertex) Edge {
6778
return baseEdge{u: u, v: v}
6879
}
6980

81+
type baseArc struct {
82+
baseEdge
83+
}
84+
85+
func (e baseArc) Source() Vertex {
86+
return e.u
87+
}
88+
89+
func (e baseArc) Target() Vertex {
90+
return e.v
91+
}
92+
93+
// Create a new basic arc.
94+
func NewArc(u, v Vertex) Arc {
95+
return baseArc{baseEdge{u: u, v: v}}
96+
}
97+
7098
// BaseWeightedEdge extends BaseEdge with weight data.
7199
type baseWeightedEdge struct {
72100
baseEdge
@@ -82,6 +110,20 @@ func NewWeightedEdge(u, v Vertex, weight float64) WeightedEdge {
82110
return baseWeightedEdge{baseEdge{u: u, v: v}, weight}
83111
}
84112

113+
type baseWeightedArc struct {
114+
baseArc
115+
w float64
116+
}
117+
118+
func (e baseWeightedArc) Weight() float64 {
119+
return e.w
120+
}
121+
122+
// Create a new weighted arc.
123+
func NewWeightedArc(u, v Vertex, weight float64) WeightedArc {
124+
return baseWeightedArc{baseArc{baseEdge{u: u, v: v}}, weight}
125+
}
126+
85127
// BaseLabeledEdge extends BaseEdge with label data.
86128
type baseLabeledEdge struct {
87129
baseEdge
@@ -97,6 +139,21 @@ func NewLabeledEdge(u, v Vertex, label string) LabeledEdge {
97139
return baseLabeledEdge{baseEdge{u: u, v: v}, label}
98140
}
99141

142+
// BaseLabeledArc extends BaseArc with label data.
143+
type baseLabeledArc struct {
144+
baseArc
145+
l string
146+
}
147+
148+
func (e baseLabeledArc) Label() string {
149+
return e.l
150+
}
151+
152+
// Create a new labeled arc.
153+
func NewLabeledArc(u, v Vertex, label string) LabeledArc {
154+
return baseLabeledArc{baseArc{baseEdge{u: u, v: v}}, label}
155+
}
156+
100157
// BaseDataEdge extends BaseEdge with arbitrary data.
101158
type baseDataEdge struct {
102159
baseEdge
@@ -111,3 +168,18 @@ func (e baseDataEdge) Data() interface{} {
111168
func NewDataEdge(u, v Vertex, data interface{}) DataEdge {
112169
return baseDataEdge{baseEdge{u: u, v: v}, data}
113170
}
171+
172+
// BaseDataArc extends BaseArc with arbitrary data.
173+
type baseDataArc struct {
174+
baseArc
175+
d interface{}
176+
}
177+
178+
func (e baseDataArc) Data() interface{} {
179+
return e.d
180+
}
181+
182+
// Create a new "data" edge - an edge with arbitrary embedded data.
183+
func NewDataArc(u, v Vertex, data interface{}) DataArc {
184+
return baseDataArc{baseArc{baseEdge{u: u, v: v}}, data}
185+
}

graph.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ type GraphSource interface {
4949
EdgeEnumerator
5050
}
5151

52+
// DigraphSource is a subset of Digraph, describing the minimal set of methods
53+
// necessary to accomplish a naive full digraph traversal and copy.
54+
type DigraphSource interface {
55+
GraphSource
56+
ArcEnumerator
57+
}
58+
5259
// Digraph (directed graph) describes a Graph where all the edges are directed.
5360
//
5461
// gogl treats edge directionality as a property of the graph, not the edge itself.
5562
// Thus, implementing this interface is gogl's only signal that a graph's edges are directed.
5663
type Digraph interface {
5764
Graph
65+
ArcEnumerator // Enumerates all arcs to an injected step function
5866
IncidentArcEnumerator // Enumerates a vertex's incident in- or out-arcs to an injected step function
5967
DirectedDegreeChecker // Reports in- and out-degree of vertices
6068
ProcessionEnumerator // Enumerates a vertex's predecessor or successor vertices to a step function
@@ -167,6 +175,11 @@ type MutableDataGraph interface {
167175
// If the step function returns true, the calling enumerator is expected to end enumeration and return control to its caller.
168176
type EdgeStep func(Edge) (terminate bool)
169177

178+
// ArcSteps are used as arguments to various enumerators. They are called once for each arc produced by the enumerator.
179+
//
180+
// If the step function returns true, the calling enumerator is expected to end enumeration and return control to its caller.
181+
type ArcStep func(Arc) (terminate bool)
182+
170183
// VertexSteps are used as arguments to various enumerators. They are called once for each vertex produced by the enumerator.
171184
//
172185
// If the step function returns true, the calling enumerator is expected to end enumeration and return control to its caller.
@@ -187,6 +200,14 @@ type EdgeEnumerator interface {
187200
EachEdge(EdgeStep)
188201
}
189202

203+
// An ArcEnumerator iteratively enumerates edges, and can indicate the number of edges present.
204+
type ArcEnumerator interface {
205+
// Calls the provided step function once with each edge in the graph. If a
206+
// specialized edge type (e.g., weighted) is known to be used by the
207+
// graph, it is the calling code's responsibility to type assert.
208+
EachArc(ArcStep)
209+
}
210+
190211
// An IncidentEdgeEnumerator iteratively enumerates a given vertex's incident edges.
191212
type IncidentEdgeEnumerator interface {
192213
// Calls the provided step function once with each edge incident to the
@@ -257,24 +278,48 @@ type EdgeSetMutator interface {
257278
RemoveEdges(edges ...Edge)
258279
}
259280

281+
// An ArcSetMutator allows the addition and removal of arcs from a set.
282+
type ArcSetMutator interface {
283+
AddArcs(arcs ...Arc)
284+
RemoveArcs(arcs ...Arc)
285+
}
286+
260287
// A WeightedEdgeSetMutator allows the addition and removal of weighted edges from a set.
261288
type WeightedEdgeSetMutator interface {
262289
AddEdges(edges ...WeightedEdge)
263290
RemoveEdges(edges ...WeightedEdge)
264291
}
265292

293+
// A WeightedArcSetMutator allows the addition and removal of weighted arcs from a set.
294+
type WeightedArcSetMutator interface {
295+
AddArcs(arcs ...WeightedArc)
296+
RemoveArcs(arcs ...WeightedArc)
297+
}
298+
266299
// A LabeledEdgeSetMutator allows the addition and removal of labeled edges from a set.
267300
type LabeledEdgeSetMutator interface {
268301
AddEdges(edges ...LabeledEdge)
269302
RemoveEdges(edges ...LabeledEdge)
270303
}
271304

305+
// A LabeledArcSetMutator allows the addition and removal of labeled arcs from a set.
306+
type LabeledArcSetMutator interface {
307+
AddArcs(arcs ...LabeledArc)
308+
RemoveArcs(arcs ...LabeledArc)
309+
}
310+
272311
// A DataEdgeSetMutator allows the addition and removal of data edges from a set.
273312
type DataEdgeSetMutator interface {
274313
AddEdges(edges ...DataEdge)
275314
RemoveEdges(edges ...DataEdge)
276315
}
277316

317+
// A DataArcSetMutator allows the addition and removal of data arcs from a set.
318+
type DataArcSetMutator interface {
319+
AddArcs(arcs ...DataArc)
320+
RemoveArcs(arcs ...DataArc)
321+
}
322+
278323
/* Optional optimization interfaces
279324
280325
These interfaces describe behaviors and information about a graph which can be

null.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var _ DataGraph = nullGraph(false)
2020

2121
func (g nullGraph) EachVertex(f VertexStep) {}
2222
func (g nullGraph) EachEdge(f EdgeStep) {}
23+
func (g nullGraph) EachArc(f ArcStep) {}
2324
func (g nullGraph) EachEdgeIncidentTo(Vertex, EdgeStep) {}
2425
func (g nullGraph) EachArcFrom(Vertex, EdgeStep) {}
2526
func (g nullGraph) EachPredecessorOf(Vertex, VertexStep) {}

0 commit comments

Comments
 (0)