-
-#include <config.h>
-
#ifdef __GNUG__
#pragma implementation
#endif
+#include "math_inset.h"
+#include "math_charinset.h"
+#include "math_scriptinset.h"
+#include "debug.h"
#include "array.h"
+#include "mathed/support.h"
+
+using std::ostream;
+using std::endl;
+
+
+MathArray::MathArray()
+{}
+
+
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
+ : bf_(array.begin() + from, array.begin() + to)
+{}
-// Is this still needed? (Lgb)
-static inline
-void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
+
+void MathArray::substitute(MathMacro const & m)
{
- char * ps = static_cast<char *>(ps_in);
- char const * pt = static_cast<char const *>(pt_in);
- while (n--) *ps++ = *pt++;
- return ps_in;
+ for (iterator it = begin(); it != end(); ++it)
+ it->substitute(m);
}
-MathedArray::MathedArray(int size)
- : last_(0)
+MathAtom * MathArray::at(size_type pos)
{
- int const newsize = (size < ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE : size;
- bf_.resize(newsize);
+ return pos < size() ? &bf_[pos] : 0;
}
-MathedArray::iterator MathedArray::begin()
+MathAtom const * MathArray::at(size_type pos) const
{
- return bf_.begin();
+ return pos < size() ? &bf_[pos] : 0;
}
-MathedArray::iterator MathedArray::end()
+void MathArray::insert(size_type pos, MathInset * p)
{
- return bf_.end();
+ //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";
}
-MathedArray::const_iterator MathedArray::begin() const
+void MathArray::insert(size_type pos, MathArray const & array)
{
- return bf_.begin();
+ bf_.insert(begin() + pos, array.begin(), array.end());
}
-MathedArray::const_iterator MathedArray::end() const
+void MathArray::push_back(MathInset * p)
+{
+ insert(size(), p);
+}
+
+
+void MathArray::push_back(MathArray const & array)
{
- return bf_.end();
+ insert(size(), array);
+}
+
+
+void MathArray::clear()
+{
+ erase();
}
-int MathedArray::empty() const
+void MathArray::swap(MathArray & array)
{
- return (last_ == 0);
+ if (this != &array)
+ bf_.swap(array.bf_);
+}
+
+
+bool MathArray::empty() const
+{
+ 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)
{
- if (newsize < ARRAY_MIN_SIZE)
- newsize = ARRAY_MIN_SIZE;
- newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
- bf_.resize(newsize);
- 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();
}
-#if 0
-void MathedArray::insert(MathedArray::iterator pos,
- MathedArray::const_iterator beg,
- MathedArray::const_iterator end)
+void MathArray::dump2(ostream & os) const
{
- bf_.insert(pos, beg, end);
- last_ = bf_.size() - 1;
+ for (const_iterator it = begin(); it != end(); ++it)
+ os << *it << ' ';
}
-#else
-void MathedArray::mergeF(MathedArray * a, int p, int dx)
+
+
+void MathArray::dump(ostream & os) const
{
- my_memcpy(&bf_[p], &a->bf_[0], dx);
+ for (const_iterator it = begin(); it != end(); ++it)
+ os << "<" << *it << ">";
+}
+
+
+std::ostream & operator<<(std::ostream & os, MathArray const & ar)
+{
+ ar.dump2(os);
+ return os;
}
-#endif
-void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) 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)
{
- my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
+ 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;
}
-#if 0
-void MathedArray::insertInset(int pos, MathedInset * p, int type)
+void MathArray::write(ostream & os, bool fragile) const
{
- //bf_.insert(pos, type);
- InsetTable tmp(pos, p);
- insetList_.push_back(tmp);
+ 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);
+ }
+ }
}
-MathedInset * MathedArray::getInset(int pos)
+void MathArray::writeNormal(ostream & os) const
{
- InsetList::const_iterator cit = insetList_.begin();
- InsetList::const_iterator end = insetList_.end();
- for (; cit != end; ++cit) {
- if ((*cit).pos == pos)
- return (*cit).inset;
+ if (empty()) {
+ os << "[par] ";
+ return;
}
- // not found
- return 0;
- // We would really like to throw an exception instead... (Lgb)
- // throw inset_not_found();
+
+ write(os, true);
}
-#else
-void MathedArray::raw_pointer_insert(void * p, int pos, int len)
+
+void MathArray::validate(LaTeXFeatures & features) const
{
- my_memcpy(&bf_[pos], &p, len);
+ for (const_iterator it = begin(); it != end(); ++it)
+ it->validate(features);
}
-#endif
-void MathedArray::strange_copy(MathedArray * dest, int dpos,
- int spos, int len)
+void MathArray::pop_back()
+{
+ if (!size()) {
+ lyxerr << "pop_back from empty array!\n";
+ return;
+ }
+ bf_.pop_back();
+}
+
+
+MathArray::const_iterator MathArray::begin() const
+{
+ return bf_.begin();
+}
+
+
+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();
}