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