]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
forward search in math insets. ugly. seems to work. don't ask why.
[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 MathCursorPos const & MathIterator::position() const
25 {
26         lyx::Assert(cursor_.size());
27         return cursor_.back();
28 }
29
30
31 MathCursorPos & MathIterator::position()
32 {
33         lyx::Assert(cursor_.size());
34         return cursor_.back();
35 }
36
37
38 MathCursor::cursor_type const & MathIterator::cursor() const
39 {
40         return cursor_;
41 }
42
43
44 MathInset const * MathIterator::par() const
45 {
46         return position().par_;
47 }
48
49
50 MathInset * MathIterator::par()
51 {
52         return position().par_;
53 }
54
55
56 MathXArray const & MathIterator::xcell() const
57 {
58         return par()->xcell(position().idx_);
59 }
60
61
62 MathArray const & MathIterator::cell() const
63 {
64         return par()->xcell(position().idx_).data_;
65 }
66
67
68 MathInset * MathIterator::nextInset() const
69 {
70         if (position().pos_ == xcell().data_.size())
71                 return 0;
72         return (xcell().begin() + position().pos_)->nucleus();
73 }
74
75
76 void MathIterator::push(MathInset * p)
77 {
78         //lyxerr << "push: " << p << endl;
79         cursor_.push_back(MathCursorPos(p));
80 }
81
82
83 void MathIterator::pop()
84 {
85         //lyxerr << "pop: " << endl;
86         lyx::Assert(cursor_.size());
87         cursor_.pop_back();
88 }
89
90
91 MathCursorPos const & MathIterator::operator*() const
92 {
93         return position();
94 }
95
96
97 MathCursorPos const & MathIterator::operator->() const
98 {
99         return position();
100 }
101
102
103 void MathIterator::goEnd()
104 {
105         position().idx_ = par()->nargs() - 1;
106         position().pos_ = xcell().data_.size();
107 }
108
109
110 void MathIterator::operator++()
111 {
112         // move into the current inset if possible
113         // it is impossible for pos() == size()!
114         if (nextInset() && nextInset()->isActive()) {
115                 push(nextInset());
116                 return;
117         }
118
119         // otherwise move on one cell position if possible
120         if (position().pos_ < xcell().data_.size()) {
121                 // pos() == size() is valid!
122                 ++position().pos_;
123                 return;
124         }
125
126         // otherwise move on one cell if possible
127         if (position().idx_ + 1 < par()->nargs()) {
128                 // idx() == nargs() is _not_ valid!
129                 ++position().idx_;
130                 position().pos_ = 0;
131                 return;
132         }
133
134         // otherwise leave array, move on one position
135         // this might yield pos() == size(), but that's a ok.
136         pop();
137         ++position().pos_;
138 }
139
140
141 void MathIterator::jump(int i)
142 {
143         position().pos_ += i;
144         lyx::Assert(position().pos_ >= 0);
145         lyx::Assert(position().pos_ <= cell().size());
146 }
147
148
149 bool operator==(MathIterator const & it, MathIterator const & jt)
150 {
151         //lyxerr << "==: " << it.cursor().size() << " " << jt.cursor().size() << endl;
152         if (it.cursor().size() != jt.cursor().size())
153                 return false;
154         return it.cursor() == jt.cursor();      
155 }
156
157
158 bool operator!=(MathIterator const & it, MathIterator const & jt)
159 {
160         //lyxerr << "!=: " << it.cursor().size() << " " << jt.cursor().size() << endl;
161         if (it.cursor().size() != jt.cursor().size())
162                 return true;
163         return it.cursor() != jt.cursor();      
164 }
165
166
167 MathIterator ibegin(MathInset * p)
168 {
169         return MathIterator(p);
170 }
171
172
173 MathIterator iend(MathInset * p)
174 {
175         MathIterator it(p);
176         it.goEnd();
177         return it;
178 }