bimap_core.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 detail/bimap_core.hpp
  9. /// \brief Bimap base definition.
  10. #ifndef BOOST_BIMAP_DETAIL_BIMAP_CORE_HPP
  11. #define BOOST_BIMAP_DETAIL_BIMAP_CORE_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <boost/mpl/placeholders.hpp>
  17. #include <boost/mpl/push_front.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/mpl/and.hpp>
  20. #include <boost/mpl/not.hpp>
  21. #include <boost/mpl/vector.hpp>
  22. #include <boost/type_traits/add_const.hpp>
  23. #include <boost/type_traits/is_same.hpp>
  24. #include <boost/concept_check.hpp>
  25. // Boost.MultiIndex
  26. #include <boost/multi_index_container.hpp>
  27. #include <boost/multi_index/member.hpp>
  28. // Boost.Bimap
  29. #include <boost/bimap/relation/mutant_relation.hpp>
  30. #include <boost/bimap/relation/member_at.hpp>
  31. #include <boost/bimap/relation/support/data_extractor.hpp>
  32. #include <boost/bimap/tags/support/default_tagged.hpp>
  33. #include <boost/bimap/tags/tagged.hpp>
  34. #include <boost/bimap/detail/manage_bimap_key.hpp>
  35. #include <boost/bimap/detail/manage_additional_parameters.hpp>
  36. #include <boost/bimap/detail/map_view_iterator.hpp>
  37. #include <boost/bimap/detail/set_view_iterator.hpp>
  38. #include <boost/bimap/set_of.hpp>
  39. #include <boost/bimap/unconstrained_set_of.hpp>
  40. #include <boost/core/allocator_access.hpp>
  41. namespace boost {
  42. namespace bimaps {
  43. /// \brief Library details
  44. namespace detail {
  45. #ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES
  46. template< class Type >
  47. struct get_value_type
  48. {
  49. typedef BOOST_DEDUCED_TYPENAME Type::value_type type;
  50. };
  51. struct independent_index_tag {};
  52. #endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES
  53. /// \brief Base for the bimap class.
  54. /**
  55. See also bimap.
  56. **/
  57. template< class LeftSetType, class RightSetType, class AP1, class AP2, class AP3 >
  58. class bimap_core
  59. {
  60. // Manage bimap key instantiation
  61. // --------------------------------------------------------------------
  62. public:
  63. typedef BOOST_DEDUCED_TYPENAME manage_bimap_key
  64. <
  65. LeftSetType
  66. >::type left_set_type;
  67. typedef BOOST_DEDUCED_TYPENAME manage_bimap_key
  68. <
  69. RightSetType
  70. >::type right_set_type;
  71. private:
  72. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::tags::support::default_tagged
  73. <
  74. BOOST_DEDUCED_TYPENAME left_set_type::user_type,
  75. ::boost::bimaps::relation::member_at::left
  76. >::type left_tagged_type;
  77. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::tags::support::default_tagged
  78. <
  79. BOOST_DEDUCED_TYPENAME right_set_type::user_type,
  80. ::boost::bimaps::relation::member_at::right
  81. >::type right_tagged_type;
  82. public:
  83. //@{
  84. typedef BOOST_DEDUCED_TYPENAME left_tagged_type::tag left_tag;
  85. typedef BOOST_DEDUCED_TYPENAME right_tagged_type::tag right_tag;
  86. //@}
  87. //@{
  88. typedef BOOST_DEDUCED_TYPENAME left_set_type::value_type left_key_type;
  89. typedef BOOST_DEDUCED_TYPENAME right_set_type::value_type right_key_type;
  90. //@}
  91. //@{
  92. typedef right_key_type left_data_type;
  93. typedef left_key_type right_data_type;
  94. //@}
  95. // Manage the additional parameters
  96. // --------------------------------------------------------------------
  97. private:
  98. typedef BOOST_DEDUCED_TYPENAME manage_additional_parameters<AP1,AP2,AP3>::type parameters;
  99. /// \brief Relation type stored by the bimap.
  100. // --------------------------------------------------------------------
  101. public:
  102. typedef ::boost::bimaps::relation::mutant_relation
  103. <
  104. ::boost::bimaps::tags::tagged<
  105. BOOST_DEDUCED_TYPENAME mpl::if_<
  106. mpl::and_
  107. <
  108. BOOST_DEDUCED_TYPENAME left_set_type::mutable_key,
  109. BOOST_DEDUCED_TYPENAME parameters::set_type_of_relation::left_mutable_key
  110. >,
  111. // {
  112. left_key_type,
  113. // }
  114. // else
  115. // {
  116. BOOST_DEDUCED_TYPENAME ::boost::add_const< left_key_type >::type
  117. // }
  118. >::type,
  119. left_tag
  120. >,
  121. ::boost::bimaps::tags::tagged<
  122. BOOST_DEDUCED_TYPENAME mpl::if_<
  123. mpl::and_
  124. <
  125. BOOST_DEDUCED_TYPENAME right_set_type::mutable_key,
  126. BOOST_DEDUCED_TYPENAME parameters::set_type_of_relation::right_mutable_key
  127. >,
  128. // {
  129. right_key_type,
  130. // }
  131. // else
  132. // {
  133. BOOST_DEDUCED_TYPENAME ::boost::add_const< right_key_type >::type
  134. // }
  135. >::type,
  136. right_tag
  137. >,
  138. // It is ::boost::mpl::na if no info_hook was included
  139. BOOST_DEDUCED_TYPENAME parameters::additional_info,
  140. // Force mutable keys
  141. true
  142. > relation;
  143. //@{
  144. typedef BOOST_DEDUCED_TYPENAME relation::left_pair left_value_type;
  145. typedef BOOST_DEDUCED_TYPENAME relation::right_pair right_value_type;
  146. //@}
  147. // Bind the member of the relation, so multi_index can manage them
  148. // --------------------------------------------------------------------
  149. private:
  150. typedef BOOST_DEDUCED_TYPENAME relation::storage_base relation_storage_base;
  151. typedef BOOST_MULTI_INDEX_MEMBER(relation_storage_base, left_key_type, left)
  152. left_member_extractor;
  153. typedef BOOST_MULTI_INDEX_MEMBER(relation_storage_base,right_key_type,right)
  154. right_member_extractor;
  155. // The core indices are somewhat complicated to calculate, because they
  156. // can be zero, one, two or three indices, depending on the use of
  157. // {side}_based set type of relations and unconstrained_set_of and
  158. // unconstrained_set_of_relation specifications.
  159. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  160. ::boost::bimaps::detail::is_unconstrained_set_of< left_set_type >,
  161. // {
  162. mpl::vector<>,
  163. // }
  164. // else
  165. // {
  166. mpl::vector
  167. <
  168. BOOST_DEDUCED_TYPENAME left_set_type::
  169. BOOST_NESTED_TEMPLATE index_bind
  170. <
  171. left_member_extractor,
  172. left_tag
  173. >::type
  174. >
  175. // }
  176. >::type left_core_indices;
  177. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  178. ::boost::bimaps::detail::is_unconstrained_set_of< right_set_type >,
  179. // {
  180. left_core_indices,
  181. // }
  182. // else
  183. // {
  184. BOOST_DEDUCED_TYPENAME mpl::push_front
  185. <
  186. left_core_indices,
  187. BOOST_DEDUCED_TYPENAME right_set_type::
  188. BOOST_NESTED_TEMPLATE index_bind
  189. <
  190. right_member_extractor,
  191. right_tag
  192. >::type
  193. >::type
  194. // }
  195. >::type basic_core_indices;
  196. // If it is based either on the left or on the right, then only the side
  197. // indices are needed. But the set type of the relation can be completely
  198. // different from the one used for the sides in which case we have to add yet
  199. // another index to the core.
  200. // TODO
  201. // If all the set types are unsconstrained there must be readable compile
  202. // time error.
  203. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  204. is_same< BOOST_DEDUCED_TYPENAME parameters::set_type_of_relation, left_based >,
  205. // {
  206. ::boost::bimaps::tags::tagged< left_set_type, left_tag >,
  207. // }
  208. /* else */ BOOST_DEDUCED_TYPENAME mpl::if_<
  209. is_same< BOOST_DEDUCED_TYPENAME parameters::set_type_of_relation, right_based >,
  210. // {
  211. ::boost::bimaps::tags::tagged< right_set_type, right_tag >,
  212. // }
  213. // else
  214. // {
  215. tags::tagged
  216. <
  217. BOOST_DEDUCED_TYPENAME parameters::
  218. set_type_of_relation::BOOST_NESTED_TEMPLATE bind_to
  219. <
  220. relation
  221. >::type,
  222. independent_index_tag
  223. >
  224. // }
  225. >::type
  226. >::type tagged_set_of_relation_type;
  227. protected:
  228. typedef BOOST_DEDUCED_TYPENAME tagged_set_of_relation_type::tag
  229. relation_set_tag;
  230. typedef BOOST_DEDUCED_TYPENAME tagged_set_of_relation_type::value_type
  231. relation_set_type_of;
  232. // Logic tags
  233. // This is a necessary extra level of indirection to allow unconstrained
  234. // sets to be plug in the design. The bimap constructors use this logic
  235. // tags.
  236. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  237. ::boost::bimaps::detail::is_unconstrained_set_of< left_set_type >,
  238. BOOST_DEDUCED_TYPENAME mpl::if_<
  239. ::boost::bimaps::detail::is_unconstrained_set_of< right_set_type >,
  240. independent_index_tag,
  241. right_tag
  242. >::type,
  243. left_tag
  244. >::type logic_left_tag;
  245. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  246. ::boost::bimaps::detail::is_unconstrained_set_of< right_set_type >,
  247. BOOST_DEDUCED_TYPENAME mpl::if_<
  248. ::boost::bimaps::detail::is_unconstrained_set_of< left_set_type >,
  249. independent_index_tag,
  250. left_tag
  251. >::type,
  252. right_tag
  253. >::type logic_right_tag;
  254. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  255. is_same< relation_set_tag, independent_index_tag >,
  256. BOOST_DEDUCED_TYPENAME mpl::if_<
  257. ::boost::bimaps::detail::
  258. is_unconstrained_set_of< relation_set_type_of >,
  259. logic_left_tag,
  260. independent_index_tag
  261. >::type,
  262. BOOST_DEDUCED_TYPENAME mpl::if_<
  263. is_same< BOOST_DEDUCED_TYPENAME parameters::set_type_of_relation, left_based >,
  264. logic_left_tag,
  265. logic_right_tag
  266. >::type
  267. >::type logic_relation_set_tag;
  268. private:
  269. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  270. mpl::and_< is_same< relation_set_tag, independent_index_tag >,
  271. mpl::not_<
  272. ::boost::bimaps::detail::
  273. is_unconstrained_set_of< relation_set_type_of >
  274. >
  275. >,
  276. // {
  277. BOOST_DEDUCED_TYPENAME mpl::push_front
  278. <
  279. basic_core_indices,
  280. BOOST_DEDUCED_TYPENAME relation_set_type_of::
  281. BOOST_NESTED_TEMPLATE index_bind
  282. <
  283. ::boost::bimaps::relation::support::both_keys_extractor<relation>,
  284. independent_index_tag
  285. >::type
  286. >::type,
  287. // }
  288. // else
  289. // {
  290. basic_core_indices
  291. // }
  292. >::type complete_core_indices;
  293. struct core_indices : public complete_core_indices {};
  294. // Define the core using compute_index_type to translate the
  295. // set type to an multi-index specification
  296. // --------------------------------------------------------------------
  297. public:
  298. typedef multi_index::multi_index_container
  299. <
  300. relation,
  301. core_indices,
  302. BOOST_DEDUCED_TYPENAME boost::allocator_rebind<BOOST_DEDUCED_TYPENAME
  303. parameters::allocator, relation>::type
  304. > core_type;
  305. // Core metadata
  306. // --------------------------------------------------------------------
  307. public:
  308. typedef BOOST_DEDUCED_TYPENAME ::boost::multi_index::
  309. index<core_type, logic_left_tag>::type left_index;
  310. typedef BOOST_DEDUCED_TYPENAME ::boost::multi_index::
  311. index<core_type,logic_right_tag>::type right_index;
  312. typedef BOOST_DEDUCED_TYPENAME left_index::iterator left_core_iterator;
  313. typedef BOOST_DEDUCED_TYPENAME left_index::const_iterator left_core_const_iterator;
  314. typedef BOOST_DEDUCED_TYPENAME right_index::iterator right_core_iterator;
  315. typedef BOOST_DEDUCED_TYPENAME right_index::const_iterator right_core_const_iterator;
  316. // Relation set view
  317. typedef BOOST_DEDUCED_TYPENAME ::boost::multi_index::index
  318. <
  319. core_type, logic_relation_set_tag
  320. >::type relation_set_core_index;
  321. typedef BOOST_DEDUCED_TYPENAME relation_set_type_of::
  322. BOOST_NESTED_TEMPLATE set_view_bind
  323. <
  324. relation_set_core_index
  325. >::type relation_set;
  326. public:
  327. typedef bimap_core bimap_core_;
  328. };
  329. // Two auxiliary metafunctions to compute the map view types
  330. // The map view type can not be computed inside the bimap core because a
  331. // they need the bimap core to be parsed first.
  332. template< class BimapBaseType >
  333. struct left_map_view_type
  334. {
  335. typedef BOOST_DEDUCED_TYPENAME BimapBaseType::left_set_type left_set_type;
  336. typedef BOOST_DEDUCED_TYPENAME
  337. left_set_type::BOOST_NESTED_TEMPLATE map_view_bind<
  338. BOOST_DEDUCED_TYPENAME BimapBaseType::left_tag, BimapBaseType
  339. >::type type;
  340. };
  341. template< class BimapBaseType >
  342. struct right_map_view_type
  343. {
  344. typedef BOOST_DEDUCED_TYPENAME BimapBaseType::right_set_type right_set_type;
  345. typedef BOOST_DEDUCED_TYPENAME
  346. right_set_type::BOOST_NESTED_TEMPLATE map_view_bind<
  347. BOOST_DEDUCED_TYPENAME BimapBaseType::right_tag, BimapBaseType
  348. >::type type;
  349. };
  350. } // namespace detail
  351. } // namespace bimaps
  352. } // namespace boost
  353. #endif // BOOST_BIMAP_DETAIL_BIMAP_CORE_HPP