From: André Pönitz Date: Thu, 9 Aug 2001 08:53:16 +0000 (+0000) Subject: iterators for MathArray; cosmetics; X-Git-Tag: 1.6.10~20886 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=20effa2eb1feaad48d8d0daf6d9d8eb250cf78be;p=features.git iterators for MathArray; cosmetics; git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2459 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/mathed/array.C b/src/mathed/array.C index 8881c07cf6..c5552aff4a 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -25,24 +25,21 @@ MathArray::~MathArray() MathArray::MathArray(MathArray const & array) : bf_(array.bf_) { - deep_copy(0, size()); + deep_copy(begin(), end()); } MathArray::MathArray(MathArray const & array, int from, int to) - : bf_(array.bf_.begin() + from, array.bf_.begin() + to) + : bf_(array.begin() + from, array.begin() + to) { - deep_copy(0, size()); + deep_copy(begin(), end()); } -void MathArray::deep_copy(int pos1, int pos2) +void MathArray::deep_copy(iterator from, iterator to) { - for (int pos = pos1; pos < pos2; ++pos) { - MathInset * p = bf_[pos]->clone(); - //lyxerr << "cloning: '" << bf_[pos] << " to " << p << "'\n"; - bf_[pos] = p; - } + for (iterator it = from; it != to; ++it) + *it = (*it)->clone(); } @@ -55,8 +52,8 @@ int MathArray::last() const void MathArray::substitute(MathMacro const & m) { MathArray tmp; - for (int pos = 0; pos < size(); ++pos) - bf_[pos]->substitute(tmp, m); + for (iterator it = begin(); it != end(); ++it) + (*it)->substitute(tmp, m); swap(tmp); } @@ -81,40 +78,22 @@ MathInset const * MathArray::nextInset(int pos) const } -unsigned char MathArray::getChar(int pos) const -{ - return (pos == size()) ? 0 : (bf_[pos]->getChar()); -} - - -MathTextCodes MathArray::getCode(int pos) const -{ - return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN; -} - - -void MathArray::setCode(int pos, MathTextCodes t) -{ - bf_[pos]->code(t); -} - - void MathArray::insert(int pos, MathInset * p) { - bf_.insert(bf_.begin() + pos, p); + bf_.insert(begin() + pos, p); } void MathArray::insert(int pos, unsigned char b, MathTextCodes t) { - bf_.insert(bf_.begin() + pos, new MathCharInset(b, t)); + bf_.insert(begin() + pos, new MathCharInset(b, t)); } void MathArray::insert(int 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()); + deep_copy(begin() + pos, begin() + pos + array.size()); } @@ -176,9 +155,9 @@ void MathArray::erase(int pos) void MathArray::erase(int pos1, int pos2) { - for (int pos = pos1; pos < pos2; ++pos) - delete bf_[pos]; - bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2); + for (iterator it = begin() + pos1; it != begin() + pos2; ++it) + delete *it; + bf_.erase(begin() + pos1, begin() + pos2); } @@ -190,15 +169,15 @@ MathInset * MathArray::back() const void MathArray::dump2(ostream & os) const { - for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it) + for (const_iterator it = begin(); it != end(); ++it) os << *it << ' '; } 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 << ">"; } @@ -211,8 +190,8 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar) void MathArray::write(ostream & os, bool fragile) const { - for (int pos = 0; pos < size(); ++pos) - nextInset(pos)->write(os, fragile); + for (const_iterator it = begin(); it != end(); ++it) + (*it)->write(os, fragile); } @@ -229,8 +208,8 @@ void MathArray::writeNormal(ostream & os) const 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)->validate(features); } @@ -244,3 +223,26 @@ void MathArray::pop_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(); +} diff --git a/src/mathed/array.h b/src/mathed/array.h index 8947ef4d47..f37e0c9843 100644 --- a/src/mathed/array.h +++ b/src/mathed/array.h @@ -40,6 +40,14 @@ class LaTeXFeatures; \version February 2001 */ class MathArray { +public: + /// + typedef std::vector buffer_type; + /// + typedef buffer_type::const_iterator const_iterator; + /// + typedef buffer_type::iterator iterator; + public: /// MathArray(); @@ -102,22 +110,22 @@ public: /// MathInset const * nextInset(int pos) const; /// - unsigned char getChar(int pos) const; - /// - MathTextCodes getCode(int pos) const; - /// - void setCode(int pos, MathTextCodes t); - /// void write(std::ostream &, bool) const; /// void writeNormal(std::ostream &) const; /// void validate(LaTeXFeatures &) const; -private: /// - typedef std::vector buffer_type; + const_iterator begin() const; + /// + const_iterator end() const; + /// + iterator begin(); + /// + iterator end(); +private: /// - void deep_copy(int pos1, int pos2); + void deep_copy(iterator from, iterator to); /// Buffer buffer_type bf_; }; diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 64dee868eb..51b92312a3 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -789,11 +789,9 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action, mathcursor->left(); } mathcursor->clearLastCode(); - // varcode = LM_TC_MIN; } else if (c == '_' && varcode == LM_TC_TEX) { mathcursor->insert(c, LM_TC_SPECIAL); mathcursor->clearLastCode(); - // varcode = LM_TC_MIN; } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) { mathcursor->macroModeOpen(); mathcursor->clearLastCode(); diff --git a/src/mathed/math_charinset.C b/src/mathed/math_charinset.C index 1a098466f8..97f6759d2b 100644 --- a/src/mathed/math_charinset.C +++ b/src/mathed/math_charinset.C @@ -76,3 +76,9 @@ void MathCharInset::writeNormal(std::ostream & os) const { os << char_; } + + +bool MathCharInset::isRelOp() const +{ + return char_ == '=' || char_ == '<' || char_ == '>'; +} diff --git a/src/mathed/math_charinset.h b/src/mathed/math_charinset.h index 9cc0fbb3d6..a6fef5ed9c 100644 --- a/src/mathed/math_charinset.h +++ b/src/mathed/math_charinset.h @@ -36,6 +36,8 @@ public: bool isCharInset() const { return true; } /// char getChar() const { return char_; } + /// + bool isRelOp() const; private: /// the character diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 370964b149..3ec44ee22e 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -861,6 +861,12 @@ void MathCursor::drawSelection(Painter & pain) const } +MathTextCodes MathCursor::nextCode() const +{ + return (pos() == size()) ? LM_TC_MIN : nextInset()->code(); +} + + void MathCursor::handleFont(MathTextCodes t) { if (selection_) { @@ -869,11 +875,13 @@ void MathCursor::handleFont(MathTextCodes t) getSelection(i1, i2); if (i1.idx_ == i2.idx_) { MathArray & ar = i1.cell(); - for (int pos = i1.pos_; pos != i2.pos_; ++pos) - if (isalnum(ar.getChar(pos))) { - MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t; - ar.setCode(pos, c); + for (int pos = i1.pos_; pos != i2.pos_; ++pos) { + MathInset * p = ar.nextInset(pos); + if (isalnum(p->getChar())) { + MathTextCodes c = (p->code() == t) ? LM_TC_VAR : t; + p->code(c); } + } } } else lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t; @@ -915,18 +923,6 @@ void MathCursor::getPos(int & x, int & y) } -MathTextCodes MathCursor::nextCode() const -{ - return array().getCode(pos()); -} - - -MathTextCodes MathCursor::prevCode() const -{ - return array().getCode(pos() - 1); -} - - MathInset * MathCursor::par() const { return cursor().par_; diff --git a/src/mathed/math_cursor.h b/src/mathed/math_cursor.h index c8b4935afa..3bdc37080f 100644 --- a/src/mathed/math_cursor.h +++ b/src/mathed/math_cursor.h @@ -187,8 +187,6 @@ public: /// MathTextCodes nextCode() const; /// - MathTextCodes prevCode() const; - /// char valign() const; /// char halign() const; diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index cdfc968a2d..6d6759e51b 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -179,6 +179,8 @@ public: /// virtual bool isActive() const { return nargs() > 0; } /// + virtual bool isRelOp() const { return false; } + /// virtual char getChar() const { return 0; } /// diff --git a/src/mathed/math_matrixinset.C b/src/mathed/math_matrixinset.C index 4684a59820..8508b4a572 100644 --- a/src/mathed/math_matrixinset.C +++ b/src/mathed/math_matrixinset.C @@ -68,9 +68,9 @@ int getCols(short int type) // used for "intelligent splitting" int firstRelOp(MathArray const & array) { - for (int pos = 0; pos < array.size(); ++pos) - if (MathIsRelOp(array.getChar(pos), array.getCode(pos))) - return pos; + for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it) + if ((*it)->isRelOp()) + return it - array.begin(); return array.size(); } diff --git a/src/mathed/math_symbolinset.C b/src/mathed/math_symbolinset.C index c35fd61b12..ea1efff9b0 100644 --- a/src/mathed/math_symbolinset.C +++ b/src/mathed/math_symbolinset.C @@ -52,3 +52,9 @@ void MathSymbolInset::draw(Painter & pain, int x, int y) const drawStr(pain, code_, size_, x, y, ssym_); } + + +bool MathSymbolInset::isRelOp() const +{ + return sym_->id == LM_leq || sym_->id == LM_geq; +} diff --git a/src/mathed/math_symbolinset.h b/src/mathed/math_symbolinset.h index e327799c54..ac54572dde 100644 --- a/src/mathed/math_symbolinset.h +++ b/src/mathed/math_symbolinset.h @@ -23,6 +23,8 @@ public: void metrics(MathStyles st) const; /// void draw(Painter &, int x, int y) const; + /// + bool isRelOp() const; private: /// diff --git a/src/mathed/support.C b/src/mathed/support.C index b006164ee1..bc62d948e4 100644 --- a/src/mathed/support.C +++ b/src/mathed/support.C @@ -16,12 +16,6 @@ using std::endl; using std::max; -bool MathIsInset(MathTextCodes x) -{ - return LM_TC_INSET == x; -} - - bool MathIsAlphaFont(MathTextCodes x) { return LM_TC_VAR <= x && x <= LM_TC_TEXTRM; @@ -741,18 +735,6 @@ MathStyles smallerStyleFrac(MathStyles st) return st; } -bool MathIsRelOp(unsigned char c, MathTextCodes f) -{ - if (f == LM_TC_BOP && (c == '=' || c == '<' || c == '>')) - return true; -#ifndef WITH_WARNINGS -#warning implement me properly -#endif - if (f == LM_TC_SYMB && (c == LM_leq || c == LM_geq)) - return true; - return false; -} - void math_font_max_dim(MathTextCodes code, MathStyles siz, int & asc, int & des) { diff --git a/src/mathed/support.h b/src/mathed/support.h index 73a79643f1..d5d2a5f942 100644 --- a/src/mathed/support.h +++ b/src/mathed/support.h @@ -32,10 +32,8 @@ int mathed_string_width(MathTextCodes type, MathStyles size, string const & s); int mathed_string_ascent(MathTextCodes type, MathStyles size, string const & s); int mathed_string_descent(MathTextCodes type, MathStyles size, string const & s); -bool MathIsInset(MathTextCodes x); bool MathIsAlphaFont(MathTextCodes x); bool MathIsSymbol(MathTextCodes x); -bool MathIsRelOp(unsigned char c, MathTextCodes f); void drawStr(Painter & pain, MathTextCodes type, MathStyles siz, int x, int y, string const & s);