Boost logo

Boost Users :

Subject: Re: [Boost-users] Multi-Index: custom iterator
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2014-08-25 15:14:49


Sensei <senseiwa <at> gmail.com> writes:

>
> Dear all,
>
> I'm not yet satisfied with my MultiIndex games, so now I am trying to do
> something that is purely cosmetic. However, I have *no idea* how I can
> do this.
>
> How can I build a custom iterator on a MultiIndex?
>
> The objective is simple. As you may recall, I am building a MultiIndex
> with 128 bits, indexing them by MSB, LSB, and HSB (in the middle). It
> works like a charm.
>
> Now I'd like to write something like this (or similar):
>
> auto it = storage.myfind<byMSB>(MSB(value128bits));
>
> for (; i != it.end(); i++)

Hi Sensei,

One easy way to get something roughly equivalent in many situations
to what you want is (warning, uncompiled):

template<typename Tag>
typename boost::multi_index::index<DataStorage,Tag>::index::const_iterator
myfind(const DataStorage& s,unsigned int v)
{
  return s.template get<Tag>().find(v);
}

...

auto it=myfind<byMSB>(storage,MSB(value128bits));

myfind returns a different iterator type depending on the tag you
specify when calling it. Note that this is *not* the same as
returning a custom iterator type that is able to traverse the
different indices DataStorage is comprised of: in order to do that
you need to implement an iterator class with *type erasure*, which
has a run-time penalty and is not entirely trivial to write.
If you want to know more about type-erasing iterators, take a look
at, for instance, adobe::any_iterator:

http://stlab.adobe.com/classadobe_1_1any__iterator.html

and Boost.TypeErasure:

http://www.boost.org/doc/html/boost_typeerasure.html

Joaquín M López Muñoz
Telefónica


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