]> git.lyx.org Git - lyx.git/blob - src/ParagraphList.C
add a ParagraphList::insert, adjust other funcs accordingly
[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         Paragraph * prev = it->previous();
92         par->next(&*it);
93         par->previous(prev);
94         prev->next(par);
95         it->previous(par);
96         return iterator(par);
97 }
98
99
100 void ParagraphList::clear()
101 {
102         while (parlist) {
103                 Paragraph * tmp = parlist->next();
104                 delete parlist;
105                 parlist = tmp;
106         }
107 }
108
109
110 void ParagraphList::erase(ParagraphList::iterator it)
111 {
112         Paragraph * prev = it->previous();
113         Paragraph * next = it->next();
114
115         prev->next(next);
116         next->previous(prev);
117
118         delete &*it;
119 }
120
121
122 ParagraphList::iterator ParagraphList::begin()
123 {
124         return iterator(parlist);
125 }
126
127
128 ParagraphList::iterator ParagraphList::begin() const
129 {
130         return iterator(parlist);
131 }
132
133
134 ParagraphList::iterator ParagraphList::end()
135 {
136         return iterator();
137 }
138
139
140 ParagraphList::iterator ParagraphList::end() const
141 {
142         return iterator();
143 }
144
145
146 void ParagraphList::set(Paragraph * p)
147 {
148         parlist = p;
149 }
150
151
152 void ParagraphList::push_back(Paragraph * p)
153 {
154         if (!parlist) {
155                 parlist = p;
156                 return;
157         }
158
159         Paragraph * pos = parlist;
160         while (pos->next())
161                 pos = pos->next();
162         pos->next(p);
163         p->previous(pos);
164 }
165
166
167 int ParagraphList::size() const
168 {
169         // When we switch to a std::container this will be O(1)
170         // instead of O(n). (Lgb)
171         Paragraph * tmp = parlist;
172         int c = 0;
173         while (tmp) {
174                 ++c;
175                 tmp = tmp->next();
176         }
177         return c;
178 }
179
180
181 bool ParagraphList::empty() const
182 {
183         return parlist == 0;
184 }