]> git.lyx.org Git - features.git/blobdiff - src/mathed/array.C
write \mathrm{x}\mathrm{y} as \mathrm{xy} again
[features.git] / src / mathed / array.C
index 9ba5cb6af162a722bfea7be064363a8bb6608d35..48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb 100644 (file)
 
 #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());
-}
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
+       : bf_(array.begin() + from, array.begin() + to)
+{}
 
 
-void MathArray::deep_copy(int pos1, int pos2)
+void MathArray::substitute(MathMacro const & m)
 {
-       for (int pos = pos1; pos < pos2; ++pos) 
-               bf_[pos] = bf_[pos]->clone();
+       for (iterator it = begin(); it != end(); ++it)
+               it->nucleus()->substitute(m);
 }
 
 
-bool MathArray::next(int & pos) const
+MathScriptInset const * MathArray::asScript(const_iterator it) const
 {
-       if (pos >= size() - 1)
-               return false;
-
-       ++pos;
-       return true;
+       if (it->nucleus()->asScriptInset())
+               return 0;
+       const_iterator jt = it + 1;
+       if (jt == end())
+               return 0;
+       return jt->nucleus()->asScriptInset();
 }
 
 
-bool MathArray::prev(int & pos) const
+MathAtom & MathArray::at(size_type pos)
 {
-       if (pos == 0)
-               return false;
-
-       --pos;
-       return true;
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
 
-bool MathArray::last(int & pos) const
+MathAtom const & MathArray::at(size_type pos) const
 {
-       pos = bf_.size();
-       return prev(pos);
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
 
-void MathArray::substitute(MathMacro const & m)
+void MathArray::insert(size_type pos, MathAtom const & t)
 {
-       MathArray tmp;
-       for (int pos = 0; pos < size(); ++pos) 
-               bf_[pos]->substitute(tmp, m);
-       swap(tmp);
+       bf_.insert(begin() + pos, t);
 }
 
 
-MathArray & MathArray::operator=(MathArray const & array)
+void MathArray::insert(size_type pos, MathArray const & array)
 {
-       MathArray tmp(array);
-       swap(tmp);
-       return *this;
+       bf_.insert(begin() + pos, array.begin(), array.end());
 }
 
 
-MathInset * MathArray::nextInset(int pos) const
-{
-       return (pos == size()) ? 0 : bf_[pos];
+void MathArray::push_back(MathAtom const & t)
+{      
+       bf_.push_back(t);
 }
 
 
-MathInset * MathArray::prevInset(int pos) const
+void MathArray::push_back(MathArray const & array)
 {
-       return (pos == 0) ? 0 : bf_[pos - 1];
+       insert(size(), array);
 }
 
 
-unsigned char MathArray::getChar(int pos) const
+void MathArray::clear()
 {
-       return (pos == size()) ? 0 : (bf_[pos]->getChar());
+       erase();
 }
 
 
-/*
-string MathArray::getString(int & pos) const
+void MathArray::swap(MathArray & 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;
+       if (this != &array) 
+               bf_.swap(array.bf_);
 }
-*/
 
 
-MathTextCodes MathArray::getCode(int pos) const
+bool MathArray::empty() const
 {
-       return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN;
+       return bf_.empty();
 }
 
 
-void MathArray::setCode(int pos, MathTextCodes t)
+MathArray::size_type MathArray::size() const
 {
-       bf_[pos]->code(t);
+       return bf_.size();
 }
 
 
-void MathArray::insert(int pos, MathInset * p)
+void MathArray::erase()
 {
-       bf_.insert(bf_.begin() + pos, p);
+       erase(0, size());
 }
 
 
-void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
+void MathArray::erase(size_type pos)
 {
-       bf_.insert(bf_.begin() + pos, new MathCharInset(b, t));
+       if (pos < size())
+               erase(pos, pos + 1);
 }
 
 
-void MathArray::insert(int pos, MathArray const & array)
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
-       deep_copy(pos, pos + array.size());
+       bf_.erase(begin() + pos1, begin() + pos2);
 }
 
 
-void MathArray::push_back(MathInset * p)
-{      
-       insert(size(), p);
-}
-
-
-void MathArray::push_back(unsigned char b, MathTextCodes c)
+MathAtom & MathArray::back()
 {
-       insert(size(), b, c);
+       return bf_.back();
 }
 
 
-void MathArray::push_back(MathArray const & array)
+void MathArray::dump2(ostream & os) const
 {
-       insert(size(), array);
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << it->nucleus() << ' ';
 }
 
 
-void MathArray::clear()
+void MathArray::dump(ostream & os) const
 {
-       erase();
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << "<" << it->nucleus() << ">";
 }
 
 
-void MathArray::swap(MathArray & array)
+std::ostream & operator<<(std::ostream & os, MathArray const & ar)
 {
-       if (this != &array) 
-               bf_.swap(array.bf_);
+       ar.dump2(os);
+       return os;
 }
 
 
-bool MathArray::empty() 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)
 {
-       return bf_.empty();
-}
-   
+       string s;
+       MathCharInset const * p = it->nucleus()->asCharInset();
+       if (!p)
+               return s;
 
-int MathArray::size() const
-{
-       return bf_.size();
+       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::erase()
+MathArray MathArray::glueChars() const
 {
-       erase(0, size());
+       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 MathArray::erase(int pos)
+void MathArray::write(MathWriteInfo & wi) const
 {
-       if (pos < size())
-               bf_.erase(bf_.begin() + pos);
+       glueChars().write1(wi);
 }
 
 
-void MathArray::erase(int pos1, int pos2)
+void MathArray::write1(MathWriteInfo & wi) const
 {
-       for (int pos = pos1; pos < pos2; ++pos)
-               delete nextInset(pos);
-       bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
+       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);
+               }
+       }
 }
 
 
