]> git.lyx.org Git - features.git/blob - src/mathed/array.C
*** empty log message ***
[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 "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(MathAtom const & t)
60 {       
61         bf_.push_back(t);
62 }
63
64
65 void MathArray::push_back(MathInset * p)
66 {       
67         bf_.push_back(MathAtom(p));
68 }
69
70
71 void MathArray::push_back(MathArray const & array)
72 {
73         insert(size(), array);
74 }
75
76
77 void MathArray::clear()
78 {
79         erase();
80 }
81
82
83 void MathArray::swap(MathArray & array)
84 {
85         if (this != &array) 
86                 bf_.swap(array.bf_);
87 }
88
89
90 bool MathArray::empty() const
91 {
92         return bf_.empty();
93 }
94    
95
96 MathArray::size_type MathArray::size() const
97 {
98         return bf_.size();
99 }
100
101
102 void MathArray::erase()
103 {
104         erase(0, size());
105 }
106
107
108 void MathArray::erase(size_type pos)
109 {
110         if (pos < size())
111                 erase(pos, pos + 1);
112 }
113
114
115 void MathArray::erase(size_type pos1, size_type pos2)
116 {
117         bf_.erase(begin() + pos1, begin() + pos2);
118 }
119
120
121 MathAtom & MathArray::back()
122 {
123         return bf_.back();
124 }
125
126
127 void MathArray::dump2(ostream & os) const
128 {
129         for (const_iterator it = begin(); it != end(); ++it)
130                 os << *it << ' ';
131 }
132
133
134 void MathArray::dump(ostream & os) const
135 {
136         for (const_iterator it = begin(); it != end(); ++it)
137                 os << "<" << *it << ">";
138 }
139
140
141 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
142 {
143         ar.dump2(os);
144         return os;
145 }
146
147
148 // returns sequence of char with same code starting at it up to end
149 // it might be less, though...
150 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
151 {
152         string s;
153         MathCharInset const * p = it->nucleus()->asCharInset();
154         if (!p)
155                 return s;
156
157         for (MathTextCodes c = p->code(); it != end; ++it) {
158                 if (!it->nucleus())
159                         break;
160                 p = it->nucleus()->asCharInset();
161                 if (!p || it->up() || it->down() || p->code() != c)
162                         break;
163                 s += p->getChar();
164         }
165         return s;
166 }
167
168
169 void MathArray::write(ostream & os, bool fragile) const
170 {
171         for (const_iterator it = begin(); it != end(); ++it) {
172                 if (it->nucleus() && it->nucleus()->asCharInset()
173                                 && !it->up() && !it->down())
174                 {
175                         MathCharInset const * p = it->nucleus()->asCharInset();
176                         // special handling for character sequences with the same code
177                         string s = charSequence(it, end());
178                         p->writeHeader(os);
179                         os << s;
180                         p->writeTrailer(os);
181                         it += s.size() - 1;
182                 } else {
183                         it->write(os, fragile);
184                 }
185         }
186 }
187
188
189 void MathArray::writeNormal(ostream & os) const
190 {
191         if (empty()) {
192                 os << "[par] ";
193                 return;
194         }
195
196         write(os, true);
197 }
198
199
200 void MathArray::validate(LaTeXFeatures & features) const
201 {
202         for (const_iterator it = begin(); it != end(); ++it)
203                 it->validate(features);
204 }
205
206
207 void MathArray::pop_back()
208 {       
209         if (!size()) {
210                 lyxerr << "pop_back from empty array!\n";
211                 return;
212         }
213         bf_.pop_back();
214 }
215
216
217 MathArray::const_iterator MathArray::begin() const
218 {
219         return bf_.begin();
220 }
221
222
223 MathArray::const_iterator MathArray::end() const
224 {
225         return bf_.end();
226 }
227
228
229 MathArray::iterator MathArray::begin()
230 {
231         return bf_.begin();
232 }
233
234
235 MathArray::iterator MathArray::end()
236 {
237         return bf_.end();
238 }