]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
small up/down tweaking
[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 bool MathIterator::normal() const
127 {
128         return back().pos_ < cell().size();
129 }
130
131
132 void MathIterator::shrink(size_type i)
133 {
134         if (i < size())
135                 erase(begin() + i, end());
136 }
137
138
139 bool operator==(MathIterator const & it, MathIterator const & jt)
140 {
141         return MathIterator::base_type(it) == MathIterator::base_type(jt);
142 }
143
144
145 bool operator!=(MathIterator const & it, MathIterator const & jt)
146 {
147         return MathIterator::base_type(it) != MathIterator::base_type(jt);
148 }
149
150
151 MathIterator ibegin(MathInset * p)
152 {
153         return MathIterator(p);
154 }
155
156
157 MathIterator iend(MathInset * p)
158 {
159         MathIterator it(p);
160         it.goEnd();
161         return it;
162 }