Question

I have a multi_index with multiple hashed_unique indixes Indexes(A,B,C).
Not all insertion to hash contain information for all indexes. Some of the indexes may be empty. And some may contain all entries.
I defined them hashed_unique because if it's not empty, it is unique.
I could define it as hashed_non_unique and put each key in a wrapper that tell if key is empty as proposed in Boost Multi_Index Question
But then i will need to search for each key, before trying to insert/modify/replace to avoid a case of non_unique keys, which is not good performance solution.
Is there a better solution?
If i defined all indexes as hashed_unique it won't accept insertion of more than one empty value per index.

For example:

(11,6,8)
(10,3,empty)
(empty,4,empty)
(empty,empty,1)

I'm using boost 1.49 and the multi index is in shared memory.

Was it helpful?

Solution

You can use the following wrapper:

#include <boost/variant.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/functional/hash.hpp>

template<typename T>
class maybe
{
public:
  maybe():impl(boost::make_shared<int>(0)){}
  maybe(const T& t):impl(t){}

  bool empty()const{return impl.which()==0;}
  T& get(){return boost::get<T>(impl);}
  const T& get()const{return boost::get<T>(impl);}

  friend bool operator==(const maybe& x,const maybe& y){return x.impl==y.impl;}
  friend std::size_t hash_value(const maybe& x){return boost::hash<impl_type>()(x.impl);}

private:
  typedef boost::variant<boost::shared_ptr<void>,T> impl_type;

  impl_type impl;
};

...

maybe<int> x=5;
maybe<int> y;  // empty
maybe<int> z;  // empty

The nice thing about maybe is that empty values are different (that, is, !(y==z) in the example), so you can keep your Boost.MultiIndex indices unique.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top