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