-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtileCoordSet.go
176 lines (154 loc) · 3.39 KB
/
tileCoordSet.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
package gridspech
import "strings"
// TileCoordSet represents a mathematical set of coordinates.
type TileCoordSet struct {
set map[TileCoord]struct{}
}
// NewTileCoordSet returns a TileCoordSet containing only tiles.
func NewTileCoordSet(tiles ...TileCoord) TileCoordSet {
var cs TileCoordSet
for _, tile := range tiles {
cs.Add(tile)
}
return cs
}
// Init initializes the tilecoordset.
func (ts *TileCoordSet) checkInit() {
if ts.set == nil {
ts.set = make(map[TileCoord]struct{})
}
}
// Add adds t to the TileSet ts.
func (ts *TileCoordSet) Add(t TileCoord) {
ts.checkInit()
ts.set[t] = struct{}{}
}
// Has returns if ts contains t.
func (ts TileCoordSet) Has(t TileCoord) bool {
_, ok := ts.set[t]
return ok
}
// Remove removes t from ts.
func (ts *TileCoordSet) Remove(t TileCoord) {
ts.checkInit()
delete(ts.set, t)
}
// RemoveIf removes each value for which pred returns true.
func (ts *TileCoordSet) RemoveIf(pred func(coord TileCoord) bool) {
for tile := range ts.set {
if pred(tile) {
ts.Remove(tile)
}
}
}
// RemoveAll removes all of the elements in o from ts (making ts the intersection of ts and o)
func (ts *TileCoordSet) RemoveAll(o TileCoordSet) {
if ts.Len() < o.Len() {
for tile := range ts.set {
if o.Has(tile) {
ts.Remove(tile)
}
}
} else {
for tile := range o.set {
if ts.Has(tile) {
ts.Remove(tile)
}
}
}
}
// Len returns the number of tiles in ts.
func (ts TileCoordSet) Len() int {
return len(ts.set)
}
// Merge adds all tiles in other into ts.
func (ts *TileCoordSet) Merge(other TileCoordSet) {
ts.checkInit()
for tile := range other.set {
ts.set[tile] = struct{}{}
}
}
// Eq returns if ts contains exactly the same contents as other.
func (ts TileCoordSet) Eq(other TileCoordSet) bool {
if ts.Len() != other.Len() {
return false
}
for tile := range ts.set {
if !other.Has(tile) {
return false
}
}
return true
}
// Iter returns an iterator for this TileSet.
func (ts TileCoordSet) Iter() <-chan TileCoord {
iter := make(chan TileCoord, 5)
go func() {
for tile := range ts.set {
iter <- tile
}
close(iter)
}()
return iter
}
// Slice returns a slice representation of ts
func (ts TileCoordSet) Slice() []TileCoord {
slice := make([]TileCoord, 0, len(ts.set))
for tile := range ts.set {
slice = append(slice, tile)
}
return slice
}
// ToTileSet converts ts into a TileSet
func (ts TileCoordSet) ToTileSet(fn func(t TileCoord) Tile) TileSet {
var result TileSet
for val := range ts.set {
result.Add(fn(val))
}
return result
}
func (ts TileCoordSet) String() string {
slice := ts.Slice()
var maxX, maxY int
for _, tile := range slice {
if tile.X > maxX {
maxX = tile.X
}
if tile.Y > maxY {
maxY = tile.Y
}
}
maxX++
maxY++
tilesAt := make([][]bool, maxX)
for x := range tilesAt {
tilesAt[x] = make([]bool, maxY)
}
for _, v := range slice {
tilesAt[v.X][v.Y] = true
}
var sb strings.Builder
sb.WriteByte('{')
for y := maxY - 1; y >= 0; y-- {
for x := 0; x < maxX; x++ {
if tilesAt[x][y] {
sb.WriteByte('x')
} else {
sb.WriteByte(' ')
}
}
if y > 0 {
sb.WriteByte('|')
}
}
sb.WriteByte('}')
return sb.String()
}
// MultiLineString returns a string representation of this tileset on multiple lines
func (ts TileCoordSet) MultiLineString() string {
next := ts.String()
next = next[1 : len(next)-1]
next = strings.ReplaceAll(next, "|", "\n")
next += "\n"
return next
}