1
votes

Can anyone provide a simple example of how to use the Boost Intrusive Hashtable? I've tried to implement it, but I'm having little luck.

I have this so far

void HashTableIndex::addToIndex(Message* message)
{
hashtable<MyMessageVector>::bucket_type base_buckets[10000];
hashtable<MyMessageVector> htable(hashtable<MyMessageVector>::bucket_traits(base_buckets, 10000));
boost::array<MyMessageVector,10000> items; 
htable.insert_unique(items[0]);

but for some reason it's not calling my Hash function which is defined above like this

size_t HashTableIndex::hash_value(MyMessageVector& b)
{
    boost::hash<string> hasher;
    return hasher(b.getKey());
};

For some reason it won't call my hash_value function. Any help on this would be much appreciated!

2
I don't see you supplying your hash function anywhere in the instantiation of the hashtable...RedX

2 Answers

1
votes

You can supply the hash function to the hash table using boost::intrusive::hash in the list of options.

1
votes

You are using a member function and boost::hash requires a free function. See boost::hash documentation:

namespace library
{
    std::size_t hash_value(book const& b)
    {
        boost::hash<int> hasher;
        return hasher(b.id);
    }
}

You can also use a "friend" function declared in the class as shown in the Boost.Intrusive unordered_set documentation:

class MyClass
{
   //...
   public:
   friend bool operator== (const MyClass &a, const MyClass &b)
   {  return a.int_ == b.int_;  }

   friend std::size_t hash_value(const MyClass &value)
   {  return std::size_t(value.int_); }
};