]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
8f508907e8bbed52a78ce3fe1d2307141bb078f0
[lyx.git] / src / mathed / math_iterator.C
1 #ifdef __GNUG__
2 #pragma implementation 
3 #endif
4
5 #include <config.h>
6
7 #include "math_iterator.h"
8 #include "math_inset.h"
9 #include "debug.h"
10 #include "support/LAssert.h"
11
12
13 MathIterator::MathIterator()
14 {}
15
16
17 MathIterator::MathIterator(MathInset * p)
18 {
19         push(p);
20 }
21
22
23 MathInset const * MathIterator::par() const
24 {
25         return back().par_;
26 }
27
28
29 MathInset * MathIterator::par()
30 {
31         return back().par_;
32 }
33
34
35 MathArray const & MathIterator::cell() const
36 {
37         MathCursorPos const & top = back();
38         return top.par_->cell(top.idx_);
39 }
40
41
42
43 void MathIterator::push(MathInset * p)
44 {
45         //lyxerr << "push: " << p << endl;
46         push_back(MathCursorPos(p));
47 }
48
49
50 void MathIterator::pop()
51 {
52         //lyxerr << "pop: " << endl;
53         lyx::Assert(size());
54         pop_back();
55 }
56
57
58 MathCursorPos const & MathIterator::operator*() const
59 {
60         return back();
61 }
62
63
64 MathCursorPos const & MathIterator::operator->() const
65 {
66         return back();
67 }
68
69
70 void MathIterator::goEnd()
71 {
72         MathCursorPos & top = back();
73         top.idx_ = top.par_->nargs() - 1;
74         top.pos_ = cell().size();
75 }
76
77
78 void MathIterator::operator++()
79 {
80         MathCursorPos   & top = back();
81         MathArray & ar  = top.par_->cell(top.idx_);
82
83         // move into the current inset if possible
84         // it is impossible for pos() == size()!
85         MathInset * n = 0;
86         if (top.pos_ != ar.size())
87                 n = (ar.begin() + top.pos_)->nucleus();
88         if (n && n->isActive()) {
89                 push(n);
90                 return;
91         }
92
93         // otherwise move on one cell back if possible
94         if (top.pos_ < ar.size()) {
95                 // pos() == size() is valid!
96                 ++top.pos_;
97                 return;
98         }
99
100         // otherwise try to move on one cell if possible
101         while (top.idx_ + 1 < top.par_->nargs()) {
102                 // idx() == nargs() is _not_ valid!
103                 ++top.idx_;
104                 if (top.par_->validCell(top.idx_)) {
105                         top.pos_ = 0;
106                         return;
107                 }
108         }
109
110         // otherwise leave array, move on one back
111         // this might yield pos() == size(), but that's a ok.
112         pop();
113         // it certainly invalidates top
114         ++back().pos_;
115 }
116
117
118 void MathIterator::jump(difference_type i)
119 {
120         back().pos_ += i;
121         //lyx::Assert(back().pos_ >= 0);
122         lyx::Assert(back().pos_ <= cell().size());
123 }
124
125
126 /*
127 void MathIterator::shrink(size_type i)
128 {
129         if (i < size())
130                 erase(begin() + i, end());
131 }
132
133
134 void MathIterator::shrink(size_type i)
135 {
136         if (i < size())
137                 erase(begin() + i, end());
138 }
139 */
140
141
142 void MathIterator::shrink(size_type i)
143 {
144         if (i < size())
145                 erase(begin() + i, end());
146 }
147
148
149 bool operator==(MathIterator const & it, MathIterator const & jt)
150 {
151         return MathIterator::base_type(it) == MathIterator::base_type(jt);
152 }
153
154
155 bool operator!=(MathIterator const & it, MathIterator const & jt)
156 {
157         return MathIterator::base_type(it) != MathIterator::base_type(jt);
158 }
159
160
161 MathIterator ibegin(MathInset * p)
162 {
163         return MathIterator(p);
164 }
165
166
167 MathIterator iend(MathInset * p)
168 {
169         MathIterator it(p);
170         it.goEnd();
171         return it;
172 }