multi_index 
unleashing the beast 
Sławomir Zborowski @
AGENDA 
What is it? 
Why is it there? 
How to use it?
WHAT IS BOOST::MULTI_INDEX
SIMPLE USE CASE 
1 struct Employee { 
2 Employee(int const id, string const& name) 
3 : id(id) 
4 , name(name) 
5 { } 
6 
7 bool operator<(Employee const& other) const { 
8 return id < other.id; 
9 } 
10 
11 int id; 
12 string name; 
13 };
SIMPLE USE CASE 
Sorting by id → easy 
Sorting by name → not really … 
copy to vector & sort with other functor 
maintain secondary container with pointers (like view 
in databases) 
Synchronization needed 
Exceptions = real trouble
STD
MULTI_INDEX - HOW TO USE 
1 typedef multi_index_container< 
2 Employee, 
3 indexed_by< 
4 ordered_unique<identity<Employee>>, 
5 ordered_non_unique<member<Employee, string, 
&Employee::name>> 
6 > 
7 > Employees; 
8 
9 Employees employees = {/* ... */}; 
10 Employees::nth_index<1>::type const& nameIndex = 
employees.get<1>(); 
11 
12 // begin(nameIndex), end(nameIndex), ...
ALICE'S ADVENTURES IN WONDERLAND 
ALICE was beginning to get very tired of 
sitting by her sister on the bank and of 
having nothing to do: once or twice she 
had peeped into the book her sister was 
reading, but it had no pictures or 
conversations in it, "and what is the use 
of a book," thought Alice, "without 
pictures or conversations?'
FOLLY 
TimeoutQueue.hpp 
Identifier 
Expiration
INDICES 
Few indices: 
sequenced: sequenced<> 
ordered: ordered_unique<>, 
ordered_non_unique<> 
hashed: hashed_unique<>, 
hashed_non_unique<> 
random: random_access<> 
Extractors: 
base object: identity<> 
object member: member<> 
functions: mem_fun<>, global_fun<> , … 
user-defined …
TAGGING
TAGGING 
1 struct name {}; 
2 
3 indexed_by< 
4 // ... 
5 ordered_non_unique<tag<name>, member<Employee, 
string, &Employee::name>> 
6 > 
7 
8 // ... 
9 employees.get<1>() == employees.get<name>();
MORE POWER 
iterators 
custom comparison predicates 
special lookup operations 
ranges …
GO GET IT! 
boost::multi_index
Q & A

Boost Multi Index

  • 1.
    multi_index unleashing thebeast Sławomir Zborowski @
  • 2.
    AGENDA What isit? Why is it there? How to use it?
  • 3.
  • 4.
    SIMPLE USE CASE 1 struct Employee { 2 Employee(int const id, string const& name) 3 : id(id) 4 , name(name) 5 { } 6 7 bool operator<(Employee const& other) const { 8 return id < other.id; 9 } 10 11 int id; 12 string name; 13 };
  • 5.
    SIMPLE USE CASE Sorting by id → easy Sorting by name → not really … copy to vector & sort with other functor maintain secondary container with pointers (like view in databases) Synchronization needed Exceptions = real trouble
  • 6.
  • 7.
    MULTI_INDEX - HOWTO USE 1 typedef multi_index_container< 2 Employee, 3 indexed_by< 4 ordered_unique<identity<Employee>>, 5 ordered_non_unique<member<Employee, string, &Employee::name>> 6 > 7 > Employees; 8 9 Employees employees = {/* ... */}; 10 Employees::nth_index<1>::type const& nameIndex = employees.get<1>(); 11 12 // begin(nameIndex), end(nameIndex), ...
  • 8.
    ALICE'S ADVENTURES INWONDERLAND ALICE was beginning to get very tired of sitting by her sister on the bank and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice, "without pictures or conversations?'
  • 9.
  • 10.
    INDICES Few indices: sequenced: sequenced<> ordered: ordered_unique<>, ordered_non_unique<> hashed: hashed_unique<>, hashed_non_unique<> random: random_access<> Extractors: base object: identity<> object member: member<> functions: mem_fun<>, global_fun<> , … user-defined …
  • 11.
  • 12.
    TAGGING 1 structname {}; 2 3 indexed_by< 4 // ... 5 ordered_non_unique<tag<name>, member<Employee, string, &Employee::name>> 6 > 7 8 // ... 9 employees.get<1>() == employees.get<name>();
  • 13.
    MORE POWER iterators custom comparison predicates special lookup operations ranges …
  • 14.
    GO GET IT! boost::multi_index
  • 15.