]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
iterators for MathArray; cosmetics;
[lyx.git] / src / mathed / array.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_inset.h"
6 #include "math_charinset.h"
7 #include "debug.h"
8 #include "array.h"
9 #include "math_scriptinset.h"
10 #include "mathed/support.h"
11
12 using std::ostream;
13 using std::endl;
14
15 MathArray::MathArray()
16 {}
17
18
19 MathArray::~MathArray()
20 {
21         erase();
22 }
23
24
25 MathArray::MathArray(MathArray const & array)
26         : bf_(array.bf_)
27 {
28         deep_copy(begin(), end());
29 }
30
31
32 MathArray::MathArray(MathArray const & array, int from, int to)
33         : bf_(array.begin() + from, array.begin() + to)
34 {
35         deep_copy(begin(), end());
36 }
37
38
39 void MathArray::deep_copy(iterator from, iterator to)
40 {
41         for (iterator it = from; it != to; ++it)
42                 *it = (*it)->clone();
43 }
44
45
46 int MathArray::last() const
47 {
48         return size() - 1;
49 }
50
51
52 void MathArray::substitute(MathMacro const & m)
53 {
54         MathArray tmp;
55         for (iterator it = begin(); it != end(); ++it)
56                 (*it)->substitute(tmp, m);
57         swap(tmp);
58 }
59
60
61 MathArray & MathArray::operator=(MathArray const & array)
62 {
63         MathArray tmp(array);
64         swap(tmp);
65         return *this;
66 }
67
68
69 MathInset * MathArray::nextInset(int pos)
70 {
71         return (pos == size()) ? 0 : bf_[pos];
72 }
73
74
75 MathInset const * MathArray::nextInset(int pos) const
76 {
77         return (pos == size()) ? 0 : bf_[pos];
78 }
79
80
81 void MathArray::insert(int pos, MathInset * p)
82 {
83         bf_.insert(begin() + pos, p);
84 }
85
86
87 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
88 {
89         bf_.insert(begin() + pos, new MathCharInset(b, t));
90 }
91
92
93 void MathArray::insert(int pos, MathArray const & array)
94 {
95         bf_.insert(begin() + pos, array.begin(), array.end());
96         deep_copy(begin() + pos, begin() + pos + array.size());
97 }
98
99
100 void MathArray::push_back(MathInset * p)
101 {       
102         insert(size(), p);
103 }
104
105
106 void MathArray::push_back(unsigned char b, MathTextCodes c)
107 {
108         insert(size(), b, c);
109 }
110
111
112 void MathArray::push_back(MathArray const & array)
113 {
114         insert(size(), array);
115 }
116
117
118 void MathArray::clear()
119 {
120         erase();
121 }
122
123
124 void MathArray::swap(MathArray & array)
125 {
126         if (this != &array) 
127                 bf_.swap(array.bf_);
128 }
129
130
131 bool MathArray::empty() const
132 {
133         return bf_.empty();
134 }
135    
136
137 int MathArray::size() const
138 {
139         return bf_.size();
140 }
141
142
143 void MathArray::erase()
144 {
145         erase(0, size());
146 }
147
148
149 void MathArray::erase(int pos)
150 {
151         if (pos < size())
152                 erase(pos, pos + 1);
153 }
154
155
156 void MathArray::erase(int pos1, int pos2)
157 {
158         for (iterator it = begin() + pos1; it != begin() + pos2; ++it)
159                 delete *it;
160         bf_.erase(begin() + pos1, begin() + pos2);
161 }
162
163
164 MathInset * MathArray::back() const
165 {
166         return size() ? bf_.back() : 0;
167 }
168
169
170 void MathArray::dump2(ostream & os) const
171 {
172         for (const_iterator it = begin(); it != end(); ++it)
173                 os << *it << ' ';
174 }
175
176
177 void MathArray::dump(ostream & os) const
178 {
179         for (const_iterator it = begin(); it != end(); ++it)
180                 os << "<" << *it << ">";
181 }
182
183
184 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
185 {
186         ar.dump2(os);
187         return os;
188 }
189
190
191 void MathArray::write(ostream & os, bool fragile) const
192 {
193         for (const_iterator it = begin(); it != end(); ++it)
194                 (*it)->write(os, fragile);
195 }
196
197
198 void MathArray::writeNormal(ostream & os) const
199 {
200         if (empty()) {
201                 os << "[par] ";
202                 return;
203         }
204
205         write(os, true);
206 }
207
208
209 void MathArray::validate(LaTeXFeatures & features) const
210 {
211         for (const_iterator it = begin(); it != end(); ++it)
212                 (*it)->validate(features);
213 }
214
215
216 void MathArray::pop_back()
217 {       
218         if (!size()) {
219                 lyxerr << "pop_back from empty array!\n";
220                 return;
221         }
222         delete back();
223         bf_.pop_back();
224 }
225
226
227 MathArray::const_iterator MathArray::begin() const
228 {
229         return bf_.begin();
230 }
231
232
233 MathArray::const_iterator MathArray::end() const
234 {
235         return bf_.end();
236 }
237
238
239 MathArray::iterator MathArray::begin()
240 {
241         return bf_.begin();
242 }
243
244
245 MathArray::iterator MathArray::end()
246 {
247         return bf_.end();
248 }