From 99fe02467a11858b93f19ca596bff97984d548d0 Mon Sep 17 00:00:00 2001 From: Alfredo Braunstein Date: Sun, 2 Nov 2003 17:56:26 +0000 Subject: [PATCH] add PosIterator git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8014 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 7 +++ src/Makefile.am | 2 + src/PosIterator.C | 149 ++++++++++++++++++++++++++++++++++++++++++++++ src/PosIterator.h | 68 +++++++++++++++++++++ src/buffer.C | 13 ++++ src/buffer.h | 5 ++ src/iterators.C | 18 ++++++ src/iterators.h | 7 +++ 8 files changed, 269 insertions(+) create mode 100644 src/PosIterator.C create mode 100644 src/PosIterator.h diff --git a/src/ChangeLog b/src/ChangeLog index 43aed57ff6..67c99bcb15 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ + +2003-11-02 Alfredo Braunstein + + * iterators.[Ch] (asPosIterator): added + * buffer.[Ch] (pos_iterator_begin, pos_iterator_end): added + * PosIterator.[Ch]: added + 2003-11-01 Lars Gullik Bjønnes * text3.C: diff --git a/src/Makefile.am b/src/Makefile.am index bae5416a49..25ff4213c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -228,6 +228,8 @@ lyx_SOURCES = \ paragraph.h \ paragraph_pimpl.C \ paragraph_pimpl.h \ + PosIterator.h \ + PosIterator.C \ SpellBase.h \ ispell.C \ ispell.h \ diff --git a/src/PosIterator.C b/src/PosIterator.C new file mode 100644 index 0000000000..f89ef75b9f --- /dev/null +++ b/src/PosIterator.C @@ -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 + +#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 + +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)); +} diff --git a/src/PosIterator.h b/src/PosIterator.h new file mode 100644 index 0000000000..2fb5e4706a --- /dev/null +++ b/src/PosIterator.h @@ -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 + + +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 stack_; +}; + +bool operator!=(PosIterator const &, PosIterator const &); + + +#endif + diff --git a/src/buffer.C b/src/buffer.C index 7f8d0b033f..81d1ee2887 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -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(¶graphs(), paragraphs().begin(), 0); +} + + +PosIterator Buffer::pos_iterator_end() +{ + return PosIterator(¶graphs(), paragraphs().end(), 0); +} + + ParIterator Buffer::par_iterator_begin() { return ParIterator(paragraphs().begin(), paragraphs()); diff --git a/src/buffer.h b/src/buffer.h index 9637b9d059..12fb74ea92 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -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(); /// diff --git a/src/iterators.C b/src/iterators.C index 817e72587b..acbe21237c 100644 --- a/src/iterators.C +++ b/src/iterators.C @@ -13,8 +13,10 @@ #include "iterators.h" #include "paragraph.h" +#include "PosIterator.h" #include "cursor.h" + #include "insets/inset.h" #include @@ -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(pp.plist), pp.pit, (*pp.it)->pos, *pp.index + 1)); + } + ParPosition const & pp = pimpl_->positions[last]; + p.stack_.push(PosIteratorItem(const_cast(pp.plist), + pp.pit, pos, 0)); + return p; +} diff --git a/src/iterators.h b/src/iterators.h index cad565e858..88711f667b 100644 --- a/src/iterators.h +++ b/src/iterators.h @@ -13,12 +13,15 @@ #define ITERATORS_H #include "ParagraphList_fwd.h" +#include "support/types.h" #include 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_; @@ -93,6 +99,7 @@ public: friend bool operator==(ParConstIterator const & iter1, ParConstIterator const & iter2); + private: struct Pimpl; boost::scoped_ptr pimpl_; -- 2.39.2