graph: revert NewNodeID -> NewNode

This commit is contained in:
kortschak
2017-07-04 15:54:07 +09:30
committed by Dan Kortschak
parent e1b3c5334d
commit 39b486f179
8 changed files with 25 additions and 26 deletions

View File

@@ -17,8 +17,6 @@ import (
type Builder interface { type Builder interface {
graph.Graph graph.Graph
graph.Builder graph.Builder
// NewNode adds a new node with a unique node ID to the graph.
NewNode() graph.Node
// NewEdge adds a new edge from the source to the destination node to the // NewEdge adds a new edge from the source to the destination node to the
// graph, or returns the existing edge if already present. // graph, or returns the existing edge if already present.
NewEdge(from, to graph.Node) graph.Edge NewEdge(from, to graph.Node) graph.Edge

View File

@@ -114,7 +114,7 @@ func newDotDirectedGraph() *dotDirectedGraph {
// NewNode adds a new node with a unique node ID to the graph. // NewNode adds a new node with a unique node ID to the graph.
func (g *dotDirectedGraph) NewNode() graph.Node { func (g *dotDirectedGraph) NewNode() graph.Node {
n := &dotNode{Node: simple.Node(g.NewNodeID())} n := &dotNode{Node: g.DirectedGraph.NewNode()}
g.AddNode(n) g.AddNode(n)
return n return n
} }
@@ -157,7 +157,7 @@ func newDotUndirectedGraph() *dotUndirectedGraph {
// NewNode adds a new node with a unique node ID to the graph. // NewNode adds a new node with a unique node ID to the graph.
func (g *dotUndirectedGraph) NewNode() graph.Node { func (g *dotUndirectedGraph) NewNode() graph.Node {
n := &dotNode{Node: simple.Node(g.NewNodeID())} n := &dotNode{Node: g.UndirectedGraph.NewNode()}
g.AddNode(n) g.AddNode(n)
return n return n
} }
@@ -186,7 +186,7 @@ func (g *dotUndirectedGraph) DOTUnmarshalerAttrs() (graph, node, edge Unmarshale
// dotNode extends simple.Node with a label field to test round-trip encoding // dotNode extends simple.Node with a label field to test round-trip encoding
// and decoding of node DOT label attributes. // and decoding of node DOT label attributes.
type dotNode struct { type dotNode struct {
simple.Node graph.Node
dotID string dotID string
// Node label. // Node label.
Label string Label string

View File

@@ -76,8 +76,9 @@ type Weighter interface {
// NodeAdder is an interface for adding arbitrary nodes to a graph. // NodeAdder is an interface for adding arbitrary nodes to a graph.
type NodeAdder interface { type NodeAdder interface {
// NewNodeID returns a new unique arbitrary ID. // NewNode returns a new Node with a unique
NewNodeID() int64 // arbitrary ID.
NewNode() Node
// Adds a node to the graph. AddNode panics if // Adds a node to the graph. AddNode panics if
// the added node ID matches an existing node ID. // the added node ID matches an existing node ID.

View File

@@ -59,13 +59,13 @@ func Duplication(dst UndirectedMutator, n int, delta, alpha, sigma float64, src
sort.Sort(ordered.ByID(nodes)) sort.Sort(ordered.ByID(nodes))
if len(nodes) == 0 { if len(nodes) == 0 {
n-- n--
u := simple.Node(dst.NewNodeID()) u := dst.NewNode()
dst.AddNode(u) dst.AddNode(u)
nodes = append(nodes, u) nodes = append(nodes, u)
} }
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
u := nodes[rndN(len(nodes))] u := nodes[rndN(len(nodes))]
d := simple.Node(dst.NewNodeID()) d := dst.NewNode()
// Add the duplicate node. // Add the duplicate node.
dst.AddNode(d) dst.AddNode(d)

View File

@@ -36,16 +36,16 @@ func NewDirectedGraph(self, absent float64) *DirectedGraph {
} }
} }
// NewNodeID returns a new unique ID for a node to be added to g. The returned ID does // NewNode returns a new unique Node to be added to g. The Node's ID does
// not become a valid ID in g until it is added to g. // not become valid in g until the Node is added to g.
func (g *DirectedGraph) NewNodeID() int64 { func (g *DirectedGraph) NewNode() graph.Node {
if len(g.nodes) == 0 { if len(g.nodes) == 0 {
return 0 return Node(0)
} }
if int64(len(g.nodes)) == maxInt { if int64(len(g.nodes)) == maxInt {
panic("simple: cannot allocate node: no slot") panic("simple: cannot allocate node: no slot")
} }
return g.nodeIDs.newID() return Node(g.nodeIDs.newID())
} }
// AddNode adds n to the graph. It panics if the added node ID matches an existing node ID. // AddNode adds n to the graph. It panics if the added node ID matches an existing node ID.

View File

@@ -50,14 +50,14 @@ func TestIssue123DirectedGraph(t *testing.T) {
}() }()
g := NewDirectedGraph(0, math.Inf(1)) g := NewDirectedGraph(0, math.Inf(1))
n0 := Node(g.NewNodeID()) n0 := g.NewNode()
g.AddNode(n0) g.AddNode(n0)
n1 := Node(g.NewNodeID()) n1 := g.NewNode()
g.AddNode(n1) g.AddNode(n1)
g.RemoveNode(n0) g.RemoveNode(n0)
n2 := Node(g.NewNodeID()) n2 := g.NewNode()
g.AddNode(n2) g.AddNode(n2)
} }

View File

@@ -34,16 +34,16 @@ func NewUndirectedGraph(self, absent float64) *UndirectedGraph {
} }
} }
// NewNodeID returns a new unique ID for a node to be added to g. The returned ID does // NewNode returns a new unique Node to be added to g. The Node's ID does
// not become a valid ID in g until it is added to g. // not become valid in g until the Node is added to g.
func (g *UndirectedGraph) NewNodeID() int64 { func (g *UndirectedGraph) NewNode() graph.Node {
if len(g.nodes) == 0 { if len(g.nodes) == 0 {
return 0 return Node(0)
} }
if int64(len(g.nodes)) == maxInt { if int64(len(g.nodes)) == maxInt {
panic("simple: cannot allocate node: no slot") panic("simple: cannot allocate node: no slot")
} }
return g.nodeIDs.newID() return Node(g.nodeIDs.newID())
} }
// AddNode adds n to the graph. It panics if the added node ID matches an existing node ID. // AddNode adds n to the graph. It panics if the added node ID matches an existing node ID.

View File

@@ -31,7 +31,7 @@ func TestMaxID(t *testing.T) {
delete(nodes, Node(0)) delete(nodes, Node(0))
g.RemoveNode(Node(2)) g.RemoveNode(Node(2))
delete(nodes, Node(2)) delete(nodes, Node(2))
n := Node(g.NewNodeID()) n := g.NewNode()
g.AddNode(n) g.AddNode(n)
if !g.Has(n) { if !g.Has(n) {
t.Error("added node does not exist in graph") t.Error("added node does not exist in graph")
@@ -50,14 +50,14 @@ func TestIssue123UndirectedGraph(t *testing.T) {
}() }()
g := NewUndirectedGraph(0, math.Inf(1)) g := NewUndirectedGraph(0, math.Inf(1))
n0 := Node(g.NewNodeID()) n0 := g.NewNode()
g.AddNode(n0) g.AddNode(n0)
n1 := Node(g.NewNodeID()) n1 := g.NewNode()
g.AddNode(n1) g.AddNode(n1)
g.RemoveNode(n0) g.RemoveNode(n0)
n2 := Node(g.NewNodeID()) n2 := g.NewNode()
g.AddNode(n2) g.AddNode(n2)
} }