]> 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 eba96ab49546616a7ac21e63d35352959502626b..48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb 100644 (file)
-
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-#include "array.h"
-#include "math_iter.h"
 #include "math_inset.h"
+#include "math_charinset.h"
+#include "math_scriptinset.h"
+#include "math_stringinset.h"
+#include "debug.h"
+#include "array.h"
+#include "mathed/support.h"
+#include "support/LAssert.h"
+
+using std::ostream;
+using std::endl;
 
-// Is this still needed? (Lgb)
-static inline
-void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
-{
-       char * ps = static_cast<char *>(ps_in);
-       char const * pt = static_cast<char const *>(pt_in);
-       while (n--) *ps++ = *pt++;
-       return ps_in;
-}
+
+MathArray::MathArray()
+{}
 
 
-MathedArray::MathedArray()
-       : bf_(1, '\0'), last_(0)
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
+       : bf_(array.begin() + from, array.begin() + to)
 {}
 
 
-MathedArray::~MathedArray()
+void MathArray::substitute(MathMacro const & m)
 {
-       // deep destruction
-       // let's leak for a while... 
-/*
-       MathedIter it;
-       it.SetData(this);
-       while (it.OK()) {
-               if (it.IsInset()) {
-                       MathedInset * inset = it.GetInset();
-                       delete inset;
-               }
-               it.Next();
-       }
-*/
+       for (iterator it = begin(); it != end(); ++it)
+               it->nucleus()->substitute(m);
 }
 
 
-MathedArray::MathedArray(MathedArray const & array)
+MathScriptInset const * MathArray::asScript(const_iterator it) const
 {
-       // this "implementation" is obviously wrong: MathedIter should be
-       // implemented by MathedArray (not the other way round) but I think
-       // getting the _interface_ of MathedArray right is more important right
-       // now (Andre')
-
-       // shallow copy
-       bf_   = array.bf_;
-       last_ = array.last_;
-
-       // deep copy
-       // we'll not yet get exeption safety
-       MathedIter it(this);
-       while (it.OK()) {
-               if (it.IsInset()) {
-                       MathedInset * inset = it.GetInset();
-                       inset = inset->Clone();
-                       raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
-               }
-               it.Next();
-       }
+       if (it->nucleus()->asScriptInset())
+               return 0;
+       const_iterator jt = it + 1;
+       if (jt == end())
+               return 0;
+       return jt->nucleus()->asScriptInset();
 }
 
 
-MathedArray & MathedArray::operator=(MathedArray const & array)
+MathAtom & MathArray::at(size_type pos)
 {
-       MathedArray tmp(array);
-       swap(tmp);
-       return *this;
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
-void MathedArray::clear()
+
+MathAtom const & MathArray::at(size_type pos) const
 {
-       last_ = 0;
-       bf_.resize(1);
-       bf_[0] = 0;
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
-void MathedArray::swap(MathedArray & array)
+
+void MathArray::insert(size_type pos, MathAtom const & t)
 {
-       if (this != &array) {
-               bf_.swap(array.bf_);
-               std::swap(last_, array.last_);
-       }
+       bf_.insert(begin() + pos, t);
 }
 
 
-MathedArray::iterator MathedArray::begin() 
+void MathArray::insert(size_type pos, MathArray const & array)
 {
-       return bf_.begin();
+       bf_.insert(begin() + pos, array.begin(), array.end());
+}
+
+
+void MathArray::push_back(MathAtom const & t)
+{      
+       bf_.push_back(t);
 }
 
 
-MathedArray::iterator MathedArray::end() 
+void MathArray::push_back(MathArray const & array)
 {
-       return bf_.end();
+       insert(size(), array);
 }
 
 
-MathedArray::const_iterator MathedArray::begin() const
+void MathArray::clear()
 {
-       return bf_.begin();
+       erase();
 }
 
 
-MathedArray::const_iterator MathedArray::end() const
+void MathArray::swap(MathArray & array)
 {
-       return bf_.end();
+       if (this != &array) 
+               bf_.swap(array.bf_);
 }
 
 
-int MathedArray::empty() const
+bool MathArray::empty() const
 {
-       return (last_ == 0);
+       return bf_.empty();
 }
-   
 
-int MathedArray::last() const
+
+MathArray::size_type MathArray::size() const
 {
-       return last_;
+       return bf_.size();
 }
 
 
-void MathedArray::last(int l)
+void MathArray::erase()
 {
-       last_ = l;
+       erase(0, size());
 }
 
 
-void MathedArray::need_size(int needed)
+void MathArray::erase(size_type pos)
 {
-       if (needed >= static_cast<int>(bf_.size()))
-               resize(needed);
+       if (pos < size())
+               erase(pos, pos + 1);
 }
 
 
-void MathedArray::resize(int newsize)
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       // still a bit smelly...
-       ++newsize;
-       bf_.resize(newsize + 1);
-       if (last_ >= newsize)
-               last_ = newsize - 1;
-       bf_[last_] = 0;
+       bf_.erase(begin() + pos1, begin() + pos2);
 }
 
 
-void MathedArray::move(int p, int shift)
+MathAtom & MathArray::back()
 {
-       if (p <= last_) {
-               need_size(last_ + shift);
-               memmove(&bf_[p + shift], &bf_[p], last_ - p);
-               last_ += shift;
-               bf_[last_] = 0;
-       }
+       return bf_.back();
 }
 
 
+void MathArray::dump2(ostream & os) const
+{
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << it->nucleus() << ' ';
+}
+
 
-void MathedArray::shrink(int pos1, int pos2)
+void MathArray::dump(ostream & os) const
 {
-       if (pos1 == 0 && pos2 >= last())        
-               return;
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << "<" << it->nucleus() << ">";
+}
 
-       short fc = 0;
-       if (pos1 > 0 && bf_[pos1] > ' ') {
-               for (int p = pos1; p >= 0; --p) {
-                       if (MathIsFont(bf_[p])) {
-                               if (p != pos1 - 1)
-                                       fc = bf_[p];
-                               else
-                                       --pos1;
-                               break;
-                       }
-               }
-       }
 
-       if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
-               --pos2;
+std::ostream & operator<<(std::ostream & os, MathArray const & ar)
+{
+       ar.dump2(os);
+       return os;
+}
+
 
-       int dx = pos2 - pos1;
-       MathedArray a;
-       a.resize(dx + 1);
-       strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
-       if (fc) {
-               a[0] = fc;
-               ++dx;
+// 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) {
+               if (!it->nucleus())
+                       break;
+               p = it->nucleus()->asCharInset();
+               if (!p || p->code() != c)
+                       break;
+               s += p->getChar();
        }
-       a.last(dx);
-       a[dx] = '\0';
+       return s;
+}
 
