Sunday, August 02, 2015

Example Boost MultiIndex Container Insertion and Iteration

Here is another easy example to get you started with boost multi index containers:

#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::member;
using boost::multi_index::nth_index;
using boost::multi_index::get;

struct employee_entry
{
 employee_entry( const std::string& first,
   const std::string& last,
   long id):
  first_name_(first),
  last_name_(last),
  id_(id)
 {}
 std::string first_name_;
 std::string last_name_;
 long id_;
};

typedef multi_index_container<
 employee_entry, 
 indexed_by<
  ordered_unique<
       member<employee_entry, std::string, &employee_entry::first_name_> 
  >, 
  ordered_non_unique<
       member<employee_entry, std::string, &employee_entry::last_name_> 
  >, 
  ordered_non_unique<
        member<employee_entry, long, &employee_entry::id_> 
  >
 >
> employee_set;

//employee set.... multi-index
employee_set m_employees;

using namespace std;

//Define the different type of iterators
typedef nth_index<employee_set, 0>::type fname_view;
fname_view& fdv = get <0> (m_employees);

typedef nth_index<employee_set, 1>::type lname_view;
lname_view& lnamev = get <1> (m_employees);


typedef nth_index<employee_set, 2>::type id_view;
id_view& idv = get <2> (m_employees);

void PrintLnameWise()
{
 ///get employees sorted by lname
 cout<<"Printing sorted by Lname... \n";
 for(lname_view::iterator it = lnamev.begin(), it_end(lnamev.end()); it != it_end; ++it)
 {
  std::cout << it->first_name_  <<" "
   << it->last_name_ << ":"
   << it->id_ << std::endl;
 }

   const std::string str(40, '-');
   std::cout << str << std::endl;
}

void PrintFnameWise()
{
 ///get employees sorted by first name
 cout<<"Printing sorted by Fname... \n";

   for(fname_view::iterator it = fdv.begin(), it_end(fdv.end()); it != it_end; ++it)
   {
       std::cout << it->first_name_  <<" "
                 << it->last_name_ << ":"
                 << it->id_ << std::endl;
   }
  
   const std::string str(40, '-');
   std::cout << str << std::endl;
}


void PrintIdWise()
{
   cout<<"Printing sorted by Id... \n";
   
   for(id_view::reverse_iterator it = idv.rbegin(), it_end(idv.rend()); it != it_end; ++it)
   {
       std::cout << it->first_name_  <<" "
                 << it->last_name_ << ":"
                 << it->id_ << std::endl;
   }

   const std::string str(40, '-');
   std::cout << str << std::endl;
}

int main()
{

   typedef nth_index<employee_set, 0>::type first_name_view;
   first_name_view& fnv = get<0>(m_employees);

   fnv.insert(employee_entry("John", "Smith", 110));
   fnv.insert(employee_entry("Fudge", "Hunk", 97));
   fnv.insert(employee_entry("Tolem", "Bathi", 87));
   fnv.insert(employee_entry("Prisha", "Agrawal", 1));
   fnv.insert(employee_entry("Shilpi", "Jain", 25));

   std::cout << "Count Structure after 5 inserts:" << m_employees.size() << std::endl;

   //Inserting via the ID Index iterator
   idv.insert(employee_entry("Carlos", "Linus", 140));
   idv.insert(employee_entry("Donald", "gates", 7));
   std::cout << "Count Structure after 2 more inserts:" << m_employees.size() << std::endl;
  
   //Inserting via the lname iterator/index
   lnamev.insert(employee_entry("Sharad", "Smith", 10));
   lnamev.insert(employee_entry("Parag", "Agrawal", 28));
   std::cout << "Final Count Structure after 2 more inserts:" << m_employees.size() << std::endl;
  
   const std::string str(40, '-');
   std::cout << str << std::endl;

   PrintIdWise();
   PrintLnameWise();
   PrintFnameWise();



   return 0;
}

Troubleshooting Packet Drops in SolarFlare Onload 10G PCI Card

If you see lots of packet drops in your onload accelerated application even after going the troubleshooting discussion we did over here ,...