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