]> git.lyx.org Git - lyx.git/blob - src/RowList.C
rowlist9
[lyx.git] / src / RowList.C
1 #include <config.h>
2
3 #include "RowList.h"
4
5 #include "lyxrow.h"
6
7 ////////// The RowList::iterator
8
9 RowList::iterator::iterator()
10         : ptr(0)
11 {}
12
13
14 RowList::iterator::iterator(Row * p)
15         : ptr(p)
16 {}
17
18
19 RowList::iterator::reference
20 RowList::iterator::operator*()
21 {
22         return *ptr;
23 }
24
25
26 RowList::iterator::pointer
27 RowList::iterator::operator->()
28 {
29         return ptr;
30 }
31
32
33 RowList::iterator &
34 RowList::iterator::operator++()
35 {
36         ptr = ptr->next();
37         return *this;
38 }
39
40
41 RowList::iterator
42 RowList::iterator::operator++(int)
43 {
44         iterator tmp = *this;
45         ++*this;
46         return tmp;
47 }
48
49
50 RowList::iterator &
51 RowList::iterator::operator--()
52 {
53         ptr = ptr->previous();
54         return *this;
55 }
56
57
58 RowList::iterator
59 RowList::iterator::operator--(int)
60 {
61         iterator tmp = *this;
62         --*this;
63         return tmp;
64 }
65
66
67 bool operator==(RowList::iterator const & i1,
68                 RowList::iterator const & i2)
69 {
70         return &(*const_cast<RowList::iterator&>(i1))
71             == &(*const_cast<RowList::iterator&>(i2));
72 }
73
74
75 bool operator!=(RowList::iterator const & i1,
76                 RowList::iterator const & i2)
77 {
78         return !(i1 == i2);
79 }
80
81
82 ////////// The RowList::const_iterator
83
84 RowList::const_iterator::const_iterator()
85         : ptr(0)
86 {}
87
88
89 RowList::const_iterator::const_iterator(Row * p)
90         : ptr(p)
91 {}
92
93
94 RowList::const_iterator::const_reference
95 RowList::const_iterator::operator*()
96 {
97         return *ptr;
98 }
99
100
101 RowList::const_iterator::const_pointer
102 RowList::const_iterator::operator->()
103 {
104         return ptr;
105 }
106
107
108 RowList::const_iterator &
109 RowList::const_iterator::operator++()
110 {
111         ptr = ptr->next();
112         return *this;
113 }
114
115
116 RowList::const_iterator
117 RowList::const_iterator::operator++(int)
118 {
119         const_iterator tmp = *this;
120         ++*this;
121         return tmp;
122 }
123
124
125 RowList::const_iterator &
126 RowList::const_iterator::operator--()
127 {
128         ptr = ptr->previous();
129         return *this;
130 }
131
132
133 RowList::const_iterator
134 RowList::const_iterator::operator--(int)
135 {
136         const_iterator tmp = *this;
137         --*this;
138         return tmp;
139 }
140
141
142 bool operator==(RowList::const_iterator const & i1,
143                 RowList::const_iterator const & i2)
144 {
145         return &(*const_cast<RowList::const_iterator&>(i1))
146             == &(*const_cast<RowList::const_iterator&>(i2));
147 }
148
149
150 bool operator!=(RowList::const_iterator const & i1,
151                 RowList::const_iterator const & i2)
152 {
153         return !(i1 == i2);
154 }
155
156
157 ////////// The RowList proper
158 RowList::RowList()
159         : rowlist(0)
160 {}
161
162
163 RowList::iterator
164 RowList::insert(RowList::iterator it, Row * row)
165 {
166         if (rowlist == 0) {
167                 rowlist = row;
168         } else if (it != end()) {
169                 Row * prev = it->previous();
170                 row->next(&*it);
171                 row->previous(prev);
172                 if (prev)
173                         prev->next(row);
174                 else
175                         rowlist = row;
176                 it->previous(row);
177         } else {
178                 // Find last par.
179                 Row * last = rowlist;
180                 while (last->next())
181                         last = last->next();
182                 last->next(row);
183                 row->previous(last);
184         }
185         return iterator(row);
186 }
187
188
189 void RowList::clear()
190 {
191         while (rowlist) {
192                 Row * tmp = rowlist->next();
193                 delete rowlist;
194                 rowlist = tmp;
195         }
196 }
197
198
199 void RowList::erase(RowList::iterator it)
200 {
201         Row * prev = it->previous();
202         Row * next = it->next();
203
204         if (prev)
205                 prev->next(next);
206         else
207                 rowlist = next;
208
209         if (next)
210                 next->previous(prev);
211
212         delete &*it;
213 }
214
215
216 RowList::iterator RowList::begin()
217 {
218         return iterator(rowlist);
219 }
220
221
222 RowList::const_iterator RowList::begin() const
223 {
224         return const_iterator(rowlist);
225 }
226
227
228 RowList::iterator RowList::end()
229 {
230         return iterator();
231 }
232
233
234 RowList::const_iterator RowList::end() const
235 {
236         return const_iterator();
237 }
238
239
240 Row const & RowList::front() const
241 {
242         return *rowlist;
243 }
244
245
246 Row & RowList::front()
247 {
248         return *rowlist;
249 }
250
251
252 Row const & RowList::back() const
253 {
254         Row * tmp = rowlist;
255         while (tmp->next())
256                 tmp = tmp->next();
257         return *tmp;
258 }
259
260
261 Row & RowList::back()
262 {
263         Row * tmp = rowlist;
264         while (tmp->next())
265                 tmp = tmp->next();
266         return *tmp;
267 }
268
269
270 void RowList::push_back(Row * p)
271 {
272         if (!rowlist) {
273                 rowlist = p;
274                 return;
275         }
276
277         Row * pos = rowlist;
278         while (pos->next())
279                 pos = pos->next();
280         pos->next(p);
281         p->previous(pos);
282 }
283
284
285 int RowList::size() const
286 {
287         // When we switch to a std::container this will be O(1)
288         // instead of O(n). (Lgb)
289         Row * tmp = rowlist;
290         int c = 0;
291         while (tmp) {
292                 ++c;
293                 tmp = tmp->next();
294         }
295         return c;
296 }
297
298
299 bool RowList::empty() const
300 {
301         return rowlist == 0;
302 }