> wish list:
> -no additional memory cost for each item
> -keeps double-link list structures (no vectors, etc...)
> -no supplementary indirection cost allowed
> -no more "typing" when using it
> -a solution that is simple, logical and semantically elegant
I have a very large group of items which are linked to each other by different double link lists and trees using members inside each item.
what we really want to have is a sort of "super-iterator" to iterate through the items in different ways
Yes, what we want are ways to access these items as different sequences. A set of polymorphic iterators which can iterate through the same items as different sequences.
there is an additional (invisible) indirection created by the virtual dispatch when you move using the super_iterator.
So we will make them polymorphic at compile-time using the earlier pointer to member idea. The example below is just for the doubly linked list like sequences.
#include <iostream>
#include <iterator>
#include <algorithm>
// iterator over a sequence using pointers to members
// for next element and prev element
template< typename T, T* T::*NEXT, T* T::*PREV >
struct list_iterator : std::iterator<std::bidirectional_iterator_tag,T*>
{
list_iterator( T* curr, T* last ) : current(curr), last_element(last) {}
T*& operator* () { return current ; }
list_iterator& operator++ ()
{ current = current->*NEXT ; return *this ; }
list_iterator& operator-- ()
{ current = current ? current->*PREV : last_element ; return *this ; }
list_iterator operator++ …