mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-13 14:32:04 +00:00
fcdb71906b
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7722 a592a061-630c-0410-9148-cb99ea01b6c8
169 lines
2.7 KiB
C
169 lines
2.7 KiB
C
/**
|
|
* \file math_iterator.C
|
|
* This file is part of LyX, the document processor.
|
|
* Licence details can be found in the file COPYING.
|
|
*
|
|
* \author André Pönitz
|
|
*
|
|
* Full author contact details are available in file CREDITS.
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
#include "math_iterator.h"
|
|
#include "math_inset.h"
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
|
MathIterator::MathIterator()
|
|
{}
|
|
|
|
|
|
MathIterator::MathIterator(MathInset * p)
|
|
{
|
|
push(p);
|
|
}
|
|
|
|
|
|
MathInset const * MathIterator::inset() const
|
|
{
|
|
return back().inset_;
|
|
}
|
|
|
|
|
|
MathInset * MathIterator::inset()
|
|
{
|
|
return back().inset_;
|
|
}
|
|
|
|
|
|
MathArray const & MathIterator::cell() const
|
|
{
|
|
CursorPos const & top = back();
|
|
return top.inset_->cell(top.idx_);
|
|
}
|
|
|
|
|
|
|
|
void MathIterator::push(MathInset * p)
|
|
{
|
|
//lyxerr << "push: " << p << endl;
|
|
push_back(CursorPos(p));
|
|
}
|
|
|
|
|
|
void MathIterator::pop()
|
|
{
|
|
//lyxerr << "pop: " << endl;
|
|
BOOST_ASSERT(size());
|
|
pop_back();
|
|
}
|
|
|
|
|
|
CursorPos const & MathIterator::operator*() const
|
|
{
|
|
return back();
|
|
}
|
|
|
|
|
|
CursorPos const & MathIterator::operator->() const
|
|
{
|
|
return back();
|
|
}
|
|
|
|
|
|
void MathIterator::goEnd()
|
|
{
|
|
CursorPos & top = back();
|
|
top.idx_ = top.inset_->nargs() - 1;
|
|
top.pos_ = cell().size();
|
|
}
|
|
|
|
|
|
void MathIterator::operator++()
|
|
{
|
|
CursorPos & top = back();
|
|
MathArray & ar = top.inset_->cell(top.idx_);
|
|
|
|
// move into the current inset if possible
|
|
// it is impossible for pos() == size()!
|
|
MathInset * n = 0;
|
|
if (top.pos_ != ar.size())
|
|
n = (ar.begin() + top.pos_)->nucleus();
|
|
if (n && n->isActive()) {
|
|
push(n);
|
|
return;
|
|
}
|
|
|
|
// otherwise move on one cell back if possible
|
|
if (top.pos_ < ar.size()) {
|
|
// pos() == size() is valid!
|
|
++top.pos_;
|
|
return;
|
|
}
|
|
|
|
// otherwise try to move on one cell if possible
|
|
while (top.idx_ + 1 < top.inset_->nargs()) {
|
|
// idx() == nargs() is _not_ valid!
|
|
++top.idx_;
|
|
if (top.inset_->validCell(top.idx_)) {
|
|
top.pos_ = 0;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// otherwise leave array, move on one back
|
|
// this might yield pos() == size(), but that's a ok.
|
|
pop();
|
|
// it certainly invalidates top
|
|
++back().pos_;
|
|
}
|
|
|
|
|
|
void MathIterator::jump(difference_type i)
|
|
{
|
|
back().pos_ += i;
|
|
//BOOST_ASSERT(back().pos_ >= 0);
|
|
BOOST_ASSERT(back().pos_ <= cell().size());
|
|
}
|
|
|
|
|
|
bool MathIterator::normal() const
|
|
{
|
|
return back().pos_ < cell().size();
|
|
}
|
|
|
|
|
|
void MathIterator::shrink(size_type i)
|
|
{
|
|
if (i < size())
|
|
erase(begin() + i, end());
|
|
}
|
|
|
|
|
|
bool operator==(MathIterator const & it, MathIterator const & jt)
|
|
{
|
|
return MathIterator::base_type(it) == MathIterator::base_type(jt);
|
|
}
|
|
|
|
|
|
bool operator!=(MathIterator const & it, MathIterator const & jt)
|
|
{
|
|
return MathIterator::base_type(it) != MathIterator::base_type(jt);
|
|
}
|
|
|
|
|
|
MathIterator ibegin(MathInset * p)
|
|
{
|
|
return MathIterator(p);
|
|
}
|
|
|
|
|
|
MathIterator iend(MathInset * p)
|
|
{
|
|
MathIterator it(p);
|
|
it.goEnd();
|
|
return it;
|
|
}
|