forked from RyanCarrier/dijkstra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
131 lines (127 loc) · 2.95 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package dijkstra
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
// Import imports a graph from the specified file returns the Graph, a map for
// if the nodes are not integers and an error if needed.
func Import(filename string) (g Graph, err error) {
g.usingMap = false
var lowestIndex int
var i int
var arc int
var idist int64
var dist float64
var ok bool
got, err := ioutil.ReadFile(filename)
if err != nil {
return
}
g.mapping = map[string]int{}
input := strings.TrimSpace(string(got))
for _, line := range strings.Split(input, "\n") {
f := strings.Fields(strings.TrimSpace(line))
if len(f) == 0 || (len(f) == 1 && f[0] == "") {
continue
}
//no need to check for size cause there must be something as the string is trimmed and split
if g.usingMap {
if i, ok = g.mapping[f[0]]; !ok {
g.mapping[f[0]] = lowestIndex
i = lowestIndex
lowestIndex++
}
} else {
i, err = strconv.Atoi(f[0])
if err != nil {
g.usingMap = true
g.mapping[f[0]] = lowestIndex
i = lowestIndex
lowestIndex++
}
}
if temp := len(g.Verticies); temp <= i { //Extend if we have to
g.Verticies = append(g.Verticies, make([]Vertex, 1+i-len(g.Verticies))...)
for ; temp < len(g.Verticies); temp++ {
g.Verticies[temp].ID = temp
g.Verticies[temp].arcs = map[int]float64{}
g.Verticies[temp].bestVerticies = []int{-1}
}
}
if len(f) == 1 {
//if there is no FROM here
continue
}
for _, set := range f[1:] {
got := strings.Split(set, ",")
if len(got) != 2 {
err = ErrWrongFormat
return
}
idist, err = strconv.ParseInt(got[1], 10, 64)
dist = float64(idist)
if err != nil {
err = ErrWrongFormat
return
}
if g.usingMap {
arc, ok = g.mapping[got[0]]
if !ok {
arc = lowestIndex
g.mapping[got[0]] = arc
lowestIndex++
}
} else {
arc, err = strconv.Atoi(got[0])
if err != nil {
err = ErrMixMapping
return
}
}
g.Verticies[i].arcs[arc] = dist
}
}
err = g.validate()
return
}
// ExportToFile exports the verticies to file currently does not take into account
// mappings (from string to int)
func (g Graph) ExportToFile(filename string) error {
var i string
var err error
os.MkdirAll(filename, 0777)
if _, err = os.Stat(filename); err == nil {
os.Remove(filename)
}
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return err
}
defer f.Close()
for _, v := range g.Verticies {
if g.usingMap {
if i, err = g.GetMapped(v.ID); err != nil {
return errors.New("mapping fail when exporting; " + err.Error())
}
fmt.Fprint(f, i)
} else {
fmt.Fprint(f, v.ID)
}
for key, val := range v.arcs {
if g.usingMap {
if i, err = g.GetMapped(key); err != nil {
return errors.New("mapping fail when exporting; " + err.Error())
}
fmt.Fprint(f, " ", i, ",", val)
} else {
fmt.Fprint(f, " ", key, ",", val)
}
}
fmt.Fprint(f, "\n")
}
return nil
}