2 * This file is part of LyX, the document processor.
3 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
14 #include "iterators.h"
15 #include "paragraph.h"
17 #include <boost/next_prior.hpp>
18 #include <boost/optional.hpp>
23 using boost::optional;
33 ParPosition(ParagraphList::iterator p, ParagraphList const & pl);
35 ParagraphList::iterator pit;
37 ParagraphList const * plist;
39 optional<InsetList::iterator> it;
45 ParPosition::ParPosition(ParagraphList::iterator p, ParagraphList const & pl)
48 if (p != const_cast<ParagraphList&>(pl).end()) {
49 it.reset(p->insetlist.begin());
54 bool operator==(ParPosition const & pos1, ParPosition const & pos2)
56 return pos1.pit == pos2.pit;
60 bool operator!=(ParPosition const & pos1, ParPosition const & pos2)
62 return !(pos1 == pos2);
70 struct ParIterator::Pimpl {
71 typedef stack<ParPosition> PosHolder;
75 ParIterator::ParIterator(ParagraphList::iterator pit, ParagraphList const & pl)
78 pimpl_->positions.push(ParPosition(pit, pl));
82 ParIterator::~ParIterator()
86 ParIterator::ParIterator(ParIterator const & pi)
87 : pimpl_(new Pimpl(*pi.pimpl_))
91 void ParIterator::operator=(ParIterator const & pi)
94 pimpl_.swap(tmp.pimpl_);
98 ParIterator & ParIterator::operator++()
100 while (!pimpl_->positions.empty()) {
101 ParPosition & p = pimpl_->positions.top();
103 // Does the current inset contain more "cells" ?
106 ParagraphList * plist = (*p.it)->inset->getParagraphs(*p.index);
107 if (plist && !plist->empty()) {
108 pimpl_->positions.push(ParPosition(plist->begin(), *plist));
113 // The following line is needed because the value of
114 // p.it may be invalid if inset was added/removed to
115 // the paragraph pointed by the iterator
116 p.it.reset(p.pit->insetlist.begin());
118 // Try to find the next inset that contains paragraphs
119 InsetList::iterator end = p.pit->insetlist.end();
120 for (; *p.it != end; ++(*p.it)) {
121 ParagraphList * plist = (*p.it)->inset->getParagraphs(0);
122 if (plist && !plist->empty()) {
124 pimpl_->positions.push(ParPosition(plist->begin(), *plist));
129 // Try to go to the next paragarph
130 if (next(p.pit) != const_cast<ParagraphList*>(p.plist)->end()
131 || pimpl_->positions.size() == 1) {
139 // Drop end and move up in the stack.
140 pimpl_->positions.pop();
146 ParagraphList::iterator ParIterator::operator*() const
148 return pimpl_->positions.top().pit;
152 ParagraphList::iterator ParIterator::operator->() const
154 return pimpl_->positions.top().pit;
158 size_t ParIterator::size() const
160 return pimpl_->positions.size();
164 ParagraphList & ParIterator::plist() const
166 return *const_cast<ParagraphList*>(pimpl_->positions.top().plist);
170 bool operator==(ParIterator const & iter1, ParIterator const & iter2)
172 return iter1.pimpl_->positions == iter2.pimpl_->positions;
176 bool operator!=(ParIterator const & iter1, ParIterator const & iter2)
178 return !(iter1 == iter2);
187 struct ParConstIterator::Pimpl {
188 typedef stack<ParPosition> PosHolder;
193 ParConstIterator::ParConstIterator(ParagraphList::iterator pit,
194 ParagraphList const & pl)
197 pimpl_->positions.push(ParPosition(pit, pl));
201 ParConstIterator::~ParConstIterator()
205 ParConstIterator::ParConstIterator(ParConstIterator const & pi)
206 : pimpl_(new Pimpl(*pi.pimpl_))
210 ParConstIterator & ParConstIterator::operator++()
212 while (!pimpl_->positions.empty()) {
213 ParPosition & p = pimpl_->positions.top();
215 // Does the current inset contain more "cells" ?
218 ParagraphList * plist = (*p.it)->inset->getParagraphs(*p.index);
219 if (plist && !plist->empty()) {
220 pimpl_->positions.push(ParPosition(plist->begin(), *plist));
225 // The following line is needed because the value of
226 // p.it may be invalid if inset was added/removed to
227 // the paragraph pointed by the iterator
228 p.it.reset(p.pit->insetlist.begin());
230 // Try to find the next inset that contains paragraphs
231 InsetList::iterator end = p.pit->insetlist.end();
232 for (; *p.it != end; ++(*p.it)) {
233 ParagraphList * plist = (*p.it)->inset->getParagraphs(0);
234 if (plist && !plist->empty()) {
236 pimpl_->positions.push(ParPosition(plist->begin(), *plist));
241 // Try to go to the next paragarph
242 if (next(p.pit) != const_cast<ParagraphList*>(p.plist)->end()
243 || pimpl_->positions.size() == 1) {
251 // Drop end and move up in the stack.
252 pimpl_->positions.pop();
259 ParagraphList::iterator ParConstIterator::operator*() const
261 return pimpl_->positions.top().pit;
265 ParagraphList::iterator ParConstIterator::operator->() const
267 return pimpl_->positions.top().pit;
271 size_t ParConstIterator::size() const
273 return pimpl_->positions.size();
277 bool operator==(ParConstIterator const & iter1, ParConstIterator const & iter2)
279 return iter1.pimpl_->positions == iter2.pimpl_->positions;
283 bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2)
285 return !(iter1 == iter2);