bimap.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. /// \file bimap.hpp
  9. /// \brief Includes the basic bimap container
  10. /** \mainpage notitle
  11. \n
  12. \image html http://matias.capeletto.googlepages.com/boost.bimap.reference.logo.png
  13. \section Introduction
  14. This is the complete reference of Boost.Bimap.
  15. After getting a good understanding of the library from a user perspective
  16. the next step will be:
  17. - Understand the tagged idiom. (boost::bimaps::tags)
  18. - Understand the internals of the relation class (boost::bimaps::relation)
  19. - Read the container_adaptor toolbox docs (boost::bimaps::container_adaptor)
  20. - Understand the internals of the bimap class. (boost::bimaps, boost::bimaps::views
  21. and boost::bimaps::detail)
  22. **/
  23. /** \defgroup mutant_group mutant idiom
  24. \brief A safe wrapper around reinterpret_cast
  25. **/
  26. /** \defgroup relation_group relation
  27. \brief The relation
  28. **/
  29. /** \defgroup tags_group tagged idiom
  30. \brief The tagged idiom
  31. **/
  32. #ifndef BOOST_BIMAP_BIMAP_HPP
  33. #define BOOST_BIMAP_BIMAP_HPP
  34. #if defined(_MSC_VER)
  35. #pragma once
  36. #endif
  37. #include <boost/config.hpp>
  38. #include <boost/bimap/detail/user_interface_config.hpp>
  39. #include <boost/mpl/aux_/na.hpp>
  40. #ifndef BOOST_BIMAP_DISABLE_SERIALIZATION
  41. #include <boost/serialization/nvp.hpp>
  42. #endif // BOOST_BIMAP_DISABLE_SERIALIZATION
  43. // Boost.Bimap
  44. #include <boost/bimap/detail/bimap_core.hpp>
  45. #include <boost/bimap/detail/map_view_base.hpp>
  46. #include <boost/bimap/detail/modifier_adaptor.hpp>
  47. #include <boost/bimap/relation/support/data_extractor.hpp>
  48. #include <boost/bimap/relation/support/member_with_tag.hpp>
  49. #include <boost/bimap/support/map_type_by.hpp>
  50. #include <boost/bimap/support/map_by.hpp>
  51. #include <boost/bimap/support/iterator_type_by.hpp>
  52. /// \brief The namespace where all the boost libraries lives.
  53. namespace boost {
  54. /// \brief Boost.Bimap library namespace
  55. /**
  56. All the entities in the library are defined in this namespace.
  57. **/
  58. namespace bimaps {
  59. /// \brief The bimap class is the entry point to the library.
  60. /**
  61. This class manages the instantiation of the desired bimap type.
  62. As there are several types of bidirectional maps that can be
  63. created using it. the main job of it is to find the desired
  64. type. This is done using metaprogramming to obtain the relation
  65. type that will be stored, the map_view type of each side and
  66. the set_view type of the general relationship. The instantiation
  67. is kept simple using an extended standard set theory, where a
  68. bidirectional map type is defined by the set types it relates.
  69. For example, a bidirectional map that has multimap semantics
  70. viewed from both sides is defined by specifying that the two
  71. keys sets are of \c multiset_of<Key> type.
  72. This allows the bimap class to support seamingless N-N, 1-N,
  73. ordered/unordered and even vector-list types of mapping.
  74. The three last parameters are used to specify the set type of
  75. the relation, an inplace hooked data class and the allocator
  76. type. As a help to the bimap user, these parameters support
  77. default types but use a special idiom that allow them to be
  78. specified without interleaving the usual use_default keyword.
  79. The possible bimap instantiation are enumerated here:
  80. \c {Side}KeyType can be directly a type, this is default to
  81. \c set_of<{Side}KeyType>, or can be a \c {SetType}_of<Type>
  82. specification. Additionally this two parameters can be tagged
  83. to specify others tags instead of the usual \c member_at::{Side}
  84. ones.
  85. \code
  86. typedef bimap
  87. <
  88. LeftCollectionType, RightCollectionType
  89. [ , SetTypeOfRelation ] // Default to left_based
  90. [ , info_hook< Info > ] // Default to no info
  91. [ , Allocator ] // Default to std::allocator<>
  92. > bm;
  93. \endcode
  94. **/
  95. template
  96. <
  97. class KeyTypeA, class KeyTypeB,
  98. class AP1 = ::boost::mpl::na,
  99. class AP2 = ::boost::mpl::na,
  100. class AP3 = ::boost::mpl::na
  101. >
  102. class bimap
  103. :
  104. // Bimap Core, use mpl magic to find the desired bimap type
  105. public ::boost::bimaps::detail::bimap_core<KeyTypeA,KeyTypeB,AP1,AP2,AP3>,
  106. // You can use bimap as a collection of relations
  107. public ::boost::bimaps::detail::bimap_core<KeyTypeA,KeyTypeB,AP1,AP2,AP3>
  108. ::relation_set,
  109. // Include extra typedefs (i.e. left_local_iterator for unordered_map)
  110. public ::boost::bimaps::detail:: left_map_view_extra_typedefs<
  111. BOOST_DEDUCED_TYPENAME ::boost::bimaps::detail::left_map_view_type<
  112. ::boost::bimaps::detail::bimap_core<KeyTypeA,KeyTypeB,AP1,AP2,AP3>
  113. >::type
  114. >,
  115. public ::boost::bimaps::detail::right_map_view_extra_typedefs<
  116. BOOST_DEDUCED_TYPENAME ::boost::bimaps::detail::right_map_view_type<
  117. ::boost::bimaps::detail::bimap_core<KeyTypeA,KeyTypeB,AP1,AP2,AP3>
  118. >::type
  119. >
  120. {
  121. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::detail::
  122. bimap_core<KeyTypeA,KeyTypeB,AP1,AP2,AP3> base_;
  123. BOOST_DEDUCED_TYPENAME base_::core_type core;
  124. public:
  125. // metadata --------------------------------------------------------
  126. /*
  127. // The rest is computed in the core, because it is quite difficult to
  128. // expose a nice interface with so many metaprogramming stuff.
  129. // Map by {side} metadata
  130. typedef -unspecified- {side}_tag;
  131. typedef -unspecified- {side}_data_type;
  132. typedef -unspecified- {side}_value_type;
  133. typedef -unspecified- {side}_key_type;
  134. // There are other typedefs for definitions of different map views
  135. ------------------------------------------------------------------*/
  136. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::detail::
  137. left_map_view_type<base_>::type left_map;
  138. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::detail::
  139. right_map_view_type<base_>::type right_map;
  140. typedef BOOST_DEDUCED_TYPENAME
  141. left_map::iterator left_iterator;
  142. typedef BOOST_DEDUCED_TYPENAME
  143. left_map::const_iterator left_const_iterator;
  144. typedef BOOST_DEDUCED_TYPENAME
  145. right_map::iterator right_iterator;
  146. typedef BOOST_DEDUCED_TYPENAME
  147. right_map::const_iterator right_const_iterator;
  148. typedef BOOST_DEDUCED_TYPENAME
  149. left_map::reference left_reference;
  150. typedef BOOST_DEDUCED_TYPENAME
  151. left_map::const_reference left_const_reference;
  152. typedef BOOST_DEDUCED_TYPENAME
  153. right_map::reference right_reference;
  154. typedef BOOST_DEDUCED_TYPENAME
  155. right_map::const_reference right_const_reference;
  156. typedef BOOST_DEDUCED_TYPENAME base_::relation::info_type info_type;
  157. typedef BOOST_DEDUCED_TYPENAME base_::core_type::allocator_type allocator_type;
  158. /// Left map view
  159. left_map left;
  160. /// Right map view
  161. right_map right;
  162. typedef BOOST_DEDUCED_TYPENAME base_::logic_relation_set_tag
  163. logic_relation_set_tag;
  164. typedef BOOST_DEDUCED_TYPENAME base_::logic_left_tag logic_left_tag;
  165. typedef BOOST_DEDUCED_TYPENAME base_::logic_right_tag logic_right_tag;
  166. typedef BOOST_DEDUCED_TYPENAME base_::core_type::ctor_args_list
  167. ctor_args_list;
  168. bimap(const allocator_type& al = allocator_type()) :
  169. base_::relation_set(
  170. ::boost::multi_index::get<
  171. logic_relation_set_tag
  172. >(core)
  173. ),
  174. core(al),
  175. left (
  176. ::boost::multi_index::get<
  177. logic_left_tag
  178. >(core)
  179. ),
  180. right (
  181. ::boost::multi_index::get<
  182. logic_right_tag
  183. >(core)
  184. )
  185. {}
  186. template< class InputIterator >
  187. bimap(InputIterator first,InputIterator last,
  188. const allocator_type& al = allocator_type()) :
  189. base_::relation_set(
  190. ::boost::multi_index::get<
  191. BOOST_DEDUCED_TYPENAME base_::logic_relation_set_tag>(core)
  192. ),
  193. core(first,last,ctor_args_list(),al),
  194. left (
  195. ::boost::multi_index::get<
  196. BOOST_DEDUCED_TYPENAME base_::logic_left_tag>(core)
  197. ),
  198. right (
  199. ::boost::multi_index::get<
  200. BOOST_DEDUCED_TYPENAME base_::logic_right_tag>(core)
  201. )
  202. {}
  203. bimap(const bimap& x) :
  204. base_::relation_set(
  205. ::boost::multi_index::get<
  206. BOOST_DEDUCED_TYPENAME base_::logic_relation_set_tag>(core)
  207. ),
  208. core(x.core),
  209. left (
  210. ::boost::multi_index::get<
  211. BOOST_DEDUCED_TYPENAME base_::logic_left_tag>(core)
  212. ),
  213. right (
  214. ::boost::multi_index::get<
  215. BOOST_DEDUCED_TYPENAME base_::logic_right_tag>(core)
  216. )
  217. {}
  218. bimap& operator=(const bimap& x)
  219. {
  220. core = x.core;
  221. return *this;
  222. }
  223. // Projection of iterators
  224. template< class IteratorType >
  225. left_iterator project_left(IteratorType iter)
  226. {
  227. return core.template project<
  228. BOOST_DEDUCED_TYPENAME base_::logic_left_tag>(iter.base());
  229. }
  230. template< class IteratorType >
  231. left_const_iterator project_left(IteratorType iter) const
  232. {
  233. return core.template project<
  234. BOOST_DEDUCED_TYPENAME base_::logic_left_tag>(iter.base());
  235. }
  236. template< class IteratorType >
  237. right_iterator project_right(IteratorType iter)
  238. {
  239. return core.template project<
  240. BOOST_DEDUCED_TYPENAME base_::logic_right_tag>(iter.base());
  241. }
  242. template< class IteratorType >
  243. right_const_iterator project_right(IteratorType iter) const
  244. {
  245. return core.template project<
  246. BOOST_DEDUCED_TYPENAME base_::logic_right_tag>(iter.base());
  247. }
  248. template< class IteratorType >
  249. BOOST_DEDUCED_TYPENAME base_::relation_set::iterator
  250. project_up(IteratorType iter)
  251. {
  252. return core.template project<
  253. BOOST_DEDUCED_TYPENAME base_::logic_relation_set_tag>(iter.base());
  254. }
  255. template< class IteratorType >
  256. BOOST_DEDUCED_TYPENAME base_::relation_set::const_iterator
  257. project_up(IteratorType iter) const
  258. {
  259. return core.template project<
  260. BOOST_DEDUCED_TYPENAME base_::logic_relation_set_tag>(iter.base());
  261. }
  262. // Support for tags
  263. template< class Tag, class IteratorType >
  264. BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
  265. iterator_type_by<Tag,bimap>::type
  266. project(IteratorType iter)
  267. {
  268. return core.template project<Tag>(iter.base());
  269. }
  270. template< class Tag, class IteratorType >
  271. BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
  272. const_iterator_type_by<Tag,bimap>::type
  273. project(IteratorType iter) const
  274. {
  275. return core.template project<Tag>(iter.base());
  276. }
  277. template< class Tag >
  278. struct map_by :
  279. public ::boost::bimaps::support::map_type_by<Tag,bimap>::type
  280. {
  281. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
  282. map_type_by<Tag,bimap>::type type;
  283. private: map_by() {}
  284. };
  285. template< class Tag >
  286. BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
  287. map_type_by<Tag,bimap>::type &by()
  288. {
  289. return ::boost::bimaps::support::map_by<Tag>(*this);
  290. }
  291. template< class Tag >
  292. const BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
  293. map_type_by<Tag,bimap>::type &by() const
  294. {
  295. return ::boost::bimaps::support::map_by<Tag>(*this);
  296. }
  297. #ifndef BOOST_BIMAP_DISABLE_SERIALIZATION
  298. // Serialization support
  299. private:
  300. friend class boost::serialization::access;
  301. template<class Archive>
  302. void serialize(Archive & ar, const unsigned int)
  303. {
  304. ar & serialization::make_nvp("mi_core",core);
  305. }
  306. #endif // BOOST_BIMAP_DISABLE_SERIALIZATION
  307. };
  308. } // namespace bimaps
  309. } // namespace boost
  310. /** \namespace boost::bimaps::support
  311. \brief Metafunctions to help working with bimaps.
  312. **/
  313. /** \namespace boost::bimaps::views
  314. \brief Bimap views.
  315. **/
  316. /** \namespace boost::bimaps::views::detail
  317. \brief Bimap views details.
  318. **/
  319. // Include basic tools for user commodity
  320. #include <boost/bimap/tags/tagged.hpp>
  321. #include <boost/bimap/relation/member_at.hpp>
  322. #include <boost/multi_index/detail/unbounded.hpp>
  323. // Bring the most used namespaces directly to the user main namespace
  324. namespace boost {
  325. namespace bimaps {
  326. using ::boost::bimaps::tags::tagged;
  327. namespace member_at = ::boost::bimaps::relation::member_at;
  328. using ::boost::multi_index::unbounded;
  329. } // namespace bimaps
  330. } // namespace boost
  331. #endif // BOOST_BIMAP_BIMAP_HPP