Boost logo

Boost Users :

From: Nebojsa Simic (nelle_at_[hidden])
Date: 2007-11-17 07:05:03


JOAQUIN LOPEZ MU?Z wrote:
> Hi Nebojsa,
>
[...]

Ola Joaquin,

first of all thanks for the swift reply.

> [...]
>
> You can't use for_each(it1,it2,someFunctor) to do what you want because
> modify() needs an iterator, and for_each passes a value reference
> to someFunctor rather than the iterator.

Yes, I forgot that ...

> So, you've got to resort to a handmade loop like:
>
> for(StatisticsByResponse::iterator it=m_statistic.project<1>
> (result.first),
> it_end=idxResponse.end();
> it!=it_end;++it){
> idxResponse.modify(it,boost::bind
> (&Statistics::addStats,::_1,val,tick));
> }

perfect.

>
> A warning about modifying elements of a multi_index_container in
> a loop: if your modification affects the key on which the index you're
> traversing depends upon, then the loop is broken, because when
> you call modify(...) the element will be rearranged in the index and
> along with it the iterator "it". If this is your case, please read the
> following post where mechanisms for range modifying are provided:
>
> http://lists.boost.org/boost-users/2006/03/18048.php
>
> (Note that post comes with some usable code attached). Hope this
> helps, thanks for using Boost.MultiIndex, if something's still
> not clear do not hesitate to come back.

In this case it is not necessary, because the addStats modifies only the
second index and not idxResponse.

template<typename Index,typename Modifier>
void modify_unstable_range(
  Index& i,typename Index::iterator first,typename Index::iterator last,
  Modifier mod)
{
  typedef std::vector<typename Index::iterator> buffer_type;
  buffer_type v;
  v.reserve( last - first ); // would this help boost performance
  while(first!=last)v.push_back(first++);

  for(typename buffer_type::iterator it=v.begin(),it_end=v.end();
      it!=it_end;i.modify(*it++,mod));
}

-- 
Nebojsa Simic

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