]> git.lyx.org Git - lyx.git/blob - src/ParagraphList.C
fix some C++ parsing bugs
[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         ptr = ptr->next();
37         return *this;
38 }
39
40
41 ParagraphList::iterator
42 ParagraphList::iterator::operator++(int)
43 {
44         iterator tmp = *this;
45         ++*this;
46         return tmp;
47 }
48
49
50 ParagraphList::iterator &
51 ParagraphList::iterator::operator--()
52 {
53         ptr = ptr->previous();
54         return *this;
55 }
56
57
58 ParagraphList::iterator
59 ParagraphList::iterator::operator--(int)
60 {
61         iterator tmp = *this;
62         --*this;
63         return tmp;
64 }
65
66
67 bool operator==(ParagraphList::iterator const & i1,
68                 ParagraphList::iterator const & i2)
69 {
70         return &(*const_cast<ParagraphList::iterator&>(i1))
71             == &(*const_cast<ParagraphList::iterator&>(i2));
72 }
73
74
75 bool operator!=(ParagraphList::iterator const & i1,
76                 ParagraphList::iterator const & i2)
77 {
78         return !(i1 == i2);
79 }
80
81
82 ////////// The ParagraphList proper
83 ParagraphList::ParagraphList()
84         : parlist(0)
85 {}
86
87
88 ParagraphList::iterator
89 ParagraphList::insert(ParagraphList::iterator it, Paragraph * par)
90 {
91         if (it != end()) {
92                 Paragraph * prev = it->previous();
93                 par->next(&*it);
94                 par->previous(prev);
95                 prev->next(par);
96                 it->previous(par);
97         } else if (parlist == 0) {
98                 parlist = par;
99         } else {
100                 // Find last par.
101                 Paragraph * last = parlist;
102                 while (last->next())
103                         last = last->next();
104                 last->next(par);
105                 par->previous(last);
106         }
107         return iterator(par);
108 }
109
110
111 void ParagraphList::clear()
112 {
113         while (parlist) {
114                 Paragraph * tmp = parlist->next();
115                 delete parlist;
116                 parlist = tmp;
117         }
118 }
119
120
121 void ParagraphList::erase(ParagraphList::iterator it)
122 {
123         Paragraph * prev = it->previous();
124         Paragraph * next = it->next();
125
126         if (prev)
127                 prev->next(next);
128         else
129                 parlist = next;
130
131         if (next)
132                 next->previous(prev);
133
134         delete &*it;
135 }
136
137
138 ParagraphList::iterator ParagraphList::begin()
139 {
140         return iterator(parlist);
141 }
142
143
144 ParagraphList::iterator ParagraphList::begin() const
145 {
146         return iterator(parlist);
147 }
148
149
150 ParagraphList::iterator ParagraphList::end()
151 {
152         return iterator();
153 }
154
155
156 ParagraphList::iterator ParagraphList::end() const
157 {
158         return iterator();
159 }
160
161
162 Paragraph const & ParagraphList::front() const
163 {
164         return *parlist;
165 }
166
167
168 Paragraph & ParagraphList::front()
169 {
170         return *parlist;
171 }
172
173
174 Paragraph const & ParagraphList::back() const
175 {
176         Paragraph * tmp = parlist;
177         while (tmp->next())
178                 tmp = tmp->next();
179         return *tmp;
180 }
181
182
183 Paragraph & ParagraphList::back()
184 {
185         Paragraph * tmp = parlist;
186         while (tmp->next())
187                 tmp = tmp->next();
188         return *tmp;
189 }
190
191
192 void ParagraphList::set(Paragraph * p)
193 {
194         parlist = p;
195 }
196
197
198 void ParagraphList::push_back(Paragraph * p)
199 {
200         if (!parlist) {
201                 parlist = p;
202                 return;
203         }
204
205         Paragraph * pos = parlist;
206         while (pos->next())
207                 pos = pos->next();
208         pos->next(p);
209         p->previous(pos);
210 }
211
212
213 int ParagraphList::size() const
214 {
215         // When we switch to a std::container this will be O(1)
216         // instead of O(n). (Lgb)
217         Paragraph * tmp = parlist;
218         int c = 0;
219         while (tmp) {
220                 ++c;
221                 tmp = tmp->next();
222         }
223         return c;
224 }
225
226
227 bool ParagraphList::empty() const
228 {
229         return parlist == 0;
230 }