-
Notifications
You must be signed in to change notification settings - Fork 7
/
zAOIList.go
163 lines (147 loc) · 3.34 KB
/
zAOIList.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
package aoi
type yAOIList struct {
aoidist Coord
head *xzaoi
tail *xzaoi
}
func newYAOIList(aoidist Coord) *yAOIList {
return &yAOIList{aoidist: aoidist}
}
func (sl *yAOIList) Insert(aoi *xzaoi) {
insertCoord := aoi.aoi.y
if sl.head != nil {
p := sl.head
for p != nil && p.aoi.y < insertCoord {
p = p.yNext
}
// now, p == nil or p.coord >= insertCoord
if p == nil { // if p == nil, insert xzaoi at the end of list
tail := sl.tail
tail.yNext = aoi
aoi.yPrev = tail
sl.tail = aoi
} else { // otherwise, p >= xzaoi, insert xzaoi before p
prev := p.yPrev
aoi.yNext = p
p.yPrev = aoi
aoi.yPrev = prev
if prev != nil {
prev.yNext = aoi
} else { // p is the head, so xzaoi should be the new head
sl.head = aoi
}
}
} else {
sl.head = aoi
sl.tail = aoi
}
}
func (sl *yAOIList) Remove(aoi *xzaoi) {
prev := aoi.yPrev
next := aoi.yNext
if prev != nil {
prev.yNext = next
aoi.yPrev = nil
} else {
sl.head = next
}
if next != nil {
next.yPrev = prev
aoi.yNext = nil
} else {
sl.tail = prev
}
}
func (sl *yAOIList) Move(aoi *xzaoi, oldCoord Coord) {
coord := aoi.aoi.y
if coord > oldCoord {
// moving to next ...
next := aoi.yNext
if next == nil || next.aoi.y >= coord {
// no need to adjust in list
return
}
prev := aoi.yPrev
//fmt.Println(1, prev, next, prev == nil || prev.yNext == xzaoi)
if prev != nil {
prev.yNext = next // remove xzaoi from list
} else {
sl.head = next // xzaoi is the head, trim it
}
next.yPrev = prev
//fmt.Println(2, prev, next, prev == nil || prev.yNext == next)
prev, next = next, next.yNext
for next != nil && next.aoi.y < coord {
prev, next = next, next.yNext
//fmt.Println(2, prev, next, prev == nil || prev.yNext == next)
}
//fmt.Println(3, prev, next)
// no we have prev.X < coord && (next == nil || next.X >= coord), so insert between prev and next
prev.yNext = aoi
aoi.yPrev = prev
if next != nil {
next.yPrev = aoi
} else {
sl.tail = aoi
}
aoi.yNext = next
//fmt.Println(4)
} else {
// moving to prev ...
prev := aoi.yPrev
if prev == nil || prev.aoi.y <= coord {
// no need to adjust in list
return
}
next := aoi.yNext
if next != nil {
next.yPrev = prev
} else {
sl.tail = prev // xzaoi is the head, trim it
}
prev.yNext = next // remove xzaoi from list
next, prev = prev, prev.yPrev
for prev != nil && prev.aoi.y > coord {
next, prev = prev, prev.yPrev
}
// no we have next.X > coord && (prev == nil || prev.X <= coord), so insert between prev and next
next.yPrev = aoi
aoi.yNext = next
if prev != nil {
prev.yNext = aoi
} else {
sl.head = aoi
}
aoi.yPrev = prev
}
}
func (sl *yAOIList) Mark(aoi *xzaoi) {
prev := aoi.yPrev
coord := aoi.aoi.y
minCoord := coord - sl.aoidist
for prev != nil && prev.aoi.y >= minCoord {
prev.markVal += 1
prev = prev.yPrev
}
next := aoi.yNext
maxCoord := coord + sl.aoidist
for next != nil && next.aoi.y <= maxCoord {
next.markVal += 1
next = next.yNext
}
}
func (sl *yAOIList) ClearMark(aoi *xzaoi) {
prev := aoi.yPrev
coord := aoi.aoi.y
minCoord := coord - sl.aoidist
for prev != nil && prev.aoi.y >= minCoord {
prev.markVal = 0
prev = prev.yPrev
}
next := aoi.yNext
maxCoord := coord + sl.aoidist
for next != nil && next.aoi.y <= maxCoord {
next.markVal = 0
next = next.yNext
}
}