]> git.lyx.org Git - features.git/blob - src/ParagraphList.C
2003-05-01 Lars Gullik Bj�nnes <larsbj@gullik.net>
[features.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 //////////
92
93 ParagraphList::ParagraphList()
94         : parlist(0)
95 {}
96
97
98 ParagraphList::ParagraphList(ParagraphList const & pl)
99         : parlist(0)
100 {
101         // Deep copy.
102         ParagraphList::iterator it = pl.begin();
103         ParagraphList::iterator end = pl.end();
104         for (; it != end; ++it) {
105                 push_back(new Paragraph(*it, false));
106         }
107 }
108
109
110 ParagraphList & ParagraphList::operator=(ParagraphList const & rhs)
111 {
112         ParagraphList tmp(rhs);
113         std::swap(parlist, tmp.parlist);
114         return *this;
115 }
116
117
118 ParagraphList::iterator
119 ParagraphList::insert(ParagraphList::iterator it, Paragraph * par)
120 {
121 #ifndef NO_NEXT
122         if (it != end()) {
123                 Paragraph * prev = it->previous_;
124                 par->next_ = &*it;
125                 par->previous_ = prev;
126                 prev->next_ = par;
127                 it->previous_ = par;
128         } else if (parlist == 0) {
129                 parlist = par;
130         } else {
131                 // Find last par.
132                 Paragraph * last = parlist;
133                 while (last->next_)
134                         last = last->next_;
135                 last->next_ = par;
136                 par->previous_ = last;
137         }
138         return iterator(par);
139 #else
140         if (it != end()) {
141                 Paragraph * prev = it->prev_par_;
142                 par->next_par_ = &*it;
143                 par->prev_par_ = prev;
144                 prev->next_par_ = par;
145                 it->prev_par_= par;
146         } else if (parlist == 0) {
147                 parlist = par;
148         } else {
149                 // Find last par.
150                 Paragraph * last = parlist;
151                 while (last->next_par_)
152                         last = last->next_par_;
153                 last->next_par_ = par;
154                 par->prev_par_ = last;
155         }
156         return iterator(par);
157 #endif
158 }
159
160
161
162 void ParagraphList::insert(iterator pos, iterator beg, iterator end)
163 {
164         for (; beg != end; ++beg) {
165                 insert(pos, new Paragraph(*beg, false));
166         }
167 }
168
169
170 void ParagraphList::assign(iterator beg, iterator end)
171 {
172         clear();
173         for (; beg != end; ++beg) {
174                 push_back(new Paragraph(*beg, false));
175         }
176 }
177
178
179
180 void ParagraphList::clear()
181 {
182 #ifndef NO_NEXT
183         while (parlist) {
184                 Paragraph * tmp = parlist->next_;
185                 delete parlist;
186                 parlist = tmp;
187         }
188 #else
189         while (parlist) {
190                 Paragraph * tmp = parlist->next_par_;
191                 delete parlist;
192                 parlist = tmp;
193         }
194 #endif
195 }
196
197
198 void ParagraphList::erase(ParagraphList::iterator it)
199 {
200 #ifndef NO_NEXT
201         Paragraph * prev = it->previous_;
202         Paragraph * next = it->next_;
203
204         if (prev)
205                 prev->next_ = next;
206         else
207                 parlist = next;
208
209         if (next)
210                 next->previous_ = prev;
211
212         it->previous_ = 0;
213         it->next_ = 0;
214         delete &*it;
215 #else
216         Paragraph * prev = it->prev_par_;
217         Paragraph * next = it->next_par_;
218
219         if (prev)
220                 prev->next_par_ = next;
221         else
222                 parlist = next;
223
224         if (next)
225                 next->prev_par_ = prev;
226
227         delete &*it;
228 #endif
229 }
230
231
232 ParagraphList::iterator ParagraphList::begin()
233 {
234         return iterator(parlist);
235 }
236
237
238 ParagraphList::iterator ParagraphList::begin() const
239 {
240         return iterator(parlist);
241 }
242
243
244 ParagraphList::iterator ParagraphList::end()
245 {
246         return iterator();
247 }
248
249
250 ParagraphList::iterator ParagraphList::end() const
251 {
252         return iterator();
253 }
254
255
256 Paragraph const & ParagraphList::front() const
257 {
258         return *parlist;
259 }
260
261
262 Paragraph & ParagraphList::front()
263 {
264         return *parlist;
265 }
266
267
268 Paragraph const & ParagraphList::back() const
269 {
270 #ifndef NO_NEXT
271         Paragraph * tmp = parlist;
272         while (tmp->next_)
273                 tmp = tmp->next_;
274         return *tmp;
275 #else
276         Paragraph * tmp = parlist;
277         while (tmp->next_par_)
278                 tmp = tmp->next_par_;
279         return *tmp;
280 #endif
281 }
282
283
284 Paragraph & ParagraphList::back()
285 {
286 #ifndef NO_NEXT
287         Paragraph * tmp = parlist;
288         while (tmp->next_)
289                 tmp = tmp->next_;
290         return *tmp;
291 #else
292         Paragraph * tmp = parlist;
293         while (tmp->next_par_)
294                 tmp = tmp->next_par_;
295         return *tmp;
296 #endif
297 }
298
299
300 void ParagraphList::set(Paragraph * p)
301 {
302         parlist = p;
303 }
304
305
306 void ParagraphList::push_back(Paragraph * p)
307 {
308 #ifndef NO_NEXT
309         if (!parlist) {
310                 parlist = p;
311                 return;
312         }
313
314         Paragraph * pos = parlist;
315         while (pos->next_)
316                 pos = pos->next_;
317         pos->next_ = p;
318         p->previous_ = pos;
319 #else
320         if (!parlist) {
321                 parlist = p;
322                 return;
323         }
324
325         Paragraph * pos = parlist;
326         while (pos->next_par_)
327                 pos = pos->next_par_;
328         pos->next_par_ = p;
329         p->prev_par_ = pos;
330 #endif
331 }
332
333
334 int ParagraphList::size() const
335 {
336 #ifndef NO_NEXT
337         // When we switch to a std::container this will be O(1)
338         // instead of O(n). (Lgb)
339         Paragraph * tmp = parlist;
340         int c = 0;
341         while (tmp) {
342                 ++c;
343                 tmp = tmp->next_;
344         }
345         return c;
346 #else
347         // When we switch to a std::container this will be O(1)
348         // instead of O(n). (Lgb)
349         Paragraph * tmp = parlist;
350         int c = 0;
351         while (tmp) {
352                 ++c;
353                 tmp = tmp->next_par_;
354         }
355         return c;
356 #endif
357 }
358
359
360 bool ParagraphList::empty() const
361 {
362         return parlist == 0;
363 }