add PosIterator

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8014 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Alfredo Braunstein 2003-11-02 17:56:26 +00:00
parent cc719fe0ce
commit 99fe02467a
8 changed files with 269 additions and 0 deletions

View File

@ -1,3 +1,10 @@
2003-11-02 Alfredo Braunstein <abraunst@libero.it>
* iterators.[Ch] (asPosIterator): added
* buffer.[Ch] (pos_iterator_begin, pos_iterator_end): added
* PosIterator.[Ch]: added
2003-11-01 Lars Gullik Bjønnes <larsbj@gullik.net>
* text3.C:

View File

@ -228,6 +228,8 @@ lyx_SOURCES = \
paragraph.h \
paragraph_pimpl.C \
paragraph_pimpl.h \
PosIterator.h \
PosIterator.C \
SpellBase.h \
ispell.C \
ispell.h \

149
src/PosIterator.C Normal file
View File

@ -0,0 +1,149 @@
/* \file PosIterator.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Alfredo Braunstein
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "PosIterator.h"
#include "buffer.h"
#include "BufferView.h"
#include "iterators.h"
#include "lyxtext.h"
#include "paragraph.h"
#include "insets/insettext.h"
#include "insets/updatableinset.h"
#include "insets/inset.h"
#include <boost/next_prior.hpp>
using boost::prior;
PosIterator & PosIterator::operator++()
{
BOOST_ASSERT(!stack_.empty());
while (true) {
PosIteratorItem & p = stack_.top();
if (p.pos < p.pit->size()) {
InsetOld * inset = p.pit->getInset(p.pos);
if (inset) {
ParagraphList * pl = inset->getParagraphs(p.index);
if (pl) {
p.index++;
stack_.push(PosIteratorItem(pl));
return *this;
}
}
p.index = 0;
++p.pos;
} else {
++p.pit;
p.pos = 0;
}
if (p.pit != p.pl->end() || stack_.size() == 1)
return *this;
stack_.pop();
}
return *this;
}
PosIterator & PosIterator::operator--()
{
BOOST_ASSERT(!stack_.empty());
// try to go one position backwards: if on the start of the
// ParagraphList, pops an item
PosIteratorItem & p = stack_.top();
if (p.pos > 0) {
--p.pos;
InsetOld * inset = p.pit->getInset(p.pos);
if (inset)
p.index = inset->numParagraphs();
} else {
if (p.pit == p.pl->begin()) {
if (stack_.size() == 1)
return *this;
stack_.pop();
--stack_.top().index;
} else {
--p.pit;
p.pos = p.pit->size();
}
}
// try to push an item if there is some left unexplored
PosIteratorItem & q = stack_.top();
if (q.pos < q.pit->size()) {
InsetOld * inset = q.pit->getInset(q.pos);
if (inset && q.index > 0) {
ParagraphList *
pl = inset->getParagraphs(q.index - 1);
BOOST_ASSERT(pl);
stack_.push(PosIteratorItem(pl, prior(pl->end()), pl->back().size()));
}
}
return *this;
}
bool operator!=(PosIterator const & lhs, PosIterator const & rhs)
{
return !(lhs == rhs);
}
bool operator==(PosIterator const & lhs, PosIterator const & rhs)
{
PosIteratorItem const & li = lhs.stack_.top();
PosIteratorItem const & ri = rhs.stack_.top();
return (li.pl == ri.pl && li.pit == ri.pit &&
(li.pit == li.pl->end() || li.pos == ri.pos));
}
bool PosIterator::at_end() const
{
return pos() == pit()->size();
}
PosIterator::PosIterator(ParagraphList * pl, ParagraphList::iterator pit,
lyx::pos_type pos)
{
stack_.push(PosIteratorItem(pl, pit, pos));
}
PosIterator::PosIterator(ParagraphList * pl)
{
stack_.push(PosIteratorItem(pl, pl->begin(), 0));
}
PosIterator::PosIterator(BufferView & bv)
{
LyXText * text = bv.getLyXText();
lyx::pos_type pos = text->cursor.pos();
ParagraphList::iterator pit = text->cursorPar();
ParIterator par = bv.buffer()->par_iterator_begin();
ParIterator end = bv.buffer()->par_iterator_end();
for ( ; par != end; ++par) {
if (par.pit() == pit)
break;
}
operator=(par.asPosIterator(pos));
}

68
src/PosIterator.h Normal file
View File

@ -0,0 +1,68 @@
// -*- C++ -*-
/* \file PosIterator.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Alfredo Braunstein
*
* Full author contact details are available in file CREDITS.
*/
#ifndef POSITERATOR_H
#define POSITERATOR_H
#include "ParagraphList_fwd.h"
#include "iterators.h"
#include "support/types.h"
#include <stack>
class BufferView;
struct PosIteratorItem
{
PosIteratorItem(ParagraphList * pl): pl(pl), pit(pl->begin()),
pos(0), index(0) {};
PosIteratorItem(ParagraphList * pl,
ParagraphList::iterator pit,
lyx::pos_type pos,
int index = 0)
: pl(pl), pit(pit), pos(pos), index(index) {};
ParagraphList * pl;
ParagraphList::iterator pit;
lyx::pos_type pos;
int index;
};
class PosIterator
{
public:
PosIterator(BufferView & bv);
PosIterator(ParIterator & par, lyx::pos_type pos);
PosIterator(ParagraphList * pl);
PosIterator(ParagraphList * pl, ParagraphList::iterator pit,
lyx::pos_type pos);
PosIterator(ParIterator const & parit, lyx::pos_type p);
PosIterator & operator++();
PosIterator & operator--();
friend bool operator==(PosIterator const &, PosIterator const &);
ParagraphList::iterator pit() const { return stack_.top().pit; }
lyx::pos_type pos() const { return stack_.top().pos; }
bool at_end() const;
friend PosIterator ParIterator::asPosIterator(lyx::pos_type) const;
private:
PosIterator() {};
std::stack<PosIteratorItem> stack_;
};
bool operator!=(PosIterator const &, PosIterator const &);
#endif

