-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.go
229 lines (197 loc) · 5.03 KB
/
input.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
package dlx
import (
"bufio"
"fmt"
"hash/crc32"
"io"
"os"
"strconv"
"strings"
)
const delta int = 4
func (m *MCC) inputMatrix(rd io.Reader) error {
var line string
scanner := bufio.NewScanner(rd)
for scanner.Scan() {
line = scanner.Text()
if len(line) > maxLine {
return fmt.Errorf("input line way too long")
}
line = strings.TrimSpace(line)
if line == "" || line[0] == '|' { // bypass comment or blank line
continue
}
m.lastItm = 1
break
}
if err := scanner.Err(); err != nil {
return err
}
if err := m.inputItemNames(line); err != nil {
return err
}
for scanner.Scan() {
line = scanner.Text()
if len(line) > maxLine {
return fmt.Errorf("input line way too long")
}
line = strings.TrimSpace(line)
if line == "" || line[0] == '|' { // bypass comment or blank line
continue
}
if err := inputOptions(m, line); err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
return err
}
if m.Debug {
fmt.Fprintf(os.Stderr,
"(%d options, %d+%d items, %d entries successfully read)\n",
m.options, m.second-1, m.lastItm-m.second, m.lastNode-m.lastItm)
}
return nil
}
func (m *MCC) inputItemNames(line string) error {
if m.lastItm == 0 {
return fmt.Errorf("no items")
}
for _, itm := range strings.Fields(line) {
if itm == "|" {
if m.second != maxCols {
return fmt.Errorf("item name line contains | twice")
}
m.second = m.lastItm
continue
}
if len(itm) > maxNameLength {
return fmt.Errorf("item name too long")
}
q, r := 1, 1
m.cl[m.lastItm].name = itm
if strings.Contains(itm, "|") {
bn := strings.Split(itm, "|")
m.cl[m.lastItm].name = bn[1]
q, _ = strconv.Atoi(bn[0])
r = q
if strings.Contains(bn[0], ":") {
bounds := strings.Split(bn[0], ":")
r, _ = strconv.Atoi(bounds[0])
q, _ = strconv.Atoi(bounds[1])
}
}
m.cl[m.lastItm].bound = q
m.cl[m.lastItm].slack = q - r
// Check for duplicate item name
for k := 1; k < m.lastItm; k++ {
if m.cl[k].name == m.cl[m.lastItm].name {
return fmt.Errorf("duplicate item name")
}
}
// Initialize lastItm to a new item with an empty list
if m.lastItm > maxCols {
return fmt.Errorf("too many items")
}
m.cl[m.lastItm-1].next = m.lastItm
m.cl[m.lastItm].prev = m.lastItm - 1
m.nd[m.lastItm].down = m.lastItm
m.nd[m.lastItm].up = m.lastItm
m.lastItm++
if m.lastItm >= len(m.cl)-delta {
m.cl = append(m.cl, make([]item, chunkSize)...)
m.nd = append(m.nd, make([]node, chunkSize)...)
}
}
if m.second == maxCols {
m.second = m.lastItm
}
m.cl[m.lastItm].prev = m.lastItm - 1
m.cl[m.lastItm-1].next = m.lastItm
m.cl[m.second].prev = m.lastItm
m.cl[m.lastItm].next = m.second
// this sequence works properly whether second == lastItm
m.cl[root].prev = m.second - 1
m.cl[m.second-1].next = root
m.lastNode = m.lastItm // reserve all the header nodes and the first spacer
return nil
}
func inputOptions(m *MCC, line string) error {
leftSpacer := m.lastNode // remember the spacer at the left of this option
nonePrimary := true
for _, opt := range strings.Fields(line) {
if len(opt) > maxNameLength {
return fmt.Errorf("item name too long")
}
if opt[0] == ':' {
return fmt.Errorf("empty item name")
}
m.cl[m.lastItm].name = opt
icr := strings.Index(opt, ":")
if icr >= 0 { // has color code
m.cl[m.lastItm].name = opt[:icr]
}
// Create a node for the item named in opt
k := 0
for ; m.cl[k].name != m.cl[m.lastItm].name; k++ {
}
if k == m.lastItm {
return fmt.Errorf("unknown item name")
}
if m.nd[k].color >= leftSpacer { // aux field
return fmt.Errorf("duplicate item name")
}
m.lastNode++
if m.lastNode == maxNodes {
return fmt.Errorf("too many nodes")
}
if m.lastNode >= len(m.nd)-delta {
m.nd = append(m.nd, make([]node, chunkSize)...)
}
m.nd[m.lastNode].itm = k
if k < m.second {
nonePrimary = false
}
m.nd[k].itm++
m.nd[k].color = m.lastNode // aux field
r := m.nd[k].up
m.nd[r].down = m.lastNode
m.nd[k].up = m.lastNode
m.nd[m.lastNode].up = r
m.nd[m.lastNode].down = k
m.nd[m.lastNode].color = 0
m.nd[m.lastNode].colorName = ""
if icr >= 0 { // has color code
if k >= m.second {
cName := opt[icr+1:]
m.nd[m.lastNode].color = int(crc32.ChecksumIEEE([]byte(cName)))
m.nd[m.lastNode].colorName = ":" + cName
} else {
return fmt.Errorf("primary item must be uncolored")
}
}
}
if nonePrimary { // Option ignored (no primary items)
for m.lastNode > leftSpacer { // Remove lastNode from its item list
k := m.nd[m.lastNode].itm
m.nd[k].itm--
m.nd[k].color = leftSpacer - 1
q, r := m.nd[m.lastNode].up, m.nd[m.lastNode].down
m.nd[q].down, m.nd[r].up = r, q
m.lastNode--
}
} else {
m.nd[leftSpacer].down = m.lastNode
m.lastNode++ // create the next spacer
if m.lastNode == maxNodes {
return fmt.Errorf("too many nodes")
}
if m.lastNode >= len(m.nd)-delta {
m.nd = append(m.nd, make([]node, chunkSize)...)
}
m.options++
m.nd[m.lastNode].up = leftSpacer + 1
m.nd[m.lastNode].itm = -m.options
}
return nil
}