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