Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using a map where value is std::shared_ptr a good design choice for having multi-indexed lists of classes?

problem is simple: We have a class that has members a,b,c,d... We want to be able to quickly search(key being value of one member) and update class list with new value by providing current value for a or b or c ... I thought about having a bunch of
std::map<decltype(MyClass.a/*b,c,d*/),shared_ptr<MyClass>>.

1) Is that a good idea?

2) Is boost multi index superior to this handcrafted solution in every way?

PS SQL is out of the question for simplicity/perf reasons.

like image 556
NoSenseEtAl Avatar asked Sep 26 '12 17:09

NoSenseEtAl


2 Answers

  1. Boost MultiIndex may have a distinct disadvantage that it will attempt to keep all indices up to date after each mutation of the collection. This may be a large performance penalty if you have a data load phase with many separate writes.

  2. The usage patterns of Boost Multi Index may not fit with the coding style (and taste...) of the project (members). This should be a minor disadvantage, but I thought I'd mention it

  3. As ildjarn mentioned, Boost MI doesn't support move semantics as of yet

Otherwise, I'd consider Boost MultiIndex superior in most occasions, since you'd be unlikely to reach the amount of testing it received.

like image 119
sehe Avatar answered Sep 21 '22 01:09

sehe


You want want to consider containing all of your maps in a single class, arbitrarily deciding on one of the containers as the one that stores the "real" objects, and then just use a std::map with a mapped type of raw pointers to elements of the first std::map.

This would be a little more difficult if you ever need to make copies of those maps, however.

like image 42
David Stone Avatar answered Sep 20 '22 01:09

David Stone