-       MathedIter it(&a);
-       it.Reset();
 
-       while (it.OK()) {
-               if (it.IsInset()) {
-                       MathedInset * inset = it.GetInset();
-                       inset = inset->Clone();
-                       a.raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
+MathArray MathArray::glueChars() const
+{
+       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;
                }
-               it.Next();
        }
-       swap(a);
+       return ar;
 }
 
 
-#if 0
-void MathedArray::insert(MathedArray::iterator pos,
-                        MathedArray::const_iterator beg,
-                        MathedArray::const_iterator end)
+void MathArray::write(MathWriteInfo & wi) const
 {
-       bf_.insert(pos, beg, end);
-       last_ = bf_.size() - 1;
+       glueChars().write1(wi);
 }
-#else
-void MathedArray::merge(MathedArray const & a, int p)
+
+
+void MathArray::write1(MathWriteInfo & wi) const
 {
-       my_memcpy(&bf_[p], &a.bf_[0], a.last());
+       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);
+               }
+       }
 }
-#endif
 
 
-void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
+void MathArray::writeNormal(ostream & os) const
 {
-       my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
+       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);
+               }
+       }
 }
 
 
-#if 0
-void MathedArray::insertInset(int pos, MathedInset * p, int type)
+void MathArray::validate(LaTeXFeatures & features) const
 {
-       //bf_.insert(pos, type);
-       InsetTable tmp(pos, p);
-       insetList_.push_back(tmp);
+       for (const_iterator it = begin(); it != end(); ++it)
+               it->nucleus()->validate(features);
 }
 
 
-MathedInset * MathedArray::getInset(int pos) 
-{
-       InsetList::const_iterator cit = insetList_.begin();
-       InsetList::const_iterator end = insetList_.end();
-       for (; cit != end; ++cit) {
-               if ((*cit).pos == pos)
-                       return (*cit).inset;
+void MathArray::pop_back()
+{      
+       if (!size()) {
+               lyxerr << "pop_back from empty array!\n";
+               return;
        }
-       // not found
-       return 0;
-       // We would really like to throw an exception instead... (Lgb)
-       // throw inset_not_found();
+       bf_.pop_back();
 }
 
-#else
-void MathedArray::raw_pointer_insert(void * p, int pos, int len)
+
+MathArray::const_iterator MathArray::begin() const
 {
-       my_memcpy(&bf_[pos], &p, len);
+       return bf_.begin();
 }
-#endif
 
 
-void MathedArray::strange_copy(MathedArray * dest, int dpos,
-                               int spos, int len)
+MathArray::const_iterator MathArray::end() const
 {
-       my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
+       return bf_.end();
 }
 
 
-byte MathedArray::operator[](int i) const
+MathArray::iterator MathArray::begin()
 {
-       return bf_[i];
+       return bf_.begin();
 }
 
 
-byte & MathedArray::operator[](int i)
+MathArray::iterator MathArray::end()
 {
-       return bf_[i];
+       return bf_.end();
 }