-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
283 lines (240 loc) · 6.96 KB
/
reader.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package mlst
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
)
// extend the bufio.Reader, and keep track of the line number
type Reader struct {
reader *bufio.Reader
lineNum int
caseNum int
line string
}
type Error struct {
lineNum int
caseNum int
line string
message string
}
type InFileReader struct {
Reader
}
type OutFileReader struct {
Reader
}
/*** Constants and Initializations ***/
const (
InvalidNumLeaves = -1
)
var (
NumberFormatRegexp [3]*regexp.Regexp
NumberExpectedMsg [3]string
)
func init() {
NumberFormatRegexp[1] = regexp.MustCompile("^(\\d+)\\n?$")
NumberFormatRegexp[2] = regexp.MustCompile("^(\\d+) (\\d+)\\n?$")
NumberExpectedMsg[1] = "Expecting one natural number on a line with " +
"no leading or trailing spaces."
NumberExpectedMsg[2] = "Expecting two natural numbers separated by a single " +
"space on a line with no leading or trailing spaces."
}
/*** Error ***/
func (e *Error) caseInfo() string {
if e.caseNum == 0 {
return ""
}
return fmt.Sprintf(" (Graph #%d)", e.caseNum)
}
func (e *Error) Error() string {
return fmt.Sprintf("Error on line %d%s: Got '%s'. %s", e.lineNum, e.caseInfo(), e.line,
e.message)
}
/*** Reader ***/
func (r *Reader) readLine() (err error) {
r.lineNum++
r.line, err = r.reader.ReadString('\n')
return
}
func (r *Reader) strippedLine() string {
if len(r.line) > 0 && r.line[len(r.line)-1] == '\n' {
return r.line[:len(r.line)-1]
}
return r.line
}
// construct a new Error struct
func (r *Reader) Error(msg string) *Error {
return &Error{lineNum: r.lineNum, caseNum: r.caseNum, line: r.strippedLine(),
message: msg}
}
// construct a new Error struct with the expected message
func (r *Reader) ErrorWithExpected(msg, expected string) *Error {
return r.Error(msg + " " + expected)
}
// read one or two numbers on a line
func (r *Reader) readNumbers(msg string, n int) ([]int, *Error) {
if err := r.readLine(); err != nil {
return nil, r.ErrorWithExpected(msg+" ("+err.Error()+")", "Expecting the next line.")
}
matches := NumberFormatRegexp[n].FindStringSubmatch(r.line)
if matches == nil || len(matches) != n+1 {
return nil, r.ErrorWithExpected(msg, NumberExpectedMsg[n])
}
ints := make([]int, len(matches)-1)
for i := range ints {
if num, err := strconv.Atoi(matches[i+1]); err != nil {
return nil, r.ErrorWithExpected(msg+" ("+err.Error()+")", NumberExpectedMsg[n])
} else {
ints[i] = num
}
}
return ints, nil
}
/*** InFileReader ***/
func NewInFileReader(rd io.Reader) *InFileReader {
var inReader InFileReader
inReader.reader = bufio.NewReader(rd)
return &inReader
}
func (r *InFileReader) ReadInputFile() ([]EdgeSet, *Error) {
nums, err := r.readNumbers("Cannot parse the number of input graphs.", 1)
if err != nil {
return nil, err
}
NumCases := nums[0]
edgeSets := make([]EdgeSet, NumCases)
for r.caseNum = 1; r.caseNum <= NumCases; r.caseNum++ {
edgeSet, err := r.ReadInputGraph()
if err != nil {
return nil, err
}
G := edgeSet.Graph()
G.Search()
if !G.EdgesInOneComponent() {
return nil, r.Error("Disconnected graph: after reading the last edge of " +
"this graph, the edges are not in the same component.")
}
edgeSets[r.caseNum-1] = edgeSet
}
if err := r.readLine(); err != io.EOF {
return nil, r.ErrorWithExpected(fmt.Sprintf(
"Extra lines after Graph #%d (line 1 says the number of graphs is %d).",
NumCases, NumCases), "Expecting EOF.")
}
return edgeSets, nil
}
func (r *InFileReader) ReadInputGraph() (EdgeSet, *Error) {
nums, err := r.readNumbers("Cannot parse the number of edges.", 1)
if err != nil {
return nil, err
}
NumEdges := nums[0]
if NumEdges > MaxNumEdges {
return nil, r.Error(fmt.Sprintf("Number of edges cannot exceed %d.", MaxNumEdges))
}
edgeSet := make(EdgeSet)
for i := 0; i < NumEdges; i++ {
nums, err = r.readNumbers("Cannot parse the next edge.", 2)
if err != nil {
return nil, err
} else {
var e Edge
e.Ends[0] = nums[0]
e.Ends[1] = nums[1]
e.Normalize()
if err := e.Error(); err != nil {
return nil, r.Error(err.Error())
}
if _, ok := edgeSet[e]; ok {
return nil, r.Error(fmt.Sprintf("Edge %s (or its reverse) is duplicated.", e))
}
edgeSet[e] = true
}
}
return edgeSet, nil
}
/*** OutFileReader ***/
func NewOutFileReader(rd io.Reader) *OutFileReader {
var outReader OutFileReader
outReader.reader = bufio.NewReader(rd)
return &outReader
}
func (r *OutFileReader) ReadOutputFile(edgeSets []EdgeSet) ([]int, *Error) {
nums, err := r.readNumbers("Cannot parse the number of output graphs.", 1)
if err != nil {
return nil, err
}
if nums[0] != len(edgeSets) {
return nil, r.Error(fmt.Sprintf("The number of output graphs (%d) should "+
"equal the number of input graphs (%d).", nums[0], len(edgeSets)))
}
NumCases := len(edgeSets)
NumLeaves := make([]int, NumCases)
for r.caseNum = 1; r.caseNum <= NumCases; r.caseNum++ {
NumLeaves[r.caseNum-1], err = r.ReadOutputGraph(edgeSets[r.caseNum-1])
if err != nil {
return nil, err
}
}
if err := r.readLine(); err != io.EOF {
return nil, r.ErrorWithExpected(fmt.Sprintf(
"Extra lines after Graph #%d (line 1 says the number of graphs is %d).",
NumCases, NumCases), "Expecting EOF.")
}
return NumLeaves, nil
}
func (r *OutFileReader) ReadOutputGraph(inEdgeSet EdgeSet) (int, *Error) {
nums, err := r.readNumbers("Cannot parse the number of edges.", 1)
if err != nil {
return InvalidNumLeaves, err
}
Gin := inEdgeSet.Graph()
Gin.Search()
if nums[0] != Gin.NumNodes-1 {
return InvalidNumLeaves, r.Error(fmt.Sprintf("Input graph has %d "+
"non-isolated nodes, output graph should have %d edges, got %d instead.",
Gin.NumNodes, Gin.NumNodes-1, nums[0]))
}
outEdgeSet := make(EdgeSet)
NumEdges := nums[0]
for i := 0; i < NumEdges; i++ {
nums, err = r.readNumbers("Cannot parse the next edge.", 2)
if err != nil {
return InvalidNumLeaves, err
} else {
var e Edge
e.Ends[0] = nums[0]
e.Ends[1] = nums[1]
e.Normalize()
if _, ok := inEdgeSet[e]; !ok {
return InvalidNumLeaves, r.Error(fmt.Sprintf("Edge %s in the output "+
"graph is absent in the input graph.", e))
}
if _, ok := outEdgeSet[e]; ok {
return InvalidNumLeaves, r.Error(fmt.Sprintf(
"Edge %s (or its reverse) is duplicated.", e))
}
outEdgeSet[e] = true
}
}
Gout := outEdgeSet.Graph()
Gout.Search()
if Gout.NumNodes != Gin.NumNodes {
return InvalidNumLeaves, r.Error(fmt.Sprintf(
"Aftering reading the last edge, the number of "+
"non-isolated nodes in the output graph (%d) should equal "+
"that of the input graph (%d) to be a spanning tree.",
Gout.NumNodes, Gin.NumNodes))
}
if !Gout.EdgesInOneComponent() {
return InvalidNumLeaves, r.Error("Disconnected graph: after reading " +
"the last edge, the output graph should be connected to be a spanning tree.")
}
if Gout.HasCycle {
return InvalidNumLeaves, r.Error("Cycle detected: after reading the " +
"last edge, the output graph should not have cycles to be a spanning tree.")
}
return Gout.NumLeaves, nil
}