graph: make slicer helpers return nil for zero length
Also fix up var names.
MODIFY
graph/nodes_edges.go
graph/nodes_edges.go
@@ -55,7 +55,11 @@ func NodesOf(it Nodes) []Node {
case NodeSlicer:
return it.NodeSlice()
}
- n := make([]Node, 0, it.Len())
+ len := it.Len()
+ if len == 0 {
+ return nil
+ }
+ n := make([]Node, 0, len)
for it.Next() {
n = append(n, it.Node())
}
@@ -91,11 +95,15 @@ func EdgesOf(it Edges) []Edge {
case EdgeSlicer:
return it.EdgeSlice()
}
- n := make([]Edge, 0, it.Len())
+ len := it.Len()
+ if len == 0 {
+ return nil
+ }
+ e := make([]Edge, 0, len)
for it.Next() {
- n = append(n, it.Edge())
+ e = append(e, it.Edge())
}
- return n
+ return e
}
// WeightedEdges is a WeightedEdge iterator.
@@ -128,11 +136,15 @@ func WeightedEdgesOf(it WeightedEdges) []WeightedEdge {
case WeightedEdgeSlicer:
return it.WeightedEdgeSlice()
}
- n := make([]WeightedEdge, 0, it.Len())
+ len := it.Len()
+ if len == 0 {
+ return nil
+ }
+ e := make([]WeightedEdge, 0, len)
for it.Next() {
- n = append(n, it.WeightedEdge())
+ e = append(e, it.WeightedEdge())
}
- return n
+ return e
}
// Lines is a Line iterator.
@@ -164,11 +176,15 @@ func LinesOf(it Lines) []Line {
case LineSlicer:
return it.LineSlice()
}
- n := make([]Line, 0, it.Len())
+ len := it.Len()
+ if len == 0 {
+ return nil
+ }
+ l := make([]Line, 0, len)
for it.Next() {
- n = append(n, it.Line())
+ l = append(l, it.Line())
}
- return n
+ return l
}
// WeightedLines is a WeightedLine iterator.
@@ -201,9 +217,13 @@ func WeightedLinesOf(it WeightedLines) []WeightedLine {
case WeightedLineSlicer:
return it.WeightedLineSlice()
}
- n := make([]WeightedLine, 0, it.Len())
+ len := it.Len()
+ if len == 0 {
+ return nil
+ }
+ l := make([]WeightedLine, 0, len)
for it.Next() {
- n = append(n, it.WeightedLine())
+ l = append(l, it.WeightedLine())
}
- return n
+ return l
}

graph: make slicer helpers return nil for zero length

Also fix up var names.