|
Boost Users : |
Subject: Re: [Boost-users] multi_index_container for composite key
From: Uthpal Urubail (uthpal.urubail_at_[hidden])
Date: 2010-09-09 02:37:41
Joaquín M López Muñoz,
Thanks for offering me the help.
One interesting observation:
If I use the below in finding the objects it is not retrieving properly
indexed_by<
ordered_unique< /// INSTEAD if I use ordered_non_unique< it works fine
composite_key<
I am still not clear with my question:2
Can I define the container like below?
I don't want to create the temporary data structure. I will get these information from a class object. Can I use them directly??
Please help me in understanding.
typedef multi_index_container<
<pointer???>, //??
indexed_by<
ordered_unique<
composite_key<
<pointer????>, //???
BookNodeExtractor<0>,
BookNodeExtractor<1>,
BookNodeExtractor<2>
>
>,
ordered_unique<
member<classname,size_t,&classname::id>
>
>
> beambook;
Thanks,
UJ
-----Original Message-----
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of joaquin_at_[hidden]
Sent: Thursday, September 09, 2010 11:31 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] multi_index_container for composite key
Uthpal Urubail escribió:
>
> Hi
>
> I am looking for a composite key container.
>
> Though I found multi_index_container for composite key is the best I
> have few doubts in its use case:
>
> *Question:01*
>
> class Book
>
> {
>
> public:
>
> size_t mID;
>
> *size_t node[3];*
>
> };
>
> How to add the below into multi_index_container?
>
>
>
> typedef multi_index_container<
>
> Book,
>
> indexed_by<
>
> ordered_unique<
>
> composite_key<
>
> Book,
>
> member<Book,size_t,&Book::node[0]>,
>
> member<Book,size_t,&Book::node[1]>,
>
> member<Book,size_t,&Book::node[2]>
>
> >
>
> >,
>
> ordered_unique<
>
> member<Book,size_t,&Book::mID>
>
> >
>
> >
>
> > beambook;
>
member<Book,size_t,&Book::node[0]> does not work for syntactic
reasons (Book::node[0] is not a member of Book), but you can overcome this
difficulty by using a custom key extractor (http://tinyurl.com/dafk2f )
just like
this:
template<int N>
struct BookNodeExtractor
{
typedef size_t result_type;
result_type operator()(const Book& b)const
{
return b.node[N];
}
};
typedef multi_index_container<
Book,
indexed_by<
ordered_unique<
composite_key<
Book,
BookNodeExtractor<0>,
BookNodeExtractor<1>,
BookNodeExtractor<2>
>
>,
ordered_unique<
member<Book,size_t,&Book::mID>
>
>
> beambook;
> *Question:02*
>
> My requirement is straight forward. I need to get the id by providing
> 3 nodes.
>
> Should I always need to create a data structure? [class Book]
>
No, in fact you don't need to create a Book object. Suppose bb is a beambook
and you want to retrieve the mID of an element in bb with nodes n0, n1
and n2.
You only have to do the following:
size_t mID=bb.find(boost::make_tuple(n0,n1,n2))->mID;
As you can see, you simply pass a tuple with the values for the
composite key,
not a whole Book object.
Hope this helps,
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net