1

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!

manlio
  • 18,345
  • 14
  • 76
  • 126
mikez
  • 160
  • 1
  • 15
  • I don't see you supplying your hash function anywhere in the instantiation of the hashtable... – RedX Apr 16 '12 at 08:14

2 Answers2

1

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

Alexandre Hamez
  • 7,725
  • 2
  • 28
  • 39
1

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_); }
};
igaztanaga
  • 161
  • 3