forked from stathissideris/ditaa
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcellset.go
222 lines (198 loc) · 4.1 KB
/
cellset.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
package main
import (
"fmt"
"os"
)
type CellSet struct {
Set map[Cell]struct{}
typ CellSetType
}
type CellBounds struct{ Min, Max Cell }
func NewCellSet() *CellSet {
return &CellSet{Set: make(map[Cell]struct{})}
}
func (s *CellSet) Add(c Cell) { s.Set[c] = struct{}{} }
func (s *CellSet) Remove(c Cell) {
s.typ = SET_UNINITIALIZED
delete(s.Set, c)
}
func (s *CellSet) Contains(c Cell) bool {
_, ok := s.Set[c]
return ok
}
func (s *CellSet) AddAll(s2 *CellSet) {
for c := range s2.Set {
s.Set[c] = struct{}{}
}
}
func (s *CellSet) HasCommonCells(s2 *CellSet) bool {
for c := range s2.Set {
if s.Contains(c) {
return true
}
}
return false
}
func (s *CellSet) Bounds() CellBounds {
var bb *CellBounds
for c := range s.Set {
if bb == nil {
bb = &CellBounds{Min: c, Max: c}
continue
}
if c.X < bb.Min.X {
bb.Min.X = c.X
}
if c.X > bb.Max.X {
bb.Max.X = c.X
}
if c.Y < bb.Min.Y {
bb.Min.Y = c.Y
}
if c.Y > bb.Max.Y {
bb.Max.Y = c.Y
}
}
if bb == nil {
return CellBounds{}
}
return *bb
}
func (s1 *CellSet) Equals(s2 *CellSet) bool {
if len(s1.Set) != len(s2.Set) {
return false
}
for k := range s1.Set {
if _, ok := s2.Set[k]; !ok {
return false
}
}
return true
}
func (s *CellSet) Type(grid *TextGrid) (typ CellSetType) {
if s.typ != SET_UNINITIALIZED {
return s.typ
}
defer func() {
s.typ = typ
}()
if len(s.Set) <= 1 {
return SET_OPEN
}
traced := s.getTypeAccordingToTraceMethod(grid)
if traced == SET_OPEN || traced == SET_CLOSED {
return traced
}
if traced != SET_UNDETERMINED {
return SET_UNDETERMINED // [akavel] can this happen?
}
filled := s.getTypeAccordingToFillMethod(grid)
switch filled {
case SET_HAS_CLOSED_AREA:
return SET_MIXED
case SET_OPEN:
return SET_OPEN
}
//in the case that both return undetermined:
return SET_UNDETERMINED
}
func (s *CellSet) getTypeAccordingToTraceMethod(grid *TextGrid) CellSetType {
workGrid := NewTextGrid(grid.Width(), grid.Height())
CopySelectedCells(workGrid, s, grid)
//start with a line end if it exists or with a "random" cell if not
start := s.SomeCell()
for c := range s.Set {
if workGrid.IsLinesEnd(c) {
start = c
break // [akavel] added this; is it ok?
}
}
prev := start
nexts := workGrid.FollowCell(prev, nil)
if len(nexts.Set) == 0 {
return SET_OPEN
}
cell := nexts.SomeCell()
for cell != start {
nexts = workGrid.FollowCell(cell, &prev)
switch len(nexts.Set) {
case 0:
// found dead end, shape is open
return SET_OPEN
case 1:
prev = cell
cell = nexts.SomeCell()
default:
return SET_UNDETERMINED
}
}
// arrived back to start, shape is closed
return SET_CLOSED
}
func (s *CellSet) getTypeAccordingToFillMethod(grid *TextGrid) CellSetType {
tempSet := NewCellSet()
tempSet.Set = s.Set
bb := s.Bounds()
tempSet.translate(-bb.Min.X+1, -bb.Min.Y+1)
subGrid := grid.SubGrid(bb.Min.X-1, bb.Min.Y-1, bb.Max.X-bb.Min.X+3, bb.Max.Y-bb.Min.Y+3)
temp := NewTextGrid(0, 0)
temp.Rows = NewAbstractionGrid(subGrid, tempSet).Rows
var fillCell *Cell
for it := temp.Iter(); it.Next(); {
c := it.Cell()
if temp.IsBlank(c) {
fillCell = &c
break
}
}
if fillCell == nil {
fmt.Fprintln(os.Stderr, "Unexpected error: fill method cannot fill anywhere")
return SET_UNDETERMINED
}
temp.fillContinuousArea(*fillCell, '*')
if temp.HasBlankCells() {
return SET_HAS_CLOSED_AREA
}
return SET_OPEN
}
func (s *CellSet) SomeCell() Cell {
for c := range s.Set {
return c
}
return Cell{} // [akavel] TODO: or panic("should not reach") ?
}
func (s *CellSet) translate(dx, dy int) {
s.typ = SET_UNINITIALIZED
result := map[Cell]struct{}{}
for c := range s.Set {
c.X += dx
c.Y += dy
result[c] = struct{}{}
}
s.Set = result
}
func (s *CellSet) SubtractSet(s2 *CellSet) {
s.typ = SET_UNINITIALIZED
for c := range s2.Set {
delete(s.Set, c)
}
}
type CellSetType int
const (
SET_UNINITIALIZED CellSetType = iota
SET_CLOSED
SET_OPEN
SET_MIXED
SET_HAS_CLOSED_AREA
SET_UNDETERMINED
)
func (s CellSet) GetCellsAsString() string {
out := ""
for c := range s.Set {
out += "/" + c.String()
}
if out == "" {
return out
}
return out[1:]
}