X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Farray.C;h=bc5b02596d7fb8a7704f6c016ce97a8cbc988018;hb=b5e793e7bbaee4db52c85fcc3b09cd117178d5d1;hp=060effb04679aff0bf3710346dd51f002dcc0daf;hpb=84b7ffa3473faa9cfe66a974d2e0a5f9d6641187;p=lyx.git diff --git a/src/mathed/array.C b/src/mathed/array.C index 060effb046..bc5b02596d 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -6,8 +6,14 @@ #endif #include "array.h" +#include "math_iter.h" +#include "math_inset.h" + +#include "support/LOstream.h" + +using std::ostream; +using std::endl; -// Is this still needed? (Lgb) static inline void * my_memcpy(void * ps_in, void const * pt_in, size_t n) { @@ -18,6 +24,105 @@ void * my_memcpy(void * ps_in, void const * pt_in, size_t n) } +MathedArray::MathedArray() + : bf_(1, '\0'), last_(0) +{} + + +MathedArray::~MathedArray() +{ + // deep destruction + // let's leak for a while... +/* + MathedIter it; + it.SetData(this); + while (it.OK()) { + if (it.IsInset()) { + MathedInset * inset = it.GetInset(); + delete inset; + } + it.Next(); + } +*/ +} + + +MathedArray::MathedArray(MathedArray const & array) +{ + // this "implementation" is obviously wrong: MathedIter should be + // implemented by MathedArray (not the other way round) but I think + // getting the _interface_ of MathedArray right is more important right + // now (Andre') + + // shallow copy + bf_ = array.bf_; + last_ = array.last_; + + // deep copy + deep_copy(); +} + +void MathedArray::deep_copy() +{ + MathedIter it(this); + while (it.OK()) { + if (it.IsInset()) { + MathedInset * inset = it.GetInset(); + inset = inset->Clone(); + raw_pointer_insert(inset, it.getPos() + 1); + } + it.Next(); + } +} + + +MathedArray & MathedArray::operator=(MathedArray const & array) +{ + MathedArray tmp(array); + swap(tmp); + return *this; +} + +void MathedArray::clear() +{ + last_ = 0; + bf_.resize(1); + bf_[0] = 0; +} + +void MathedArray::swap(MathedArray & array) +{ + if (this != &array) { + bf_.swap(array.bf_); + std::swap(last_, array.last_); + } +} + + +MathedArray::iterator MathedArray::begin() +{ + return bf_.begin(); +} + + +MathedArray::iterator MathedArray::end() +{ + return bf_.end(); +} + + +MathedArray::const_iterator MathedArray::begin() const +{ + return bf_.begin(); +} + + +MathedArray::const_iterator MathedArray::end() const +{ + return bf_.end(); +} + + int MathedArray::empty() const { return (last_ == 0); @@ -36,38 +141,24 @@ void MathedArray::last(int l) } -int MathedArray::maxsize() const -{ - return static_cast(bf_.size()); -} - - void MathedArray::need_size(int needed) { - if (needed >= maxsize()) + if (needed >= static_cast(bf_.size())) resize(needed); } void MathedArray::resize(int newsize) { - if (newsize < ARRAY_MIN_SIZE) - newsize = ARRAY_MIN_SIZE; - newsize += ARRAY_STEP - (newsize % ARRAY_STEP); - bf_.resize(newsize); - if (last_ >= newsize) last_ = newsize - 1; + // still a bit smelly... + ++newsize; + bf_.resize(newsize + 1); + if (last_ >= newsize) + last_ = newsize - 1; bf_[last_] = 0; } -MathedArray::MathedArray(int size) -{ - int newsize = (size < ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE : size; - bf_.resize(newsize); - last_ = 0; -} - - void MathedArray::move(int p, int shift) { if (p <= last_) { @@ -79,10 +170,58 @@ void MathedArray::move(int p, int shift) } -void MathedArray::mergeF(MathedArray * a, int p, int dx) + +void MathedArray::shrink(int pos1, int pos2) +{ + if (pos1 == 0 && pos2 >= last()) + return; + + short fc = 0; + if (pos1 > 0 && bf_[pos1] > ' ') { + for (int p = pos1; p >= 0; --p) { + if (MathIsFont(bf_[p])) { + if (p != pos1 - 1) + fc = bf_[p]; + else + --pos1; + break; + } + } + } + + if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1])) + --pos2; + + int dx = pos2 - pos1; + MathedArray a; + a.resize(dx + 1); + strange_copy(&a, (fc) ? 1 : 0, pos1, dx); + if (fc) { + a[0] = fc; + ++dx; + } + a.last(dx); + a[dx] = '\0'; + + swap(a); + deep_copy(); +} + + +#if 0 +void MathedArray::insert(MathedArray::iterator pos, + MathedArray::const_iterator beg, + MathedArray::const_iterator end) { - my_memcpy(&bf_[p], &a->bf_[0], dx); + bf_.insert(pos, beg, end); + last_ = bf_.size() - 1; } +#else +void MathedArray::merge(MathedArray const & a, int p) +{ + my_memcpy(&bf_[p], &a.bf_[0], a.last()); +} +#endif void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const @@ -91,10 +230,35 @@ void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const } -void MathedArray::raw_pointer_insert(void * p, int pos, int len) +#if 0 +void MathedArray::insertInset(int pos, MathedInset * p, int type) +{ + //bf_.insert(pos, type); + InsetTable tmp(pos, p); + insetList_.push_back(tmp); +} + + +MathedInset * MathedArray::getInset(int pos) +{ + InsetList::const_iterator cit = insetList_.begin(); + InsetList::const_iterator end = insetList_.end(); + for (; cit != end; ++cit) { + if ((*cit).pos == pos) + return (*cit).inset; + } + // not found + return 0; + // We would really like to throw an exception instead... (Lgb) + // throw inset_not_found(); +} + +#else +void MathedArray::raw_pointer_insert(void * p, int pos) { - my_memcpy(&bf_[pos], &p, len); + my_memcpy(&bf_[pos], &p, sizeof(p)); } +#endif void MathedArray::strange_copy(MathedArray * dest, int dpos, @@ -116,15 +280,14 @@ byte & MathedArray::operator[](int i) } -void MathedArray::insert(int pos, byte c) +void MathedArray::dump(ostream & os) const { - if (pos < 0) pos = last_; - - // I think this should be replaced by need_size(pos). Note that the - // current code looks troublesome if pos > maxsize() + ARRAY_STEP. - if (pos >= maxsize()) - resize(maxsize() + ARRAY_STEP); - bf_[pos] = c; - if (pos >= last_) - last_ = pos + 1; + buffer_type::const_iterator cit = bf_.begin(); + buffer_type::const_iterator end = bf_.end(); + for (; cit != end; ++cit) { + os << (*cit); + } + os << endl; } + +