]> git.lyx.org Git - features.git/blob - src/mathed/math_iterator.C
e06157c864bfca74ad0c42029a66347dac85d1e8
[features.git] / src / mathed / math_iterator.C
1 /**
2  * \file math_iterator.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_iterator.h"
14 #include "math_inset.h"
15 #include "math_data.h"
16
17 #include <boost/assert.hpp>
18
19
20 MathArray const & MathIterator::cell() const
21 {
22         CursorSlice const & top = back();
23         return top.asMathInset()->cell(top.idx_);
24 }
25
26
27 void MathIterator::operator++()
28 {
29         CursorSlice & top = back();
30         MathArray   & ar  = top.asMathInset()->cell(top.idx_);
31
32         // move into the current inset if possible
33         // it is impossible for pos() == size()!
34         MathInset * n = 0;
35         if (top.pos_ != ar.size())
36                 n = (ar.begin() + top.pos_)->nucleus();
37         if (n && n->isActive()) {
38                 push_back(CursorSlice(n));
39                 return;
40         }
41
42         // otherwise move on one cell back if possible
43         if (top.pos_ < ar.size()) {
44                 // pos() == size() is valid!
45                 ++top.pos_;
46                 return;
47         }
48
49         // otherwise try to move on one cell if possible
50         while (top.idx_ + 1 < top.asMathInset()->nargs()) {
51                 // idx() == nargs() is _not_ valid!
52                 ++top.idx_;
53                 if (top.asMathInset()->validCell(top.idx_)) {
54                         top.pos_ = 0;
55                         return;
56                 }
57         }
58
59         // otherwise leave array, move on one back
60         // this might yield pos() == size(), but that's a ok.
61         pop_back();
62         // it certainly invalidates top
63         ++back().pos_;
64 }
65
66
67 bool operator==(MathIterator const & it, MathIterator const & jt)
68 {
69         return MathIterator::base_type(it) == MathIterator::base_type(jt);
70 }
71
72
73 bool operator!=(MathIterator const & it, MathIterator const & jt)
74 {
75         return MathIterator::base_type(it) != MathIterator::base_type(jt);
76 }
77
78
79 MathIterator ibegin(MathInset * p)
80 {
81         MathIterator it;
82         it.push_back(CursorSlice(p));
83         return it;
84 }
85
86
87 MathIterator iend(MathInset * p)
88 {
89         MathIterator it;
90         it.push_back(CursorSlice(p));
91         return it;
92         CursorSlice & top = it.back();
93         top.idx_ = top.asMathInset()->nargs() - 1;
94         top.pos_ = it.cell().size();
95         return it;
96 }