Repository navigation
Expand file tree
/
Copy pathsolve.go
More file actions
102 lines (94 loc) · 1.7 KB
/
Copy pathsolve.go
File metadata and controls
102 lines (94 loc) · 1.7 KB
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
// Copyright 2016 The Gini Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package dimacs
import (
"bufio"
"fmt"
"io"
"strings"
)
func ReadSolve(r io.Reader, vis SolveVis) error {
cfiltRdr := NewCommentFilter(r)
s := &sRdr{
rdr: bufio.NewReader(cfiltRdr),
vis: vis}
return s.read()
}
type solveState int
const (
neither solveState = iota
sol
values
)
type sRdr struct {
rdr *bufio.Reader
vis SolveVis
}
func (s *sRdr) handleSolution(sol string) error {
sol = strings.TrimSpace(sol)
switch sol {
case "SATISFIABLE":
s.vis.Solution(1)
case "UNSATISFIABLE":
s.vis.Solution(-1)
case "UNKNOWN":
s.vis.Solution(0)
default:
return fmt.Errorf("unknown solution '%s", sol)
}
return nil
}
func (s *sRdr) read() error {
rdr := s.rdr
vis := s.vis
state := neither
solbuf := make([]byte, 0, 32)
line := 0
for {
c, e := rdr.ReadByte()
if e == io.EOF {
if len(solbuf) != 0 {
if e := s.handleSolution(string(solbuf)); e != nil {
return e
}
}
vis.Eof()
return nil
}
if e != nil {
return e
}
if c == byte('\n') {
line++
state = neither
if len(solbuf) != 0 {
if e := s.handleSolution(string(solbuf)); e != nil {
return e
}
}
solbuf = solbuf[:0]
continue
}
switch state {
case neither:
if c == byte('s') {
state = sol
} else if c == byte('v') {
state = values
} else {
return fmt.Errorf("unexpected character '%c' at line %d\n", c, line)
}
case values:
if e := rdr.UnreadByte(); e != nil {
return e
}
m, e := readLit(rdr)
if e != nil {
return e
}
vis.Value(m)
case sol:
solbuf = append(solbuf, c)
}
}
}