]> git.lyx.org Git - features.git/commitdiff
iterators for MathArray; cosmetics;
authorAndré Pönitz <poenitz@gmx.net>
Thu, 9 Aug 2001 08:53:16 +0000 (08:53 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Thu, 9 Aug 2001 08:53:16 +0000 (08:53 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2459 a592a061-630c-0410-9148-cb99ea01b6c8

13 files changed:
src/mathed/array.C
src/mathed/array.h
src/mathed/formulabase.C
src/mathed/math_charinset.C
src/mathed/math_charinset.h
src/mathed/math_cursor.C
src/mathed/math_cursor.h
src/mathed/math_inset.h
src/mathed/math_matrixinset.C
src/mathed/math_symbolinset.C
src/mathed/math_symbolinset.h
src/mathed/support.C
src/mathed/support.h

index 8881c07cf6478d66708c630bef8c6dc22c39bd8f..c5552aff4a6c622a0d43eff1e8c68bc1b427bd41 100644 (file)
@@ -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();
+}
index 8947ef4d47c00c03ed784959c3123a61ead24766..f37e0c9843912eab04ac8e95346b684ce760b5b8 100644 (file)
@@ -40,6 +40,14 @@ class LaTeXFeatures;
     \version February 2001
   */
 class MathArray  {
+public:
+       ///
+       typedef std::vector<MathInset *>     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<MathInset *>           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_;
 };
index 64dee868ebb8c0b20833ae7a6323500de717346a..51b92312a3e4960c3777a974496657b6e212bef3 100644 (file)
@@ -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();
index 1a098466f8a564d4140646d38b94b4ae7b9c3fbe..97f6759d2bd883e2ea798ffdcadd5b6f52deda21 100644 (file)
@@ -76,3 +76,9 @@ void MathCharInset::writeNormal(std::ostream & os) const
 {
        os << char_;
 }
+
+
+bool MathCharInset::isRelOp() const
+{
+       return char_ == '=' || char_ == '<' || char_ == '>';
+}
index 9cc0fbb3d696c2599c22eda56665cc4e00ce1049..a6fef5ed9c5a4d721c91047ed709eafc97e6d0b0 100644 (file)
@@ -36,6 +36,8 @@ public:
        bool isCharInset() const { return true; }
        ///
        char getChar() const { return char_; }
+       ///
+       bool isRelOp() const;
 
 private:
        /// the character
index 370964b1495ffae783319bed4c471f5ed6bf7892..3ec44ee22e18613a166be1283c420419fd6360cc 100644 (file)
@@ -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_;
index c8b4935afa3075cd2817d280d131c7ad287f739a..3bdc37080fab2662f5daee8c727033d4f15b37c1 100644 (file)
@@ -187,8 +187,6 @@ public:
        ///
        MathTextCodes nextCode() const;
        ///
-       MathTextCodes prevCode() const;
-       ///
        char valign() const;
        ///
        char halign() const;
index cdfc968a2dbdf6789b1edb97277d36f9bc41be9c..6d6759e51b48757ab5450f8011f1767885988908 100644 (file)
@@ -179,6 +179,8 @@ public:
        ///
        virtual bool isActive() const { return nargs() > 0; }
        ///
+       virtual bool isRelOp() const { return false; }
+       ///
        virtual char getChar() const { return 0; }
 
        ///
index 4684a59820c8ee3a471f888513eebe7f14bef21a..8508b4a57245f1b5833cfa88ff29fc9a1de2b7d4 100644 (file)
@@ -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();
 }
 
index c35fd61b12830ebd7aba254f1a59157b4fa9bc81..ea1efff9b069556d46081cf2c86c4ef3d1a52c76 100644 (file)
@@ -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;
+}
index e327799c54e093e35ab99f5f85e61a3bb96db0c2..ac54572ddeaab64d40f262f62235ccd87537b8fe 100644 (file)
@@ -23,6 +23,8 @@ public:
        void metrics(MathStyles st) const;
        ///
        void draw(Painter &, int x, int y) const;
+       ///
+       bool isRelOp() const;
 
 private:
        ///
index b006164ee179dc9e2c6aacefe7b48a3027b2df57..bc62d948e43cad03e9fe3d42adcb3c3aa2d42967 100644 (file)
@@ -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)
 {
index 73a79643f1ae6968f14366b58e2e3f1fadf20b80..d5d2a5f942c6acad6ad1b86444d52b926d04b7ac 100644 (file)
@@ -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);