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 12 #include <boost/assert.hpp> 13 #include <boost/concept_check.hpp> 14 #include <boost/range/begin.hpp> 15 #include <boost/range/end.hpp> 16 #include <boost/range/concepts.hpp> 17 #include <algorithm> 18 19 namespace boost 20 { 21 namespace range 22 { 23 24 /// \brief template function transform 25 /// 26 /// range-based version of the transform std algorithm 27 /// 28 /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept 29 /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept 30 /// \pre OutputIterator is a model of the OutputIteratorConcept 31 /// \pre UnaryOperation is a model of the UnaryFunctionConcept 32 /// \pre BinaryOperation is a model of the BinaryFunctionConcept 33 template< class SinglePassRange1, 34 class OutputIterator, 35 class UnaryOperation > 36 inline OutputIterator transform(const SinglePassRange1 & rng,OutputIterator out,UnaryOperation fun)37 transform(const SinglePassRange1& rng, 38 OutputIterator out, 39 UnaryOperation fun) 40 { 41 BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> )); 42 return std::transform(boost::begin(rng),boost::end(rng),out,fun); 43 } 44 45 } // namespace range 46 47 namespace range_detail 48 { 49 template< class SinglePassTraversalReadableIterator1, 50 class SinglePassTraversalReadableIterator2, 51 class OutputIterator, 52 class BinaryFunction > 53 inline OutputIterator transform_impl(SinglePassTraversalReadableIterator1 first1,SinglePassTraversalReadableIterator1 last1,SinglePassTraversalReadableIterator2 first2,SinglePassTraversalReadableIterator2 last2,OutputIterator out,BinaryFunction fn)54 transform_impl(SinglePassTraversalReadableIterator1 first1, 55 SinglePassTraversalReadableIterator1 last1, 56 SinglePassTraversalReadableIterator2 first2, 57 SinglePassTraversalReadableIterator2 last2, 58 OutputIterator out, 59 BinaryFunction fn) 60 { 61 for (; first1 != last1 && first2 != last2; ++first1, ++first2) 62 { 63 *out = fn(*first1, *first2); 64 ++out; 65 } 66 return out; 67 } 68 } 69 70 namespace range 71 { 72 73 /// \overload 74 template< class SinglePassRange1, 75 class SinglePassRange2, 76 class OutputIterator, 77 class BinaryOperation > 78 inline OutputIterator transform(const SinglePassRange1 & rng1,const SinglePassRange2 & rng2,OutputIterator out,BinaryOperation fun)79 transform(const SinglePassRange1& rng1, 80 const SinglePassRange2& rng2, 81 OutputIterator out, 82 BinaryOperation fun) 83 { 84 BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> )); 85 BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> )); 86 return boost::range_detail::transform_impl( 87 boost::begin(rng1), boost::end(rng1), 88 boost::begin(rng2), boost::end(rng2), 89 out, fun); 90 } 91 92 } // namespace range 93 using range::transform; 94 } // namespace boost 95 96 #endif // include guard 97