Skip to content

Commit

Permalink
defer close + naming
Browse files Browse the repository at this point in the history
  • Loading branch information
LdDl committed Apr 30, 2021
1 parent 49ffb55 commit 9413bf1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
14 changes: 7 additions & 7 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ func (graph *Graph) ExportVerticesToFile(fname string) error {
// weight - float64, Weight of an shortcut
// via_vertex_id - int64, ID of vertex through which the shortcut exists
func (graph *Graph) ExportShortcutsToFile(fname string) error {
fileContractions, err := os.Create(fname)
fileShortcuts, err := os.Create(fname)
if err != nil {
return errors.Wrap(err, "Can't create shortcuts file")
}
defer fileContractions.Close()
writerContractions := csv.NewWriter(fileContractions)
defer writerContractions.Flush()
writerContractions.Comma = ';'
err = writerContractions.Write([]string{"from_vertex_id", "to_vertex_id", "weight", "via_vertex_id"})
defer fileShortcuts.Close()
writerShortcuts := csv.NewWriter(fileShortcuts)
defer writerShortcuts.Flush()
writerShortcuts.Comma = ';'
err = writerShortcuts.Write([]string{"from_vertex_id", "to_vertex_id", "weight", "via_vertex_id"})
if err != nil {
return errors.Wrap(err, "Can't write header to shortucts file")
}
Expand All @@ -147,7 +147,7 @@ func (graph *Graph) ExportShortcutsToFile(fname string) error {
for targetVertexInternal, viaNodeInternal := range to {
targetVertexExternal := graph.Vertices[targetVertexInternal].Label
viaNodeExternal := graph.Vertices[viaNodeInternal.ViaVertex].Label
err = writerContractions.Write([]string{
err = writerShortcuts.Write([]string{
fmt.Sprintf("%d", sourceVertexExternal),
fmt.Sprintf("%d", targetVertexExternal),
strconv.FormatFloat(viaNodeInternal.Cost, 'f', -1, 64),
Expand Down
7 changes: 4 additions & 3 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ func (graph *Graph) AddEdge(from, to int64, weight float64) error {

// AddShortcut Adds new shortcut between two vertices
//
// from User's definied ID of first vertex of shortcut
// to User's definied ID of last vertex of shortcut
// weight User's definied weight of shortcut
// from - User's definied ID of first vertex of shortcut
// to - User's definied ID of last vertex of shortcut
// via - User's defined ID of vertex through which the shortcut exists
// weight - User's definied weight of shortcut
//
func (graph *Graph) AddShortcut(from, to, via int64, weight float64) error {
if graph.frozen {
Expand Down
13 changes: 8 additions & 5 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func ImportFromFile(edgesFname, verticesFname, contractionsFname string) (*Graph
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
reader.Comma = ';'

Expand Down Expand Up @@ -81,6 +82,7 @@ func ImportFromFile(edgesFname, verticesFname, contractionsFname string) (*Graph
if err != nil {
return nil, err
}
defer fileVertices.Close()
readerVertices := csv.NewReader(fileVertices)
readerVertices.Comma = ';'

Expand Down Expand Up @@ -118,20 +120,21 @@ func ImportFromFile(edgesFname, verticesFname, contractionsFname string) (*Graph
}

// Read contractions
fileContractions, err := os.Open(contractionsFname)
fileShortcuts, err := os.Open(contractionsFname)
if err != nil {
return nil, err
}
readerContractions := csv.NewReader(fileContractions)
readerContractions.Comma = ';'
defer fileShortcuts.Close()
readerShortcuts := csv.NewReader(fileShortcuts)
readerShortcuts.Comma = ';'
// Skip header of CSV-file
_, err = readerContractions.Read()
_, err = readerShortcuts.Read()
if err != nil {
return nil, err
}
// Read file line by line
for {
record, err := readerContractions.Read()
record, err := readerShortcuts.Read()
if err == io.EOF {
break
}
Expand Down

0 comments on commit 9413bf1

Please sign in to comment.