]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
fix typo that put too many include paths for most people
[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                 iterator it = begin() + i;
234                 const_iterator rt = rep.from.begin();
235                 const_iterator et = rep.from.end();
236                 for (const_iterator jt = it; jt != end() && rt != et; ++jt, ++rt)
237                         if (!jt->nucleus()->match(rt->nucleus()))
238                                 break;
239                 if (rt == et) {
240                         // match found
241                         lyxerr << "match found!\n";
242                         erase(it, it + rep.from.size());
243                         insert(i, rep.to);
244                 }
245         }
246
247         for (const_iterator it = begin(); it != end(); ++it)
248                 it->nucleus()->replace(rep);
249 }
250
251
252 bool MathArray::contains(MathArray const & ar) const
253 {
254         for (size_type i = 0; i + ar.size() <= size(); ++i) {
255                 const_iterator it = begin() + i;
256                 const_iterator rt = ar.begin();
257                 const_iterator et = ar.end();
258                 for (const_iterator jt = it; rt != et; ++jt, ++rt)
259                         if (!jt->nucleus()->match(rt->nucleus()))
260                                 break;
261                 if (rt == et)
262                         return true;
263         }
264
265         for (const_iterator it = begin(); it != end(); ++it)
266                 if (it->nucleus()->contains(ar))
267                         return true;
268
269         return false;
270 }