]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
mathed28.diff
[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 // Is this still needed? (Lgb)
13 static inline
14 void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
15 {
16         char * ps = static_cast<char *>(ps_in);
17         char const * pt = static_cast<char const *>(pt_in);
18         while (n--) *ps++ = *pt++;
19         return ps_in;
20 }
21
22
23 MathedArray::MathedArray()
24         : bf_(1, 0), last_(0)
25 {}
26
27 MathedArray::~MathedArray()
28 {
29         // deep destruction
30         // let's leak for a while... 
31 /*
32         MathedIter it;
33         it.SetData(this);
34         while (it.OK()) {
35                 if (it.IsInset()) {
36                         MathedInset * inset = it.GetInset();
37                         delete inset;
38                 }
39                 it.Next();
40         }
41 */
42 }
43
44
45 MathedArray::MathedArray(MathedArray const & array)
46 {
47         // this "implementation" is obviously wrong: MathedIter should be
48         // implemented by MathedArray (not the other way round) but I think
49         // getting the _interface_ of MathedArray right is more important right
50         // now (Andre')
51
52         // shallow copy
53         bf_   = array.bf_;
54         last_ = array.last_;
55
56         // deep copy
57         // we'll not yet get exeption safety
58         MathedIter it;
59         it.SetData(this);
60         while (it.OK()) {
61                 if (it.IsInset()) {
62                         MathedInset * inset = it.GetInset();
63                         inset = inset->Clone();
64                         raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
65                 }
66                 it.Next();
67         }
68 }
69
70
71 MathedArray & MathedArray::operator=(MathedArray const & array)
72 {
73         MathedArray tmp(array);
74         swap(tmp);
75         return *this;
76 }
77
78 void MathedArray::swap(MathedArray & array)
79 {
80         if (this != &array) {
81                 bf_.swap(array.bf_);
82                 std::swap(last_, array.last_);
83         }
84 }
85
86
87 MathedArray::iterator MathedArray::begin() 
88 {
89         return bf_.begin();
90 }
91
92
93 MathedArray::iterator MathedArray::end() 
94 {
95         return bf_.end();
96 }
97
98
99 MathedArray::const_iterator MathedArray::begin() const
100 {
101         return bf_.begin();
102 }
103
104
105 MathedArray::const_iterator MathedArray::end() const
106 {
107         return bf_.end();
108 }
109
110
111 int MathedArray::empty() const
112 {
113         return (last_ == 0);
114 }
115    
116
117 int MathedArray::last() const
118 {
119         return last_;
120 }
121
122
123 void MathedArray::last(int l)
124 {
125         last_ = l;
126 }
127
128
129 void MathedArray::need_size(int needed)
130 {
131         if (needed >= static_cast<int>(bf_.size()))
132                 resize(needed);
133 }
134
135
136 void MathedArray::resize(int newsize)
137 {
138         // still a bit smelly...
139         ++newsize;
140         bf_.resize(newsize + 1);
141         if (last_ >= newsize)
142                 last_ = newsize - 1;
143         bf_[last_] = 0;
144 }
145
146
147 void MathedArray::move(int p, int shift)
148 {
149         if (p <= last_) {
150                 need_size(last_ + shift);
151                 memmove(&bf_[p + shift], &bf_[p], last_ - p);
152                 last_ += shift;
153                 bf_[last_] = 0;
154         }
155 }
156
157
158 #if 0
159 void MathedArray::insert(MathedArray::iterator pos,
160                          MathedArray::const_iterator beg,
161                          MathedArray::const_iterator end)
162 {
163         bf_.insert(pos, beg, end);
164         last_ = bf_.size() - 1;
165 }
166 #else
167 void MathedArray::mergeF(MathedArray * a, int p, int dx)
168 {
169         my_memcpy(&bf_[p], &a->bf_[0], dx);
170 }
171 #endif
172
173
174 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
175 {
176         my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
177 }
178
179
180 #if 0
181 void MathedArray::insertInset(int pos, MathedInset * p, int type)
182 {
183         //bf_.insert(pos, type);
184         InsetTable tmp(pos, p);
185         insetList_.push_back(tmp);
186 }
187
188
189 MathedInset * MathedArray::getInset(int pos) 
190 {
191         InsetList::const_iterator cit = insetList_.begin();
192         InsetList::const_iterator end = insetList_.end();
193         for (; cit != end; ++cit) {
194                 if ((*cit).pos == pos)
195                         return (*cit).inset;
196         }
197         // not found
198         return 0;
199         // We would really like to throw an exception instead... (Lgb)
200         // throw inset_not_found();
201 }
202
203 #else
204 void MathedArray::raw_pointer_insert(void * p, int pos, int len)
205 {
206         my_memcpy(&bf_[pos], &p, len);
207 }
208 #endif
209
210
211 void MathedArray::strange_copy(MathedArray * dest, int dpos,
212                                 int spos, int len)
213 {
214         my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
215 }
216
217
218 byte MathedArray::operator[](int i) const
219 {
220         return bf_[i];
221 }
222
223
224 byte & MathedArray::operator[](int i)
225 {
226         return bf_[i];
227 }