Topic: iterator for hierarchial class template
Author: "Matt" <devmail@gmx.net>
Date: 2000/11/19 Raw View
I've got a hierarchial template like this one :
template<class T>
class Tree
{
// ...
// pure virtual access funcs
};
template<class T>
class Leaf : public Tree<T>
{
// ...
private:
T* data;
};
template<class T>
class Node : public Tree<T>
{
// ...
private:
vector<Tree*> members;
};
so far so good.
Now I want to add find functionality (amongst others). I could do that by
using a recursive virtual function find(...) in Tree, but I thought it'd be
nice if I could add iterator functionality, so that I can use the STL
algorithms on my tree.
But it seems that iterator functionality is the hell to implement in a
hierarchial structure. The implementation I found was just too complicated &
unperformant - I needed 4 additional classes (one extern iterator class, an
abstract intern iterator base and for each class a derived one....) and
these iterators needed to keep a vector of previous iterators with them....
So I wondered if someone has already done sth. like that -
maybe I just missed the most obvious and easy implementation.
thx in advance,
Matt
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "James Montgomerie" <tetrizdg0qm001@sneakemail.com>
Date: 2000/11/20 Raw View
----- Original Message -----
From: "Matt" <devmail@gmx.net>
Subject : iterator for hierarchial class template
> Now I want to add find functionality (amongst others). I could do that by
> using a recursive virtual function find(...) in Tree, but I thought it'd
be
> nice if I could add iterator functionality, so that I can use the STL
> algorithms on my tree.
> But it seems that iterator functionality is the hell to implement in a
> hierarchial structure. The implementation I found was just too complicated
&
> unperformant - I needed 4 additional classes (one extern iterator class,
an
> abstract intern iterator base and for each class a derived one....) and
> these iterators needed to keep a vector of previous iterators with
them....
> So I wondered if someone has already done sth. like that -
> maybe I just missed the most obvious and easy implementation.
> thx in advance,
> Matt
I've tried something like that before. It's a class templated on an adapter
you can write for any tree structure and provides iterators for that tree.
You can also provide template parameters to to specify agenda-based
traversal patterns (it's very easy to do breadth of depth first traversal).
You can see it at
http://www.montgomerie.net/tEmPlate.cgi/downloads/treetraverser/. I'm
afraid it's in an 'almost standards compliant, worked on MSVC and the
version of GCC I was using at the time' state at the moment, I hope to get
an updated version up soon. Due to the performance and storage
'limitations' you mentioned, it only provides forward-iterators, but they're
enough for my needs (do you really need to walk a tree backwards?).
Jamie.
P.S. my service provider seems to be experiencing problems, you might need
to hit refresh a few times before you see the page.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]