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