X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Farray.C;h=48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb;hb=dff2911bda426ad439e6475f62183cedd7044801;hp=2d08fc10e39a23915856563da2b43d01755e1e1a;hpb=408b556ed9be7fea7c77c92cc4e009a1044b9cad;p=features.git diff --git a/src/mathed/array.C b/src/mathed/array.C index 2d08fc10e3..48deaf73ab 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -1,132 +1,270 @@ - -#include - #ifdef __GNUG__ #pragma implementation #endif +#include "math_inset.h" +#include "math_charinset.h" +#include "math_scriptinset.h" +#include "math_stringinset.h" +#include "debug.h" #include "array.h" -#include "math_defs.h" +#include "mathed/support.h" +#include "support/LAssert.h" + +using std::ostream; +using std::endl; + + +MathArray::MathArray() +{} -// Is this still needed? (Lgb) -static inline -void * my_memcpy(void * ps_in, void const * pt_in, size_t n) +MathArray::MathArray(MathArray const & array, size_type from, size_type to) + : bf_(array.begin() + from, array.begin() + to) +{} + + +void MathArray::substitute(MathMacro const & m) { - char * ps = static_cast(ps_in); - char const * pt = static_cast(pt_in); - while (n--) *ps++ = *pt++; - return ps_in; + for (iterator it = begin(); it != end(); ++it) + it->nucleus()->substitute(m); } -int MathedArray::empty() const +MathScriptInset const * MathArray::asScript(const_iterator it) const { - return (last_ == 0); + if (it->nucleus()->asScriptInset()) + return 0; + const_iterator jt = it + 1; + if (jt == end()) + return 0; + return jt->nucleus()->asScriptInset(); } - -int MathedArray::last() const + +MathAtom & MathArray::at(size_type pos) { - return last_; + lyx::Assert(pos < size()); + return bf_[pos]; } -void MathedArray::last(int l) +MathAtom const & MathArray::at(size_type pos) const { - last_ = l; + lyx::Assert(pos < size()); + return bf_[pos]; } -int MathedArray::maxsize() const +void MathArray::insert(size_type pos, MathAtom const & t) { - return static_cast(bf_.size()); + bf_.insert(begin() + pos, t); } -void MathedArray::need_size(int needed) +void MathArray::insert(size_type pos, MathArray const & array) { - if (needed >= maxsize()) - resize(needed); + bf_.insert(begin() + pos, array.begin(), array.end()); } -void MathedArray::resize(int newsize) +void MathArray::push_back(MathAtom const & t) +{ + bf_.push_back(t); +} + + +void MathArray::push_back(MathArray const & array) +{ + insert(size(), array); +} + + +void MathArray::clear() { - if (newsize < ARRAY_MIN_SIZE) - newsize = ARRAY_MIN_SIZE; - newsize += ARRAY_STEP - (newsize % ARRAY_STEP); - bf_.resize(newsize); - if (last_ >= newsize) last_ = newsize - 1; - bf_[last_] = 0; + erase(); } -MathedArray::MathedArray(int size) +void MathArray::swap(MathArray & array) { - int newsize = (size < ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE : size; - bf_.resize(newsize); - last_ = 0; + if (this != &array) + bf_.swap(array.bf_); } -void MathedArray::move(int p, int shift) +bool MathArray::empty() const { - if (p <= last_) { - need_size(last_ + shift); - memmove(&bf_[p + shift], &bf_[p], last_ - p); - last_ += shift; - bf_[last_] = 0; + return bf_.empty(); +} + + +MathArray::size_type MathArray::size() const +{ + return bf_.size(); +} + + +void MathArray::erase() +{ + erase(0, size()); +} + + +void MathArray::erase(size_type pos) +{ + if (pos < size()) + erase(pos, pos + 1); +} + + +void MathArray::erase(size_type pos1, size_type pos2) +{ + bf_.erase(begin() + pos1, begin() + pos2); +} + + +MathAtom & MathArray::back() +{ + return bf_.back(); +} + + +void MathArray::dump2(ostream & os) const +{ + for (const_iterator it = begin(); it != end(); ++it) + os << it->nucleus() << ' '; +} + + +void MathArray::dump(ostream & os) const +{ + for (const_iterator it = begin(); it != end(); ++it) + os << "<" << it->nucleus() << ">"; +} + + +std::ostream & operator<<(std::ostream & os, MathArray const & ar) +{ + ar.dump2(os); + return os; +} + + +// returns sequence of char with same code starting at it up to end +// it might be less, though... +string charSequence(MathArray::const_iterator it, MathArray::const_iterator end) +{ + string s; + MathCharInset const * p = it->nucleus()->asCharInset(); + if (!p) + return s; + + for (MathTextCodes c = p->code(); it != end; ++it) { + if (!it->nucleus()) + break; + p = it->nucleus()->asCharInset(); + if (!p || p->code() != c) + break; + s += p->getChar(); + } + return s; +} + + +MathArray MathArray::glueChars() const +{ + MathArray ar; + const_iterator it = begin(); + while (it != end()) { + if (it->nucleus() && it->nucleus()->asCharInset()) { + string s = charSequence(it, end()); + MathTextCodes c = it->nucleus()->asCharInset()->code(); + ar.push_back(MathAtom(new MathStringInset(s, c))); + it += s.size(); + } else { + ar.push_back(*it); + ++it; + } } + return ar; } -void MathedArray::mergeF(MathedArray * a, int p, int dx) +void MathArray::write(MathWriteInfo & wi) const { - my_memcpy(&bf_[p], &a->bf_[0], dx); + glueChars().write1(wi); } -void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const +void MathArray::write1(MathWriteInfo & wi) const { - my_memcpy(p, &bf_[pos], sizeof(MathedInset*)); + for (const_iterator it = begin(); it != end(); ++it) { + MathInset * p = it->nucleus(); + if (!p) + continue; + if (MathScriptInset const * q = asScript(it)) { + q->write(p, wi); + ++it; + } else { + p->write(wi); + } + } } -void MathedArray::raw_pointer_insert(void * p, int pos, int len) +void MathArray::writeNormal(ostream & os) const { - my_memcpy(&bf_[pos], &p, len); + for (const_iterator it = begin(); it != end(); ++it) { + MathInset * p = it->nucleus(); + if (!p) + continue; + if (MathScriptInset const * q = asScript(it)) { + q->writeNormal(p, os); + ++it; + } else { + p->writeNormal(os); + } + } } -void MathedArray::strange_copy(MathedArray * dest, int dpos, - int spos, int len) +void MathArray::validate(LaTeXFeatures & features) const { - my_memcpy(&dest[dpos], &bf_[spos], len); + for (const_iterator it = begin(); it != end(); ++it) + it->nucleus()->validate(features); +} + + +void MathArray::pop_back() +{ + if (!size()) { + lyxerr << "pop_back from empty array!\n"; + return; + } + bf_.pop_back(); } -byte MathedArray::operator[](int i) const +MathArray::const_iterator MathArray::begin() const { - return bf_[i]; + return bf_.begin(); } -byte & MathedArray::operator[](int i) +MathArray::const_iterator MathArray::end() const { - return bf_[i]; + return bf_.end(); } -void MathedArray::insert(int pos, byte c) +MathArray::iterator MathArray::begin() { - if (pos < 0) pos = last_; + return bf_.begin(); +} - // 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; + +MathArray::iterator MathArray::end() +{ + return bf_.end(); }