]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
4aa7431b64a03124842bc50ae377ea16865e5c83
[lyx.git] / src / mathed / math_data.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_data.h"
8 #include "math_inset.h"
9 #include "math_deliminset.h"
10 #include "math_charinset.h"
11 #include "math_scriptinset.h"
12 #include "math_stringinset.h"
13 #include "math_matrixinset.h"
14 #include "math_mathmlstream.h"
15 #include "math_support.h"
16 #include "math_replace.h"
17 #include "debug.h"
18 #include "support/LAssert.h"
19
20
21 MathArray::MathArray()
22 {}
23
24
25 MathArray::MathArray(MathArray const & ar, size_type from, size_type to)
26         : bf_(ar.begin() + from, ar.begin() + to)
27 {}
28
29
30 MathArray::MathArray(iterator from, iterator to)
31         : bf_(from, to)
32 {}
33
34
35 void MathArray::substitute(MathMacro const & m)
36 {
37         for (iterator it = begin(); it != end(); ++it)
38                 it->nucleus()->substitute(m);
39 }
40
41
42 MathAtom & MathArray::at(size_type pos)
43 {
44         lyx::Assert(pos < size());
45         return bf_[pos];
46 }
47
48
49 MathAtom const & MathArray::at(size_type pos) const
50 {
51         lyx::Assert(pos < size());
52         return bf_[pos];
53 }
54
55
56 void MathArray::insert(size_type pos, MathAtom const & t)
57 {
58         bf_.insert(begin() + pos, t);
59 }
60
61
62 void MathArray::insert(size_type pos, MathArray const & ar)
63 {
64         bf_.insert(begin() + pos, ar.begin(), ar.end());
65 }
66
67
68 void MathArray::push_back(MathAtom const & t)
69 {
70         bf_.push_back(t);
71 }
72
73
74 void MathArray::push_back(MathArray const & ar)
75 {
76         insert(size(), ar);
77 }
78
79
80 void MathArray::clear()
81 {
82         erase();
83 }
84
85
86 void MathArray::swap(MathArray & ar)
87 {
88         if (this != &ar)
89                 bf_.swap(ar.bf_);
90 }
91
92
93 bool MathArray::empty() const
94 {
95         return bf_.empty();
96 }
97
98
99 MathArray::size_type MathArray::size() const
100 {
101         return bf_.size();
102 }
103
104
105 void MathArray::erase()
106 {
107         bf_.erase(begin(), end());
108 }
109
110
111 void MathArray::erase(size_type pos)
112 {
113         if (pos < size())
114                 erase(pos, pos + 1);
115 }
116
117
118 void MathArray::erase(iterator pos1, iterator pos2)
119 {
120         bf_.erase(pos1, pos2);
121 }
122
123
124 void MathArray::erase(iterator pos)
125 {
126         bf_.erase(pos);
127 }
128
129
130 void MathArray::erase(size_type pos1, size_type pos2)
131 {
132         bf_.erase(begin() + pos1, begin() + pos2);
133 }
134
135
136 MathAtom & MathArray::back()
137 {
138         return bf_.back();
139 }
140
141
142 MathAtom & MathArray::front()
143 {
144         return bf_.front();
145 }
146
147
148 MathAtom const & MathArray::front() const
149 {
150         return bf_.front();
151 }
152
153
154 void MathArray::dump2() const
155 {
156         NormalStream ns(lyxerr);
157         for (const_iterator it = begin(); it != end(); ++it)
158                 ns << it->nucleus() << ' ';
159 }
160
161
162 void MathArray::dump() const
163 {
164         NormalStream ns(lyxerr);
165         for (const_iterator it = begin(); it != end(); ++it)
166                 ns << "<" << it->nucleus() << ">";
167 }
168
169
170 void MathArray::validate(LaTeXFeatures & features) const
171 {
172         for (const_iterator it = begin(); it != end(); ++it)
173                 if (it->nucleus())
174                         it->nucleus()->validate(features);
175 }
176
177
178 void MathArray::pop_back()
179 {
180         if (!size()) {
181                 lyxerr << "pop_back from empty array!\n";
182                 return;
183         }
184         bf_.pop_back();
185 }
186
187
188 MathArray::const_iterator MathArray::begin() const
189 {
190         return bf_.begin();
191 }
192
193
194 MathArray::const_iterator MathArray::end() const
195 {
196         return bf_.end();
197 }
198
199
200 MathArray::iterator MathArray::begin()
201 {
202         return bf_.begin();
203 }
204
205
206 MathArray::iterator MathArray::end()
207 {
208         return bf_.end();
209 }
210
211
212 bool MathArray::match(MathArray const & ar) const
213 {
214         return size() == ar.size() && matchpart(ar, 0);
215 }
216
217
218 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
219 {
220         if (size() < ar.size() + pos)
221                 return false;
222         const_iterator it = begin() + pos;
223         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
224                 if (!jt->nucleus()->match(it->nucleus()))
225                         return false;
226         return true;
227 }
228
229
230 void MathArray::replace(ReplaceData & rep)
231 {
232         for (size_type i = 0; i < size(); ++i) {
233                 if (find1(rep.from, i)) {
234                         // match found
235                         lyxerr << "match found!\n";
236                         erase(i, i + rep.from.size());
237                         insert(i, rep.to);
238                 }
239         }
240
241         for (const_iterator it = begin(); it != end(); ++it)
242                 it->nucleus()->replace(rep);
243 }
244
245
246 bool MathArray::find1(MathArray const & ar, size_type pos) const
247 {
248         //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
249         for (size_type i = 0, n = ar.size(); i < n; ++i)
250                 if (!at(pos + i)->match(ar[i].nucleus()))
251                         return false;
252         return true;
253 }
254
255
256 MathArray::size_type MathArray::find(MathArray const & ar) const
257 {
258         for (int i = 0, last = size() - ar.size(); i < last; ++i)
259                 if (find1(ar, i))
260                         return i;
261         return size();
262 }
263
264
265 MathArray::size_type MathArray::find_last(MathArray const & ar) const
266 {
267         for (int i = size() - ar.size(); i >= 0; --i) 
268                 if (find1(ar, i))
269                         return i;
270         return size();
271 }
272
273
274 bool MathArray::contains(MathArray const & ar) const
275 {
276         if (find(ar) != size())
277                 return true;
278         for (const_iterator it = begin(); it != end(); ++it)
279                 if (it->nucleus()->contains(ar))
280                         return true;
281         return false;
282 }