]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
remove unneeded LFUN_MATH_MACROARG
[lyx.git] / src / mathed / math_iterator.C
1
2 #include <config.h>
3
4 #include "math_iterator.h"
5 #include "debug.h"
6 #include "support/LAssert.h"
7
8
9 //MathIterator::MathIterator()
10 //{}
11
12
13 MathIterator::MathIterator(MathInset * p)
14 {
15         push(p);
16 }
17
18
19 //MathIterator::MathIterator(MathCursor::cursor_type const & c)
20 //      : cursor_(c)
21 //{}
22
23
24 MathCursor::cursor_type const & MathIterator::cursor() const
25 {
26         return cursor_;
27 }
28
29
30 MathInset const * MathIterator::par() const
31 {
32         return position().par_;
33 }
34
35
36 MathInset * MathIterator::par()
37 {
38         return position().par_;
39 }
40
41
42 MathArray const & MathIterator::cell() const
43 {
44         MathCursorPos const & top = position();
45         return top.par_->cell(top.idx_);
46 }
47
48
49
50 void MathIterator::push(MathInset * p)
51 {
52         //lyxerr << "push: " << p << endl;
53         cursor_.push_back(MathCursorPos(p));
54 }
55
56
57 void MathIterator::pop()
58 {
59         //lyxerr << "pop: " << endl;
60         lyx::Assert(cursor_.size());
61         cursor_.pop_back();
62 }
63
64
65 MathCursorPos const & MathIterator::operator*() const
66 {
67         return position();
68 }
69
70
71 MathCursorPos const & MathIterator::operator->() const
72 {
73         return position();
74 }
75
76
77 void MathIterator::goEnd()
78 {
79         MathCursorPos & top = position();
80         top.idx_ = top.par_->nargs() - 1;
81         top.pos_ = cell().size();
82 }
83
84
85 void MathIterator::operator++()
86 {
87         MathCursorPos   & top = position();
88         MathArray const & ar  = top.par_->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 position if possible
101         if (top.pos_ < ar.size()) {
102                 // pos() == size() is valid!
103                 ++top.pos_;
104                 return;
105         }
106
107         // otherwise move on one cell if possible
108         if (top.idx_ + 1 < top.par_->nargs()) {
109                 // idx() == nargs() is _not_ valid!
110                 ++top.idx_;
111                 top.pos_ = 0;
112                 return;
113         }
114
115         // otherwise leave array, move on one position
116         // this might yield pos() == size(), but that's a ok.
117         pop(); 
118         // it certainly invalidates top
119         ++position().pos_;
120 }
121
122
123 void MathIterator::jump(MathInset::difference_type i)
124 {
125         position().pos_ += i;
126         //lyx::Assert(position().pos_ >= 0);
127         lyx::Assert(position().pos_ <= cell().size());
128 }
129
130
131 bool operator==(MathIterator const & it, MathIterator const & jt)
132 {
133         //lyxerr << "==: " << it.cursor().size() << " " << jt.cursor().size() << endl;
134         if (it.cursor().size() != jt.cursor().size())
135                 return false;
136         return it.cursor() == jt.cursor();      
137 }
138
139
140 bool operator!=(MathIterator const & it, MathIterator const & jt)
141 {
142         //lyxerr << "!=: " << it.cursor().size() << " " << jt.cursor().size() << endl;
143         if (it.cursor().size() != jt.cursor().size())
144                 return true;
145         return it.cursor() != jt.cursor();      
146 }
147
148
149 MathIterator ibegin(MathInset * p)
150 {
151         return MathIterator(p);
152 }
153
154
155 MathIterator iend(MathInset * p)
156 {
157         MathIterator it(p);
158         it.goEnd();
159         return it;
160 }