Boost logo

Boost Users :

Subject: [Boost-users] Multi Index Insertion with hashed index - non-primitive key
From: Jason Leger (jasonoleger_at_[hidden])
Date: 2009-07-31 10:00:51


Hey Everyone,

I am basically trying to create a sorted hash map using boost multiindex. I
am following the examples but I am unable to insert my object into the
container when I use a non-primitive key.

Is it possible to use a non-primitive key?

Any help or pointers would be appreciated.

Thanks

J.O.

#include <inttypes.h>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>

using namespace ::boost;
using namespace ::boost::multi_index;

#include <iostream>

struct Key
{
    uint32_t k1;
    uint32_t k2;

    Key(uint32_t k1, uint32_t k2) : k1(k1), k2(k2) {}

    bool operator==(const Key &key) const
    {
        if (k1 != key.k1) return false;
        if (k2 != key.k2) return false;
        return true;
    }
};

struct KeyEqual
{
  bool operator()(const Key &key1, Key &key2) const
  {
      return key1 == key2;
  }
};

struct KeyHash
{
    std::size_t operator()(const Key& key)const
    {
        return boost::hash<uint32_t>()(key.k1) +
boost::hash<uint32_t>()(key.k2);
    }
};

struct Data
{
    Key key;
    int value;

    Data(const Key& key, int value): key(key), value(value) {}

    bool operator<(const Data& data)const
    {
        return (value < data.value);
    }
};

typedef multi_index_container<
  Data,
  indexed_by<
    // sort by Data::operator<
    ordered_unique<identity<Data> >,

    // hashed on key
    hashed_unique<member<Data,Key,&Data::key> >
>
> DataSet;

typedef DataSet::nth_index<1>::type HashIndex;

int main()
{
    DataSet dataSet;
    HashIndex &hashIndex = dataSet.get<1>();

    Key newKey(1,2);
    Data newData(newKey,3);

    dataSet.insert(newData); // *** Error - See below ***

    // find an Data by key
    Data data=*(hashIndex.find(newKey,KeyHash(),KeyEqual()));

};

This is the error I get:

g++ -I../include -rdynamic -DCOREDUMP -DLINUX -pthread -pipe -Wall -W -g
-DDEBUG -c mIndex.cc -o mIndex.o
mIndex.cc: In member function ‘size_t boost::hash<T>::operator()(const T&)
const [with T = Key]’:
/usr/include/boost/multi_index/hashed_index.hpp:957: instantiated from
‘void boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred,
SuperMeta, TagList, Category>::unchecked_rehash(size_t) [with KeyFromValue =
boost::multi_index::member<Data, Key, &Data::key>, Hash = boost::hash<Key>,
Pred = std::equal_to<Key>, SuperMeta =
boost::multi_index::detail::nth_layer<2, Data,
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0<mpl_::na>,
Category = boost::multi_index::detail::hashed_unique_tag]’
/usr/include/boost/multi_index/hashed_index.hpp:942: instantiated from
‘void boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred,
SuperMeta, TagList, Category>::reserve(size_t) [with KeyFromValue =
boost::multi_index::member<Data, Key, &Data::key>, Hash = boost::hash<Key>,
Pred = std::equal_to<Key>, SuperMeta =
boost::multi_index::detail::nth_layer<2, Data,
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0<mpl_::na>,
Category = boost::multi_index::detail::hashed_unique_tag]’
/usr/include/boost/multi_index/hashed_index.hpp:629: instantiated from
‘boost::multi_index::detail::hashed_index_node<typename
SuperMeta::type::node_type>*
boost::multi_index::detail::hashed_index<KeyFromValue, Hash, Pred,
SuperMeta, TagList, Category>::insert_(typename boost::call_traits<typename
boost::multi_index::detail::hashed_index_node<typename
SuperMeta::type::node_type>::value_type>::param_type,
boost::multi_index::detail::hashed_index_node<typename
SuperMeta::type::node_type>*) [with KeyFromValue =
boost::multi_index::member<Data, Key, &Data::key>, Hash = boost::hash<Key>,
Pred = std::equal_to<Key>, SuperMeta =
boost::multi_index::detail::nth_layer<2, Data,
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0<mpl_::na>,
Category = boost::multi_index::detail::hashed_unique_tag]’
/usr/include/boost/multi_index/ordered_index.hpp:556: instantiated from
‘boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>*
boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta,
TagList, Category>::insert_(typename boost::call_traits<typename
boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>::value_type>::param_type,
boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>*) [with KeyFromValue =
boost::multi_index::identity<Data>, Compare = std::less<Data>, SuperMeta =
boost::multi_index::detail::nth_layer<1, Data,
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0<mpl_::na>,
Category = boost::multi_index::detail::ordered_unique_tag]’
/usr/include/boost/multi_index_container.hpp:465: instantiated from
‘std::pair<typename boost::multi_index::detail::multi_index_base_type<Value,
IndexSpecifierList, Allocator>::type::node_type*, bool>
boost::multi_index::multi_index_container<Value, IndexSpecifierList,
Allocator>::insert_(const Value&) [with Value = Data, IndexSpecifierList =
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, Allocator = std::allocator<Data>]’
/usr/include/boost/multi_index/detail/index_base.hpp:148: instantiated
from ‘std::pair<typename
boost::multi_index::detail::multi_index_node_type<Value, IndexSpecifierList,
Allocator>::type*, bool> boost::multi_index::detail::index_base<Value,
IndexSpecifierList, Allocator>::final_insert_(typename
boost::call_traits<Value>::param_type) [with Value = Data,
IndexSpecifierList =
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, Allocator = std::allocator<Data>]’
/usr/include/boost/multi_index/ordered_index.hpp:252: instantiated from
‘std::pair<boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>, mpl_::na>, bool>
boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta,
TagList, Category>::insert(typename boost::call_traits<typename
boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>::value_type>::param_type) [with KeyFromValue =
boost::multi_index::identity<Data>, Compare = std::less<Data>, SuperMeta =
boost::multi_index::detail::nth_layer<1, Data,
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<Data>,
mpl_::na, mpl_::na>,
boost::multi_index::hashed_unique<boost::multi_index::member<Data, Key,
&Data::key>, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0<mpl_::na>,
Category = boost::multi_index::detail::ordered_unique_tag]’
mIndex.cc:94: instantiated from here
mIndex.cc:79: error: ‘struct hash_value’ is not a function,
/usr/include/boost/functional/hash/hash.hpp:290: error: conflict with
‘template<class K, class T, class C, class A> size_t boost::hash_value(const
std::multimap<K, T, C, A>&)’
/usr/include/boost/functional/hash/hash.hpp:487: error: in call to
‘hash_value’
/usr/include/boost/functional/hash/hash.hpp:487: error: no matching function
for call to ‘hash_value(const Key&)’
/usr/include/boost/functional/hash/hash.hpp:105: note: candidates are:
size_t boost::hash_value(int)
/usr/include/boost/functional/hash/hash.hpp:110: note:
size_t boost::hash_value(unsigned int)
/usr/include/boost/functional/hash/hash.hpp:115: note:
size_t boost::hash_value(long int)
/usr/include/boost/functional/hash/hash.hpp:120: note:
size_t boost::hash_value(long unsigned int)
/usr/include/boost/functional/hash/hash.hpp:229: note:
size_t boost::hash_value(float)
/usr/include/boost/functional/hash/hash.hpp:234: note:
size_t boost::hash_value(double)
/usr/include/boost/functional/hash/hash.hpp:239: note:
size_t boost::hash_value(long double)
make: *** [mIndex.o] Error 1



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