#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()
-{
- erase();
-}
-
-
-MathArray::MathArray(MathArray const & array)
- : bf_(array.bf_)
-{
- deep_copy(begin(), end());
-}
-
-
-MathArray::MathArray(MathArray const & array, int from, int to)
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
: bf_(array.begin() + from, array.begin() + to)
-{
- deep_copy(begin(), end());
-}
-
-
-void MathArray::deep_copy(iterator from, iterator to)
-{
- for (iterator it = from; it != to; ++it)
- *it = (*it)->clone();
-}
-
-
-int MathArray::last() const
-{
- return size() - 1;
-}
+{}
void MathArray::substitute(MathMacro const & m)
{
- MathArray tmp;
for (iterator it = begin(); it != end(); ++it)
- (*it)->substitute(tmp, m);
- swap(tmp);
-}
-
-
-MathArray & MathArray::operator=(MathArray const & array)
-{
- MathArray tmp(array);
- swap(tmp);
- return *this;
+ 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;
}
-void MathArray::insert(int pos, MathInset * p)
+void MathArray::insert(size_type pos, MathInset * p)
{
- bf_.insert(begin() + pos, p);
+ //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";
}
-void MathArray::insert(int pos, MathArray const & array)
+void MathArray::insert(size_type pos, MathArray const & array)
{
bf_.insert(begin() + pos, array.begin(), array.end());
- deep_copy(begin() + pos, begin() + pos + array.size());
}
}
-int MathArray::size() const
+MathArray::size_type MathArray::size() const
{
return bf_.size();
}
}
-void MathArray::erase(int pos)
+void MathArray::erase(size_type pos)
{
if (pos < size())
erase(pos, pos + 1);
}
-void MathArray::erase(int pos1, int pos2)
+void MathArray::erase(size_type pos1, size_type pos2)
{
- for (iterator it = begin() + pos1; it != begin() + pos2; ++it)
- delete *it;
bf_.erase(begin() + pos1, begin() + pos2);
}
-MathInset * MathArray::back() const
+MathAtom & MathArray::back()
{
- return size() ? bf_.back() : 0;
+ return bf_.back();
}
return os;
}
+
// 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)->asCharInset();
+ MathCharInset const * p = it->nucleus()->asCharInset();
if (!p)
return s;
- MathTextCodes c = p->code();
- while (it != end && (p = (*it)->asCharInset()) && p->code() == c) {
+ 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();
- ++it;
}
return s;
}
void MathArray::write(ostream & os, bool fragile) const
{
- for (const_iterator it = begin(); it != end(); ) {
- MathCharInset const * p = (*it)->asCharInset();
- if (p) {
+ 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();
+ it += s.size() - 1;
} else {
- (*it)->write(os, fragile);
- ++it;
+ it->write(os, fragile);
}
}
}
void MathArray::validate(LaTeXFeatures & features) const
{
for (const_iterator it = begin(); it != end(); ++it)
- (*it)->validate(features);
+ it->validate(features);
}
lyxerr << "pop_back from empty array!\n";
return;
}
- delete back();
bf_.pop_back();
}