help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Difficulty: Wrap hash set and hash set iterators


From: benjamin . bihler
Subject: Difficulty: Wrap hash set and hash set iterators
Date: Wed, 04 Jul 2007 12:59:01 -0700
User-agent: G2/1.0

Hi,


I have written a class that wraps __gnu_cxx::hash_set's. It works fine
now (thanks for your help!). It is implemented with templates so that
I can use it for different storage types. I would like to use an
iterator over the hash set. But that is quite complicated.

The STL-Iterator of my hash set as I have defined it in my wrapper
class is:

typename __gnu_cxx::hash_set< PointerTemplate, pointerHashFunction,
pointerEqualityFunction >::iterator
                        m_hsitIterator;

with my own implementations of "pointerHashFunction" and
"pointerEqualityFunction". I don't want to write a getter for that
iterator, since I would loose the encapsulation if the caller of the
getter needs to know exactly the type definition of the iterator.

I could of course implement some methods "setToFirstElement",
"hasMoreElements", "nextElement" and use the iterator only internally.
But there is the danger, that there is only one internal iterator and
if that iterator is used from more than one places, it will produce
nonsense results! And it is quite probable, that my object is used in
a lot of places at the same time. :-(

Do you know a good solution for that?

I have tried the following: I have created a position object, that
stores an integer position number and a boolean bHasNext. The hash set
object has methods "getBeginPosition()" and "getNext( Position
position )". The advantage: this concept is very general, since I
could use a position number for every collection type (and I intend to
do that for a hash map wrapper class and so on).

I can use these methods now like that:

        CTWTHashPosition position = m_hsHashSet.getBegin();

        while (position.bHasNext() )
        {
                CTWTProperty *pProperty = m_hsHashSet.getNext(position );
...
        }

The method getNext sets an internal iterator to the first element of
the hash set and advances by the position number. Then it is set to
the current position. From here it is clear, what it must do. But I am
not sure, whether "advance()" is fast enough!

Very probably advance() steps through the hash set element by element
and this will make my method very, very slow for large hash sets. Is
that true? Is there a better way to advance? Or do you know a better
concept?

------------------------------------------------------------------------------

template<class PointerTemplate> PointerTemplate
CTWTHashSet<PointerTemplate>::getNext( CTWTHashPosition &position )
{
        typename __gnu_cxx::hash_set< PointerTemplate, pointerHashFunction,
pointerEqualityFunction >::iterator
                                setIterator = m_hsPointerSet.begin();

        // Is this method fast??? Otherwise change it!
        advance( setIterator, position.getPosition());

        if( setIterator == m_hsPointerSet.end() )
        {
                throw "GetNext may not be called for the last element.";
        }

        PointerTemplate positionObject = *setIterator;

        // Advance
        setIterator++;

        // Increment position
        position.setPosition( position.getPosition() + 1 );

        if( setIterator == m_hsPointerSet.end() )
        {
                position.setHasNext( false );
        }
        else
        {
                position.setHasNext( true );
        }

        return positionObject;
}

------------------------------------------------------------------------------


Thank you very much and bye,
Benjamin Bihler



reply via email to

[Prev in Thread] Current Thread [Next in Thread]