Boost logo

Boost Users :

From: Julie Larson (julie.larson_at_[hidden])
Date: 2008-05-14 00:07:23


Hello,

I'm using boost 1.35.0 and found this sample code about the hash set
functionality:

http://www.boost.org/doc/libs/1_35_0/doc/html/intrusive/advanced_lookups_insertions.html

and modified it as shown below. I find that when iterating through
the hash table, the thing crashes here (where the get_next function
contains an invalid value for n):

template<class VoidPointer> struct slist_node_traits
{
....
   static node_ptr get_next(const_node_ptr n)
----------> { return n->next_; }
....
};

Should I use an earlier more stable version of boost or am I doing
something dreadfully wrong?

Thank you for any insights
-Julie

#include <stdio.h>
#include <string>
#include <iostream> // for std::cout
#include <boost/foreach.hpp>
#include <boost/intrusive/unordered_set.hpp>

using namespace boost::intrusive;

struct CStringHasher
{
        std::size_t operator()(const std::string& str) const
        {
                std::size_t seed = 0;
                const char* sz = str.c_str();
                if ( ! sz )
                        return seed;
                for(; *sz; ++sz)
                        boost::hash_combine(seed, *sz);
                return seed;
        }
};

class CWord : public unordered_set_base_hook<>
{ //This is a derivation hook
public:
        std::string m_strWord;
        int m_nID;

        unordered_set_member_hook<> member_hook_; //This is a member hook

        CWord( const std::string& strWord, int nID ) : m_strWord( strWord ),
m_nID( nID ) {}

        friend bool operator== (const CWord& a, const CWord& b)
        { return a.m_strWord == b.m_strWord; }

        friend std::size_t hash_value(const CWord& value)
        { return CStringHasher()(value.m_strWord.c_str()); }
};

class CWordHash
{
public:
        typedef unordered_set<CWord> word_hash_set;
        typedef word_hash_set::bucket_type word_bucket_type;
        typedef word_hash_set::bucket_traits word_bucket_traits;
        typedef word_hash_set::iterator word_iter;

        CWordHash() : m_hWords( word_bucket_traits( m_Buckets, s_nNumBuckets ) ) {}
        ~CWordHash()
        {
                BOOST_FOREACH( CWord& w, m_hWords )
                {
                        CWord* pWord = &w;
                        delete pWord;
                }
        }

        struct CStrExpensiveClassEqual
        {
           bool operator()(const std::string& str, const CWord &c) const {
return str == c.m_strWord; }
           bool operator()(const CWord& c, const std::string& str) const {
return str == c.m_strWord; }
        };

        CWord* get( const std::string& str )
        {
                word_iter it = m_hWords.find( str, CStringHasher(),
CStrExpensiveClassEqual());

                if( it == m_hWords.end() )
                        return NULL;

                return &*it;
        }

        bool insert( const std::string& strKey, int nID )
        {
                word_hash_set::insert_commit_data insert_data;
                bool bSuccess = m_hWords.insert_check( strKey, CStringHasher(),
CStrExpensiveClassEqual(), insert_data).second;
                if ( bSuccess )
                {
                        CWord* pWord = new CWord(strKey, nID);
                        m_hWords.insert_commit(*pWord, insert_data);
                }
                return bSuccess;
        }

        static const int s_nNumBuckets = 10000;
        word_bucket_type m_Buckets[s_nNumBuckets];
        word_hash_set m_hWords;
};

extern int main(int argc, char *argv[])
{
        CWordHash hWords; // hash string to word id
        int nID = 1;
        std::string strWord = "hello";
        hWords.insert( strWord, nID );
        nID = 2;
        strWord = "world";
        hWords.insert( strWord, nID );

        for ( CWordHash::word_hash_set::iterator it =
hWords.m_hWords.begin(); it != hWords.m_hWords.end(); it++ )
        {
                CWord& w = *it;
                std::cout << w.m_strWord << "\t" << w.m_nID << "\n";
        }

        return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net