]> git.lyx.org Git - features.git/blob - src/mathed/array.C
move things around
[features.git] / src / mathed / array.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_inset.h"
6 #include "debug.h"
7 #include "array.h"
8 #include "mathed/support.h"
9
10 using std::ostream;
11 using std::endl;
12
13 MathArray::MathArray()
14 {}
15
16
17 MathArray::~MathArray()
18 {
19         erase();
20 }
21
22
23 MathArray::MathArray(MathArray const & array)
24         : bf_(array.bf_)
25 {
26         deep_copy(begin(), end());
27 }
28
29
30 MathArray::MathArray(MathArray const & array, int from, int to)
31         : bf_(array.begin() + from, array.begin() + to)
32 {
33         deep_copy(begin(), end());
34 }
35
36
37 void MathArray::deep_copy(iterator from, iterator to)
38 {
39         for (iterator it = from; it != to; ++it)
40                 *it = (*it)->clone();
41 }
42
43
44 int MathArray::last() const
45 {
46         return size() - 1;
47 }
48
49
50 void MathArray::substitute(MathMacro const & m)
51 {
52         MathArray tmp;
53         for (iterator it = begin(); it != end(); ++it)
54                 (*it)->substitute(tmp, m);
55         swap(tmp);
56 }
57
58
59 MathArray & MathArray::operator=(MathArray const & array)
60 {
61         MathArray tmp(array);
62         swap(tmp);
63         return *this;
64 }
65
66
67 MathInset * MathArray::nextInset(int pos)
68 {
69         return (pos == size()) ? 0 : bf_[pos];
70 }
71
72
73 MathInset const * MathArray::nextInset(int pos) const
74 {
75         return (pos == size()) ? 0 : bf_[pos];
76 }
77
78
79 void MathArray::insert(int pos, MathInset * p)
80 {
81         bf_.insert(begin() + pos, p);
82 }
83
84
85 void MathArray::insert(int pos, MathArray const & array)
86 {
87         bf_.insert(begin() + pos, array.begin(), array.end());
88         deep_copy(begin() + pos, begin() + pos + array.size());
89 }
90
91
92 void MathArray::push_back(MathInset * p)
93 {       
94         insert(size(), p);
95 }
96
97
98 void MathArray::push_back(MathArray const & array)
99 {
100         insert(size(), array);
101 }
102
103
104 void MathArray::clear()
105 {
106         erase();
107 }
108
109
110 void MathArray::swap(MathArray & array)
111 {
112         if (this != &array) 
113                 bf_.swap(array.bf_);
114 }
115
116
117 bool MathArray::empty() const
118 {
119         return bf_.empty();
120 }
121    
122
123 int MathArray::size() const
124 {
125         return bf_.size();
126 }
127
128
129 void MathArray::erase()
130 {
131         erase(0, size());
132 }
133
134
135 void MathArray::erase(int pos)
136 {
137         if (pos < size())
138                 erase(pos, pos + 1);
139 }
140
141
142 void MathArray::erase(int pos1, int pos2)
143 {
144         for (iterator it = begin() + pos1; it != begin() + pos2; ++it)
145                 delete *it;
146         bf_.erase(begin() + pos1, begin() + pos2);
147 }
148
149
150 MathInset * MathArray::back() const
151 {
152         return size() ? bf_.back() : 0;
153 }
154
155
156 void MathArray::dump2(ostream & os) const
157 {
158         for (const_iterator it = begin(); it != end(); ++it)
159                 os << *it << ' ';
160 }
161
162
163 void MathArray::dump(ostream & os) const
164 {
165         for (const_iterator it = begin(); it != end(); ++it)
166                 os << "<" << *it << ">";
167 }
168
169
170 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
171 {
172         ar.dump2(os);
173         return os;
174 }
175
176
177 void MathArray::write(ostream & os, bool fragile) const
178 {
179         for (const_iterator it = begin(); it != end(); ++it)
180                 (*it)->write(os, fragile);
181 }
182
183
184 void MathArray::writeNormal(ostream & os) const
185 {
186         if (empty()) {
187                 os << "[par] ";
188                 return;
189         }
190
191         write(os, true);
192 }
193
194
195 void MathArray::validate(LaTeXFeatures & features) const
196 {
197         for (const_iterator it = begin(); it != end(); ++it)
198                 (*it)->validate(features);
199 }
200
201
202 void MathArray::pop_back()
203 {       
204         if (!size()) {
205                 lyxerr << "pop_back from empty array!\n";
206                 return;
207         }
208         delete back();
209         bf_.pop_back();
210 }
211
212
213 MathArray::const_iterator MathArray::begin() const
214 {
215         return bf_.begin();
216 }
217
218
219 MathArray::const_iterator MathArray::end() const
220 {
221         return bf_.end();
222 }
223
224
225 MathArray::iterator MathArray::begin()
226 {
227         return bf_.begin();
228 }
229
230
231 MathArray::iterator MathArray::end()
232 {
233         return bf_.end();
234 }