From ef87c26b55b41a848338ada7a4677194ef632289 Mon Sep 17 00:00:00 2001 From: John Levon Date: Fri, 8 Nov 2002 01:08:27 +0000 Subject: [PATCH] Lar's ParConstIterator git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5602 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 11 +++++++++++ src/buffer.C | 12 ++++++++++++ src/buffer.h | 5 +++++ src/iterators.C | 42 ++++++++++++++++++++++++++++++++++++++++++ src/iterators.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/paragraph.C | 4 ++-- src/paragraph.h | 4 ++-- src/toc.C | 7 ++++--- src/toc.h | 10 +++++----- 9 files changed, 125 insertions(+), 12 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index f31ef9fd61..7c1a246df8 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,14 @@ +2002-11-08 John Levon + + * iterators.h: + * iterators.C: + * buffer.h: + * buffer.C: + * paragraph.h: + * paragraph.C: + * toc.h: + * toc.C: ParConstIterator, and use it (from Lars) + 2002-11-07 Ben Stanley * lyxtextclass.[Ch]: revise and add doxygen comments diff --git a/src/buffer.C b/src/buffer.C index fba66f3317..5c07e74c56 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -3343,6 +3343,18 @@ ParIterator Buffer::par_iterator_end() return ParIterator(); } +ParConstIterator Buffer::par_iterator_begin() const +{ + return ParConstIterator(&*(paragraphs.begin())); +} + + +ParConstIterator Buffer::par_iterator_end() const +{ + return ParConstIterator(); +} + + void Buffer::addUser(BufferView * u) { diff --git a/src/buffer.h b/src/buffer.h index a60d06de65..b3f1feeb0a 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -35,6 +35,7 @@ class TeXErrors; class LaTeXFeatures; class Language; class ParIterator; +class ParConstIterator; /// @@ -402,7 +403,11 @@ public: /// ParIterator par_iterator_begin(); /// + ParConstIterator par_iterator_begin() const; + /// ParIterator par_iterator_end(); + /// + ParConstIterator par_iterator_end() const; /// Inset * getInsetFromID(int id_arg) const; diff --git a/src/iterators.C b/src/iterators.C index bc1e2fc4ef..6622fd31ac 100644 --- a/src/iterators.C +++ b/src/iterators.C @@ -42,3 +42,45 @@ ParIterator & ParIterator::operator++() } return *this; } + + +ParConstIterator & ParConstIterator::operator++() +{ + while (!positions.empty()) { + ParPosition & p = positions.top(); + + // Does the current inset contain more "cells" ? + if (p.index >= 0) { + ++p.index; + Paragraph * par = p.it.getInset()->getFirstParagraph(p.index); + if (par) { + positions.push(ParPosition(par)); + return *this; + } + ++p.it; + } else + // The following line is needed because the value of + // p.it may be invalid if inset was added/removed to + // the paragraph pointed by the iterator + p.it = p.par->insetlist.begin(); + + // Try to find the next inset that contains paragraphs + InsetList::iterator end = p.par->insetlist.end(); + for (; p.it != end; ++p.it) { + Paragraph * par = p.it.getInset()->getFirstParagraph(0); + if (par) { + p.index = 0; + positions.push(ParPosition(par)); + return *this; + } + } + // Try to go to the next paragarph + if (p.par->next()) { + p = ParPosition(p.par->next()); + return *this; + } + + positions.pop(); + } + return *this; +} diff --git a/src/iterators.h b/src/iterators.h index ba85b6c46a..8bc2128a08 100644 --- a/src/iterators.h +++ b/src/iterators.h @@ -74,4 +74,46 @@ bool operator!=(ParIterator const & iter1, ParIterator const & iter2) { return !(iter1 == iter2); } + +class ParConstIterator { +public: + /// + typedef std::stack PosHolder; + /// + ParConstIterator() {} + /// + ParConstIterator(Paragraph * par) { + positions.push(ParPosition(par)); + } + /// + ParConstIterator & operator++(); + /// + Paragraph const * operator*() { + return positions.top().par; + } + /// + PosHolder::size_type size() const + { return positions.size(); } + /// + friend + bool operator==(ParConstIterator const & iter1, + ParConstIterator const & iter2); +private: + /// + PosHolder positions; +}; + + +/// +inline +bool operator==(ParConstIterator const & iter1, ParConstIterator const & iter2) { + return iter1.positions == iter2.positions; +} + +/// +inline +bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2) { + return !(iter1 == iter2); +} + #endif diff --git a/src/paragraph.C b/src/paragraph.C index bee7384f6b..d6701c8e4b 100644 --- a/src/paragraph.C +++ b/src/paragraph.C @@ -1727,7 +1727,7 @@ bool Paragraph::isMultiLingual(BufferParams const & bparams) // Convert the paragraph to a string. // Used for building the table of contents -string const Paragraph::asString(Buffer const * buffer, bool label) +string const Paragraph::asString(Buffer const * buffer, bool label) const { BufferParams const & bparams = buffer->params; string s; @@ -1755,7 +1755,7 @@ string const Paragraph::asString(Buffer const * buffer, bool label) string const Paragraph::asString(Buffer const * buffer, - pos_type beg, pos_type end, bool label) + pos_type beg, pos_type end, bool label) const { ostringstream ost; diff --git a/src/paragraph.h b/src/paragraph.h index 5317f01011..4f49050a4d 100644 --- a/src/paragraph.h +++ b/src/paragraph.h @@ -77,10 +77,10 @@ public: bool isMultiLingual(BufferParams const &); /// - string const asString(Buffer const *, bool label); + string const asString(Buffer const *, bool label) const; /// string const asString(Buffer const *, lyx::pos_type beg, lyx::pos_type end, - bool label); + bool label) const; /// void write(Buffer const *, std::ostream &, BufferParams const &, diff --git a/src/toc.C b/src/toc.C index b52154bc1b..e33f4098df 100644 --- a/src/toc.C +++ b/src/toc.C @@ -76,10 +76,11 @@ TocList const getTocList(Buffer const * buf) LyXTextClass const & textclass = buf->params.getLyXTextClass(); - ParIterator pit = buf->par_iterator_begin(); - ParIterator end = buf->par_iterator_end(); + ParConstIterator pit = buf->par_iterator_begin(); + ParConstIterator end = buf->par_iterator_end(); for (; pit != end; ++pit) { - Paragraph * par = *pit; + + Paragraph const * par = *pit; #ifdef WITH_WARNINGS #warning bogus type (Lgb) diff --git a/src/toc.h b/src/toc.h index 5af9e4959f..6f45519de4 100644 --- a/src/toc.h +++ b/src/toc.h @@ -21,7 +21,7 @@ #endif #include - + #include "support/LOstream.h" #include "LString.h" @@ -34,12 +34,12 @@ class Paragraph; /** Nice functions and objects to handle TOCs */ -namespace toc +namespace toc { /// struct TocItem { - TocItem(Paragraph * p, int d, string const & s) + TocItem(Paragraph const * p, int d, string const & s) : par(p), depth(d), str(s) {} /// string const asString() const; @@ -48,7 +48,7 @@ struct TocItem { /// the action corresponding to the goTo above int action() const; /// - Paragraph * par; + Paragraph const * par; /// int depth; /// @@ -68,7 +68,7 @@ std::vector const getTypes(Buffer const *); /// void asciiTocList(string const &, Buffer const *, std::ostream &); - + /** Given the cmdName of the TOC param, returns the type used by ControlToc::getContents() */ string const getType(string const & cmdName);