Skip to content

Commit 9e3e8cb

Browse files
committed
use receiver
1 parent 4a3dcd4 commit 9e3e8cb

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

dance.go

+30-30
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88
)
99

10-
func option(m *MCC, p, head, score int) Option {
10+
func (m *MCC) option(p, head, score int) Option {
1111
var opt Option
1212
cl, nd := m.cl, m.nd
1313

@@ -46,7 +46,7 @@ func option(m *MCC, p, head, score int) Option {
4646
return opt
4747
}
4848

49-
func hide(m *MCC, rr int) {
49+
func (m *MCC) hide(rr int) {
5050
nd := m.nd
5151
for nn := rr + 1; nn != rr; {
5252
if nd[nn].color >= 0 {
@@ -64,19 +64,19 @@ func hide(m *MCC, rr int) {
6464
}
6565
}
6666

67-
func cover(m *MCC, c int, deact bool) {
67+
func (m *MCC) cover(c int, deact bool) {
6868
cl, nd := m.cl, m.nd
6969
if deact {
7070
l, r := cl[c].prev, cl[c].next
7171
cl[l].next, cl[r].prev = r, l
7272
}
7373
m.updates++
7474
for rr := nd[c].down; rr >= m.lastItm; rr = nd[rr].down {
75-
hide(m, rr)
75+
m.hide(rr)
7676
}
7777
}
7878

79-
func unhide(m *MCC, rr int) {
79+
func (m *MCC) unhide(rr int) {
8080
nd := m.nd
8181
for nn := rr + 1; nn != rr; {
8282
if nd[nn].color >= 0 {
@@ -93,47 +93,47 @@ func unhide(m *MCC, rr int) {
9393
}
9494
}
9595

96-
func uncover(m *MCC, c int, react bool) {
96+
func (m *MCC) uncover(c int, react bool) {
9797
cl, nd := m.cl, m.nd
9898
for rr := nd[c].down; rr >= m.lastItm; rr = nd[rr].down {
99-
unhide(m, rr)
99+
m.unhide(rr)
100100
}
101101
if react {
102102
l, r := cl[c].prev, cl[c].next
103103
cl[r].prev, cl[l].next = c, c
104104
}
105105
}
106106

107-
func purify(m *MCC, p int) {
107+
func (m *MCC) purify(p int) {
108108
nd := m.nd
109109
cc := nd[p].itm
110110
x := nd[p].color
111111
nd[cc].color = x
112112
m.cleansings++
113113
for rr := nd[cc].down; rr >= m.lastItm; rr = nd[rr].down {
114114
if nd[rr].color != x {
115-
hide(m, rr)
115+
m.hide(rr)
116116
} else if rr != p {
117117
m.cleansings++
118118
nd[rr].color = -1
119119
}
120120
}
121121
}
122122

123-
func unpurify(m *MCC, p int) {
123+
func (m *MCC) unpurify(p int) {
124124
nd := m.nd
125125
cc := nd[p].itm
126126
x := nd[p].color
127127
for rr := nd[cc].up; rr >= m.lastItm; rr = nd[rr].up {
128128
if nd[rr].color < 0 {
129129
nd[rr].color = x
130130
} else if rr != p {
131-
unhide(m, rr)
131+
m.unhide(rr)
132132
}
133133
}
134134
}
135135

136-
func tweak(m *MCC, n, block int) {
136+
func (m *MCC) tweak(n, block int) {
137137
nd := m.nd
138138
nn := n
139139
if block != 0 {
@@ -158,7 +158,7 @@ func tweak(m *MCC, n, block int) {
158158
}
159159
}
160160

161-
func untweak(m *MCC, c, x, unblock int) {
161+
func (m *MCC) untweak(c, x, unblock int) {
162162
nd := m.nd
163163
z := nd[c].down
164164
nd[c].down = x
@@ -167,19 +167,19 @@ func untweak(m *MCC, c, x, unblock int) {
167167
nd[rr].up = qq
168168
k++
169169
if unblock != 0 {
170-
unhide(m, rr)
170+
m.unhide(rr)
171171
}
172172
}
173173
nd[rr].up = qq
174174
nd[c].itm += k
175175
if unblock == 0 {
176-
uncover(m, c, false)
176+
m.uncover(c, false)
177177
}
178178
}
179179

180180
// Dance generates all exact covers
181181
func (m *MCC) Dance(rd io.Reader) Result {
182-
if err := inputMatrix(m, rd); err != nil {
182+
if err := m.inputMatrix(rd); err != nil {
183183
panic(err)
184184
}
185185

@@ -252,7 +252,7 @@ func (m *MCC) Dance(rd io.Reader) Result {
252252
if head == 0 {
253253
head = nd[cc].down
254254
}
255-
sol[k] = option(m, pp, head, scor[k])
255+
sol[k] = m.option(pp, head, scor[k])
256256
}
257257

258258
select {
@@ -278,11 +278,11 @@ func (m *MCC) Dance(rd io.Reader) Result {
278278
cl[bestItm].bound--
279279

280280
if cl[bestItm].bound == 0 && cl[bestItm].slack == 0 {
281-
cover(m, bestItm, true)
281+
m.cover(bestItm, true)
282282
} else {
283283
firstTweak[level] = curNode
284284
if cl[bestItm].bound == 0 {
285-
cover(m, bestItm, true)
285+
m.cover(bestItm, true)
286286
}
287287
}
288288

@@ -294,7 +294,7 @@ func (m *MCC) Dance(rd io.Reader) Result {
294294
} else if nd[bestItm].itm <= cl[bestItm].bound-cl[bestItm].slack {
295295
goto backup
296296
} else if curNode != bestItm {
297-
tweak(m, curNode, cl[bestItm].bound)
297+
m.tweak(curNode, cl[bestItm].bound)
298298
} else if cl[bestItm].bound != 0 {
299299
p, q := cl[bestItm].prev, cl[bestItm].next
300300
cl[p].next, cl[q].prev = q, p
@@ -303,9 +303,9 @@ func (m *MCC) Dance(rd io.Reader) Result {
303303
if m.Debug {
304304
fmt.Fprintf(os.Stderr, "L%d: ", level)
305305
if cl[bestItm].bound == 0 && cl[bestItm].slack == 0 {
306-
option(m, curNode, nd[bestItm].down, score)
306+
m.option(curNode, nd[bestItm].down, score)
307307
} else {
308-
option(m, curNode, firstTweak[level], score)
308+
m.option(curNode, firstTweak[level], score)
309309
}
310310
}
311311

@@ -319,13 +319,13 @@ func (m *MCC) Dance(rd io.Reader) Result {
319319
if cc < m.second {
320320
cl[cc].bound--
321321
if cl[cc].bound == 0 {
322-
cover(m, cc, true)
322+
m.cover(cc, true)
323323
}
324324
} else {
325325
if nd[pp].color == 0 {
326-
cover(m, cc, true)
326+
m.cover(cc, true)
327327
} else if nd[pp].color > 0 {
328-
purify(m, pp)
328+
m.purify(pp)
329329
}
330330
}
331331
pp++
@@ -345,9 +345,9 @@ func (m *MCC) Dance(rd io.Reader) Result {
345345

346346
backup: // Restore the original state of bestItm
347347
if cl[bestItm].bound == 0 && cl[bestItm].slack == 0 {
348-
uncover(m, bestItm, true)
348+
m.uncover(bestItm, true)
349349
} else {
350-
untweak(m, bestItm, firstTweak[level], cl[bestItm].bound)
350+
m.untweak(bestItm, firstTweak[level], cl[bestItm].bound)
351351
}
352352
cl[bestItm].bound++
353353

@@ -376,14 +376,14 @@ func (m *MCC) Dance(rd io.Reader) Result {
376376
} else {
377377
if cc < m.second {
378378
if cl[cc].bound == 0 {
379-
uncover(m, cc, true)
379+
m.uncover(cc, true)
380380
}
381381
cl[cc].bound++
382382
} else {
383383
if nd[pp].color == 0 {
384-
uncover(m, cc, true)
384+
m.uncover(cc, true)
385385
} else if nd[pp].color > 0 {
386-
unpurify(m, pp)
386+
m.unpurify(pp)
387387
}
388388
}
389389
pp--

input.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
const delta int = 4
1414

15-
func inputMatrix(m *MCC, rd io.Reader) error {
15+
func (m *MCC) inputMatrix(rd io.Reader) error {
1616
var line string
1717

1818
scanner := bufio.NewScanner(rd)
@@ -32,7 +32,7 @@ func inputMatrix(m *MCC, rd io.Reader) error {
3232
return err
3333
}
3434

35-
if err := inputItemNames(m, line); err != nil {
35+
if err := m.inputItemNames(line); err != nil {
3636
return err
3737
}
3838

@@ -63,7 +63,7 @@ func inputMatrix(m *MCC, rd io.Reader) error {
6363
return nil
6464
}
6565

66-
func inputItemNames(m *MCC, line string) error {
66+
func (m *MCC) inputItemNames(line string) error {
6767
if m.lastItm == 0 {
6868
return fmt.Errorf("no items")
6969
}

0 commit comments

Comments
 (0)