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_IMPL_HPP_INCLUDED
|
Chris@16
|
12 #define BOOST_RANGE_ADAPTOR_REPLACED_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@16
|
20 #include <boost/iterator/iterator_adaptor.hpp>
|
Chris@16
|
21 #include <boost/iterator/transform_iterator.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost
|
Chris@16
|
24 {
|
Chris@16
|
25 namespace range_detail
|
Chris@16
|
26 {
|
Chris@16
|
27 template< class Value >
|
Chris@16
|
28 class replace_value
|
Chris@16
|
29 {
|
Chris@16
|
30 public:
|
Chris@16
|
31 typedef const Value& result_type;
|
Chris@16
|
32 typedef const Value& first_argument_type;
|
Chris@16
|
33
|
Chris@16
|
34 replace_value(const Value& from, const Value& to)
|
Chris@16
|
35 : m_from(from), m_to(to)
|
Chris@16
|
36 {
|
Chris@16
|
37 }
|
Chris@16
|
38
|
Chris@16
|
39 const Value& operator()(const Value& x) const
|
Chris@16
|
40 {
|
Chris@16
|
41 return (x == m_from) ? m_to : x;
|
Chris@16
|
42 }
|
Chris@16
|
43
|
Chris@16
|
44 private:
|
Chris@16
|
45 Value m_from;
|
Chris@16
|
46 Value m_to;
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49 template< class R >
|
Chris@16
|
50 class replaced_range :
|
Chris@16
|
51 public boost::iterator_range<
|
Chris@16
|
52 boost::transform_iterator<
|
Chris@16
|
53 replace_value< BOOST_DEDUCED_TYPENAME range_value<R>::type >,
|
Chris@16
|
54 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > >
|
Chris@16
|
55 {
|
Chris@16
|
56 private:
|
Chris@16
|
57 typedef replace_value< BOOST_DEDUCED_TYPENAME range_value<R>::type > Fn;
|
Chris@16
|
58
|
Chris@16
|
59 typedef boost::iterator_range<
|
Chris@16
|
60 boost::transform_iterator<
|
Chris@16
|
61 replace_value< BOOST_DEDUCED_TYPENAME range_value<R>::type >,
|
Chris@16
|
62 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > > base_t;
|
Chris@16
|
63
|
Chris@16
|
64 public:
|
Chris@16
|
65 typedef BOOST_DEDUCED_TYPENAME range_value<R>::type value_type;
|
Chris@16
|
66
|
Chris@16
|
67 replaced_range( R& r, value_type from, value_type to )
|
Chris@16
|
68 : base_t( make_transform_iterator( boost::begin(r), Fn(from, to) ),
|
Chris@16
|
69 make_transform_iterator( boost::end(r), Fn(from, to) ) )
|
Chris@16
|
70 { }
|
Chris@16
|
71 };
|
Chris@16
|
72
|
Chris@16
|
73 template< class T >
|
Chris@16
|
74 class replace_holder : public holder2<T>
|
Chris@16
|
75 {
|
Chris@16
|
76 public:
|
Chris@16
|
77 replace_holder( const T& from, const T& to )
|
Chris@16
|
78 : holder2<T>(from, to)
|
Chris@16
|
79 { }
|
Chris@16
|
80 private:
|
Chris@16
|
81 // not assignable
|
Chris@16
|
82 void operator=(const replace_holder&);
|
Chris@16
|
83 };
|
Chris@16
|
84
|
Chris@16
|
85 template< class InputRng >
|
Chris@16
|
86 inline replaced_range<InputRng>
|
Chris@16
|
87 operator|( InputRng& r,
|
Chris@16
|
88 const replace_holder<BOOST_DEDUCED_TYPENAME range_value<InputRng>::type>& f )
|
Chris@16
|
89 {
|
Chris@16
|
90 return replaced_range<InputRng>(r, f.val1, f.val2);
|
Chris@16
|
91 }
|
Chris@16
|
92
|
Chris@16
|
93 template< class InputRng >
|
Chris@16
|
94 inline replaced_range<const InputRng>
|
Chris@16
|
95 operator|( const InputRng& r,
|
Chris@16
|
96 const replace_holder<BOOST_DEDUCED_TYPENAME range_value<InputRng>::type>& f )
|
Chris@16
|
97 {
|
Chris@16
|
98 return replaced_range<const InputRng>(r, f.val1, f.val2);
|
Chris@16
|
99 }
|
Chris@16
|
100 } // 'range_detail'
|
Chris@16
|
101
|
Chris@16
|
102 using range_detail::replaced_range;
|
Chris@16
|
103
|
Chris@16
|
104 namespace adaptors
|
Chris@16
|
105 {
|
Chris@16
|
106 namespace
|
Chris@16
|
107 {
|
Chris@16
|
108 const range_detail::forwarder2<range_detail::replace_holder>
|
Chris@16
|
109 replaced =
|
Chris@16
|
110 range_detail::forwarder2<range_detail::replace_holder>();
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 template<class InputRange>
|
Chris@16
|
114 inline replaced_range<InputRange>
|
Chris@16
|
115 replace(InputRange& rng,
|
Chris@16
|
116 BOOST_DEDUCED_TYPENAME range_value<InputRange>::type from,
|
Chris@16
|
117 BOOST_DEDUCED_TYPENAME range_value<InputRange>::type to)
|
Chris@16
|
118 {
|
Chris@16
|
119 return replaced_range<InputRange>(rng, from, to);
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 template<class InputRange>
|
Chris@16
|
123 inline replaced_range<const InputRange>
|
Chris@16
|
124 replace(const InputRange& rng,
|
Chris@16
|
125 BOOST_DEDUCED_TYPENAME range_value<const InputRange>::type from,
|
Chris@16
|
126 BOOST_DEDUCED_TYPENAME range_value<const InputRange>::type to)
|
Chris@16
|
127 {
|
Chris@16
|
128 return replaced_range<const InputRange>(rng, from ,to);
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 } // 'adaptors'
|
Chris@16
|
132 } // 'boost'
|
Chris@16
|
133
|
Chris@16
|
134 #endif // include guard
|