]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_iterator.C
drop another function unknown to the outer world...
[lyx.git] / src / mathed / math_iterator.C
index 3d6406c799c2faa4f9b33aafff4db283ff77a6e4..8c11cad1196d5d6e76f1519ccec6b79336414807 100644 (file)
@@ -1,13 +1,24 @@
+/**
+ * \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 "debug.h"
-#include "support/LAssert.h"
+#include "math_inset.h"
+#include "math_data.h"
 
+#include <boost/assert.hpp>
 
-//MathIterator::MathIterator()
-//{}
+
+MathIterator::MathIterator()
+{}
 
 
 MathIterator::MathIterator(MathInset * p)
@@ -16,151 +27,100 @@ MathIterator::MathIterator(MathInset * p)
 }
 
 
-//MathIterator::MathIterator(MathCursor::cursor_type const & c)
-//     : cursor_(c)
-//{}
-
-
-MathCursorPos const & MathIterator::position() const
-{
-       lyx::Assert(cursor_.size());
-       return cursor_.back();
-}
-
-
-MathCursorPos & MathIterator::position()
-{
-       lyx::Assert(cursor_.size());
-       return cursor_.back();
-}
-
-
-MathCursor::cursor_type const & MathIterator::cursor() const
-{
-       return cursor_;
-}
-
-
-MathInset const * MathIterator::par() const
-{
-       return position().par_;
-}
-
-
-MathInset * MathIterator::par()
-{
-       return position().par_;
-}
-
-
-MathXArray const & MathIterator::xcell() const
-{
-       return par()->xcell(position().idx_);
-}
-
 
 MathArray const & MathIterator::cell() const
 {
-       return par()->xcell(position().idx_).data_;
+       CursorSlice const & top = back();
+       return top.asMathInset()->cell(top.idx_);
 }
 
 
-MathInset * MathIterator::nextInset() const
-{
-       if (position().pos_ == xcell().data_.size())
-               return 0;
-       return (xcell().begin() + position().pos_)->nucleus();
-}
-
 
 void MathIterator::push(MathInset * p)
 {
        //lyxerr << "push: " << p << endl;
-       cursor_.push_back(MathCursorPos(p));
+       push_back(CursorSlice(p));
 }
 
 
 void MathIterator::pop()
 {
        //lyxerr << "pop: " << endl;
-       lyx::Assert(cursor_.size());
-       cursor_.pop_back();
-}
-
-
-MathCursorPos const & MathIterator::operator*() const
-{
-       return position();
-}
-
-
-MathCursorPos const & MathIterator::operator->() const
-{
-       return position();
+       BOOST_ASSERT(size());
+       pop_back();
 }
 
 
 void MathIterator::goEnd()
 {
-       position().idx_ = par()->nargs() - 1;
-       position().pos_ = xcell().data_.size();
+       CursorSlice & top = back();
+       top.idx_ = top.asMathInset()->nargs() - 1;
+       top.pos_ = cell().size();
 }
 
 
 void MathIterator::operator++()
 {
+       CursorSlice & top = back();
+       MathArray   & ar  = top.asMathInset()->cell(top.idx_);
+
        // move into the current inset if possible
        // it is impossible for pos() == size()!
-       if (nextInset() && nextInset()->isActive()) {
-               push(nextInset());
+       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 position if possible
-       if (position().pos_ < xcell().data_.size()) {
+       // otherwise move on one cell back if possible
+       if (top.pos_ < ar.size()) {
                // pos() == size() is valid!
-               ++position().pos_;
+               ++top.pos_;
                return;
        }
 
-       // otherwise move on one cell if possible
-       if (position().idx_ + 1 < par()->nargs()) {
+       // otherwise try to move on one cell if possible
+       while (top.idx_ + 1 < top.asMathInset()->nargs()) {
                // idx() == nargs() is _not_ valid!
-               ++position().idx_;
-               position().pos_ = 0;
-               return;
+               ++top.idx_;
+               if (top.asMathInset()->validCell(top.idx_)) {
+                       top.pos_ = 0;
+                       return;
+               }
        }
 
-       // otherwise leave array, move on one position
+       // otherwise leave array, move on one back
        // this might yield pos() == size(), but that's a ok.
        pop();
-       ++position().pos_;
+       // it certainly invalidates top
+       ++back().pos_;
+}
+
+
+bool MathIterator::normal() const
+{
+       return back().pos_ < cell().size();
 }
 
 
-void MathIterator::jump(MathInset::difference_type i)
+void MathIterator::shrink(size_type i)
 {
-       position().pos_ += i;
-       //lyx::Assert(position().pos_ >= 0);
-       lyx::Assert(position().pos_ <= cell().size());
+       if (i < size())
+               erase(begin() + i, end());
 }
 
 
 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();      
+       return MathIterator::base_type(it) == MathIterator::base_type(jt);
 }
 
 
 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();      
+       return MathIterator::base_type(it) != MathIterator::base_type(jt);
 }