multi index container

Hi all, I have to store pointer of type (let's say) "employee" in a multi-index-container: struct employee { employee(int id):id(id){} int id; }; Further I have to check if every id is unique: employee* e0 = new employee(1); employee* e1 = e0; employee* e2 = new employee(1); employee* e3 = new employee(2); my_multi_index_conatiner.insert(e0); // OK my_multi_index_conatiner.insert(e1); // BAD my_multi_index_conatiner.insert(e2); // BAD my_multi_index_conatiner.insert(e0); // OK Can someone tell me how a typedef of a multi_index_container fullfilling prerequisities above looks? Or is a multi_index_container not the right tool? I've tried with a method in struct employee which returns the this-pointer. But this-pointer is const const and useless for my problem. Thanks in advance!

Dejan escribió:
Hi all,
I have to store pointer of type (let's say) "employee" in a multi-index-container:
struct employee { employee(int id):id(id){} int id; };
Further I have to check if every id is unique:
employee* e0 = new employee(1); employee* e1 = e0; employee* e2 = new employee(1); employee* e3 = new employee(2);
my_multi_index_conatiner.insert(e0); // OK my_multi_index_conatiner.insert(e1); // BAD my_multi_index_conatiner.insert(e2); // BAD my_multi_index_conatiner.insert(e0); // OK
(I guess you meant e3 in the line above.)
Can someone tell me how a typedef of a multi_index_container fullfilling prerequisities above looks?
The following would do: typedef multi_index_container< employee*, indexed_by< ordered_unique< member<employee,int,&employee::id> > >
multi_t;
The lib is smart enough that can index by employee::id even if what you store are not employees proper, but pointers to employee.
Or is a multi_index_container not the right tool?
Using Boost.MultiIndex might be overkill here if this is all you need from it: an std::set can be used also with a suitable compare predicate. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

(I guess you meant e3 in the line above.)
Yes, my fault.
Can someone tell me how a typedef of a multi_index_container fullfilling prerequisities above looks?
The following would do:
typedef multi_index_container< employee*, indexed_by< ordered_unique< member<employee,int,&employee::id> > >
multi_t;
The lib is smart enough that can index by employee::id even if what you store are not employees proper, but pointers to employee.
Ahh, ok.
Or is a multi_index_container not the right tool?
Using Boost.MultiIndex might be overkill here if this is all you need from it: an std::set can be used also with a suitable compare predicate.
I don't know if finding pointer OR id is possible in a std::set. I've tried a std::set with a compare class (which compares pointer and id). To store is not a problem but to "container.find(pointer)" AND "container.find(id)". Thanks

Dejan escribió:
Or is a multi_index_container not the right tool?
Using Boost.MultiIndex might be overkill here if this is all you need from it: an std::set can be used also with a suitable compare predicate.
I don't know if finding pointer OR id is possible in a std::set. I've tried a std::set with a compare class (which compares pointer and id). To store is not a problem but to "container.find(pointer)" AND "container.find(id)".
OK, if you need to find elements by two different criteria then this is Boost.MultiIndex is designed for. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

OK, if you need to find elements by two different criteria then this is Boost.MultiIndex is designed for. What is the correct index for a multi_index_container to search for its elements as pointer?
Actual I have this: typedef multi_index_container < employee *, indexed_by < // WRONG: // ordered_unique<member>employee, emplyoee*, &employee> >, // OK ordered_unique<member<employee,int,&employee::id> >
employee_set;

struct employee { employee(int id):id(id){} int id; };
Further I have to check if every id is unique:
employee* e0 = new employee(1); employee* e1 = e0; employee* e2 = new employee(1); employee* e3 = new employee(2);
my_multi_index_conatiner.insert(e0); // OK my_multi_index_conatiner.insert(e1); // BAD my_multi_index_conatiner.insert(e2); // BAD my_multi_index_conatiner.insert(e0); // OK
Can someone tell me how a typedef of a multi_index_container fullfilling prerequisities above looks?
typedef mi::multi_index_container< employee *, mi::indexed_by< mi::ordered_unique<mi::member<employee, int, &employee::id> >
employee_set;

typedef mi::multi_index_container< employee *, mi::indexed_by< mi::ordered_unique<mi::member<employee, int, &employee::id> >
employee_set;
Maybe i should mention that I search for something like this: employee_set.find(employee* pointer); and employee_set.find(int id); Any hints? Thanks

Dejan escribió:
typedef mi::multi_index_container< employee *, mi::indexed_by< mi::ordered_unique<mi::member<employee, int, &employee::id> >
employee_set;
Maybe i should mention that I search for something like this:
employee_set.find(employee* pointer);
and
employee_set.find(int id)
Now that I think again about it, when you write: employee* p=... employee_set.find(p); Can't you just write this employee* p=... employee_set.find(p->id); and be done with only one lookup criterion? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

________________________________________ De: boost-users-bounces@lists.boost.org [boost-users-bounces@lists.boost.org] En nombre de Dejan [xamiw@arcor.de] Enviado el: miércoles, 03 de febrero de 2010 18:18 Para: boost-users@lists.boost.org Asunto: Re: [Boost-users] multi index container [solved]
Got it: [...]
That's right, but did you consider what I commented in my previous post? "Now that I think again about it, when you write: employee* p=... employee_set.find(p); Can't you just write this employee* p=... employee_set.find(p->id); and be done with only one lookup criterion?" Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

That's right, but did you consider what I commented in my previous post?
Yeah, I did but the variable id is coming from outside (user) and not from inside (software). So p->id is never used. The user can enter any kind of id (e.g. non-existend id) and with this id the object is found (or not found) and edited (by the user). The software uses pointer to find the variable if something inside changes. E.g. a member inside a object changes (by usage of a pointer outside the object). This member will notify all objects where it is a member (via pointer). But I have again a problem. I can't get the member variable (like id in this example) directly. I have to use get_id(). When defining: typedef multi_index_container < employee*, indexed_by < ordered_unique<identity<employee*> >, // next line does not work ordered_unique<member<employee, int, &employee::get_id()> >
employee_set;
The code does not compile: error: ‘Employee::get_id() const’ cannot appear in a constant-expression error: a function call cannot appear in a constant-expression How can I fix it?

Dejan <xamiw <at> arcor.de> writes:
That's right, but did you consider what I commented in my previous post?
Yeah, I did but the variable id is coming from outside (user) and not from inside (software). So p->id is never used.
The idea is to implement it the other way around, i.e. sorting by id: * When a user enters an id, you just look for it. * When the software has a pointer p to look for, it simply has to find(p->id) Clearer now?
[...]
But I have again a problem. I can't get the member variable (like id in this example) directly. I have to use get_id(). When defining:
typedef multi_index_container < employee*, indexed_by < ordered_unique<identity<employee*> >,
// next line does not work ordered_unique<member<employee, int, &employee::get_id()> >
employee_set;
The code does not compile: error: ‘Employee::get_id() const’ cannot appear in a constant-expression error: a function call cannot appear in a constant-expression
You have to use const_mem_fun rather than member: http://www.boost.org/libs/multi_index/doc/tutorial/key_extraction.html#const... Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

I may be simplifying this a bit. But it sounds like a std::map problem. hmm, to "a std:: ..." or "an std:: ..." On 3 February 2010 14:30, Dejan <xamiw@arcor.de> wrote:
Hi all,
I have to store pointer of type (let's say) "employee" in a multi-index-container:
struct employee { employee(int id):id(id){} int id; };
Further I have to check if every id is unique:
employee* e0 = new employee(1); employee* e1 = e0; employee* e2 = new employee(1); employee* e3 = new employee(2);
my_multi_index_conatiner.insert(e0); // OK my_multi_index_conatiner.insert(e1); // BAD my_multi_index_conatiner.insert(e2); // BAD my_multi_index_conatiner.insert(e0); // OK
Can someone tell me how a typedef of a multi_index_container fullfilling prerequisities above looks? Or is a multi_index_container not the right tool? I've tried with a method in struct employee which returns the this-pointer. But this-pointer is const const and useless for my problem.
Thanks in advance!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (6)
-
Alan Tennant
-
Dejan
-
Igor R
-
Joaquin M Lopez Munoz
-
JOAQUIN M. LOPEZ MUÑOZ
-
joaquin@tid.es