for_each.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. 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_ALGORITHM_EXT_FOR_EACH_HPP_INCLUDED
  11. #define BOOST_RANGE_ALGORITHM_EXT_FOR_EACH_HPP_INCLUDED
  12. #include <boost/range/config.hpp>
  13. #include <boost/range/concepts.hpp>
  14. #include <boost/range/difference_type.hpp>
  15. #include <boost/range/begin.hpp>
  16. #include <boost/range/end.hpp>
  17. #include <boost/assert.hpp>
  18. namespace boost
  19. {
  20. namespace range_detail
  21. {
  22. template<class InputIterator1, class InputIterator2, class Fn2>
  23. inline Fn2 for_each_impl(InputIterator1 first1, InputIterator1 last1,
  24. InputIterator2 first2, InputIterator2 last2,
  25. Fn2 fn)
  26. {
  27. for (; first1 != last1 && first2 != last2; ++first1, ++first2)
  28. {
  29. fn(*first1, *first2);
  30. }
  31. return fn;
  32. }
  33. }
  34. namespace range
  35. {
  36. template<class SinglePassRange1, class SinglePassRange2, class Fn2>
  37. inline Fn2 for_each(const SinglePassRange1& rng1, const SinglePassRange2& rng2, Fn2 fn)
  38. {
  39. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  40. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  41. return ::boost::range_detail::for_each_impl(
  42. ::boost::begin(rng1), ::boost::end(rng1),
  43. ::boost::begin(rng2), ::boost::end(rng2), fn);
  44. }
  45. template<class SinglePassRange1, class SinglePassRange2, class Fn2>
  46. inline Fn2 for_each(const SinglePassRange1& rng1, SinglePassRange2& rng2, Fn2 fn)
  47. {
  48. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  49. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  50. return ::boost::range_detail::for_each_impl(
  51. ::boost::begin(rng1), ::boost::end(rng1),
  52. ::boost::begin(rng2), ::boost::end(rng2), fn);
  53. }
  54. template<class SinglePassRange1, class SinglePassRange2, class Fn2>
  55. inline Fn2 for_each(SinglePassRange1& rng1, const SinglePassRange2& rng2, Fn2 fn)
  56. {
  57. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange1> ));
  58. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  59. return ::boost::range_detail::for_each_impl(
  60. ::boost::begin(rng1), ::boost::end(rng1),
  61. ::boost::begin(rng2), ::boost::end(rng2), fn);
  62. }
  63. template<class SinglePassRange1, class SinglePassRange2, class Fn2>
  64. inline Fn2 for_each(SinglePassRange1& rng1, SinglePassRange2& rng2, Fn2 fn)
  65. {
  66. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange1> ));
  67. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  68. return ::boost::range_detail::for_each_impl(
  69. ::boost::begin(rng1), ::boost::end(rng1),
  70. ::boost::begin(rng2), ::boost::end(rng2), fn);
  71. }
  72. } // namespace range
  73. using range::for_each;
  74. } // namespace boost
  75. #endif // include guard