]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/array.C
further code uglification to make Jean-Marc's compiler happy
[lyx.git] / src / mathed / array.C
index a42580f17af8ee48f933cc4a783d2278264bca1e..cdf974e615889e990e7a862f4d770d72a1b02c92 100644 (file)
 
 #include "math_inset.h"
 #include "math_charinset.h"
+#include "math_scriptinset.h"
 #include "debug.h"
 #include "array.h"
-#include "math_scriptinset.h"
 #include "mathed/support.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;
-       }
-}
-
-
-bool MathArray::next(int & pos) const
-{
-       if (pos >= size() - 1)
-               return false;
-
-       ++pos;
-       return true;
-}
-
-
-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;
+       for (iterator it = begin(); it != end(); ++it)
+               it->substitute(m);
 }
 
 
-MathInset * MathArray::nextInset(int pos)
+MathAtom * MathArray::at(size_type pos)
 {
-       return (pos == size()) ? 0 : bf_[pos];
+       return pos < size() ? &bf_[pos] : 0;
 }
 
 
-MathInset const * MathArray::nextInset(int pos) const
+MathAtom const * MathArray::at(size_type pos) const
 {
-       return (pos == size()) ? 0 : bf_[pos];
+       return pos < size() ? &bf_[pos] : 0;
 }
 
 
-unsigned char MathArray::getChar(int pos) const
+void MathArray::insert(size_type pos, MathInset * p)
 {
-       return (pos == size()) ? 0 : (bf_[pos]->getChar());
+       //cerr << "\n  1: "; p->write(cerr, true); cerr << p << "\n";
+       // inserting here invalidates the pointer!
+       bf_.insert(begin() + pos, MathAtom(p));
+       //cerr << "\n  2: "; p->write(cerr, true); cerr << p << "\n";
 }
 
 
-/*
-string MathArray::getString(int & pos) const
+void MathArray::insert(size_type pos, MathArray const & array)
 {
-       string s;
-       if (isInset(pos))
-               return s;
-
-       MathTextCodes const fcode = getCode(pos);
-       do {
-               s += getChar(pos);
-               next(pos);
-       } while (pos < size() && !isInset(pos) && getCode(pos) == fcode);
-
-       return s;
-}
-*/
-
-
-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);
-}
-
-
-void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
-{
-       bf_.insert(bf_.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());
 }
 
 
@@ -152,12 +62,6 @@ void MathArray::push_back(MathInset * p)
 }
 
 
-void MathArray::push_back(unsigned char b, MathTextCodes c)
-{
-       insert(size(), b, c);
-}
-
-
 void MathArray::push_back(MathArray const & array)
 {
        insert(size(), array);
@@ -183,7 +87,7 @@ bool MathArray::empty() const
 }
    
 
-int MathArray::size() const
+MathArray::size_type MathArray::size() const
 {
        return bf_.size();
 }
@@ -195,38 +99,36 @@ void MathArray::erase()
 }
 
 
-void MathArray::erase(int pos)
+void MathArray::erase(size_type pos)
 {
        if (pos < size())
-               bf_.erase(bf_.begin() + pos);
+               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 nextInset(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)
+       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 << ">";
 }
 
 
@@ -237,10 +139,40 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar)
 }
 
 
+// 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) {
+               p = it->nucleus()->asCharInset();
+               if (!p || it->up() || it->down() || p->code() != c)
+                       break;
+               s += p->getChar();
+       }
+       return s;
+}
+
+
 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) {
+               MathCharInset const * p = it->nucleus()->asCharInset();
+               if (p && !it->up() && !it->down()) {
+                       // special handling for character sequences with the same code
+                       string s = charSequence(it, end());
+                       p->writeHeader(os);
+                       os << s;
+                       p->writeTrailer(os);
+                       it += s.size() - 1;
+               } else {
+                       it->write(os, fragile);
+               }
+       }
 }
 
 
@@ -257,8 +189,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);
 }
 
 
@@ -268,7 +200,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();
+}