]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
revert most of the stuff that got reverted yesterday but was not
[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 MathInset * MathIterator::nextInset() const
63 {
64         if (position().pos_ == xcell().data_.size())
65                 return 0;
66         return (xcell().begin() + position().pos_)->nucleus();
67 }
68
69
70 void MathIterator::push(MathInset * p)
71 {
72         //lyxerr << "push: " << p << endl;
73         cursor_.push_back(MathCursorPos(p));
74 }
75
76
77 void MathIterator::pop()
78 {
79         //lyxerr << "pop: " << endl;
80         lyx::Assert(cursor_.size());
81         cursor_.pop_back();
82 }
83
84
85 MathCursorPos const & MathIterator::operator*() const
86 {
87         return position();
88 }
89
90
91 MathCursorPos const & MathIterator::operator->() const
92 {
93         return position();
94 }
95
96
97 void MathIterator::goEnd()
98 {
99         position().idx_ = par()->nargs() - 1;
100         position().pos_ = xcell().data_.size();
101 }
102
103
104 void MathIterator::operator++()
105 {
106         // move into the current inset if possible
107         // it is impossible for pos() == size()!
108         if (nextInset() && nextInset()->isActive()) {
109                 push(nextInset());
110                 return;
111         }
112
113         // otherwise move on one cell position if possible
114         if (position().pos_ < xcell().data_.size()) {
115                 // pos() == size() is valid!
116                 ++position().pos_;
117                 return;
118         }
119
120         // otherwise move on one cell if possible
121         if (position().idx_ + 1 < par()->nargs()) {
122                 // idx() == nargs() is _not_ valid!
123                 ++position().idx_;
124                 position().pos_ = 0;
125                 return;
126         }
127
128         // otherwise leave array, move on one position
129         // this might yield pos() == size(), but that's a ok.
130         pop();
131         ++position().pos_;
132 }
133
134
135
136 bool operator==(MathIterator const & it, MathIterator const & jt)
137 {
138         //lyxerr << "==: " << it.cursor().size() << " " << jt.cursor().size() << endl;
139         if (it.cursor().size() != jt.cursor().size())
140                 return false;
141         return it.cursor() == jt.cursor();      
142 }
143
144
145 bool operator!=(MathIterator const & it, MathIterator const & jt)
146 {
147         //lyxerr << "!=: " << it.cursor().size() << " " << jt.cursor().size() << endl;
148         if (it.cursor().size() != jt.cursor().size())
149                 return true;
150         return it.cursor() != jt.cursor();      
151 }
152
153
154
155 MathIterator ibegin(MathInset * p)
156 {
157         return MathIterator(p);
158 }
159
160
161 MathIterator iend(MathInset * p)
162 {
163         MathIterator it(p);
164         it.goEnd();
165         return it;
166 }