-
Notifications
You must be signed in to change notification settings - Fork 33
/
util.go
143 lines (135 loc) · 3.49 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
132
133
134
135
136
137
138
139
140
141
142
143
package dijkstra
import (
"fmt"
"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(data string) (g Graph, err error) {
var i int
var arc int
var dist uint64
input := strings.TrimSpace(string(data))
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
i, err = strconv.Atoi(f[0])
if err != nil {
return g, err
}
if temp := len(g.vertexArcs); temp <= i { //Extend if we have to
g.vertexArcs = append(g.vertexArcs, make([]map[int]uint64, 1+i-len(g.vertexArcs))...)
for ; temp < len(g.vertexArcs); temp++ {
g.vertexArcs[temp] = map[int]uint64{}
}
}
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
}
dist, err = strconv.ParseUint(got[1], 10, 64)
if err != nil {
err = ErrWrongFormat
return
}
arc, err = strconv.Atoi(got[0])
if err != nil {
return g, err
}
g.vertexArcs[i][arc] = dist
}
}
return g, g.validate()
}
func ImportStringMapped(data string) (mg MappedGraph[string], err error) {
var lowestIndex int
var i int
var arc int
var dist uint64
var ok bool
mg.mapping = map[string]int{}
input := strings.TrimSpace(string(data))
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 i, ok = mg.mapping[f[0]]; !ok {
mg.mapping[f[0]] = lowestIndex
i = lowestIndex
lowestIndex++
}
if temp := len(mg.graph.vertexArcs); temp <= i { //Extend if we have to
mg.graph.vertexArcs = append(mg.graph.vertexArcs, make([]map[int]uint64, 1+i-len(mg.graph.vertexArcs))...)
for ; temp < len(mg.graph.vertexArcs); temp++ {
mg.graph.vertexArcs[temp] = map[int]uint64{}
}
}
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
}
dist, err = strconv.ParseUint(got[1], 10, 64)
if err != nil {
err = ErrWrongFormat
return
}
arc, ok = mg.mapping[got[0]]
if !ok {
arc = lowestIndex
mg.mapping[got[0]] = arc
lowestIndex++
}
mg.graph.vertexArcs[i][arc] = dist
}
}
err = mg.validate()
return
}
func (g Graph) Export() (string, error) {
var result = strings.Builder{}
for id, v := range g.vertexArcs {
result.WriteString(strconv.Itoa(id))
for key, val := range v {
result.WriteString(" " + strconv.Itoa(key) + "," + strconv.FormatUint(val, 10))
}
result.WriteRune('\n')
}
return result.String(), nil
}
func (mg MappedGraph[T]) Export() (string, error) {
var err error
var result = strings.Builder{}
for k, v := range mg.mapping {
result.WriteString(fmt.Sprint(k))
if err = mg.graph.vertexValid(v); err != nil {
return "", err
}
for to, dist := range mg.graph.vertexArcs[v] {
name, err := mg.getInverseMap(to)
if err != nil {
return "", err
}
result.WriteString(" " + fmt.Sprint(name) + "," + strconv.FormatUint(dist, 10))
}
result.WriteRune('\n')
}
return result.String(), nil
}