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