Chris@16
|
1 // Boost.Range library
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright Neil Groves 2007. Use, modification and
|
Chris@16
|
4 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see http://www.boost.org/libs/range/
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_RANGE_ADAPTOR_REPLACED_IF_IMPL_HPP_INCLUDED
|
Chris@16
|
12 #define BOOST_RANGE_ADAPTOR_REPLACED_IF_IMPL_HPP_INCLUDED
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/config.hpp>
|
Chris@16
|
15 #include <boost/range/adaptor/argument_fwd.hpp>
|
Chris@16
|
16 #include <boost/range/iterator_range.hpp>
|
Chris@16
|
17 #include <boost/range/begin.hpp>
|
Chris@16
|
18 #include <boost/range/end.hpp>
|
Chris@16
|
19 #include <boost/range/value_type.hpp>
|
Chris@101
|
20 #include <boost/range/concepts.hpp>
|
Chris@16
|
21 #include <boost/iterator/iterator_adaptor.hpp>
|
Chris@16
|
22 #include <boost/iterator/transform_iterator.hpp>
|
Chris@101
|
23 #include <boost/optional/optional.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost
|
Chris@16
|
26 {
|
Chris@16
|
27 namespace range_detail
|
Chris@16
|
28 {
|
Chris@16
|
29 template< class Pred, class Value >
|
Chris@16
|
30 class replace_value_if
|
Chris@16
|
31 {
|
Chris@16
|
32 public:
|
Chris@16
|
33 typedef const Value& result_type;
|
Chris@16
|
34 typedef const Value& first_argument_type;
|
Chris@16
|
35
|
Chris@101
|
36 // Rationale:
|
Chris@101
|
37 // required to allow the iterator to be default constructible.
|
Chris@101
|
38 replace_value_if()
|
Chris@101
|
39 {
|
Chris@101
|
40 }
|
Chris@101
|
41
|
Chris@16
|
42 replace_value_if(const Pred& pred, const Value& to)
|
Chris@101
|
43 : m_impl(data(pred, to))
|
Chris@16
|
44 {
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 const Value& operator()(const Value& x) const
|
Chris@16
|
48 {
|
Chris@101
|
49 return m_impl->m_pred(x) ? m_impl->m_to : x;
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 private:
|
Chris@101
|
53 struct data
|
Chris@101
|
54 {
|
Chris@101
|
55 data(const Pred& p, const Value& t)
|
Chris@101
|
56 : m_pred(p), m_to(t)
|
Chris@101
|
57 {
|
Chris@101
|
58 }
|
Chris@101
|
59
|
Chris@101
|
60 Pred m_pred;
|
Chris@101
|
61 Value m_to;
|
Chris@101
|
62 };
|
Chris@101
|
63 boost::optional<data> m_impl;
|
Chris@16
|
64 };
|
Chris@16
|
65
|
Chris@16
|
66 template< class Pred, class R >
|
Chris@16
|
67 class replaced_if_range :
|
Chris@16
|
68 public boost::iterator_range<
|
Chris@16
|
69 boost::transform_iterator<
|
Chris@16
|
70 replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type >,
|
Chris@16
|
71 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > >
|
Chris@16
|
72 {
|
Chris@16
|
73 private:
|
Chris@16
|
74 typedef replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type > Fn;
|
Chris@16
|
75
|
Chris@16
|
76 typedef boost::iterator_range<
|
Chris@16
|
77 boost::transform_iterator<
|
Chris@16
|
78 replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type >,
|
Chris@16
|
79 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > > base_t;
|
Chris@16
|
80
|
Chris@16
|
81 public:
|
Chris@16
|
82 typedef BOOST_DEDUCED_TYPENAME range_value<R>::type value_type;
|
Chris@16
|
83
|
Chris@16
|
84 replaced_if_range( R& r, const Pred& pred, value_type to )
|
Chris@16
|
85 : base_t( make_transform_iterator( boost::begin(r), Fn(pred, to) ),
|
Chris@16
|
86 make_transform_iterator( boost::end(r), Fn(pred, to) ) )
|
Chris@16
|
87 { }
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 template< class Pred, class T >
|
Chris@16
|
91 class replace_if_holder
|
Chris@16
|
92 {
|
Chris@16
|
93 public:
|
Chris@16
|
94 replace_if_holder( const Pred& pred, const T& to )
|
Chris@16
|
95 : m_pred(pred), m_to(to)
|
Chris@16
|
96 { }
|
Chris@16
|
97
|
Chris@16
|
98 const Pred& pred() const { return m_pred; }
|
Chris@16
|
99 const T& to() const { return m_to; }
|
Chris@16
|
100
|
Chris@16
|
101 private:
|
Chris@16
|
102 Pred m_pred;
|
Chris@16
|
103 T m_to;
|
Chris@16
|
104 };
|
Chris@16
|
105
|
Chris@101
|
106 template< class Pred, class SinglePassRange >
|
Chris@101
|
107 inline replaced_if_range<Pred, SinglePassRange>
|
Chris@101
|
108 operator|(
|
Chris@101
|
109 SinglePassRange& r,
|
Chris@101
|
110 const replace_if_holder<
|
Chris@101
|
111 Pred,
|
Chris@101
|
112 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f)
|
Chris@16
|
113 {
|
Chris@101
|
114 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
115 SinglePassRangeConcept<SinglePassRange>));
|
Chris@101
|
116
|
Chris@101
|
117 return replaced_if_range<Pred, SinglePassRange>(
|
Chris@101
|
118 r, f.pred(), f.to());
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@101
|
121 template< class Pred, class SinglePassRange >
|
Chris@101
|
122 inline replaced_if_range<Pred, const SinglePassRange>
|
Chris@101
|
123 operator|(
|
Chris@101
|
124 const SinglePassRange& r,
|
Chris@101
|
125 const replace_if_holder<
|
Chris@101
|
126 Pred,
|
Chris@101
|
127 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f)
|
Chris@16
|
128 {
|
Chris@101
|
129 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
130 SinglePassRangeConcept<const SinglePassRange>));
|
Chris@101
|
131
|
Chris@101
|
132 return replaced_if_range<Pred, const SinglePassRange>(
|
Chris@101
|
133 r, f.pred(), f.to());
|
Chris@16
|
134 }
|
Chris@16
|
135 } // 'range_detail'
|
Chris@16
|
136
|
Chris@16
|
137 using range_detail::replaced_if_range;
|
Chris@16
|
138
|
Chris@16
|
139 namespace adaptors
|
Chris@16
|
140 {
|
Chris@16
|
141 namespace
|
Chris@16
|
142 {
|
Chris@16
|
143 const range_detail::forwarder2TU<range_detail::replace_if_holder>
|
Chris@16
|
144 replaced_if =
|
Chris@16
|
145 range_detail::forwarder2TU<range_detail::replace_if_holder>();
|
Chris@16
|
146 }
|
Chris@101
|
147
|
Chris@101
|
148 template<class Pred, class SinglePassRange>
|
Chris@101
|
149 inline replaced_if_range<Pred, SinglePassRange>
|
Chris@101
|
150 replace_if(SinglePassRange& rng, Pred pred,
|
Chris@101
|
151 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type to)
|
Chris@16
|
152 {
|
Chris@101
|
153 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
154 SinglePassRangeConcept<SinglePassRange>));
|
Chris@101
|
155
|
Chris@101
|
156 return range_detail::replaced_if_range<Pred, SinglePassRange>(
|
Chris@101
|
157 rng, pred, to);
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@101
|
160 template<class Pred, class SinglePassRange>
|
Chris@101
|
161 inline replaced_if_range<Pred, const SinglePassRange>
|
Chris@101
|
162 replace_if(
|
Chris@101
|
163 const SinglePassRange& rng,
|
Chris@101
|
164 Pred pred,
|
Chris@101
|
165 BOOST_DEDUCED_TYPENAME range_value<const SinglePassRange>::type to)
|
Chris@16
|
166 {
|
Chris@101
|
167 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
168 SinglePassRangeConcept<const SinglePassRange>));
|
Chris@101
|
169
|
Chris@101
|
170 return range_detail::replaced_if_range<Pred, const SinglePassRange>(
|
Chris@101
|
171 rng, pred, to);
|
Chris@16
|
172 }
|
Chris@16
|
173 } // 'adaptors'
|
Chris@101
|
174
|
Chris@16
|
175 } // 'boost'
|
Chris@16
|
176
|
Chris@16
|
177 #endif // include guard
|