graph/multi: don't allocate line maps before they are needed
MODIFY
graph/multi/directed.go
graph/multi/directed.go
@@ -53,8 +53,6 @@ func (g *DirectedGraph) AddNode(n graph.Node) {
panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
}
g.nodes[n.ID()] = n
- g.from[n.ID()] = make(map[int64]map[int64]graph.Line)
- g.to[n.ID()] = make(map[int64]map[int64]graph.Line)
g.nodeIDs.Use(n.ID())
}
@@ -242,20 +240,29 @@ func (g *DirectedGraph) SetLine(l graph.Line) {
} else {
g.nodes[fid] = from
}
- if g.from[fid][tid] == nil {
- g.from[fid][tid] = make(map[int64]graph.Line)
- }
if _, ok := g.nodes[tid]; !ok {
g.AddNode(to)
} else {
g.nodes[tid] = to
}
- if g.to[tid][fid] == nil {
- g.to[tid][fid] = make(map[int64]graph.Line)
- }
+ switch {
+ case g.from[fid] == nil:
+ g.from[fid] = map[int64]map[int64]graph.Line{tid: map[int64]graph.Line{lid: l}}
+ case g.from[fid][tid] == nil:
+ g.from[fid][tid] = map[int64]graph.Line{lid: l}
+ default:
g.from[fid][tid][lid] = l
+ }
+ switch {
+ case g.to[tid] == nil:
+ g.to[tid] = map[int64]map[int64]graph.Line{fid: map[int64]graph.Line{lid: l}}
+ case g.to[tid][fid] == nil:
+ g.to[tid][fid] = map[int64]graph.Line{lid: l}
+ default:
g.to[tid][fid][lid] = l
+ }
+
g.lineIDs.Use(lid)
}

MODIFY
graph/multi/undirected.go
graph/multi/undirected.go
@@ -51,7 +51,6 @@ func (g *UndirectedGraph) AddNode(n graph.Node) {
panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
}
g.nodes[n.ID()] = n
- g.lines[n.ID()] = make(map[int64]map[int64]graph.Line)
g.nodeIDs.Use(n.ID())
}
@@ -242,19 +241,28 @@ func (g *UndirectedGraph) SetLine(l graph.Line) {
} else {
g.nodes[fid] = from
}
- if g.lines[fid][tid] == nil {
- g.lines[fid][tid] = make(map[int64]graph.Line)
- }
if _, ok := g.nodes[tid]; !ok {
g.AddNode(to)
} else {
g.nodes[tid] = to
}
- if g.lines[tid][fid] == nil {
- g.lines[tid][fid] = make(map[int64]graph.Line)
- }
+ switch {
+ case g.lines[fid] == nil:
+ g.lines[fid] = map[int64]map[int64]graph.Line{tid: map[int64]graph.Line{lid: l}}
+ case g.lines[fid][tid] == nil:
+ g.lines[fid][tid] = map[int64]graph.Line{lid: l}
+ default:
g.lines[fid][tid][lid] = l
+ }
+ switch {
+ case g.lines[tid] == nil:
+ g.lines[tid] = map[int64]map[int64]graph.Line{fid: map[int64]graph.Line{lid: l}}
+ case g.lines[tid][fid] == nil:
+ g.lines[tid][fid] = map[int64]graph.Line{lid: l}
+ default:
g.lines[tid][fid][lid] = l
+ }
+
g.lineIDs.Use(lid)
}

MODIFY
graph/multi/weighted_directed.go
graph/multi/weighted_directed.go
@@ -62,8 +62,6 @@ func (g *WeightedDirectedGraph) AddNode(n graph.Node) {
panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
}
g.nodes[n.ID()] = n
- g.from[n.ID()] = make(map[int64]map[int64]graph.WeightedLine)
- g.to[n.ID()] = make(map[int64]map[int64]graph.WeightedLine)
g.nodeIDs.Use(n.ID())
}
@@ -251,20 +249,29 @@ func (g *WeightedDirectedGraph) SetWeightedLine(l graph.WeightedLine) {
} else {
g.nodes[fid] = from
}
- if g.from[fid][tid] == nil {
- g.from[fid][tid] = make(map[int64]graph.WeightedLine)
- }
if _, ok := g.nodes[tid]; !ok {
g.AddNode(to)
} else {
g.nodes[tid] = to
}
- if g.to[tid][fid] == nil {
- g.to[tid][fid] = make(map[int64]graph.WeightedLine)
- }
+ switch {
+ case g.from[fid] == nil:
+ g.from[fid] = map[int64]map[int64]graph.WeightedLine{tid: map[int64]graph.WeightedLine{lid: l}}
+ case g.from[fid][tid] == nil:
+ g.from[fid][tid] = map[int64]graph.WeightedLine{lid: l}
+ default:
g.from[fid][tid][lid] = l
+ }
+ switch {
+ case g.to[tid] == nil:
+ g.to[tid] = map[int64]map[int64]graph.WeightedLine{fid: map[int64]graph.WeightedLine{lid: l}}
+ case g.to[tid][fid] == nil:
+ g.to[tid][fid] = map[int64]graph.WeightedLine{lid: l}
+ default:
g.to[tid][fid][lid] = l
+ }
+
g.lineIDs.Use(l.ID())
}

MODIFY
graph/multi/weighted_undirected.go
graph/multi/weighted_undirected.go
@@ -60,7 +60,6 @@ func (g *WeightedUndirectedGraph) AddNode(n graph.Node) {
panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
}
g.nodes[n.ID()] = n
- g.lines[n.ID()] = make(map[int64]map[int64]graph.WeightedLine)
g.nodeIDs.Use(n.ID())
}
@@ -252,20 +251,29 @@ func (g *WeightedUndirectedGraph) SetWeightedLine(l graph.WeightedLine) {
} else {
g.nodes[fid] = from
}
- if g.lines[fid][tid] == nil {
- g.lines[fid][tid] = make(map[int64]graph.WeightedLine)
- }
if _, ok := g.nodes[tid]; !ok {
g.AddNode(to)
} else {
g.nodes[tid] = to
}
- if g.lines[tid][fid] == nil {
- g.lines[tid][fid] = make(map[int64]graph.WeightedLine)
- }
+ switch {
+ case g.lines[fid] == nil:
+ g.lines[fid] = map[int64]map[int64]graph.WeightedLine{tid: map[int64]graph.WeightedLine{lid: l}}
+ case g.lines[fid][tid] == nil:
+ g.lines[fid][tid] = map[int64]graph.WeightedLine{lid: l}
+ default:
g.lines[fid][tid][lid] = l
+ }
+ switch {
+ case g.lines[tid] == nil:
+ g.lines[tid] = map[int64]map[int64]graph.WeightedLine{fid: map[int64]graph.WeightedLine{lid: l}}
+ case g.lines[tid][fid] == nil:
+ g.lines[tid][fid] = map[int64]graph.WeightedLine{lid: l}
+ default:
g.lines[tid][fid][lid] = l
+ }
+
g.lineIDs.Use(lid)
}

graph/multi: don't allocate line maps before they are needed