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