X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Farray.C;h=48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb;hb=dff2911bda426ad439e6475f62183cedd7044801;hp=db18ed90bba31def5ec94dced0812969ecf172c4;hpb=089f4fca7d614f9b3830ce9278cb1f29d0df5a07;p=features.git diff --git a/src/mathed/array.C b/src/mathed/array.C index db18ed90bb..48deaf73ab 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -4,147 +4,73 @@ #include "math_inset.h" #include "math_charinset.h" +#include "math_scriptinset.h" +#include "math_stringinset.h" #include "debug.h" #include "array.h" -#include "math_scriptinset.h" #include "mathed/support.h" +#include "support/LAssert.h" using std::ostream; using std::endl; + MathArray::MathArray() {} -MathArray::~MathArray() -{ - erase(); -} - - -MathArray::MathArray(MathArray const & array) - : bf_(array.bf_) -{ - deep_copy(0, size()); -} - - -MathArray::MathArray(MathArray const & array, int from, int to) - : bf_(array.bf_.begin() + from, array.bf_.begin() + to) -{ - deep_copy(0, size()); -} - - -void MathArray::deep_copy(int pos1, int pos2) -{ - for (int pos = pos1; pos < pos2; ++pos) { - MathInset * p = bf_[pos]->clone(); - //lyxerr << "cloning: '" << bf_[pos] << " to " << p << "'\n"; - bf_[pos] = p; - } -} - - -int MathArray::last() const -{ - return size() - 1; -} +MathArray::MathArray(MathArray const & array, size_type from, size_type to) + : bf_(array.begin() + from, array.begin() + to) +{} void MathArray::substitute(MathMacro const & m) { - MathArray tmp; - for (int pos = 0; pos < size(); ++pos) - bf_[pos]->substitute(tmp, m); - swap(tmp); -} - - -MathArray & MathArray::operator=(MathArray const & array) -{ - MathArray tmp(array); - swap(tmp); - return *this; -} - - -MathInset * MathArray::nextInset(int pos) -{ - return (pos == size()) ? 0 : bf_[pos]; -} - - -MathInset const * MathArray::nextInset(int pos) const -{ - return (pos == size()) ? 0 : bf_[pos]; -} - - -unsigned char MathArray::getChar(int pos) const -{ - return (pos == size()) ? 0 : (bf_[pos]->getChar()); -} - - -/* -string MathArray::getString(int & pos) const -{ - string s; - if (isInset(pos)) - return s; - - MathTextCodes const fcode = getCode(pos); - do { - s += getChar(pos); - ++pos; - } while (pos < size() && !isInset(pos) && getCode(pos) == fcode); - - return s; + for (iterator it = begin(); it != end(); ++it) + it->nucleus()->substitute(m); } -*/ -MathTextCodes MathArray::getCode(int pos) const +MathScriptInset const * MathArray::asScript(const_iterator it) const { - return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN; + if (it->nucleus()->asScriptInset()) + return 0; + const_iterator jt = it + 1; + if (jt == end()) + return 0; + return jt->nucleus()->asScriptInset(); } -void MathArray::setCode(int pos, MathTextCodes t) +MathAtom & MathArray::at(size_type pos) { - bf_[pos]->code(t); + lyx::Assert(pos < size()); + return bf_[pos]; } -void MathArray::insert(int pos, MathInset * p) +MathAtom const & MathArray::at(size_type pos) const { - bf_.insert(bf_.begin() + pos, p); + lyx::Assert(pos < size()); + return bf_[pos]; } -void MathArray::insert(int pos, unsigned char b, MathTextCodes t) +void MathArray::insert(size_type pos, MathAtom const & t) { - bf_.insert(bf_.begin() + pos, new MathCharInset(b, t)); + bf_.insert(begin() + pos, t); } -void MathArray::insert(int pos, MathArray const & array) +void MathArray::insert(size_type pos, MathArray const & array) { - bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end()); - deep_copy(pos, pos + array.size()); + bf_.insert(begin() + pos, array.begin(), array.end()); } -void MathArray::push_back(MathInset * p) +void MathArray::push_back(MathAtom const & t) { - insert(size(), p); -} - - -void MathArray::push_back(unsigned char b, MathTextCodes c) -{ - insert(size(), b, c); + bf_.push_back(t); } @@ -171,9 +97,9 @@ bool MathArray::empty() const { return bf_.empty(); } - -int MathArray::size() const + +MathArray::size_type MathArray::size() const { return bf_.size(); } @@ -185,38 +111,36 @@ void MathArray::erase() } -void MathArray::erase(int pos) +void MathArray::erase(size_type pos) { if (pos < size()) erase(pos, pos + 1); } -void MathArray::erase(int pos1, int pos2) +void MathArray::erase(size_type pos1, size_type pos2) { - for (int pos = pos1; pos < pos2; ++pos) - delete bf_[pos]; - bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2); + bf_.erase(begin() + pos1, begin() + pos2); } -MathInset * MathArray::back() const +MathAtom & MathArray::back() { - return size() ? bf_.back() : 0; + return bf_.back(); } void MathArray::dump2(ostream & os) const { - for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it) - os << *it << ' '; + for (const_iterator it = begin(); it != end(); ++it) + os << it->nucleus() << ' '; } void MathArray::dump(ostream & os) const { - for (int pos = 0; pos < size(); ++pos) - os << "<" << nextInset(pos) << ">"; + for (const_iterator it = begin(); it != end(); ++it) + os << "<" << it->nucleus() << ">"; } @@ -227,28 +151,88 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar) } -void MathArray::write(ostream & os, bool fragile) const +// 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) { - for (int pos = 0; pos < size(); ++pos) - nextInset(pos)->write(os, fragile); + 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; } -void MathArray::writeNormal(ostream & os) const +MathArray MathArray::glueChars() const { - if (empty()) { - os << "[par] "; - return; + 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; +} + - write(os, true); +void MathArray::write(MathWriteInfo & wi) const +{ + glueChars().write1(wi); +} + + +void MathArray::write1(MathWriteInfo & wi) const +{ + 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 MathArray::writeNormal(ostream & os) const +{ + 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 MathArray::validate(LaTeXFeatures & features) const { - for (int pos = 0; pos < size(); ++pos) - nextInset(pos)->validate(features); + for (const_iterator it = begin(); it != end(); ++it) + it->nucleus()->validate(features); } @@ -258,7 +242,29 @@ void MathArray::pop_back() lyxerr << "pop_back from empty array!\n"; return; } - delete back(); bf_.pop_back(); } + +MathArray::const_iterator MathArray::begin() const +{ + return bf_.begin(); +} + + +MathArray::const_iterator MathArray::end() const +{ + return bf_.end(); +} + + +MathArray::iterator MathArray::begin() +{ + return bf_.begin(); +} + + +MathArray::iterator MathArray::end() +{ + return bf_.end(); +}