]> git.lyx.org Git - features.git/blob - src/mathed/math_iterator.C
0ea88d50987c367e389434e00eff43df30d236ba
[features.git] / src / mathed / math_iterator.C
1
2 #include <config.h>
3
4 #include "debug.h"
5 #include "math_iterator.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 MathCursorPos const & MathIterator::position() const
24 {
25         return cursor_.back();
26 }
27
28
29 MathCursorPos & MathIterator::position()
30 {
31         return cursor_.back();
32 }
33
34
35 MathCursor::cursor_type const & MathIterator::cursor() const
36 {
37         return cursor_;
38 }
39
40
41 MathInset * MathIterator::par() const
42 {
43         return cursor_.size() ? cursor_.back().par_ : 0;
44 }
45
46
47 MathXArray const & MathIterator::xcell() const
48 {
49         if (!par())
50                 lyxerr << "MathIterator::xcell: no cell\n";
51         return par()->xcell(position().idx_);
52 }
53
54
55 MathInset * MathIterator::nextInset() const
56 {
57         if (position().pos_ == xcell().data_.size())
58                 return 0;
59         return (xcell().begin() + position().pos_)->nucleus();
60 }
61
62
63 void MathIterator::push(MathInset * p)
64 {
65         //lyxerr << "push: " << p << endl;
66         cursor_.push_back(MathCursorPos(p));
67 }
68
69
70 void MathIterator::pop()
71 {
72         //lyxerr << "pop: " << endl;
73         cursor_.pop_back();
74 }
75
76
77 MathCursorPos const & MathIterator::operator*() const
78 {
79         return position();
80 }
81
82
83 MathCursorPos const & MathIterator::operator->() const
84 {
85         return position();
86 }
87
88
89 void MathIterator::operator++()
90 {
91         // move into the current inset if possible
92         // it is impossible for pos() == size()!
93         if (nextInset() && nextInset()->isActive()) {
94                 push(nextInset());
95                 return;
96         }
97
98         // otherwise move on one cell position if possible
99         if (position().pos_ < xcell().data_.size()) {
100                 // pos() == size() is valid!
101                 ++position().pos_;
102                 return;
103         }
104
105         // otherwise move on one cell if possible
106         if (position().idx_ + 1 < par()->nargs()) {
107                 // idx() == nargs() is _not_ valid!
108                 ++position().idx_;
109                 position().pos_ = 0;
110                 return;
111         }
112
113         // otherwise leave array, move on one cell
114         // this might yield pos() == size(), but that's a ok.
115         pop();
116         ++position().pos_;
117 }
118
119
120
121 bool operator==(MathIterator const & it, MathIterator const & jt)
122 {
123         //lyxerr << "==: " << it.cursor().size() << " " << jt.cursor().size() << endl;
124         if (it.cursor().size() != jt.cursor().size())
125                 return false;
126         return it.cursor() == jt.cursor();      
127 }
128
129
130 bool operator!=(MathIterator const & it, MathIterator const & jt)
131 {
132         //lyxerr << "!=: " << it.cursor().size() << " " << jt.cursor().size() << endl;
133         if (it.cursor().size() != jt.cursor().size())
134                 return true;
135         return it.cursor() != jt.cursor();      
136 }
137