diff --git a/export.go b/export.go index 6edf950..80f943f 100644 --- a/export.go +++ b/export.go @@ -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") } @@ -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), diff --git a/graph.go b/graph.go index 539174b..48bc280 100644 --- a/graph.go +++ b/graph.go @@ -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 { diff --git a/import.go b/import.go index 2e1c561..573f616 100644 --- a/import.go +++ b/import.go @@ -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 = ';' @@ -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 = ';' @@ -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 }