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