View File

@ -36,6 +36,7 @@
#include "paragraph.h"
#include "paragraph_funcs.h"
#include "ParagraphParameters.h"
#include "PosIterator.h"
#include "sgml.h"
#include "texrow.h"
#include "undo.h"
@ -1472,6 +1473,18 @@ bool Buffer::hasParWithID(int id) const
}
PosIterator Buffer::pos_iterator_begin()
{
return PosIterator(&paragraphs(), paragraphs().begin(), 0);
}
PosIterator Buffer::pos_iterator_end()
{
return PosIterator(&paragraphs(), paragraphs().end(), 0);
}
ParIterator Buffer::par_iterator_begin()
{
return ParIterator(paragraphs().begin(), paragraphs());

View File

@ -40,6 +40,7 @@ class LatexRunParams;
class Language;
class Messages;
class ParIterator;
class PosIterator;
class ParConstIterator;
class TeXErrors;
class TexRow;
@ -345,6 +346,10 @@ public:
/// return the const end of all *top-level* insets in the buffer
inset_iterator inset_const_iterator_end() const;
///
PosIterator pos_iterator_begin();
///
PosIterator pos_iterator_end();
///
ParIterator par_iterator_begin();
///

View File

@ -13,8 +13,10 @@
#include "iterators.h"
#include "paragraph.h"
#include "PosIterator.h"
#include "cursor.h"
#include "insets/inset.h"
#include <boost/next_prior.hpp>
@ -356,3 +358,19 @@ bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2)
{
return !(iter1 == iter2);
}
PosIterator ParIterator::asPosIterator(lyx::pos_type pos) const
{
PosIterator p;
int const last = size() - 1;
for (int i = 0; i < last; ++i) {
ParPosition & pp = pimpl_->positions[i];
p.stack_.push(PosIteratorItem(const_cast<ParagraphList *>(pp.plist), pp.pit, (*pp.it)->pos, *pp.index + 1));
}
ParPosition const & pp = pimpl_->positions[last];
p.stack_.push(PosIteratorItem(const_cast<ParagraphList *>(pp.plist),
pp.pit, pos, 0));
return p;
}

View File

@ -13,12 +13,15 @@
#define ITERATORS_H
#include "ParagraphList_fwd.h"
#include "support/types.h"
#include <boost/scoped_ptr.hpp>
class LyXText;
class InsetOld;
class Cursor;
class PosIterator;
class ParIterator {
public:
@ -56,6 +59,9 @@ public:
///
friend
bool operator==(ParIterator const & iter1, ParIterator const & iter2);
///
PosIterator asPosIterator(lyx::pos_type) const;
private:
struct Pimpl;
boost::scoped_ptr<Pimpl> pimpl_;
@ -93,6 +99,7 @@ public:
friend
bool operator==(ParConstIterator const & iter1,
ParConstIterator const & iter2);
private:
struct Pimpl;
boost::scoped_ptr<Pimpl> pimpl_;