]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
further code uglification to make Jean-Marc's compiler happy
[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 "math_scriptinset.h"
8 #include "debug.h"
9 #include "array.h"
10 #include "mathed/support.h"
11
12 using std::ostream;
13 using std::endl;
14
15
16 MathArray::MathArray()
17 {}
18
19
20 MathArray::MathArray(MathArray const & array, size_type from, size_type to)
21         : bf_(array.begin() + from, array.begin() + to)
22 {}
23
24
25 void MathArray::substitute(MathMacro const & m)
26 {
27         for (iterator it = begin(); it != end(); ++it)
28                 it->substitute(m);
29 }
30
31
32 MathAtom * MathArray::at(size_type pos)
33 {
34         return pos < size() ? &bf_[pos] : 0;
35 }
36
37
38 MathAtom const * MathArray::at(size_type pos) const
39 {
40         return pos < size() ? &bf_[pos] : 0;
41 }
42
43
44 void MathArray::insert(size_type pos, MathInset * p)
45 {
46         //cerr << "\n  1: "; p->write(cerr, true); cerr << p << "\n";
47         // inserting here invalidates the pointer!
48         bf_.insert(begin() + pos, MathAtom(p));
49         //cerr << "\n  2: "; p->write(cerr, true); cerr << p << "\n";
50 }
51
52
53 void MathArray::insert(size_type pos, MathArray const & array)
54 {
55         bf_.insert(begin() + pos, array.begin(), array.end());
56 }
57
58
59 void MathArray::push_back(MathInset * p)
60 {       
61         insert(size(), p);
62 }
63
64
65 void MathArray::push_back(MathArray const & array)
66 {
67         insert(size(), array);
68 }
69
70
71 void MathArray::clear()
72 {
73         erase();
74 }
75
76
77 void MathArray::swap(MathArray & array)
78 {
79         if (this != &array) 
80                 bf_.swap(array.bf_);
81 }
82
83
84 bool MathArray::empty() const
85 {
86         return bf_.empty();
87 }
88    
89
90 MathArray::size_type MathArray::size() const
91 {
92         return bf_.size();
93 }
94
95
96 void MathArray::erase()
97 {
98         erase(0, size());
99 }
100
101
102 void MathArray::erase(size_type pos)
103 {
104         if (pos < size())
105                 erase(pos, pos + 1);
106 }
107
108
109 void MathArray::erase(size_type pos1, size_type pos2)
110 {
111         bf_.erase(begin() + pos1, begin() + pos2);
112 }
113
114
115 MathAtom & MathArray::back()
116 {
117         return bf_.back();
118 }
119
120
121 void MathArray::dump2(ostream & os) const
122 {
123         for (const_iterator it = begin(); it != end(); ++it)
124                 os << *it << ' ';
125 }
126
127
128 void MathArray::dump(ostream & os) const
129 {
130         for (const_iterator it = begin(); it != end(); ++it)
131                 os << "<" << *it << ">";
132 }
133
134
135 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
136 {
137         ar.dump2(os);
138         return os;
139 }
140
141
142 // returns sequence of char with same code starting at it up to end
143 // it might be less, though...
144 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
145 {
146         string s;
147         MathCharInset const * p = it->nucleus()->asCharInset();
148         if (!p)
149                 return s;
150
151         for (MathTextCodes c = p->code(); it != end; ++it) {
152                 p = it->nucleus()->asCharInset();
153                 if (!p || it->up() || it->down() || p->code() != c)
154                         break;
155                 s += p->getChar();
156         }
157         return s;
158 }
159
160
161 void MathArray::write(ostream & os, bool fragile) const
162 {
163         for (const_iterator it = begin(); it != end(); ++it) {
164                 MathCharInset const * p = it->nucleus()->asCharInset();
165                 if (p && !it->up() && !it->down()) {
166                         // special handling for character sequences with the same code
167                         string s = charSequence(it, end());
168                         p->writeHeader(os);
169                         os << s;
170                         p->writeTrailer(os);
171                         it += s.size() - 1;
172                 } else {
173                         it->write(os, fragile);
174                 }
175         }
176 }
177
178
179 void MathArray::writeNormal(ostream & os) const
180 {
181         if (empty()) {
182                 os << "[par] ";
183                 return;
184         }
185
186         write(os, true);
187 }
188
189
190 void MathArray::validate(LaTeXFeatures & features) const
191 {
192         for (const_iterator it = begin(); it != end(); ++it)
193                 it->validate(features);
194 }
195
196
197 void MathArray::pop_back()
198 {       
199         if (!size()) {
200                 lyxerr << "pop_back from empty array!\n";
201                 return;
202         }
203         bf_.pop_back();
204 }
205
206
207 MathArray::const_iterator MathArray::begin() const
208 {
209         return bf_.begin();
210 }
211
212
213 MathArray::const_iterator MathArray::end() const
214 {
215         return bf_.end();
216 }
217
218
219 MathArray::iterator MathArray::begin()
220 {
221         return bf_.begin();
222 }
223
224
225 MathArray::iterator MathArray::end()
226 {
227         return bf_.end();
228 }