map.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_ADAPTOR_MAP_HPP
  11. #define BOOST_RANGE_ADAPTOR_MAP_HPP
  12. #include <boost/range/adaptor/transformed.hpp>
  13. #include <boost/range/iterator_range.hpp>
  14. #include <boost/range/value_type.hpp>
  15. #include <boost/range/reference.hpp>
  16. #include <boost/range/concepts.hpp>
  17. namespace boost
  18. {
  19. namespace range_detail
  20. {
  21. struct map_keys_forwarder {};
  22. struct map_values_forwarder {};
  23. template< class Map >
  24. struct select_first
  25. {
  26. typedef BOOST_DEDUCED_TYPENAME range_reference<const Map>::type argument_type;
  27. typedef const BOOST_DEDUCED_TYPENAME range_value<const Map>::type::first_type& result_type;
  28. result_type operator()( argument_type r ) const
  29. {
  30. return r.first;
  31. }
  32. };
  33. template< class Map >
  34. struct select_second_mutable
  35. {
  36. typedef BOOST_DEDUCED_TYPENAME range_reference<Map>::type argument_type;
  37. typedef BOOST_DEDUCED_TYPENAME range_value<Map>::type::second_type& result_type;
  38. result_type operator()( argument_type r ) const
  39. {
  40. return r.second;
  41. }
  42. };
  43. template< class Map >
  44. struct select_second_const
  45. {
  46. typedef BOOST_DEDUCED_TYPENAME range_reference<const Map>::type argument_type;
  47. typedef const BOOST_DEDUCED_TYPENAME range_value<const Map>::type::second_type& result_type;
  48. result_type operator()( argument_type r ) const
  49. {
  50. return r.second;
  51. }
  52. };
  53. template<class StdPairRng>
  54. class select_first_range
  55. : public transformed_range<
  56. select_first<StdPairRng>,
  57. const StdPairRng>
  58. {
  59. typedef transformed_range<select_first<StdPairRng>, const StdPairRng> base;
  60. public:
  61. typedef select_first<StdPairRng> transform_fn_type;
  62. typedef const StdPairRng source_range_type;
  63. select_first_range(transform_fn_type fn, source_range_type& rng)
  64. : base(fn, rng)
  65. {
  66. }
  67. select_first_range(const base& other) : base(other) {}
  68. };
  69. template<class StdPairRng>
  70. class select_second_mutable_range
  71. : public transformed_range<
  72. select_second_mutable<StdPairRng>,
  73. StdPairRng>
  74. {
  75. typedef transformed_range<select_second_mutable<StdPairRng>, StdPairRng> base;
  76. public:
  77. typedef select_second_mutable<StdPairRng> transform_fn_type;
  78. typedef StdPairRng source_range_type;
  79. select_second_mutable_range(transform_fn_type fn, source_range_type& rng)
  80. : base(fn, rng)
  81. {
  82. }
  83. select_second_mutable_range(const base& other) : base(other) {}
  84. };
  85. template<class StdPairRng>
  86. class select_second_const_range
  87. : public transformed_range<
  88. select_second_const<StdPairRng>,
  89. const StdPairRng>
  90. {
  91. typedef transformed_range<select_second_const<StdPairRng>, const StdPairRng> base;
  92. public:
  93. typedef select_second_const<StdPairRng> transform_fn_type;
  94. typedef const StdPairRng source_range_type;
  95. select_second_const_range(transform_fn_type fn, source_range_type& rng)
  96. : base(fn, rng)
  97. {
  98. }
  99. select_second_const_range(const base& other) : base(other) {}
  100. };
  101. template< class StdPairRng >
  102. inline select_first_range<StdPairRng>
  103. operator|( const StdPairRng& r, map_keys_forwarder )
  104. {
  105. BOOST_RANGE_CONCEPT_ASSERT((
  106. SinglePassRangeConcept<const StdPairRng>));
  107. return operator|( r,
  108. boost::adaptors::transformed( select_first<StdPairRng>() ) );
  109. }
  110. template< class StdPairRng >
  111. inline select_second_mutable_range<StdPairRng>
  112. operator|( StdPairRng& r, map_values_forwarder )
  113. {
  114. BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<StdPairRng>));
  115. return operator|( r,
  116. boost::adaptors::transformed( select_second_mutable<StdPairRng>() ) );
  117. }
  118. template< class StdPairRng >
  119. inline select_second_const_range<StdPairRng>
  120. operator|( const StdPairRng& r, map_values_forwarder )
  121. {
  122. BOOST_RANGE_CONCEPT_ASSERT((
  123. SinglePassRangeConcept<const StdPairRng>));
  124. return operator|( r,
  125. boost::adaptors::transformed( select_second_const<StdPairRng>() ) );
  126. }
  127. } // 'range_detail'
  128. using range_detail::select_first_range;
  129. using range_detail::select_second_mutable_range;
  130. using range_detail::select_second_const_range;
  131. namespace adaptors
  132. {
  133. namespace
  134. {
  135. const range_detail::map_keys_forwarder map_keys =
  136. range_detail::map_keys_forwarder();
  137. const range_detail::map_values_forwarder map_values =
  138. range_detail::map_values_forwarder();
  139. }
  140. template<class StdPairRange>
  141. inline select_first_range<StdPairRange>
  142. keys(const StdPairRange& rng)
  143. {
  144. BOOST_RANGE_CONCEPT_ASSERT((
  145. SinglePassRangeConcept<const StdPairRange>));
  146. return select_first_range<StdPairRange>(
  147. range_detail::select_first<StdPairRange>(), rng );
  148. }
  149. template<class StdPairRange>
  150. inline select_second_const_range<StdPairRange>
  151. values(const StdPairRange& rng)
  152. {
  153. BOOST_RANGE_CONCEPT_ASSERT((
  154. SinglePassRangeConcept<const StdPairRange>));
  155. return select_second_const_range<StdPairRange>(
  156. range_detail::select_second_const<StdPairRange>(), rng );
  157. }
  158. template<class StdPairRange>
  159. inline select_second_mutable_range<StdPairRange>
  160. values(StdPairRange& rng)
  161. {
  162. BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<StdPairRange>));
  163. return select_second_mutable_range<StdPairRange>(
  164. range_detail::select_second_mutable<StdPairRange>(), rng );
  165. }
  166. } // 'adaptors'
  167. }
  168. #endif