/Src/Dependencies/Boost/boost/range/adaptor/replaced_if.hpp

http://hadesmem.googlecode.com/ · C++ Header · 136 lines · 106 code · 21 blank · 9 comment · 0 complexity · b42a439d9b6400cfacafc1ffd3abf40d MD5 · raw file

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