I use boost :: multi_index_container to provide random access and hash access to a set of elements. I would like to change the random access index of an element without changing the index of the hash function.
Here is the code snippet:
# include <string>
# include <boost/multi_index_container.hpp>
# include <boost/multi_index/random_access_index.hpp>
# include <boost/multi_index/hashed_index.hpp>
# include <boost/multi_index/member.hpp>
using namespace std ;
using namespace boost ;
using namespace boost::multi_index ;
class Element
{
public :
Element(const string & new_key) : key(new_key) {}
string key ;
private :
} ;
typedef multi_index_container<
Element,
indexed_by<
random_access< >,
hashed_unique<
member<Element, string, &Element::key>
>
>
> ElementContainer ;
typedef ElementContainer::nth_index<0>::type::iterator ElementRandomIter ;
typedef ElementContainer::nth_index<1>::type::iterator ElementHashedIter ;
int main(int, char*[])
{
ElementContainer ec ;
ec.push_back(Element("Alice")) ;
ec.push_back(Element("Bob")) ;
ec.push_back(Element("Carl")) ;
ec.push_back(Element("Denis")) ;
ElementRandomIter it = ec.get<0>().begin() + 3 ;
Element e = *(it) ;
ec.get<0>().erase(it) ;
it = ec.get<0>().begin() + 1 ;
ec.get<0>().insert(it, e) ;
return 0 ;
}
I know that even if I used only random access iterators in this example to control the elements, hashing happens behind the scenes at least twice multi_index_containerin addition to copying the object, which can be expensive.
Is there a way to change the random access index of an item inside boost::multi_indexwithout requiring expensive delete and paste-save-copy utilities?
multi_index_container, , - . !
: :)