]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
2f949af5fda80cb362ba445df61446cbeda2ec30
[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 void MathIterator::operator++()
21 {
22         CursorSlice & top = back();
23         MathArray   & ar  = top.asMathInset()->cell(top.idx_);
24
25         // move into the current inset if possible
26         // it is impossible for pos() == size()!
27         MathInset * n = 0;
28         if (top.pos_ != ar.size())
29                 n = (ar.begin() + top.pos_)->nucleus();
30         if (n && n->isActive()) {
31                 push_back(CursorSlice(n));
32                 return;
33         }
34
35         // otherwise move on one cell back if possible
36         if (top.pos_ < ar.size()) {
37                 // pos() == size() is valid!
38                 ++top.pos_;
39                 return;
40         }
41
42         // otherwise try to move on one cell if possible
43         while (top.idx_ + 1 < top.asMathInset()->nargs()) {
44                 // idx() == nargs() is _not_ valid!
45                 ++top.idx_;
46                 if (top.asMathInset()->validCell(top.idx_)) {
47                         top.pos_ = 0;
48                         return;
49                 }
50         }
51
52         // otherwise leave array, move on one back
53         // this might yield pos() == size(), but that's a ok.
54         pop_back();
55         // it certainly invalidates top
56         ++back().pos_;
57 }
58
59
60 bool operator==(MathIterator const & it, MathIterator const & jt)
61 {
62         return MathIterator::base_type(it) == MathIterator::base_type(jt);
63 }
64
65
66 bool operator!=(MathIterator const & it, MathIterator const & jt)
67 {
68         return MathIterator::base_type(it) != MathIterator::base_type(jt);
69 }
70
71
72 MathIterator ibegin(MathInset * p)
73 {
74         MathIterator it;
75         it.push_back(CursorSlice(p));
76         return it;
77 }
78
79
80 MathIterator iend(MathInset * p)
81 {
82         MathIterator it;
83         it.push_back(CursorSlice(p));
84         CursorSlice & top = it.back();
85         top.idx_ = top.asMathInset()->nargs() - 1;
86         top.pos_ = top.asMathInset()->cell(top.idx_).size();
87         return it;
88 }