Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost MultiIndex - objects or pointers (and how to use them?)?

Tags:

c++

hash

boost

I'm programming an agent-based simulation and have decided that Boost's MultiIndex is probably the most efficient container for my agents. I'm not a professional programmer, and my background is very spotty. I've two questions:

  1. Is it better to have the container contain the agents (of class Host) themselves, or is it more efficient for the container to hold Host *? Hosts will sometimes be deleted from memory (that's my plan, anyway... need to read up on new and delete). Hosts' private variables will get updated occasionally, which I hope to do through the modify function in MultiIndex. There will be no other copies of Hosts in the simulation, i.e., they will not be used in any other containers.
  2. If I use pointers to Hosts, how do I set up the key extraction properly? My code below doesn't compile.
// main.cpp - ATTEMPTED POINTER VERSION
...
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/tokenizer.hpp>

typedef multi_index_container<
  Host *,
  indexed_by< 
    // hash by Host::id
    hashed_unique< BOOST_MULTI_INDEX_MEM_FUN(Host,int,Host::getID) > // arg errors here
    > // end indexed_by
  > HostContainer;

...
int main() {

   ...
   HostContainer testHosts;
   Host * newHostPtr;
   newHostPtr = new Host( t, DOB, idCtr, 0, currentEvents );
   testHosts.insert( newHostPtr );
   ... 
}

I can't find a precisely analogous example in the Boost documentation, and my knowledge of C++ syntax is still very weak. The code does appear to work when I replace all the pointer references with the class objects themselves.


As best I can read it, the Boost documentation (see summary table at bottom) implies I should be able to use member functions with pointer elements.

like image 446
Sarah Avatar asked Apr 15 '10 19:04

Sarah


1 Answers

If Host contains lot of data you could use shared_ptr to avoid copying. You could use MultiIndex with shared_ptr in it:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::multi_index;

struct Host
{
   int get_id() const { return id; }
 private:
   int id;
   // more members here
};

typedef multi_index_container<
  boost::shared_ptr<Host>,    // use pointer instead of real Host
  indexed_by< 
    // hash using function Host::get_id
    hashed_unique< const_mem_fun<Host, int, &Host::get_id> >
    > // end indexed_by
  > HostContainer;

Then you could use it as follows:

int main()
{
   HostContainer testHosts;
   Host * newHostPtr;
   newHostPtr = new Host;
   testHosts.insert( boost::shared_ptr<Host>(newHostPtr) );

  return 0;
}
like image 149
Kirill V. Lyadvinsky Avatar answered Sep 20 '22 04:09

Kirill V. Lyadvinsky