Boost logo

Boost :

From: Tal Agmon (tal.boost_at_[hidden])
Date: 2006-08-21 08:01:49


>Still I'm having difficulties with understanding your question, but let me
> try my hand

Your are good! :) I should knock myself for not sending you a snippet code

below you can fiind a "small" example with only 2 indices

******** BEGIN CODE ********

typedef int handle;
typedef int group_id;

struct NodeBase:boost::enable_shared_from_this<NodeBase>
{

 virtual ~NodeBase(){}

 virtual group_id memberOf()const=0;
 virtual handle getHandle()const=0;
};

struct NodeType:NodeBase
{
 NodeType(handle h,group_id gid):h(h),gid(gid){}

 virtual group_id memberOf()const{return gid;}
 virtual handle getHandle()const{return h;}

private:
 group_id gid;
 handle h;
};

struct NodeBaseHandleExtractor
{
 typedef handle result_type;
 result_type operator()(const boost::shared_ptr<NodeBase>& x)
 {
   return x->getHandle();
 }
};

struct NodeBaseGroupIdExtractor
{
 typedef group_id result_type;
 result_type operator()(const boost::shared_ptr<NodeBase>& x)
 {
   return x->memberOf();

 }
};

typedef boost::multi_index::multi_index_container<
 boost::shared_ptr<NodeBase>,
 boost::multi_index::indexed_by<
   boost::multi_index::ordered_unique<

     NodeBaseHandleExtractor

>,
   boost::multi_index::ordered_non_unique<

     NodeBaseGroupIdExtractor

>
>
> NodePtrSet;

int main()
{
 NodePtrSet ns;

 typedef boost::shared_ptr<NodeBase> element_type;

 ns.insert(element_type(new NodeType(0,10))); // 0 = id, 10 = group_id
 ns.insert(element_type(new NodeType(1,10)));
 ns.insert(element_type(new NodeType(2,20)));

// Now lets say I want to get a list of unique group_id values.
// so I should get {10,20} event thought I have 3 elements

//if i use the following
for(index_t::const_iterator it=i.begin();it!=i.end();){ // note: no ++it
 // output the key

 std::cout<<i.key_extractor()(*it)<<std::endl;

 // jump to the next different key value ( which is great btw)
 it=i.upper_bound(i.key_extractor()(*it));
}

//Note that the for loop still iterates 3 times and not 2 times.
// Meaning that the results of the key_extractor()(*it) key_value are
{10,10,20}

// My goal is to get back just {10,20} and not {10,10,20}
//assuming that in this non_unique_index container the key (group_id)
is at least unique even though 10 points to 2 elements.

 return 0;
}

I hope I made my point this time.

Regards
Tal Agmon

On 8/21/06, Joaquín Mª López Muñoz <joaquin_at_[hidden]> wrote:
> Tal Agmon ha escrito:
>
> > Hi Joaquin,
> >
> > Sorry for the lack of info in my previous quote.
> > I appreicate your affort to promptly help me.
> >
> > What I have is a non_unique which I carry to get all the keys.( I
> > don't care about the elements)
> >
> > My guess is that key_extractor() returns a key_form_element, is quite
> > redundant in my case.
> >
>
> Hello again.
>
> Still I'm having difficulties with understanding your question, but let me
> try my hand: are you looking for a way to produce all *different* key
> values contained by an index? If so, something like the following would do:
>
> for(index_t::const_iterator it=i.begin();it!=i.end();){ // note: no ++it
> // output the key
> std::cout<<i.key_extractor()(*it)<<std::endl;
>
> // jump to the next different key value
> it=i.upper_bound(i.key_extractor()(*it));
> }
>
> Now, is this what you were asking about?
>
> >
> > Regards
> > Tal
> >
>
> Best,
>
> Joaquín M López Muñoz
> Telefónica, Investigación y Desarrollo
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk