boost multi index

15
multi_index unleashing the beast Sławomir Zborowski @

Upload: szborows

Post on 12-Jul-2015

81 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Boost Multi Index

multi_indexunleashing the beast

Sławomir Zborowski @ 

Page 2: Boost Multi Index

AGENDA

What is it?Why is it there?How to use it?

Page 3: Boost Multi Index

WHAT IS BOOST::MULTI_INDEX

Page 4: 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 };

Page 5: Boost Multi Index

SIMPLE USE CASE

Sorting by id → easySorting by name → not really …

copy to vector & sort with other functormaintain secondary container with pointers (like viewin databases)

Synchronization neededExceptions = real trouble

Page 6: Boost Multi Index

STD

Page 7: Boost Multi Index

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), ...

Page 8: Boost Multi Index

ALICE'S ADVENTURES IN WONDERLAND

ALICE was beginning to get very tired ofsitting by her sister on the bank and ofhaving nothing to do: once or twice shehad peeped into the book her sister wasreading, but it had no pictures orconversations in it, "and what is the useof a book," thought Alice, "withoutpictures or conversations?'

Page 9: Boost Multi Index

FOLLY

T i m e o u t Q u e u e . h p pIdentifierExpiration

Page 10: Boost Multi Index

INDICES

Few indices:sequenced: s e q u e n c e d < >ordered: o r d e r e d _ u n i q u e < > ,o r d e r e d _ n o n _ u n i q u e < >hashed: h a s h e d _ u n i q u e < > ,h a s h e d _ n o n _ u n i q u e < >random: r a n d o m _ a c c e s s < >

Extractors:base object: i d e n t i t y < >object member: m e m b e r < >functions: m e m _ f u n < > , g l o b a l _ f u n < > , …user-defined …

Page 11: Boost Multi Index

TAGGING

Page 12: Boost Multi Index

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>();

Page 13: Boost Multi Index

MORE POWER

iteratorscustom comparison predicatesspecial lookup operationsranges …

Page 15: Boost Multi Index

Q & A