Boost logo

Boost Users :

Subject: Re: [Boost-users] Request for conditional index feature in boost multi-index
From: joaquin_at_[hidden]
Date: 2010-12-22 02:59:51


Gokulakannan Somasundaram escribió:
> Hi,
> First of all thanks for the great library, which had reduced our
> development
> time by a lot.
> In boost multi-index, we currently have almost all the indexing
> concepts
> integrated. Hence i am requesting for a new feature called
> conditional indexes,
> where in an index/view inside multi-index will only index a portion
> of the
> entries inside the entire multi-index(Say something that satisfies
> a functor).
> This is in resemblance to the partial indexes in Postgresql.
> I feel that would provide elegant solutions to lot of issues
> without forming
> round about solutions. For example i have a requirement where in
> there are
> two sets of data with the same search criteria, but different
> eviction criteria
>
> Say i have two sets of data with same search condition, but one set
> needs a
> MRU(Most Recently Used) list of 100 and the other set requires a MRU
> of 200.
> Say the entry is like this
> [...]
>

The general request (composing partial indices to cover the whole
sequence) is
interesting and a considered it some years ago, though I never found the
time
to implement it (and I'm suspicious it might be a little too "smart" for
the general
public).

In any case, I think you can solve your particular problem without that
feature
in a convenient manner. Please see the attached example and report whether
it satisfies your needs.

Best regards,

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


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

using namespace boost::multi_index;

struct student
{
  student(int num,char sex):num(num),sex(sex){}

  int num;
  char sex;

  friend std::ostream& operator<<(std::ostream& os,const student& s)
  {
    os<<"("<<s.num<<","<<s.sex<<") ";
    return os;
  }
};

class student_mru_list
{
  typedef multi_index_container<
    student,
    indexed_by<
      random_access<>,
      hashed_unique<member<student,int,&student::num> >
>
> student_list;

public:
  typedef student_list::iterator iterator;

  student_mru_list(std::size_t max_male,std::size_t max_female):
    max_male(max_male),max_female(max_female),
    size_male(0),size_female(0){}

  void insert(const student& s)
  {
    bool is_male=(s.sex=='m');
    std::size_t& size =is_male? size_male :size_female;
    std::size_t max_size=is_male? max_male :max_female;
    iterator it =is_male? male_begin():female_begin();

    std::pair<iterator,bool> p=sl.insert(it,s);
    if(!p.second){ /* duplicate item */
      sl.relocate(it,p.first); /* put in front */
    }
    else{
      ++size;
      if(size>max_size){ /* keep the length bounded */
        sl.erase(is_male?male_end()-1:female_end()-1);
        --size;
      }
    }
  }

  iterator male_begin(){return sl.begin();}
  iterator male_end(){return male_begin()+size_male;}

  iterator female_begin(){return male_end();}
  iterator female_end(){return sl.end();}

  iterator begin(){return male_begin();}
  iterator end(){return female_end();}

private:
  student_list sl;
  std::size_t max_male,max_female;
  std::size_t size_male,size_female;
};

void dump(student_mru_list& sl)
{
  std::copy(sl.begin(),sl.end(),std::ostream_iterator<student>(std::cout));
  std::cout<<std::endl;
}

int main()
{
  student_mru_list sl(3,2);
  sl.insert(student(0,'m'));
  sl.insert(student(1,'f'));
  sl.insert(student(2,'m'));
  sl.insert(student(3,'f'));
  sl.insert(student(4,'m'));
  sl.insert(student(5,'f'));
  sl.insert(student(6,'m'));
  dump(sl);
  sl.insert(student(3,'f'));
  sl.insert(student(4,'m'));
  dump(sl);
  return 0;
}


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