bimap_and_boost.qbk 16 KB

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