]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
remove unneeded inset() member function
[lyx.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 MathIterator::MathIterator()
21 {}
22
23
24 MathIterator::MathIterator(MathInset * p)
25 {
26         push(p);
27 }
28
29
30
31 MathArray const & MathIterator::cell() const
32 {
33         CursorSlice const & top = back();
34         return top.asMathInset()->cell(top.idx_);
35 }
36
37
38
39 void MathIterator::push(MathInset * p)
40 {
41         //lyxerr << "push: " << p << endl;
42         push_back(CursorSlice(p));
43 }
44
45
46 void MathIterator::pop()
47 {
48         //lyxerr << "pop: " << endl;
49         BOOST_ASSERT(size());
50         pop_back();
51 }
52
53
54 CursorSlice const & MathIterator::operator*() const
55 {
56         return back();
57 }
58
59
60 CursorSlice const & MathIterator::operator->() const
61 {
62         return back();
63 }
64
65
66 void MathIterator::goEnd()
67 {
68         CursorSlice & top = back();
69         top.idx_ = top.asMathInset()->nargs() - 1;
70         top.pos_ = cell().size();
71 }
72
73
74 void MathIterator::operator++()
75 {
76         CursorSlice & top = back();
77         MathArray   & ar  = top.asMathInset()->cell(top.idx_);
78
79         // move into the current inset if possible
80         // it is impossible for pos() == size()!
81         MathInset * n = 0;
82         if (top.pos_ != ar.size())
83                 n = (ar.begin() + top.pos_)->nucleus();
84         if (n && n->isActive()) {
85                 push(n);
86                 return;
87         }
88
89         // otherwise move on one cell back if possible
90         if (top.pos_ < ar.size()) {
91                 // pos() == size() is valid!
92                 ++top.pos_;
93                 return;
94         }
95
96         // otherwise try to move on one cell if possible
97         while (top.idx_ + 1 < top.asMathInset()->nargs()) {
98                 // idx() == nargs() is _not_ valid!
99                 ++top.idx_;
100                 if (top.asMathInset()->validCell(top.idx_)) {
101                         top.pos_ = 0;
102                         return;
103                 }
104         }
105
106         // otherwise leave array, move on one back
107         // this might yield pos() == size(), but that's a ok.
108         pop();
109         // it certainly invalidates top
110         ++back().pos_;
111 }
112
113
114 void MathIterator::jump(difference_type i)
115 {
116         back().pos_ += i;
117         //BOOST_ASSERT(back().pos_ >= 0);
118         BOOST_ASSERT(back().pos_ <= cell().size());
119 }
120
121
122 bool MathIterator::normal() const
123 {
124         return back().pos_ < cell().size();
125 }
126
127
128 void MathIterator::shrink(size_type i)
129 {
130         if (i < size())
131                 erase(begin() + i, end());
132 }
133
134
135 bool operator==(MathIterator const & it, MathIterator const & jt)
136 {
137         return MathIterator::base_type(it) == MathIterator::base_type(jt);
138 }
139
140
141 bool operator!=(MathIterator const & it, MathIterator const & jt)
142 {
143         return MathIterator::base_type(it) != MathIterator::base_type(jt);
144 }
145
146
147 MathIterator ibegin(MathInset * p)
148 {
149         return MathIterator(p);
150 }
151
152
153 MathIterator iend(MathInset * p)
154 {
155         MathIterator it(p);
156         it.goEnd();
157         return it;
158 }