1[/license 2 3Boost.Bimap 4 5Copyright (c) 2006-2007 Matias Capeletto 6 7Distributed under the Boost Software License, Version 1.0. 8(See accompanying file LICENSE_1_0.txt or copy at 9http://www.boost.org/LICENSE_1_0.txt) 10 11] 12 13[/ QuickBook Document version 1.4 ] 14 15[section Bimap and Boost] 16 17[section Bimap and MultiIndex] 18 19['MISC] - [*M]ulti-[*I]ndex [*S]pecialized [*C]ontainers 20 21[:[' 22Let's be generic, construct frameworks, describe the world in an 23unified way... 24]] 25[:[' 26No!, it is better to be specialized, design easy-to-use components, 27offer plug-and-play objects... 28]] 29[:[* 30Why not take advantage of the best of both worlds? 31]] 32 33__MI_FRAMEWORK__ 34 35With Boost.Bimap, you can build associative containers in which both 36types can be used as key. There is a library in Boost that already 37allows the creation of this kind of container: Boost.MultiIndex. It 38offers great flexibility and lets you construct almost any container 39that you could dream of. The framework is very clean. You might want to 40read this library's tutorial to learn about the power that has been 41achieved. 42 43 44But generality comes at a price: the interface that results might not be 45the best for every specialization. People may end up wrapping a B.MI 46container in its own class every time they want to use it as a 47bidirectional map. Boost.Bimap takes advantage of the narrower scope to 48produce a better interface for bidirectional maps 49[footnote In the same fashion, Boost.MRU will allow the creation of ['most 50recent updated] aware containers, hiding the complexity of Boost.MultiIndex.]. 51There is no learning curve if you know how to use standard containers. 52Great effort was put into mapping the naming scheme of the STL to Boost.Bimap. 53The library is designed to match the common STL containers. 54 55Boost.MultiIndex is, in fact, the core of the bimap container. 56 57However, Boost.Bimap do not aim to tackle every problem with two indexed 58types. There exist some problems that are better modelled with Boost.MultiIndex. 59 60 61[blurb 62 63[*Problem I - An employee register] 64 65['Store an ID and a name for an employee, with fast search on each member.] 66 67This type of problem is better modelled as a database table, and 68[*Boost.MultiIndex] is the preferred choice. It is possible that other data 69will need to be indexed later. 70 71] 72 73[blurb 74 75[*Problem II - A partners container] 76 77['Store the names of couples and be able to get the name of a person's 78partner.] 79 80This problem is better modelled as a collection of relations, and [*Boost.Bimap] 81fits nicely here. 82 83] 84 85You can also read 86[link boost_bimap.the_tutorial.additional_information Additional Information] for more 87information about the relation of this two libraries. 88 89[endsect] 90 91[section Boost Libraries that work well with Boost.Bimap] 92 93[section Introduction] 94 95[table 96[[Name][Description][author][Purpose]] 97 98[[ __BOOST_SERIALIZATION__ ][ 99Serialization for persistence and marshalling] 100[Robert Ramey] 101[Serialization support for bimap containers and iterators]] 102 103[[ __BOOST_ASSIGN__ ][ 104Filling containers with constant or generated data has never been easier] 105[Thorsten Ottosen] 106[Help to fill a bimap or views of it]] 107 108[[ __BOOST_HASH__ ][ 109A TR1 hash function object that can be extended to hash user defined types] 110[Daniel James] 111[Default hashing function]] 112 113[[ __BOOST_LAMBDA__ ][ 114Define small unnamed function objects at the actual call site, and more] 115[from Jaakko Järvi, Gary Powell] 116[Functors for modify, range, lower_bound and upper_bound]] 117 118[[ __BOOST_RANGE__ ][ 119A new infrastructure for generic algorithms that builds on top of the new 120iterator concepts] 121[Thorsten Ottosen] 122[Range based algorithms]] 123 124[[ __BOOST_FOREACH__ ][ 125BOOST_FOREACH macro for easily iterating over the elements of a sequence] 126[Eric Niebler] 127[Iteration]] 128 129[[ __BOOST_TYPEOF__ ][ 130Typeof operator emulation] 131[Arkadiy Vertleyb, Peder Holt] 132[Using BOOST_AUTO while we wait for C++0x]] 133 134[[ __BOOST_XPRESSIVE__ ][ 135Regular expressions that can be written as strings or as expression templates] 136[Eric Niebler] 137[Help to fill a bimap from a string]] 138 139[[ __BOOST_PROPERTY_MAP__ ][ 140Concepts defining interfaces which map key objects to value objects] 141[Jeremy Siek] 142[Integration with BGL]] 143] 144 145[endsect] 146 147[section Boost.Serialization] 148 149A bimap can be archived and retrieved by means of the Boost.Serialization Library. 150Both regular and XML archives are supported. The usage is straightforward and does 151not differ from that of any other serializable type. For instance: 152 153[import ../example/bimap_and_boost/serialization.cpp] 154 155[@../../example/bimap_and_boost/serialization.cpp Go to source code] 156 157[code_bimap_and_boost_serialization] 158 159Serialization capabilities are automatically provided by just linking with the 160appropriate Boost.Serialization library module: it is not necessary to explicitly 161include any header from Boost.Serialization, apart from those declaring the type 162of archive used in the process. If not used, however, serialization support can 163be disabled by globally defining the macro BOOST_BIMAP_DISABLE_SERIALIZATION. 164Disabling serialization for Boost.MultiIndex can yield a small improvement in 165build times, and may be necessary in those defective compilers that fail to 166correctly process Boost.Serialization headers. 167 168[warning Boost.Bimap and Boost.MultiIndex share a lot of serialization code. 169The macro `BOOST_BIMAP_DISABLE_SERIALIZATION` disables serialization in *both* 170libraries. The same happens when `BOOST_MULTI_INDEX_DISABLE_SERIALIZATION` is 171defined. 172] 173 174Retrieving an archived bimap restores not only the elements, but also the order 175they were arranged in the views of the container. There is an exception to this rule, 176though: for unordered sets, no guarantee is made about the order in which elements 177will be iterated in the restored container; in general, it is unwise to rely on 178the ordering of elements of a hashed view, since it can change in arbitrary ways 179during insertion or rehashing --this is precisely the reason why hashed indices 180and TR1 unordered associative containers do not define an equality operator. 181 182Iterators of a bimap can also be serialized. Serialization of an 183iterator must be done only after serializing its corresponding container. 184 185[endsect] 186 187[section Boost.Assign] 188 189The purpose of this library is to make it easy to fill containers with data by 190overloading operator,() and operator()(). These two operators make it possible 191to construct lists of values that are then copied into a container. 192 193These lists are particularly useful in learning, testing, and prototyping 194situations, but can also be handy otherwise. The library comes with predefined 195operators for the containers of the standard library, but most functionality will 196work with any standard compliant container. The library also makes it possible 197to extend user defined types so for example a member function can be called for 198a list of values instead of its normal arguments. 199 200Boost.Assign can be used with bimap containers. 201The views of a bimap are signature-compatible with their standard 202counterparts, so we can use other Boost.Assign utilities with them. 203 204[import ../example/bimap_and_boost/assign.cpp] 205 206[@../../example/bimap_and_boost/assign.cpp Go to source code] 207 208[code_bimap_and_boost_assign] 209 210[endsect] 211 212[section Boost.Hash] 213 214The hash function is the very core of the fast lookup capabilities of the 215unordered sets: a hasher is just a Unary Function returning an std::size_t value 216for any given key. In general, it is impossible that every key map to a 217different hash value, for the space of keys can be greater than the number of permissible hash codes: what makes for a good hasher is that the probability of a collision (two different keys with the same hash value) is as close to zero as possible. 218 219This is a statistical property depending on the typical distribution of keys in a given application, so it is not feasible to have a general-purpose hash function with excellent results in every possible scenario; the default value for this parameter uses Boost.Hash, which often provides good enough results. 220 221Boost.Hash can be 222[@http://www.boost.org/doc/html/hash/custom.html 223extended for custom data types], 224enabling to use the default parameter of the unordered set types with any user types. 225 226[endsect] 227 228[section Boost.Lambda] 229 230The Boost Lambda Library (BLL in the sequel) is a C++ template library, which implements 231form of lambda abstractions for C++. The term originates from functional programming and 232lambda calculus, where a lambda abstraction defines an unnamed function. 233Lambda expressions are very useful to construct the function objects required by some of 234the functions in a bimap view. 235 236Boost.Bimap defines new placeholders in `<boost/bimap/support/lambda.hpp>` 237to allow a sounder solution. The placeholders are named _key and _data and both 238are equivalent to boost::lambda::_1. There are two reasons to include this placeholders: 239the code looks better with them and they avoid the clash problem between lambda::_1 and 240boost::_1 from Boost.Bind. 241 242[import ../example/bimap_and_boost/lambda.cpp] 243 244[@../../example/bimap_and_boost/lambda.cpp Go to source code] 245 246[code_bimap_and_boost_lambda] 247 248[endsect] 249 250[section Boost.Range] 251 252Boost.Range is a collection of concepts and utilities that are particularly useful 253for specifying and implementing generic algorithms. 254Generic algorithms have so far been specified in terms of two or more iterators. 255Two iterators would together form a range of values that the algorithm could 256work on. This leads to a very general interface, but also to a somewhat clumsy 257use of the algorithms with redundant specification of container names. Therefore 258we would like to raise the abstraction level for algorithms so they specify their 259interface in terms of Ranges as much as possible. 260 261As Boost.Bimap views are signature-compatible with their standard 262container counterparts, they are compatible with the concept of a range. 263As an additional feature, ordered bimap views offer a function named 264`range` that allows a range of values to be obtained. 265 266[import ../example/bimap_and_boost/range.cpp] 267 268If we have some generic functions that accepts ranges: 269 270[code_bimap_and_boost_range_functions] 271 272We can use them with Boost.Bimap with the help of the `range` function. 273 274[code_bimap_and_boost_range] 275 276[@../../example/bimap_and_boost/range.cpp Go to source code] 277 278[endsect] 279 280[section Boost.Foreach] 281 282In C++, writing a loop that iterates over a sequence is tedious. 283We can either use iterators, which requires a considerable amount of 284boiler-plate, or we can use the std::for_each() algorithm and move our 285loop body into a predicate, which requires no less boiler-plate and forces 286us to move our logic far from where it will be used. In contrast, some other 287languages, like Perl, provide a dedicated "foreach" construct that automates 288this process. BOOST_FOREACH is just such a construct for C++. It iterates 289over sequences for us, freeing us from having to deal directly with iterators 290or write predicates. 291 292You can use BOOST_FOREACH macro with Boost.Bimap views. The generated code will 293be as efficient as a std::for_each iteration. 294Here are some examples: 295 296[import ../example/bimap_and_boost/foreach.cpp] 297 298[code_bimap_and_boost_foreach] 299 300You can use it directly with ranges too: 301 302[code_bimap_and_boost_foreach_using_range] 303 304[@../../example/bimap_and_boost/foreach.cpp Go to source code] 305 306[endsect] 307 308[section Boost.Typeof] 309 310[import ../example/bimap_and_boost/typeof.cpp] 311 312Once C++0x is out we are going to be able to write code like: 313 314 auto iter = bm.by<name>().find("john"); 315 316instead of the more verbose 317 318 bm_type::map_by<name>::iterator iter = bm.by<name>().find("john"); 319 320Boost.Typeof defines a macro BOOST_AUTO that can be used as a library 321solution to the auto keyword while we wait for the next standard. 322 323If we have 324 325[code_bimap_and_boost_typeof_first] 326 327The following code snippet 328 329[code_bimap_and_boost_typeof_not_using_auto] 330 331can be rewritten as 332 333[code_bimap_and_boost_typeof_using_auto] 334 335[@../../example/bimap_and_boost/typeof.cpp Go to source code] 336 337[endsect] 338 339[section Boost.Xpressive] 340 341[import ../example/bimap_and_boost/xpressive.cpp] 342 343Using Boost.Xpressive we can parse a file and insert the relations in a bimap 344in the same step. It is just amazing the power of four lines of code. 345Here is an example (it is just beatifull) 346 347[code_bimap_and_boost_xpressive] 348 349[@../../example/bimap_and_boost/xpressive.cpp Go to source code] 350 351[endsect] 352 353[section Boost.Property_map] 354 355The Boost Property Map Library consists mainly of interface specifications in the form of 356concepts (similar to the iterator concepts in the STL). These interface specifications 357are intended for use by implementers of generic libraries in communicating requirements on 358template parameters to their users. In particular, the Boost Property Map concepts define a 359general purpose interface for mapping key objects to corresponding value objects, thereby 360hiding the details of how the mapping is implemented from algorithms. 361 362The need for the property map interface came from the Boost Graph Library (BGL), which 363contains many examples of algorithms that use the property map concepts to specify their 364interface. For an example, note the ColorMap template parameter of the breadth_first_search. 365In addition, the BGL contains many examples of concrete types that implement the property map 366interface. The adjacency_list class implements property maps for accessing objects 367(properties) that are attached to vertices and edges of the graph. 368 369The counterparts of two of the views of Boost.Bimap map, the `set` and 370`unordered_set`, are read-write property maps. In order to use these, you 371need to include one of the following headers: 372 373 #include <boost/bimap/property_map/set_support.hpp> 374 #include <boost/bimap/property_map/unordered_set_support.hpp> 375 376The following is adapted from the example in the Boost.PropertyMap 377documentation. 378 379[import ../example/bimap_and_boost/property_map.cpp] 380 381[@../../example/bimap_and_boost/property_map.cpp Go to source code] 382 383[code_bimap_and_boost_property_map] 384 385[endsect] 386 387[endsect] 388 389[section Dependencies] 390 391Boost.Bimap is built on top of several Boost libraries. The rationale 392behind this decision is keeping the Boost code base small by reusing 393existent code. The libraries used are well-established and have been 394tested extensively, making this library easy to port since all the hard 395work has already been done. The glue that holds everything together is 396Boost.MPL. Clearly Boost.MultiIndex is the heart of this library. 397 398[table Boost Libraries needed by Boost.Bimap 399[[Name][Description][author]] 400 401[[ __BOOST_MULTI_INDEX__ ][ 402Containers with multiple STL-compatible access interfaces] 403[Joaquín M López Muñoz]] 404 405[[ __BOOST_MPL__ ][ 406Template metaprogramming framework of compile-time algorithms, sequences and metafunction classes] 407[Aleksey Gurtovoy]] 408 409[[ __BOOST_TYPE_TRAITS__ ][ 410Templates for fundamental properties of types.] 411[John Maddock, Steve Cleary]] 412 413[[ __BOOST_ENABLE_IF__ ][ 414Selective inclusion of function template overloads] 415[Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine]] 416 417[[ __BOOST_ITERATORS__ ][ 418Iterator construction framework, adaptors, concepts, and more.] 419[Dave Abrahams, Jeremy Siek, Thomas Witt]] 420 421[[ __BOOST_CALL_TRAITS__ ][ 422Defines types for passing parameters.] 423[John Maddock, Howard Hinnant]] 424 425[[ __BOOST_STATIC_ASSERT__ ][ 426Static assertions (compile time assertions).] 427[John Maddock]] 428 429] 430 431[table Optional Boost Libraries 432[[Name][Description][author][Purpose]] 433 434[[ __BOOST_SERIALIZATION__ ][ 435Serialization for persistence and marshalling] 436[Robert Ramey] 437[Serialization support for bimap containers and iterators]] 438 439[[ __BOOST_ASSIGN__ ][ 440Filling containers with constant or generated data has never been easier] 441[Thorsten Ottosen] 442[Help to fill a bimap or views of it]] 443 444[[ __BOOST_HASH__ ][ 445A TR1 hash function object that can be extended to hash user defined types] 446[Daniel James] 447[Default hashing function]] 448 449[[ __BOOST_LAMBDA__ ][ 450Define small unnamed function objects at the actual call site, and more] 451[from Jaakko Järvi, Gary Powell] 452[Functors for modify, range, lower_bound and upper_bound]] 453 454[[ __BOOST_RANGE__ ][ 455A new infrastructure for generic algorithms that builds on top of the new 456iterator concepts] 457[Thorsten Ottosen] 458[Range based algorithms]] 459 460[[ __BOOST_PROPERTY_MAP__ ][ 461Concepts defining interfaces which map key objects to value objects] 462[Jeremy Siek] 463[Integration with BGL]] 464] 465 466[table Additional Boost Libraries needed to run the test-suite 467[[Name][Description][author]] 468 469[[ __BOOST_TEST__ ][ 470Support for simple program testing, full unit testing, and for program execution monitoring.] 471[Gennadiy Rozental] 472] 473] 474 475[endsect] 476 477[endsect] 478