Boost logo

Boost Users :

From: Todd Day (today_at_[hidden])
Date: 2005-12-31 00:06:39


I've got the following in my project

// dummies needed for tagging
struct by_name {};
struct by_id {};
struct by_vreg {};

// breaking this up into two steps makes things easier on the compiler,
and produces readable? error messages
struct ParmListIndices :
        multi_index::indexed_by
        <
                multi_index::sequenced<>,
                multi_index::ordered_unique<multi_index::tag<by_name>,
multi_index::const_mem_fun<ParmBase, string, &ParmBase::GetNameStripped>
>,
                multi_index::ordered_non_unique<multi_index::tag<by_id>,
multi_index::const_mem_fun<ParmBase, int, &ParmBase::GetID> >,
        
multi_index::ordered_non_unique<multi_index::tag<by_vreg>,
multi_index::const_mem_fun<ParmBase, int32, &ParmBase::GetVreg> >
> {};

struct ParmList : multi_index::multi_index_container<ParmBase*,
ParmListIndices> {};

typedef multi_index::nth_index<ParmList, 1>::type ParmList_by_name;
typedef multi_index::nth_index<ParmList, 2>::type ParmList_by_id;
typedef multi_index::nth_index<ParmList, 3>::type ParmList_by_vreg;

typedef pair<ParmList::iterator, bool> ParmListResultPair;

Note that I used structs to wrap a complicated multi_index instead of
using typedefs. I learned this trick from a previous web discussion
about multi_index. This relieves stress on the compiler (important for
MSVC7.1 with maximum debugging turned on) and makes the error messages
easier to read. I used typedef shortcuts for referring to the
nth_index'es 'cause structs don't work there. This allowed me to write
the following fairly simple looking code. Note the type of the variable
"byName" and the iterator "f" below.

ParmBase* ParmGroup::FindName(const string& name)
{
        ParmList_by_name& byName = pParmList->get<by_name>();
        ParmList_by_name::iterator f = byName.find(name);
        return (f != byName.end()) ? *f : NULL;
}

These sorts of tricks make the code a little bit more readable. You can
do a similar thing for your iterator pair in your range variable.

-todd-

> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of Evan Carew
> Sent: Friday, December 30, 2005 1:03 PM
> To: boost-users_at_[hidden]
> Subject: [Boost-users] multi_index new user question
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> The following is todays log of my efforts to get multi_index to work:
>
> - ---First entry:
> I'm using boost multi index for the first time. I've managed to
populate
> the container with my data & now I am trying to get the non-unique
> ordered index to work. Does anyone have a source example (not from the
> boost.multiindex tutorial please) used to get a list, set, whatever,
of
> tuples from such a search on such an index?
>
> Thanks,
> Evan Carew
>
> P.S.
> G++ V3.4.2
> Boost version 103300
>
> - ---Second entry:
> Hmm... Looks like their list server is down. I am unable to register.
<sigh>
>
> - ---Third entry:
> Well, lets try this example source code:
>
>
> typedef multi_index::multi_index_container<
> person,
> multi_index::indexed_by<
> multi_index::ordered_unique<
> multi_index::tag<voterid>,
> BOOST_MULTI_INDEX_MEMBER(person,unsigned long,voterid)>,
> multi_index::ordered_non_unique<
>
>
multi_index::tag<lastname>,BOOST_MULTI_INDEX_MEMBER(person,string,lastna
me)
> >,
> multi_index::ordered_non_unique<
> multi_index::tag<resstreetnum>,
> BOOST_MULTI_INDEX_MEMBER(person,int,resstreetnum)> >
> > people_set;
>
> .....
>
> int main(){
> const people_set::nth_index<2>::type& resstreet_index=ps.get<2>();
> pair<people_set::iterator, people_set::iterator> range;
> range = resstreet_index.equal_range((*domoItor).first);
> }
>
> The above code doesn't compile.
>
> - ---Fourth entry:
> By changing the pair instance to:
>
> pair<people_set::nth_index<2>::type::iterator,
> people_set::nth_index<2>::type::iterator> range;
>
> I finally got it to compile. I managed this not by reading the
tutorial
> for the library (the information isn't there) but from reading the
> rather prodigious code in multi_index.
>
> - ---Fifth entry:
> Am I headed in the right direction, or is there specific interface
that
> defines the iterator types in a less painful/safe way?
>
> Evan Carew


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