]> git.lyx.org Git - features.git/blob - src/mathed/array.C
Fix a leak; cosmetics
[features.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(0, size());
29 }
30
31
32 MathArray::MathArray(MathArray const & array, int from, int to)
33         : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
34 {
35         deep_copy(0, size());
36 }
37
38
39 void MathArray::deep_copy(int pos1, int pos2)
40 {
41         for (int pos = pos1; pos < pos2; ++pos) {
42                 MathInset * p = bf_[pos]->clone();
43                 //lyxerr << "cloning: '" <<  bf_[pos] << " to " << p << "'\n";
44                 bf_[pos] = p;
45         }
46 }
47
48
49 int MathArray::last() const
50 {
51         return size() - 1;
52 }
53
54
55 void MathArray::substitute(MathMacro const & m)
56 {
57         MathArray tmp;
58         for (int pos = 0; pos < size(); ++pos) 
59                 bf_[pos]->substitute(tmp, m);
60         swap(tmp);
61 }
62
63
64 MathArray & MathArray::operator=(MathArray const & array)
65 {
66         MathArray tmp(array);
67         swap(tmp);
68         return *this;
69 }
70
71
72 MathInset * MathArray::nextInset(int pos)
73 {
74         return (pos == size()) ? 0 : bf_[pos];
75 }
76
77
78 MathInset const * MathArray::nextInset(int pos) const
79 {
80         return (pos == size()) ? 0 : bf_[pos];
81 }
82
83
84 unsigned char MathArray::getChar(int pos) const
85 {
86         return (pos == size()) ? 0 : (bf_[pos]->getChar());
87 }
88
89
90 /*
91 string MathArray::getString(int & pos) const
92 {
93         string s;
94         if (isInset(pos))
95                 return s;
96
97         MathTextCodes const fcode = getCode(pos);
98         do {
99                 s += getChar(pos);
100                 ++pos;
101         } while (pos < size() && !isInset(pos) && getCode(pos) == fcode);
102
103         return s;
104 }
105 */
106
107
108 MathTextCodes MathArray::getCode(int pos) const
109 {
110         return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN;
111 }
112
113
114 void MathArray::setCode(int pos, MathTextCodes t)
115 {
116         bf_[pos]->code(t);
117 }
118
119
120 void MathArray::insert(int pos, MathInset * p)
121 {
122         bf_.insert(bf_.begin() + pos, p);
123 }
124
125
126 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
127 {
128         bf_.insert(bf_.begin() + pos, new MathCharInset(b, t));
129 }
130
131
132 void MathArray::insert(int pos, MathArray const & array)
133 {
134         bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
135         deep_copy(pos, pos + array.size());
136 }
137
138
139 void MathArray::push_back(MathInset * p)
140 {       
141         insert(size(), p);
142 }
143
144
145 void MathArray::push_back(unsigned char b, MathTextCodes c)
146 {
147         insert(size(), b, c);
148 }
149
150
151 void MathArray::push_back(MathArray const & array)
152 {
153         insert(size(), array);
154 }
155
156
157 void MathArray::clear()
158 {
159         erase();
160 }
161
162
163 void MathArray::swap(MathArray & array)
164 {
165         if (this != &array) 
166                 bf_.swap(array.bf_);
167 }
168
169
170 bool MathArray::empty() const
171 {
172         return bf_.empty();
173 }
174    
175
176 int MathArray::size() const
177 {
178         return bf_.size();
179 }
180
181
182 void MathArray::erase()
183 {
184         erase(0, size());
185 }
186
187
188 void MathArray::erase(int pos)
189 {
190         if (pos < size())
191                 erase(pos, pos + 1);
192 }
193
194
195 void MathArray::erase(int pos1, int pos2)
196 {
197         for (int pos = pos1; pos < pos2; ++pos)
198                 delete bf_[pos];
199         bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
200 }
201
202
203 MathInset * MathArray::back() const
204 {
205         return size() ? bf_.back() : 0;
206 }
207
208
209 void MathArray::dump2(ostream & os) const
210 {
211         for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
212                 os << *it << ' ';
213 }
214
215
216 void MathArray::dump(ostream & os) const
217 {
218         for (int pos = 0; pos < size(); ++pos)
219                 os << "<" << nextInset(pos) << ">";
220 }
221
222
223 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
224 {
225         ar.dump2(os);
226         return os;
227 }
228
229
230 void MathArray::write(ostream & os, bool fragile) const
231 {
232         for (int pos = 0; pos < size(); ++pos)
233                 nextInset(pos)->write(os, fragile);
234 }
235
236
237 void MathArray::writeNormal(ostream & os) const
238 {
239         if (empty()) {
240                 os << "[par] ";
241                 return;
242         }
243
244         write(os, true);
245 }
246
247
248 void MathArray::validate(LaTeXFeatures & features) const
249 {
250         for (int pos = 0; pos < size(); ++pos)
251                 nextInset(pos)->validate(features);
252 }
253
254
255 void MathArray::pop_back()
256 {       
257         if (!size()) {
258                 lyxerr << "pop_back from empty array!\n";
259                 return;
260         }
261         delete back();
262         bf_.pop_back();
263 }
264