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-11 15:35:18 +00:00
|
|
|
MathIterator::MathIterator(MathInset * p)
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
push(p);
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
//MathIterator::MathIterator(MathCursor::cursor_type const & c)
|
|
|
|
// : cursor_(c)
|
|
|
|
//{}
|
2001-12-05 17:50:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-11 15:35:18 +00:00
|
|
|
MathInset const * MathIterator::par() const
|
2001-12-10 10:09:00 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
return position().par_;
|
2001-12-10 10:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
MathInset * MathIterator::par()
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
return position().par_;
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathXArray const & MathIterator::xcell() const
|
|
|
|
{
|
|
|
|
return par()->xcell(position().idx_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-03 09:41:26 +00:00
|
|
|
MathArray const & MathIterator::cell() const
|
|
|
|
{
|
|
|
|
return par()->xcell(position().idx_).data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
MathInset * MathIterator::nextInset() const
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
|
|
|
if (position().pos_ == xcell().data_.size())
|
|
|
|
return 0;
|
2001-12-11 15:35:18 +00:00
|
|
|
return (xcell().begin() + position().pos_)->nucleus();
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
void MathIterator::push(MathInset * p)
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
|
|
|
//lyxerr << "push: " << p << endl;
|
2001-12-11 15:35:18 +00:00
|
|
|
cursor_.push_back(MathCursorPos(p));
|
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-11 15:35:18 +00:00
|
|
|
if (nextInset() && nextInset()->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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-03 09:41:26 +00:00
|
|
|
void MathIterator::jump(int i)
|
|
|
|
{
|
|
|
|
position().pos_ += i;
|
|
|
|
lyx::Assert(position().pos_ >= 0);
|
|
|
|
lyx::Assert(position().pos_ <= cell().size());
|
|
|
|
}
|
|
|
|
|
2001-12-05 17:50:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
MathIterator ibegin(MathInset * p)
|
2001-12-10 10:09:00 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
return MathIterator(p);
|
2001-12-10 10:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
MathIterator iend(MathInset * p)
|
2001-12-10 10:09:00 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
MathIterator it(p);
|
2001-12-10 10:09:00 +00:00
|
|
|
it.goEnd();
|
|
|
|
return it;
|
|
|
|
}
|