]> git.lyx.org Git - features.git/blob - src/ParagraphList.C
add more functions to ParagraphList
[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 void ParagraphList::assign(iterator beg, iterator end)
162 {
163         clear();
164         for (; beg != end; ++beg) {
165                 push_back(new Paragraph(*beg, false));
166         }
167 }
168
169
170
171 void ParagraphList::clear()
172 {
173 #ifndef NO_NEXT
174         while (parlist) {
175                 Paragraph * tmp = parlist->next_;
176                 delete parlist;
177                 parlist = tmp;
178         }
179 #else
180         while (parlist) {
181                 Paragraph * tmp = parlist->next_par_;
182                 delete parlist;
183                 parlist = tmp;
184         }
185 #endif
186 }
187
188
189 void ParagraphList::erase(ParagraphList::iterator it)
190 {
191 #ifndef NO_NEXT
192         Paragraph * prev = it->previous_;
193         Paragraph * next = it->next_;
194
195         if (prev)
196                 prev->next_ = next;
197         else
198                 parlist = next;
199
200         if (next)
201                 next->previous_ = prev;
202
203         it->previous_ = 0;
204         it->next_ = 0;
205         delete &*it;
206 #else
207         Paragraph * prev = it->prev_par_;
208         Paragraph * next = it->next_par_;
209
210         if (prev)
211                 prev->next_par_ = next;
212         else
213                 parlist = next;
214
215         if (next)
216                 next->prev_par_ = prev;
217
218         delete &*it;
219 #endif
220 }
221
222
223 ParagraphList::iterator ParagraphList::begin()
224 {
225         return iterator(parlist);
226 }
227
228
229 ParagraphList::iterator ParagraphList::begin() const
230 {
231         return iterator(parlist);
232 }
233
234
235 ParagraphList::iterator ParagraphList::end()
236 {
237         return iterator();
238 }
239
240
241 ParagraphList::iterator ParagraphList::end() const
242 {
243         return iterator();
244 }
245
246
247 Paragraph const & ParagraphList::front() const
248 {
249         return *parlist;
250 }
251
252
253 Paragraph & ParagraphList::front()
254 {
255         return *parlist;
256 }
257
258
259 Paragraph const & ParagraphList::back() const
260 {
261 #ifndef NO_NEXT
262         Paragraph * tmp = parlist;
263         while (tmp->next_)
264                 tmp = tmp->next_;
265         return *tmp;
266 #else
267         Paragraph * tmp = parlist;
268         while (tmp->next_par_)
269                 tmp = tmp->next_par_;
270         return *tmp;
271 #endif
272 }
273
274
275 Paragraph & ParagraphList::back()
276 {
277 #ifndef NO_NEXT
278         Paragraph * tmp = parlist;
279         while (tmp->next_)
280                 tmp = tmp->next_;
281         return *tmp;
282 #else
283         Paragraph * tmp = parlist;
284         while (tmp->next_par_)
285                 tmp = tmp->next_par_;
286         return *tmp;
287 #endif
288 }
289
290
291 void ParagraphList::set(Paragraph * p)
292 {
293         parlist = p;
294 }
295
296
297 void ParagraphList::push_back(Paragraph * p)
298 {
299 #ifndef NO_NEXT
300         if (!parlist) {
301                 parlist = p;
302                 return;
303         }
304
305         Paragraph * pos = parlist;
306         while (pos->next_)
307                 pos = pos->next_;
308         pos->next_ = p;
309         p->previous_ = pos;
310 #else
311         if (!parlist) {
312                 parlist = p;
313                 return;
314         }
315
316         Paragraph * pos = parlist;
317         while (pos->next_par_)
318                 pos = pos->next_par_;
319         pos->next_par_ = p;
320         p->prev_par_ = pos;
321 #endif
322 }
323
324
325 int ParagraphList::size() const
326 {
327 #ifndef NO_NEXT
328         // When we switch to a std::container this will be O(1)
329         // instead of O(n). (Lgb)
330         Paragraph * tmp = parlist;
331         int c = 0;
332         while (tmp) {
333                 ++c;
334                 tmp = tmp->next_;
335         }
336         return c;
337 #else
338         // When we switch to a std::container this will be O(1)
339         // instead of O(n). (Lgb)
340         Paragraph * tmp = parlist;
341         int c = 0;
342         while (tmp) {
343                 ++c;
344                 tmp = tmp->next_par_;
345         }
346         return c;
347 #endif
348 }
349
350
351 bool ParagraphList::empty() const
352 {
353         return parlist == 0;
354 }