Boost logo

Boost Users :

From: joaquin_at_[hidden]
Date: 2008-06-12 13:09:09


Igor R escribió:
> Hi,
>
> I'm trying to replace few data structures and types defined in my
> application in terms of std containers, with the
> mutli_index_container. Some of those types look like this:
>
> struct Entry
> {
> int someForeignKey;
> int data1;
> std::string data2;
> //....
> };
> typedef std::deque<Entry> Entries;
> typedef std::map<int, Entries> EntriesByForeignKey;
>
> I'd like to rewrite the above map as follows:
>
> typedef mi::multi_index_container
> <
> Entry,
> mi::indexed_by
> <
> mi::hashed_non_unique<mi::tag<foreignKey>, mi::member<Entry, int,
> &Entry::someForeignKey> >
> >
>
>> Entries;
>>
>
> typedef Entries::index<foreignKey>::type EntriesByForeignKey;
>
> Now, I'd like to iterate through the *keys* of this index, and for
> each key - iterate through all its values.[...]

Hi Igor, if I'm understanding your request correctly I think you can do
that like
this:

  for(Entries::iterator
it_key=entries.begin(),end=entries.end();it_key!=end;){
    // get all elements with key it_key->someForeignKey
    std::pair<Entries::iterator,Entries::iterator> values=
      entries.equal_range(it_key->someForeignKey);
    while(values.first!=values.second){
      // process element
      ++values.first;
    }
    // next key is just that of the element right after the current
value group
    it_key=values.second;
  }

Is this what you were after? Please find attached a small program
exercising this
idea. HTH,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <iostream>
#include <string>

struct Entry
{
  int someForeignKey;
  int data1;
  std::string data2;
  
  Entry(int someForeignKey,int data1,const std::string& data2):
    someForeignKey(someForeignKey),data1(data1),data2(data2)
  {}
};

namespace mi=boost::multi_index;

struct foreignKey;

typedef mi::multi_index_container
<
  Entry,
  mi::indexed_by
  <
    mi::hashed_non_unique
    <
      mi::tag<foreignKey>,
      mi::member<Entry, int,&Entry::someForeignKey>
>
>
> Entries;

typedef Entries::index<foreignKey>::type EntriesByForeignKey;

int main()
{
  Entries entries;
  entries.insert(Entry(0,1,"hello"));
  entries.insert(Entry(0,2,"bye"));
  entries.insert(Entry(0,3,"boost"));
  entries.insert(Entry(1,4,"multi_index"));
  entries.insert(Entry(1,5,"igor"));
  entries.insert(Entry(2,6,"container"));

  for(Entries::iterator it_key=entries.begin(),end=entries.end();it_key!=end;){
    std::cout<<"key: "<<it_key->someForeignKey<<"\n";
    std::pair<Entries::iterator,Entries::iterator> values=
      entries.equal_range(it_key->someForeignKey);
    while(values.first!=values.second){
      std::cout<<" ("<<values.first->data1<<","<<values.first->data2<<")\n";
      ++values.first;
    }
    it_key=values.second;
  }
}


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