-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbox_list.go
183 lines (164 loc) · 3.07 KB
/
box_list.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
package main
type BoxList struct {
front, back Box
length int
}
func NewBoxList() *BoxList {
return new(BoxList)
}
func (bl *BoxList) Front() Box {
return bl.front
}
func (bl *BoxList) Back() Box {
return bl.back
}
func (bl *BoxList) Len() int {
return bl.length
}
func (bl *BoxList) PushFront(b Box) {
if bl.front == nil {
bl.front, bl.back = b, b
b.SetPrev(nil)
b.SetNext(nil)
b.SetList(bl)
bl.length = 1
return
}
bl.InsertBefore(b, bl.front)
}
func (bl *BoxList) PushBack(b Box) {
if bl.back == nil {
bl.front, bl.back = b, b
b.SetPrev(nil)
b.SetNext(nil)
b.SetList(bl)
bl.length = 1
return
}
bl.InsertAfter(b, bl.back)
}
// Returns false if b not in bl
func (bl *BoxList) InsertBefore(b, mark Box) bool {
if mark.List() != bl {
return false
}
if mark.Prev() == nil {
bl.front = b
} else {
mark.Prev().SetNext(b)
}
b.SetPrev(mark.Prev())
mark.SetPrev(b)
b.SetNext(mark)
b.SetList(bl)
bl.length++
return true
}
// Returns false if mark not in bl
func (bl *BoxList) InsertAfter(b, mark Box) bool {
if mark.List() != bl {
return false
}
if mark.Next() == nil {
bl.back = b
} else {
mark.Next().SetPrev(b)
}
b.SetNext(mark.Next())
mark.SetNext(b)
b.SetPrev(mark)
b.SetList(bl)
bl.length++
return true
}
func (bl *BoxList) BoxByWindow(w Window, full_tree bool) Box {
for b := bl.Front(); b != nil; b = b.Next() {
if b.Window() == w {
return b
}
if full_tree {
c := b.Children().BoxByWindow(w, true)
if c != nil {
return c
}
}
}
return nil
}
func (bl *BoxList) Remove(b Box) {
if b.List() != bl {
l.Panic("Can't remove a non existent box form a list")
}
if b.Prev() == nil {
bl.front = b.Next()
} else {
b.Prev().SetNext(b.Next())
}
if b.Next() == nil {
bl.back = b.Prev()
} else {
b.Next().SetPrev(b.Prev())
}
b.SetPrev(nil)
b.SetNext(nil)
b.SetList(nil)
bl.length--
}
func (bl *BoxList) FrontIter() BoxListIterator {
return &frontBoxListIterator{
boxListIterator{bl.Front(), nil},
}
}
func (bl *BoxList) BackIter() BoxListIterator {
return &backBoxListIterator{
boxListIterator{bl.Back(), nil},
}
}
// Iterates ovet full tree
type BoxListIterator interface {
Next() Box
}
type boxListIterator struct {
current Box
child BoxListIterator
}
type frontBoxListIterator struct {
boxListIterator
}
// Returns nil if end of list
func (i *frontBoxListIterator) Next() (b Box) {
if i.child != nil {
b = i.child.Next()
if b != nil {
return
}
i.child = nil // There is no more data in child iterator
}
if i.current == nil {
return // There is no more data at all
}
b = i.current
i.current = i.current.Next()
i.child = b.Children().FrontIter()
return
}
type backBoxListIterator struct {
boxListIterator
}
// Returns nil if end of list
func (i *backBoxListIterator) Next() (b Box) {
if i.child != nil {
b = i.child.Next()
if b != nil {
return
}
i.child = nil // There is no more data in child iterator
}
if i.current == nil {
return // There is no more data at all
}
b = i.current
i.current = i.current.Prev()
i.child = b.Children().BackIter()
return
}