-MathInset * MathArray::back() const
+void MathArray::writeNormal(ostream & os) const
 {
-       return prevInset(size());
+       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::dump2(ostream & os) const
+void MathArray::validate(LaTeXFeatures & features) const
 {
-       for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
-               os << int(*it) << ' ';
-       os << endl;
+       for (const_iterator it = begin(); it != end(); ++it)
+               it->nucleus()->validate(features);
 }
 
 
-void MathArray::dump(ostream & os) const
-{
-       for (int pos = 0; pos < size(); ++pos)
-               os << "<" << nextInset(pos) << ">";
+void MathArray::pop_back()
+{      
+       if (!size()) {
+               lyxerr << "pop_back from empty array!\n";
+               return;
+       }
+       bf_.pop_back();
 }
 
 
-std::ostream & operator<<(std::ostream & os, MathArray const & ar)
+MathArray::const_iterator MathArray::begin() const
 {
-       ar.dump2(os);
-       return os;
+       return bf_.begin();
 }
 
 
-void MathArray::write(ostream & os, bool fragile) const
+MathArray::const_iterator MathArray::end() const
 {
-       for (int pos = 0; pos < size(); ++pos)
-               nextInset(pos)->write(os, fragile);
+       return bf_.end();
 }
 
 
-void MathArray::writeNormal(ostream & os) const
+MathArray::iterator MathArray::begin()
 {
-       if (empty()) {
-               os << "[par] ";
-               return;
-       }
-
-       write(os, true);
+       return bf_.begin();
 }
 
 
-void MathArray::validate(LaTeXFeatures & features) const
+MathArray::iterator MathArray::end()
 {
-       for (int pos = 0; pos < size(); ++pos)
-               nextInset(pos)->validate(features);
+       return bf_.end();
 }
-
-
-void MathArray::pop_back()
-{      
-       int pos = size();
-       prev(pos);
-       erase(pos, size());
-}
-