1 | // Boost.Range library |
---|
2 | // |
---|
3 | // Copyright Neil Groves 2007. 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 | |
---|
11 | #ifndef BOOST_RANGE_ADAPTOR_REPLACED_IF_IMPL_HPP_INCLUDED |
---|
12 | #define BOOST_RANGE_ADAPTOR_REPLACED_IF_IMPL_HPP_INCLUDED |
---|
13 | |
---|
14 | #include <boost/config.hpp> |
---|
15 | #include <boost/range/adaptor/argument_fwd.hpp> |
---|
16 | #include <boost/range/iterator_range.hpp> |
---|
17 | #include <boost/range/begin.hpp> |
---|
18 | #include <boost/range/end.hpp> |
---|
19 | #include <boost/range/value_type.hpp> |
---|
20 | #include <boost/iterator/iterator_adaptor.hpp> |
---|
21 | #include <boost/iterator/transform_iterator.hpp> |
---|
22 | |
---|
23 | namespace boost |
---|
24 | { |
---|
25 | namespace range_detail |
---|
26 | { |
---|
27 | template< class Pred, class Value > |
---|
28 | class replace_value_if |
---|
29 | { |
---|
30 | public: |
---|
31 | typedef const Value& result_type; |
---|
32 | typedef const Value& first_argument_type; |
---|
33 | |
---|
34 | replace_value_if(const Pred& pred, const Value& to) |
---|
35 | : m_pred(pred), m_to(to) |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | const Value& operator()(const Value& x) const |
---|
40 | { |
---|
41 | return m_pred(x) ? m_to : x; |
---|
42 | } |
---|
43 | |
---|
44 | private: |
---|
45 | Pred m_pred; |
---|
46 | Value m_to; |
---|
47 | }; |
---|
48 | |
---|
49 | template< class Pred, class R > |
---|
50 | class replace_if_range : |
---|
51 | public boost::iterator_range< |
---|
52 | boost::transform_iterator< |
---|
53 | replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type >, |
---|
54 | BOOST_DEDUCED_TYPENAME range_iterator<R>::type > > |
---|
55 | { |
---|
56 | private: |
---|
57 | typedef replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type > Fn; |
---|
58 | |
---|
59 | typedef boost::iterator_range< |
---|
60 | boost::transform_iterator< |
---|
61 | replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type >, |
---|
62 | BOOST_DEDUCED_TYPENAME range_iterator<R>::type > > base_t; |
---|
63 | |
---|
64 | public: |
---|
65 | typedef BOOST_DEDUCED_TYPENAME range_value<R>::type value_type; |
---|
66 | |
---|
67 | replace_if_range( R& r, const Pred& pred, value_type to ) |
---|
68 | : base_t( make_transform_iterator( boost::begin(r), Fn(pred, to) ), |
---|
69 | make_transform_iterator( boost::end(r), Fn(pred, to) ) ) |
---|
70 | { } |
---|
71 | }; |
---|
72 | |
---|
73 | template< class Pred, class T > |
---|
74 | class replace_if_holder |
---|
75 | { |
---|
76 | public: |
---|
77 | replace_if_holder( const Pred& pred, const T& to ) |
---|
78 | : m_pred(pred), m_to(to) |
---|
79 | { } |
---|
80 | |
---|
81 | const Pred& pred() const { return m_pred; } |
---|
82 | const T& to() const { return m_to; } |
---|
83 | |
---|
84 | private: |
---|
85 | Pred m_pred; |
---|
86 | T m_to; |
---|
87 | }; |
---|
88 | |
---|
89 | template< class Pred, class InputRng > |
---|
90 | inline replace_if_range<Pred, InputRng> |
---|
91 | operator|( InputRng& r, |
---|
92 | const replace_if_holder<Pred, BOOST_DEDUCED_TYPENAME range_value<InputRng>::type>& f ) |
---|
93 | { |
---|
94 | return replace_if_range<Pred, InputRng>(r, f.pred(), f.to()); |
---|
95 | } |
---|
96 | |
---|
97 | template< class Pred, class InputRng > |
---|
98 | inline replace_if_range<Pred, const InputRng> |
---|
99 | operator|( const InputRng& r, |
---|
100 | const replace_if_holder<Pred, BOOST_DEDUCED_TYPENAME range_value<InputRng>::type>& f ) |
---|
101 | { |
---|
102 | return replace_if_range<Pred, const InputRng>(r, f.pred(), f.to()); |
---|
103 | } |
---|
104 | } // 'range_detail' |
---|
105 | |
---|
106 | using range_detail::replace_if_range; |
---|
107 | |
---|
108 | namespace adaptors |
---|
109 | { |
---|
110 | namespace |
---|
111 | { |
---|
112 | const range_detail::forwarder2TU<range_detail::replace_if_holder> |
---|
113 | replaced_if = |
---|
114 | range_detail::forwarder2TU<range_detail::replace_if_holder>(); |
---|
115 | } |
---|
116 | |
---|
117 | template<class Pred, class InputRange> |
---|
118 | inline replace_if_range<Pred, InputRange> |
---|
119 | replace_if(InputRange& rng, Pred pred, |
---|
120 | BOOST_DEDUCED_TYPENAME range_value<InputRange>::type to) |
---|
121 | { |
---|
122 | return range_detail::replace_if_range<Pred, InputRange>(rng, pred, to); |
---|
123 | } |
---|
124 | |
---|
125 | template<class Pred, class InputRange> |
---|
126 | inline replace_if_range<Pred, const InputRange> |
---|
127 | replace_if(const InputRange& rng, Pred pred, |
---|
128 | BOOST_DEDUCED_TYPENAME range_value<const InputRange>::type to) |
---|
129 | { |
---|
130 | return range_detail::replace_if_range<Pred, const InputRange>(rng, pred, to); |
---|
131 | } |
---|
132 | } // 'adaptors' |
---|
133 | |
---|
134 | } // 'boost' |
---|
135 | |
---|
136 | #endif // include guard |
---|