]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
drop another function unknown to the outer world...
[lyx.git] / src / mathed / math_iterator.C
1 /**
2  * \file math_iterator.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_iterator.h"
14 #include "math_inset.h"
15 #include "math_data.h"
16
17 #include <boost/assert.hpp>
18
19
20 MathIterator::MathIterator()
21 {}
22
23
24 MathIterator::MathIterator(MathInset * p)
25 {
26         push(p);
27 }
28
29
30
31 MathArray const & MathIterator::cell() const
32 {
33         CursorSlice const & top = back();
34         return top.asMathInset()->cell(top.idx_);
35 }
36
37
38
39 void MathIterator::push(MathInset * p)
40 {
41         //lyxerr << "push: " << p << endl;
42         push_back(CursorSlice(p));
43 }
44
45
46 void MathIterator::pop()
47 {
48         //lyxerr << "pop: " << endl;
49         BOOST_ASSERT(size());
50         pop_back();
51 }
52
53
54 void MathIterator::goEnd()
55 {
56         CursorSlice & top = back();
57         top.idx_ = top.asMathInset()->nargs() - 1;
58         top.pos_ = cell().size();
59 }
60
61
62 void MathIterator::operator++()
63 {
64         CursorSlice & top = back();
65         MathArray   & ar  = top.asMathInset()->cell(top.idx_);
66
67         // move into the current inset if possible
68         // it is impossible for pos() == size()!
69         MathInset * n = 0;
70         if (top.pos_ != ar.size())
71                 n = (ar.begin() + top.pos_)->nucleus();
72         if (n && n->isActive()) {
73                 push(n);
74                 return;
75         }
76
77         // otherwise move on one cell back if possible
78         if (top.pos_ < ar.size()) {
79                 // pos() == size() is valid!
80                 ++top.pos_;
81                 return;
82         }
83
84         // otherwise try to move on one cell if possible
85         while (top.idx_ + 1 < top.asMathInset()->nargs()) {
86                 // idx() == nargs() is _not_ valid!
87                 ++top.idx_;
88                 if (top.asMathInset()->validCell(top.idx_)) {
89                         top.pos_ = 0;
90                         return;
91                 }
92         }
93
94         // otherwise leave array, move on one back
95         // this might yield pos() == size(), but that's a ok.
96         pop();
97         // it certainly invalidates top
98         ++back().pos_;
99 }
100
101
102 bool MathIterator::normal() const
103 {
104         return back().pos_ < cell().size();
105 }
106
107
108 void MathIterator::shrink(size_type i)
109 {
110         if (i < size())
111                 erase(begin() + i, end());
112 }
113
114
115 bool operator==(MathIterator const & it, MathIterator const & jt)
116 {
117         return MathIterator::base_type(it) == MathIterator::base_type(jt);
118 }
119
120
121 bool operator!=(MathIterator const & it, MathIterator const & jt)
122 {
123         return MathIterator::base_type(it) != MathIterator::base_type(jt);
124 }
125
126
127 MathIterator ibegin(MathInset * p)
128 {
129         return MathIterator(p);
130 }
131
132
133 MathIterator iend(MathInset * p)
134 {
135         MathIterator it(p);
136         it.goEnd();
137         return it;
138 }