2001-12-05 17:50:18 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "math_iterator.h"
|
2001-12-10 10:09:00 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "support/LAssert.h"
|
2001-12-05 17:50:18 +00:00
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
//MathIterator::MathIterator()
|
|
|
|
//{}
|
2001-12-05 17:50:18 +00:00
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
MathIterator::MathIterator(MathAtom & t)
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
push(t);
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathIterator::MathIterator(MathCursor::cursor_type const & c)
|
|
|
|
: cursor_(c)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursorPos const & MathIterator::position() const
|
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
lyx::Assert(cursor_.size());
|
2001-12-05 17:50:18 +00:00
|
|
|
return cursor_.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursorPos & MathIterator::position()
|
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
lyx::Assert(cursor_.size());
|
2001-12-05 17:50:18 +00:00
|
|
|
return cursor_.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursor::cursor_type const & MathIterator::cursor() const
|
|
|
|
{
|
|
|
|
return cursor_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
MathAtom const & MathIterator::par() const
|
|
|
|
{
|
|
|
|
return *(position().par_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom & MathIterator::par()
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
return *(position().par_);
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathXArray const & MathIterator::xcell() const
|
|
|
|
{
|
|
|
|
return par()->xcell(position().idx_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
MathAtom * MathIterator::nextInset() const
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
|
|
|
if (position().pos_ == xcell().data_.size())
|
|
|
|
return 0;
|
2001-12-10 10:09:00 +00:00
|
|
|
return const_cast<MathAtom *>(xcell().begin() + position().pos_);
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
void MathIterator::push(MathAtom & t)
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
|
|
|
//lyxerr << "push: " << p << endl;
|
2001-12-10 10:09:00 +00:00
|
|
|
cursor_.push_back(MathCursorPos(t));
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathIterator::pop()
|
|
|
|
{
|
|
|
|
//lyxerr << "pop: " << endl;
|
2001-12-10 10:09:00 +00:00
|
|
|
lyx::Assert(cursor_.size());
|
2001-12-05 17:50:18 +00:00
|
|
|
cursor_.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursorPos const & MathIterator::operator*() const
|
|
|
|
{
|
|
|
|
return position();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursorPos const & MathIterator::operator->() const
|
|
|
|
{
|
|
|
|
return position();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
void MathIterator::goEnd()
|
|
|
|
{
|
|
|
|
position().idx_ = par()->nargs() - 1;
|
|
|
|
position().pos_ = xcell().data_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-05 17:50:18 +00:00
|
|
|
void MathIterator::operator++()
|
|
|
|
{
|
|
|
|
// move into the current inset if possible
|
|
|
|
// it is impossible for pos() == size()!
|
2001-12-10 10:09:00 +00:00
|
|
|
if (nextInset() && nextInset()->nucleus()->isActive()) {
|
|
|
|
push(*nextInset());
|
2001-12-05 17:50:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move on one cell position if possible
|
|
|
|
if (position().pos_ < xcell().data_.size()) {
|
|
|
|
// pos() == size() is valid!
|
|
|
|
++position().pos_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move on one cell if possible
|
|
|
|
if (position().idx_ + 1 < par()->nargs()) {
|
|
|
|
// idx() == nargs() is _not_ valid!
|
|
|
|
++position().idx_;
|
|
|
|
position().pos_ = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
// otherwise leave array, move on one position
|
2001-12-05 17:50:18 +00:00
|
|
|
// this might yield pos() == size(), but that's a ok.
|
|
|
|
pop();
|
|
|
|
++position().pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(MathIterator const & it, MathIterator const & jt)
|
|
|
|
{
|
|
|
|
//lyxerr << "==: " << it.cursor().size() << " " << jt.cursor().size() << endl;
|
|
|
|
if (it.cursor().size() != jt.cursor().size())
|
|
|
|
return false;
|
|
|
|
return it.cursor() == jt.cursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(MathIterator const & it, MathIterator const & jt)
|
|
|
|
{
|
|
|
|
//lyxerr << "!=: " << it.cursor().size() << " " << jt.cursor().size() << endl;
|
|
|
|
if (it.cursor().size() != jt.cursor().size())
|
|
|
|
return true;
|
|
|
|
return it.cursor() != jt.cursor();
|
|
|
|
}
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
MathIterator ibegin(MathAtom & t)
|
|
|
|
{
|
|
|
|
return MathIterator(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathIterator iend(MathAtom & t)
|
|
|
|
{
|
|
|
|
MathIterator it(t);
|
|
|
|
it.goEnd();
|
|
|
|
return it;
|
|
|
|
}
|