associative_container_adaptor.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 container_adaptor/associative_container_adaptor.hpp
  9. /// \brief Container adaptor to build a type that is compliant to the concept of an associative container.
  10. #ifndef BOOST_BIMAP_CONTAINER_ADAPTOR_ASSOCIATIVE_CONTAINER_ADAPTOR_HPP
  11. #define BOOST_BIMAP_CONTAINER_ADAPTOR_ASSOCIATIVE_CONTAINER_ADAPTOR_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <utility>
  17. #include <boost/mpl/vector.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/mpl/aux_/na.hpp>
  20. #include <boost/bimap/container_adaptor/detail/identity_converters.hpp>
  21. #include <boost/bimap/container_adaptor/container_adaptor.hpp>
  22. #include <boost/call_traits.hpp>
  23. namespace boost {
  24. namespace bimaps {
  25. namespace container_adaptor {
  26. #ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES
  27. template
  28. <
  29. class Base, class Iterator, class ConstIterator, class KeyType,
  30. class IteratorToBaseConverter, class IteratorFromBaseConverter,
  31. class ValueToBaseConverter, class ValueFromBaseConverter, class KeyToBaseConverter,
  32. class FunctorsFromDerivedClasses
  33. >
  34. struct associative_container_adaptor_base
  35. {
  36. typedef container_adaptor
  37. <
  38. Base,
  39. Iterator, ConstIterator,
  40. IteratorToBaseConverter, IteratorFromBaseConverter,
  41. ValueToBaseConverter , ValueFromBaseConverter,
  42. BOOST_DEDUCED_TYPENAME mpl::push_front<
  43. FunctorsFromDerivedClasses,
  44. BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<KeyToBaseConverter>,
  45. // {
  46. detail::key_to_base_identity
  47. <
  48. BOOST_DEDUCED_TYPENAME Base::key_type, KeyType
  49. >,
  50. // }
  51. // else
  52. // {
  53. KeyToBaseConverter
  54. // }
  55. >::type
  56. >::type
  57. > type;
  58. };
  59. #endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES
  60. /// \brief Container adaptor to build a type that is compliant to the concept of an associative container.
  61. template
  62. <
  63. class Base,
  64. class Iterator,
  65. class ConstIterator,
  66. class KeyType,
  67. class IteratorToBaseConverter = ::boost::mpl::na,
  68. class IteratorFromBaseConverter = ::boost::mpl::na,
  69. class ValueToBaseConverter = ::boost::mpl::na,
  70. class ValueFromBaseConverter = ::boost::mpl::na,
  71. class KeyToBaseConverter = ::boost::mpl::na,
  72. class FunctorsFromDerivedClasses = mpl::vector<>
  73. >
  74. class associative_container_adaptor :
  75. public associative_container_adaptor_base
  76. <
  77. Base, Iterator, ConstIterator, KeyType,
  78. IteratorToBaseConverter, IteratorFromBaseConverter,
  79. ValueToBaseConverter, ValueFromBaseConverter, KeyToBaseConverter,
  80. FunctorsFromDerivedClasses
  81. >::type
  82. {
  83. // MetaData -------------------------------------------------------------
  84. typedef typename associative_container_adaptor_base
  85. <
  86. Base, Iterator, ConstIterator, KeyType,
  87. IteratorToBaseConverter, IteratorFromBaseConverter,
  88. ValueToBaseConverter, ValueFromBaseConverter, KeyToBaseConverter,
  89. FunctorsFromDerivedClasses
  90. >::type base_;
  91. public:
  92. typedef KeyType key_type;
  93. protected:
  94. typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<KeyToBaseConverter>,
  95. // {
  96. detail::key_to_base_identity
  97. <
  98. BOOST_DEDUCED_TYPENAME Base::key_type, KeyType
  99. >,
  100. // }
  101. // else
  102. // {
  103. KeyToBaseConverter
  104. // }
  105. >::type key_to_base;
  106. public:
  107. explicit associative_container_adaptor(Base & c)
  108. : base_(c) {}
  109. protected:
  110. typedef associative_container_adaptor associative_container_adaptor_;
  111. // Interface --------------------------------------------------------------
  112. public:
  113. template< class CompatibleKey >
  114. BOOST_DEDUCED_TYPENAME base_::size_type erase(const CompatibleKey & k)
  115. {
  116. return this->base().erase
  117. (
  118. this->template functor<key_to_base>()(k)
  119. );
  120. }
  121. // As we redefine erase, the other overloads need to be manually routed
  122. BOOST_DEDUCED_TYPENAME base_::iterator erase(
  123. BOOST_DEDUCED_TYPENAME base_::iterator pos)
  124. {
  125. return base_::container_adaptor_::erase(pos);
  126. }
  127. BOOST_DEDUCED_TYPENAME base_::iterator erase(
  128. BOOST_DEDUCED_TYPENAME base_::iterator first,
  129. BOOST_DEDUCED_TYPENAME base_::iterator last)
  130. {
  131. return base_::container_adaptor_::erase(first,last);
  132. }
  133. template< class CompatibleKey >
  134. BOOST_DEDUCED_TYPENAME base_::size_type count(const CompatibleKey & k) const
  135. {
  136. return this->base().count(
  137. this->template functor<key_to_base>()(k)
  138. );
  139. }
  140. template< class CompatibleKey >
  141. BOOST_DEDUCED_TYPENAME base_::iterator find(const CompatibleKey & k)
  142. {
  143. return this->template functor<typename base_::iterator_from_base>()
  144. (
  145. this->base().find(
  146. this->template functor<key_to_base>()(k)
  147. )
  148. );
  149. }
  150. template< class CompatibleKey >
  151. BOOST_DEDUCED_TYPENAME base_::const_iterator
  152. find(const CompatibleKey & k) const
  153. {
  154. return this->template functor<
  155. BOOST_DEDUCED_TYPENAME base_::iterator_from_base>()
  156. (
  157. this->base().find(
  158. this->template functor<key_to_base>()(k)
  159. )
  160. );
  161. }
  162. template< class CompatibleKey >
  163. std::pair
  164. <
  165. BOOST_DEDUCED_TYPENAME base_::iterator,
  166. BOOST_DEDUCED_TYPENAME base_::iterator
  167. >
  168. equal_range(const CompatibleKey & k)
  169. {
  170. std::pair<
  171. BOOST_DEDUCED_TYPENAME Base::iterator,
  172. BOOST_DEDUCED_TYPENAME Base::iterator
  173. > r( this->base().equal_range(
  174. this->template functor<key_to_base>()(k)
  175. )
  176. );
  177. return std::pair
  178. <
  179. BOOST_DEDUCED_TYPENAME base_::iterator,
  180. BOOST_DEDUCED_TYPENAME base_::iterator
  181. >(
  182. this->template functor<
  183. BOOST_DEDUCED_TYPENAME base_::iterator_from_base
  184. >() ( r.first ),
  185. this->template functor<
  186. BOOST_DEDUCED_TYPENAME base_::iterator_from_base
  187. >() ( r.second )
  188. );
  189. }
  190. template< class CompatibleKey >
  191. std::pair
  192. <
  193. BOOST_DEDUCED_TYPENAME base_::const_iterator,
  194. BOOST_DEDUCED_TYPENAME base_::const_iterator
  195. >
  196. equal_range(const CompatibleKey & k) const
  197. {
  198. std::pair<
  199. BOOST_DEDUCED_TYPENAME Base::const_iterator,
  200. BOOST_DEDUCED_TYPENAME Base::const_iterator
  201. > r( this->base().equal_range(
  202. this->template functor<key_to_base>()(k)
  203. )
  204. );
  205. return std::pair
  206. <
  207. BOOST_DEDUCED_TYPENAME base_::const_iterator,
  208. BOOST_DEDUCED_TYPENAME base_::const_iterator
  209. >(
  210. this->template functor<
  211. BOOST_DEDUCED_TYPENAME base_::iterator_from_base
  212. >() ( r.first ),
  213. this->template functor<
  214. BOOST_DEDUCED_TYPENAME base_::iterator_from_base
  215. >() ( r.second )
  216. );
  217. }
  218. };
  219. } // namespace container_adaptor
  220. } // namespace bimaps
  221. } // namespace boost
  222. #endif // BOOST_BIMAP_CONTAINER_ADAPTOR_ASSOCIATIVE_CONTAINER_ADAPTOR_HPP