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