Alfredo Braunstein wrote:

> Is this ok (attached)?

oops wrong patch (I sent the same old one). Hmmmm... retry.
 
> a
> 
> PosIterator ParIterator::asPosIterator(lyx::pos_type)
> 
> member, made friend in class PosIterator


Alfredo
? PosIterator.C-save
? PosIterator.h-save
? all.diff
? bfs.cpp
? files
? save
? frontends/screen.C-save
Index: Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/Makefile.am,v
retrieving revision 1.197
diff -u -p -u -r1.197 Makefile.am
--- Makefile.am	29 Oct 2003 10:47:12 -0000	1.197
+++ Makefile.am	2 Nov 2003 16:44:35 -0000
@@ -228,6 +228,8 @@ lyx_SOURCES = \
 	paragraph.h \
 	paragraph_pimpl.C \
 	paragraph_pimpl.h \
+	PosIterator.h \
+	PosIterator.C \
 	SpellBase.h \
 	ispell.C \
 	ispell.h \
Index: PosIterator.C
===================================================================
RCS file: PosIterator.C
diff -N PosIterator.C
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ PosIterator.C	2 Nov 2003 16:44:35 -0000
@@ -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 PosIterator::operator!=(PosIterator const & a) const
+{
+	return !operator==(a);
+}
+
+
+bool PosIterator::operator==(PosIterator const & a) const 
+{
+	
+	PosIteratorItem const & pa = a.stack_.top();
+	PosIteratorItem const & p = stack_.top();
+	
+	return (pa.pl == p.pl && pa.pit == p.pit &&
+		(p.pit == p.pl->end() || pa.pos == p.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));
+}
Index: PosIterator.h
===================================================================
RCS file: PosIterator.h
diff -N PosIterator.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ PosIterator.h	2 Nov 2003 16:44:35 -0000
@@ -0,0 +1,67 @@
+// -*- 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--();
+	bool operator!=(PosIterator const &) const;
+	bool operator==(PosIterator const &) 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);
+	
+private:
+	PosIterator() {};
+	std::stack<PosIteratorItem> stack_;
+};
+
+
+#endif
+
Index: buffer.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.C,v
retrieving revision 1.538
diff -u -p -u -r1.538 buffer.C
--- buffer.C	31 Oct 2003 18:45:34 -0000	1.538
+++ buffer.C	2 Nov 2003 16:44:37 -0000
@@ -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"
@@ -1469,6 +1470,18 @@ bool Buffer::hasParWithID(int id) const
 			return true;
 
 	return false;
+}
+
+
+PosIterator Buffer::pos_iterator_begin()
+{
+	return PosIterator(&paragraphs(), paragraphs().begin(), 0);	
+}
+
+
+PosIterator Buffer::pos_iterator_end()
+{
+	return PosIterator(&paragraphs(), paragraphs().end(), 0);	
 }
 
 
Index: buffer.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.h,v
retrieving revision 1.170
diff -u -p -u -r1.170 buffer.h
--- buffer.h	31 Oct 2003 18:45:34 -0000	1.170
+++ buffer.h	2 Nov 2003 16:44:38 -0000
@@ -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();
 	///
Index: iterators.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/iterators.C,v
retrieving revision 1.23
diff -u -p -u -r1.23 iterators.C
--- iterators.C	29 Oct 2003 12:18:06 -0000	1.23
+++ iterators.C	2 Nov 2003 16:44:38 -0000
@@ -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>
@@ -355,4 +357,21 @@ bool operator==(ParConstIterator const &
 bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2)
 {
 	return !(iter1 == iter2);
+}
+
+
+PosIterator ParIterator::asPosIterator(lyx::pos_type pos)
+{
+	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;
 }
Index: iterators.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/iterators.h,v
retrieving revision 1.19
diff -u -p -u -r1.19 iterators.h
--- iterators.h	29 Oct 2003 12:18:06 -0000	1.19
+++ iterators.h	2 Nov 2003 16:44:39 -0000
@@ -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);
 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_;

Reply via email to