-
Notifications
You must be signed in to change notification settings - Fork 1
/
positions.go
81 lines (70 loc) · 1.34 KB
/
positions.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
package pix
type posList struct {
rest []Pos
first Pos
}
func (p *posList) insert(pos Pos) {
p.rest = append(p.rest, pos)
}
func (p *posList) delete(pos Pos) bool {
rest := p.rest
n := len(rest)
if p.first == pos {
if n > 0 {
// move a guy from rest to first
p.first = rest[n-1]
p.rest = rest[:n-1]
} else {
// the first position has been removed and there are no others.
return true
}
} else {
// remove the position from the rest list
var found bool
for i, x := range rest {
if x == pos {
rest[i] = rest[n-1]
p.rest = rest[:n-1]
found = true
break
}
}
if !found {
panic("attempting to remove a non-existent position from the frontier")
}
}
return false
}
func (p *posList) arbitrary() Pos {
return p.first
}
/* for later, benchmark this first-less version:
package pix
type posList struct {
rest []Pos
}
func (p *posList) insert(pos Pos) {
p.rest = append(p.rest, pos)
}
func (p *posList) delete(pos Pos) bool {
rest := p.rest
n := len(rest)
// remove the position from the rest list
var found bool
for i, x := range rest {
if x == pos {
rest[i] = rest[n-1]
p.rest = rest[:n-1]
found = true
break
}
}
if !found {
panic("attempting to remove a non-existent position from the frontier")
}
return n == 1
}
func (p *posList) arbitrary() Pos {
return p.rest[0]
}
*/