Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Defer adding new nodes to graph
Browse files Browse the repository at this point in the history
This allows the ID of the node to be used in place of the returned node,
or the returned node to be placed in a graph.Node embedded field prior
to adding the wrapping struct.
  • Loading branch information
kortschak committed Feb 22, 2015
1 parent 96f5ec5 commit a403c99
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 8 deletions.
3 changes: 0 additions & 3 deletions concrete/mutablegr.go
Expand Up @@ -67,13 +67,11 @@ func NewGraph() *Graph {
func (g *Graph) NewNode() graph.Node {
if g.maxID != maxInt {
g.maxID++
g.AddNode(Node(g.maxID))
return Node(g.maxID)
}

// Implicitly checks if len(g.freeMap) == 0
for id := range g.freeMap {
g.AddNode(Node(id))
return Node(id)
}

Expand All @@ -84,7 +82,6 @@ func (g *Graph) NewNode() graph.Node {

for i := 0; i < maxInt; i++ {
if _, ok := g.nodeMap[i]; !ok {
g.AddNode(Node(i))
return Node(i)
}
}
Expand Down
4 changes: 4 additions & 0 deletions concrete/mutablegr_test.go
Expand Up @@ -33,6 +33,10 @@ func TestMaxID(t *testing.T) {
g.RemoveNode(concrete.Node(2))
delete(nodes, concrete.Node(2))
n := g.NewNode()
g.AddNode(n)
if !g.NodeExists(n) {
t.Error("added node does not exist in graph")
}
if _, exists := nodes[n]; exists {
t.Errorf("Created already existing node id: %v", n.ID())
}
Expand Down
3 changes: 0 additions & 3 deletions concrete/mutdir.go
Expand Up @@ -37,13 +37,11 @@ func NewDirectedGraph() *DirectedGraph {
func (g *DirectedGraph) NewNode() graph.Node {
if g.maxID != maxInt {
g.maxID++
g.AddNode(Node(g.maxID))
return Node(g.maxID)
}

// Implicitly checks if len(g.freeMap) == 0
for id := range g.freeMap {
g.AddNode(Node(id))
return Node(id)
}

Expand All @@ -54,7 +52,6 @@ func (g *DirectedGraph) NewNode() graph.Node {

for i := 0; i < maxInt; i++ {
if _, ok := g.nodeMap[i]; !ok {
g.AddNode(Node(i))
return Node(i)
}
}
Expand Down
3 changes: 1 addition & 2 deletions graph.go
Expand Up @@ -139,8 +139,7 @@ type HeuristicCoster interface {
// itself. That said, any function that takes Mutable[x], the destination mutable should
// always be a different graph than the source.
type Mutable interface {
// NewNode adds a node with an arbitrary ID and returns the new, unique ID
// used.
// NewNode returns a node with a unique arbitrary ID.
NewNode() Node

// Adds a node to the graph. If this is called multiple times for the same ID, the newer node
Expand Down
1 change: 1 addition & 0 deletions search/graph_search.go
Expand Up @@ -249,6 +249,7 @@ func Johnson(g graph.Graph, cost graph.CostFunc) (nodePaths map[int]map[int][]gr

/* Step 1: Dummy node with 0 cost edge weights to every other node*/
dummyNode := dummyGraph.NewNode()
dummyGraph.AddNode(dummyNode)
for _, node := range g.NodeList() {
dummyGraph.AddDirectedEdge(concrete.Edge{dummyNode, node}, 0)
}
Expand Down

0 comments on commit a403c99

Please sign in to comment.