transform.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_TRANSFORM_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_TRANSFORM_HPP_INCLUDED
  11. #include <boost/assert.hpp>
  12. #include <boost/concept_check.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/concepts.hpp>
  16. #include <algorithm>
  17. namespace boost
  18. {
  19. namespace range
  20. {
  21. /// \brief template function transform
  22. ///
  23. /// range-based version of the transform std algorithm
  24. ///
  25. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  26. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  27. /// \pre OutputIterator is a model of the OutputIteratorConcept
  28. /// \pre UnaryOperation is a model of the UnaryFunctionConcept
  29. /// \pre BinaryOperation is a model of the BinaryFunctionConcept
  30. template< class SinglePassRange1,
  31. class OutputIterator,
  32. class UnaryOperation >
  33. inline OutputIterator
  34. transform(const SinglePassRange1& rng,
  35. OutputIterator out,
  36. UnaryOperation fun)
  37. {
  38. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  39. return std::transform(boost::begin(rng),boost::end(rng),out,fun);
  40. }
  41. } // namespace range
  42. namespace range_detail
  43. {
  44. template< class SinglePassTraversalReadableIterator1,
  45. class SinglePassTraversalReadableIterator2,
  46. class OutputIterator,
  47. class BinaryFunction >
  48. inline OutputIterator
  49. transform_impl(SinglePassTraversalReadableIterator1 first1,
  50. SinglePassTraversalReadableIterator1 last1,
  51. SinglePassTraversalReadableIterator2 first2,
  52. SinglePassTraversalReadableIterator2 last2,
  53. OutputIterator out,
  54. BinaryFunction fn)
  55. {
  56. for (; first1 != last1 && first2 != last2; ++first1, ++first2)
  57. {
  58. *out = fn(*first1, *first2);
  59. ++out;
  60. }
  61. return out;
  62. }
  63. }
  64. namespace range
  65. {
  66. /// \overload
  67. template< class SinglePassRange1,
  68. class SinglePassRange2,
  69. class OutputIterator,
  70. class BinaryOperation >
  71. inline OutputIterator
  72. transform(const SinglePassRange1& rng1,
  73. const SinglePassRange2& rng2,
  74. OutputIterator out,
  75. BinaryOperation fun)
  76. {
  77. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  78. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  79. return boost::range_detail::transform_impl(
  80. boost::begin(rng1), boost::end(rng1),
  81. boost::begin(rng2), boost::end(rng2),
  82. out, fun);
  83. }
  84. } // namespace range
  85. using range::transform;
  86. } // namespace boost
  87. #endif // include guard