graph: move NewEdge method from encoding.Builder to graph.EdgeAdder
Also rename graph.EdgeSetter to graph.EdgeAdder.
Fixes
MODIFY
graph/encoding/dot/decode.go
graph/encoding/dot/decode.go
@@ -167,21 +167,23 @@ func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) {
}
// addEdgeStmt adds the given edge statement to the graph.
-func (gen *generator) addEdgeStmt(dst encoding.Builder, e *ast.EdgeStmt) {
- fs := gen.addVertex(dst, e.From)
- ts := gen.addEdge(dst, e.To)
+func (gen *generator) addEdgeStmt(dst encoding.Builder, stmt *ast.EdgeStmt) {
+ fs := gen.addVertex(dst, stmt.From)
+ ts := gen.addEdge(dst, stmt.To)
for _, f := range fs {
for _, t := range ts {
- edge, ok := dst.NewEdge(f, t).(encoding.AttributeSetter)
+ edge := dst.NewEdge(f, t)
+ dst.SetEdge(edge)
+ e, ok := edge.(encoding.AttributeSetter)
if !ok {
continue
}
- for _, attr := range e.Attrs {
+ for _, attr := range stmt.Attrs {
a := encoding.Attribute{
Key: attr.Key,
Value: attr.Val,
}
- if err := edge.SetAttribute(a); err != nil {
+ if err := e.SetAttribute(a); err != nil {
panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value))
}
}
@@ -216,7 +218,8 @@ func (gen *generator) addEdge(dst encoding.Builder, to *ast.Edge) []graph.Node {
ts := gen.addEdge(dst, to.To)
for _, f := range fs {
for _, t := range ts {
- dst.NewEdge(f, t)
+ edge := dst.NewEdge(f, t)
+ dst.SetEdge(edge)
}
}
}

MODIFY
graph/encoding/dot/decode_test.go
graph/encoding/dot/decode_test.go
@@ -118,15 +118,9 @@ func (g *dotDirectedGraph) NewNode() graph.Node {
return &dotNode{Node: g.DirectedGraph.NewNode()}
}
-// NewEdge adds a new edge from the source to the destination node to the graph,
-// or returns the existing edge if already present.
+// NewEdge returns a new Edge from the source to the destination node.
func (g *dotDirectedGraph) NewEdge(from, to graph.Node) graph.Edge {
- if e := g.Edge(from, to); e != nil {
- return e
- }
- e := &dotEdge{Edge: simple.Edge{F: from, T: to}}
- g.SetEdge(e)
- return e
+ return &dotEdge{Edge: g.DirectedGraph.NewEdge(from, to)}
}
// DOTAttributers implements the dot.Attributers interface.
@@ -159,15 +153,9 @@ func (g *dotUndirectedGraph) NewNode() graph.Node {
return &dotNode{Node: g.UndirectedGraph.NewNode()}
}
-// NewEdge adds a new edge from the source to the destination node to the graph,
-// or returns the existing edge if already present.
+// NewEdge returns a new Edge from the source to the destination node.
func (g *dotUndirectedGraph) NewEdge(from, to graph.Node) graph.Edge {
- if e := g.Edge(from, to); e != nil {
- return e
- }
- e := &dotEdge{Edge: simple.Edge{F: from, T: to}}
- g.SetEdge(e)
- return e
+ return &dotEdge{Edge: g.UndirectedGraph.NewEdge(from, to)}
}
// DOTAttributers implements the dot.Attributers interface.
@@ -222,7 +210,7 @@ func (n *dotNode) Attributes() []encoding.Attribute {
// dotEdge extends simple.Edge with a label field to test round-trip encoding and
// decoding of edge DOT label attributes.
type dotEdge struct {
- simple.Edge
+ graph.Edge
// Edge label.
Label string
}

MODIFY
graph/encoding/encoding.go
graph/encoding/encoding.go
@@ -11,9 +11,6 @@ import "gonum.org/v1/gonum/graph"
type Builder interface {
graph.Graph
graph.Builder
- // NewEdge adds a new edge from the source to the destination node to the
- // graph, or returns the existing edge if already present.
- NewEdge(from, to graph.Node) graph.Edge
}
// AttributeSetter is implemented by types that can set an encoded graph

MODIFY
graph/encoding/graphql/decode_test.go
graph/encoding/graphql/decode_test.go
@@ -163,12 +163,7 @@ func (g *directedGraph) NewNode() graph.Node {
}
func (g *directedGraph) NewEdge(from, to graph.Node) graph.Edge {
- if e := g.Edge(from, to); e != nil {
- return e
- }
- e := &edge{Edge: simple.Edge{F: from, T: to}}
- g.SetEdge(e)
- return e
+ return &edge{Edge: g.DirectedGraph.NewEdge(from, to)}
}
type node struct {
@@ -189,7 +184,7 @@ func (n *node) SetIDFromString(uid string) error {
}
type edge struct {
- simple.Edge
+ graph.Edge
label string
}

MODIFY
graph/graph.go
graph/graph.go
@@ -93,13 +93,16 @@ type NodeRemover interface {
RemoveNode(Node)
}
-// EdgeSetter is an interface for adding edges to a graph.
-type EdgeSetter interface {
+// EdgeAdder is an interface for adding edges to a graph.
+type EdgeAdder interface {
+ // NewEdge returns a new Edge from the source to the destination node.
+ NewEdge(from, to Node) Edge
+
// SetEdge adds an edge from one node to another.
// If the graph supports node addition the nodes
// will be added if they do not exist, otherwise
// SetEdge will panic.
- // The behavior of an EdgeSetter when the IDs
+ // The behavior of an EdgeAdder when the IDs
// returned by e.From and e.To are equal is
// implementation-dependent.
SetEdge(e Edge)
@@ -116,7 +119,7 @@ type EdgeRemover interface {
// Builder is a graph that can have nodes and edges added.
type Builder interface {
NodeAdder
- EdgeSetter
+ EdgeAdder
}
// UndirectedBuilder is an undirected graph builder.

MODIFY
graph/simple/directed.go
graph/simple/directed.go
@@ -80,6 +80,11 @@ func (g *DirectedGraph) RemoveNode(n graph.Node) {
g.nodeIDs.release(n.ID())
}
+// NewEdge returns a new Edge from the source to the destination node.
+func (g *DirectedGraph) NewEdge(from, to graph.Node) graph.Edge {
+ return &Edge{F: from, T: to}
+}
+
// SetEdge adds e, an edge from one node to another. If the nodes do not exist, they are added.
// It will panic if the IDs of the e.From and e.To are equal.
func (g *DirectedGraph) SetEdge(e graph.Edge) {

MODIFY
graph/simple/undirected.go
graph/simple/undirected.go
@@ -72,6 +72,11 @@ func (g *UndirectedGraph) RemoveNode(n graph.Node) {
g.nodeIDs.release(n.ID())
}
+// NewEdge returns a new Edge from the source to the destination node.
+func (g *UndirectedGraph) NewEdge(from, to graph.Node) graph.Edge {
+ return &Edge{F: from, T: to}
+}
+
// SetEdge adds e, an edge from one node to another. If the nodes do not exist, they are added.
// It will panic if the IDs of the e.From and e.To are equal.
func (g *UndirectedGraph) SetEdge(e graph.Edge) {

graph: move NewEdge method from encoding.Builder to graph.EdgeAdder

Also rename graph.EdgeSetter to graph.EdgeAdder.

Fixes #56.