]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
add std support
[lyx.git] / src / mathed / array.C
1
2 #include <config.h>
3
4 #ifdef __GNUG__
5 #pragma implementation
6 #endif
7
8 #include "array.h"
9 #include "math_iter.h"
10 #include "math_inset.h"
11
12 #include "support/LOstream.h"
13
14 using std::ostream;
15 using std::endl;
16
17 // Is this still needed? (Lgb)
18 static inline
19 void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
20 {
21         char * ps = static_cast<char *>(ps_in);
22         char const * pt = static_cast<char const *>(pt_in);
23         while (n--) *ps++ = *pt++;
24         return ps_in;
25 }
26
27
28 MathedArray::MathedArray()
29         : bf_(1, '\0'), last_(0)
30 {}
31
32
33 MathedArray::~MathedArray()
34 {
35         // deep destruction
36         // let's leak for a while... 
37 /*
38         MathedIter it;
39         it.SetData(this);
40         while (it.OK()) {
41                 if (it.IsInset()) {
42                         MathedInset * inset = it.GetInset();
43                         delete inset;
44                 }
45                 it.Next();
46         }
47 */
48 }
49
50
51 MathedArray::MathedArray(MathedArray const & array)
52 {
53         // this "implementation" is obviously wrong: MathedIter should be
54         // implemented by MathedArray (not the other way round) but I think
55         // getting the _interface_ of MathedArray right is more important right
56         // now (Andre')
57
58         // shallow copy
59         bf_   = array.bf_;
60         last_ = array.last_;
61
62         // deep copy
63         // we'll not yet get exeption safety
64         MathedIter it(this);
65         while (it.OK()) {
66                 if (it.IsInset()) {
67                         MathedInset * inset = it.GetInset();
68                         inset = inset->Clone();
69                         raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
70                 }
71                 it.Next();
72         }
73 }
74
75
76 MathedArray & MathedArray::operator=(MathedArray const & array)
77 {
78         MathedArray tmp(array);
79         swap(tmp);
80         return *this;
81 }
82
83 void MathedArray::clear()
84 {
85         last_ = 0;
86         bf_.resize(1);
87         bf_[0] = 0;
88 }
89
90 void MathedArray::swap(MathedArray & array)
91 {
92         if (this != &array) {
93                 bf_.swap(array.bf_);
94                 std::swap(last_, array.last_);
95         }
96 }
97
98
99 MathedArray::iterator MathedArray::begin() 
100 {
101         return bf_.begin();
102 }
103
104
105 MathedArray::iterator MathedArray::end() 
106 {
107         return bf_.end();
108 }
109
110
111 MathedArray::const_iterator MathedArray::begin() const
112 {
113         return bf_.begin();
114 }
115
116
117 MathedArray::const_iterator MathedArray::end() const
118 {
119         return bf_.end();
120 }
121
122
123 int MathedArray::empty() const
124 {
125         return (last_ == 0);
126 }
127    
128
129 int MathedArray::last() const
130 {
131         return last_;
132 }
133
134
135 void MathedArray::last(int l)
136 {
137         last_ = l;
138 }
139
140
141 void MathedArray::need_size(int needed)
142 {
143         if (needed >= static_cast<int>(bf_.size()))
144                 resize(needed);
145 }
146
147
148 void MathedArray::resize(int newsize)
149 {
150         // still a bit smelly...
151         ++newsize;
152         bf_.resize(newsize + 1);
153         if (last_ >= newsize)
154                 last_ = newsize - 1;
155         bf_[last_] = 0;
156 }
157
158
159 void MathedArray::move(int p, int shift)
160 {
161         if (p <= last_) {
162                 need_size(last_ + shift);
163                 memmove(&bf_[p + shift], &bf_[p], last_ - p);
164                 last_ += shift;
165                 bf_[last_] = 0;
166         }
167 }
168
169
170
171 void MathedArray::shrink(int pos1, int pos2)
172 {
173         if (pos1 == 0 && pos2 >= last())        
174                 return;
175
176         short fc = 0;
177         if (pos1 > 0 && bf_[pos1] > ' ') {
178                 for (int p = pos1; p >= 0; --p) {
179                         if (MathIsFont(bf_[p])) {
180                                 if (p != pos1 - 1)
181                                         fc = bf_[p];
182                                 else
183                                         --pos1;
184                                 break;
185                         }
186                 }
187         }
188
189         if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
190                 --pos2;
191
192         int dx = pos2 - pos1;
193         MathedArray a;
194         a.resize(dx + 1);
195         strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
196         if (fc) {
197                 a[0] = fc;
198                 ++dx;
199         }
200         a.last(dx);
201         a[dx] = '\0';
202
203         MathedIter it(&a);
204         it.Reset();
205
206         while (it.OK()) {
207                 if (it.IsInset()) {
208                         MathedInset * inset = it.GetInset();
209                         inset = inset->Clone();
210                         a.raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
211                 }
212                 it.Next();
213         }
214         swap(a);
215 }
216
217
218 #if 0
219 void MathedArray::insert(MathedArray::iterator pos,
220                          MathedArray::const_iterator beg,
221                          MathedArray::const_iterator end)
222 {
223         bf_.insert(pos, beg, end);
224         last_ = bf_.size() - 1;
225 }
226 #else
227 void MathedArray::merge(MathedArray const & a, int p)
228 {
229         my_memcpy(&bf_[p], &a.bf_[0], a.last());
230 }
231 #endif
232
233
234 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
235 {
236         my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
237 }
238
239
240 #if 0
241 void MathedArray::insertInset(int pos, MathedInset * p, int type)
242 {
243         //bf_.insert(pos, type);
244         InsetTable tmp(pos, p);
245         insetList_.push_back(tmp);
246 }
247
248
249 MathedInset * MathedArray::getInset(int pos) 
250 {
251         InsetList::const_iterator cit = insetList_.begin();
252         InsetList::const_iterator end = insetList_.end();
253         for (; cit != end; ++cit) {
254                 if ((*cit).pos == pos)
255                         return (*cit).inset;
256         }
257         // not found
258         return 0;
259         // We would really like to throw an exception instead... (Lgb)
260         // throw inset_not_found();
261 }
262
263 #else
264 void MathedArray::raw_pointer_insert(void * p, int pos, int len)
265 {
266         my_memcpy(&bf_[pos], &p, len);
267 }
268 #endif
269
270
271 void MathedArray::strange_copy(MathedArray * dest, int dpos,
272                                 int spos, int len)
273 {
274         my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
275 }
276
277
278 byte MathedArray::operator[](int i) const
279 {
280         return bf_[i];
281 }
282
283
284 byte & MathedArray::operator[](int i)
285 {
286         return bf_[i];
287 }
288
289
290 void MathedArray::dump(ostream & os) const
291 {
292         buffer_type::const_iterator cit = bf_.begin();
293         buffer_type::const_iterator end = bf_.end();
294         for (; cit != end; ++cit) {
295                 os << (*cit);
296         }
297         os << endl;
298 }
299
300