Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get numeric index from Boost multi-index iterator

I'm storing a bunch of the following

struct Article {
    std::string title;
    unsigned db_id;     // id field in MediaWiki database dump
};

in a Boost.MultiIndex container, defined as

typedef boost::multi_index_container<
    Article,
    indexed_by<
        random_access<>,
        hashed_unique<tag<by_db_id>,
                      member<Article, unsigned, &Article::db_id> >,
        hashed_unique<tag<by_title>,
                      member<Article, std::string, &Article::title> >
    >
> ArticleSet;

Now I've got two iterators, one from index<by_title> and one from index<by_id>. What is the easiest way to transform these to indexes into the random access part of the container, without adding a data member to struct Article?

like image 291
Fred Foo Avatar asked Nov 18 '10 17:11

Fred Foo


1 Answers

Every index supports generation of an iterator by value using iterator_to. If you already have an iterator to the target value in one index, you could use this to convert to an iterator in another index.

iterator       iterator_to(const value_type& x);
const_iterator iterator_to(const value_type& x)const;

For conversion to index you can likely follow the model in random_access_index.hpp:

  iterator erase(iterator first,iterator last)
  {
    BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
    BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
    BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
    BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
    BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
    BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
    difference_type n=last-first;
    relocate(end(),first,last);
    while(n--)pop_back();
    return last;
  }
like image 192
Steve Townsend Avatar answered Sep 20 '22 12:09

Steve Townsend