File indexing completed on 2025-01-18 09:51:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_RANGE_ADAPTOR_MAP_HPP
0012 #define BOOST_RANGE_ADAPTOR_MAP_HPP
0013
0014 #include <boost/range/adaptor/transformed.hpp>
0015 #include <boost/range/iterator_range.hpp>
0016 #include <boost/range/value_type.hpp>
0017 #include <boost/range/reference.hpp>
0018 #include <boost/range/concepts.hpp>
0019
0020 namespace boost
0021 {
0022 namespace range_detail
0023 {
0024 struct map_keys_forwarder {};
0025 struct map_values_forwarder {};
0026
0027 template< class Map >
0028 struct select_first
0029 {
0030 typedef BOOST_DEDUCED_TYPENAME range_reference<const Map>::type argument_type;
0031 typedef const BOOST_DEDUCED_TYPENAME range_value<const Map>::type::first_type& result_type;
0032
0033 result_type operator()( argument_type r ) const
0034 {
0035 return r.first;
0036 }
0037 };
0038
0039 template< class Map >
0040 struct select_second_mutable
0041 {
0042 typedef BOOST_DEDUCED_TYPENAME range_reference<Map>::type argument_type;
0043 typedef BOOST_DEDUCED_TYPENAME range_value<Map>::type::second_type& result_type;
0044
0045 result_type operator()( argument_type r ) const
0046 {
0047 return r.second;
0048 }
0049 };
0050
0051 template< class Map >
0052 struct select_second_const
0053 {
0054 typedef BOOST_DEDUCED_TYPENAME range_reference<const Map>::type argument_type;
0055 typedef const BOOST_DEDUCED_TYPENAME range_value<const Map>::type::second_type& result_type;
0056
0057 result_type operator()( argument_type r ) const
0058 {
0059 return r.second;
0060 }
0061 };
0062
0063 template<class StdPairRng>
0064 class select_first_range
0065 : public transformed_range<
0066 select_first<StdPairRng>,
0067 const StdPairRng>
0068 {
0069 typedef transformed_range<select_first<StdPairRng>, const StdPairRng> base;
0070 public:
0071 typedef select_first<StdPairRng> transform_fn_type;
0072 typedef const StdPairRng source_range_type;
0073
0074 select_first_range(transform_fn_type fn, source_range_type& rng)
0075 : base(fn, rng)
0076 {
0077 }
0078
0079 select_first_range(const base& other) : base(other) {}
0080 };
0081
0082 template<class StdPairRng>
0083 class select_second_mutable_range
0084 : public transformed_range<
0085 select_second_mutable<StdPairRng>,
0086 StdPairRng>
0087 {
0088 typedef transformed_range<select_second_mutable<StdPairRng>, StdPairRng> base;
0089 public:
0090 typedef select_second_mutable<StdPairRng> transform_fn_type;
0091 typedef StdPairRng source_range_type;
0092
0093 select_second_mutable_range(transform_fn_type fn, source_range_type& rng)
0094 : base(fn, rng)
0095 {
0096 }
0097
0098 select_second_mutable_range(const base& other) : base(other) {}
0099 };
0100
0101 template<class StdPairRng>
0102 class select_second_const_range
0103 : public transformed_range<
0104 select_second_const<StdPairRng>,
0105 const StdPairRng>
0106 {
0107 typedef transformed_range<select_second_const<StdPairRng>, const StdPairRng> base;
0108 public:
0109 typedef select_second_const<StdPairRng> transform_fn_type;
0110 typedef const StdPairRng source_range_type;
0111
0112 select_second_const_range(transform_fn_type fn, source_range_type& rng)
0113 : base(fn, rng)
0114 {
0115 }
0116
0117 select_second_const_range(const base& other) : base(other) {}
0118 };
0119
0120 template< class StdPairRng >
0121 inline select_first_range<StdPairRng>
0122 operator|( const StdPairRng& r, map_keys_forwarder )
0123 {
0124 BOOST_RANGE_CONCEPT_ASSERT((
0125 SinglePassRangeConcept<const StdPairRng>));
0126
0127 return operator|( r,
0128 boost::adaptors::transformed( select_first<StdPairRng>() ) );
0129 }
0130
0131 template< class StdPairRng >
0132 inline select_second_mutable_range<StdPairRng>
0133 operator|( StdPairRng& r, map_values_forwarder )
0134 {
0135 BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<StdPairRng>));
0136
0137 return operator|( r,
0138 boost::adaptors::transformed( select_second_mutable<StdPairRng>() ) );
0139 }
0140
0141 template< class StdPairRng >
0142 inline select_second_const_range<StdPairRng>
0143 operator|( const StdPairRng& r, map_values_forwarder )
0144 {
0145 BOOST_RANGE_CONCEPT_ASSERT((
0146 SinglePassRangeConcept<const StdPairRng>));
0147
0148 return operator|( r,
0149 boost::adaptors::transformed( select_second_const<StdPairRng>() ) );
0150 }
0151
0152 }
0153
0154 using range_detail::select_first_range;
0155 using range_detail::select_second_mutable_range;
0156 using range_detail::select_second_const_range;
0157
0158 namespace adaptors
0159 {
0160 namespace
0161 {
0162 const range_detail::map_keys_forwarder map_keys =
0163 range_detail::map_keys_forwarder();
0164
0165 const range_detail::map_values_forwarder map_values =
0166 range_detail::map_values_forwarder();
0167 }
0168
0169 template<class StdPairRange>
0170 inline select_first_range<StdPairRange>
0171 keys(const StdPairRange& rng)
0172 {
0173 BOOST_RANGE_CONCEPT_ASSERT((
0174 SinglePassRangeConcept<const StdPairRange>));
0175
0176 return select_first_range<StdPairRange>(
0177 range_detail::select_first<StdPairRange>(), rng );
0178 }
0179
0180 template<class StdPairRange>
0181 inline select_second_const_range<StdPairRange>
0182 values(const StdPairRange& rng)
0183 {
0184 BOOST_RANGE_CONCEPT_ASSERT((
0185 SinglePassRangeConcept<const StdPairRange>));
0186
0187 return select_second_const_range<StdPairRange>(
0188 range_detail::select_second_const<StdPairRange>(), rng );
0189 }
0190
0191 template<class StdPairRange>
0192 inline select_second_mutable_range<StdPairRange>
0193 values(StdPairRange& rng)
0194 {
0195 BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<StdPairRange>));
0196
0197 return select_second_mutable_range<StdPairRange>(
0198 range_detail::select_second_mutable<StdPairRange>(), rng );
0199 }
0200 }
0201
0202 }
0203
0204 #endif