iteration.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2005 Daniel Wallin.
  2. // Copyright 2005 Joel de Guzman.
  3. // Copyright 2005 Dan Marsden.
  4. //
  5. // Use, modification and distribution is subject to the Boost Software
  6. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Modeled after range_ex, Copyright 2004 Eric Niebler
  10. #ifndef BOOST_PHOENIX_ALGORITHM_ITERATION_HPP
  11. #define BOOST_PHOENIX_ALGORITHM_ITERATION_HPP
  12. #include <algorithm>
  13. #include <numeric>
  14. #include <boost/phoenix/stl/algorithm/detail/begin.hpp>
  15. #include <boost/phoenix/stl/algorithm/detail/end.hpp>
  16. #include <boost/phoenix/function/adapt_callable.hpp>
  17. namespace boost { namespace phoenix {
  18. namespace impl
  19. {
  20. struct for_each
  21. {
  22. template <typename Sig>
  23. struct result;
  24. template<typename This, class R, class F>
  25. struct result<This(R&, F)>
  26. : result<This(R&, F const &)>
  27. {};
  28. template<typename This, class R, class F>
  29. struct result<This(R&, F &)>
  30. {
  31. typedef F type;
  32. };
  33. template<class R, class F>
  34. F const operator()(R& r, F const& fn) const
  35. {
  36. return std::for_each(detail::begin_(r), detail::end_(r), fn);
  37. }
  38. };
  39. struct accumulate
  40. {
  41. template <typename Sig>
  42. struct result;
  43. template<typename This, class R, class I>
  44. struct result<This(R&, I)>
  45. : result<This(R&, I const &)>
  46. {};
  47. template<typename This, class R, class I>
  48. struct result<This(R&, I &)>
  49. {
  50. typedef I type;
  51. };
  52. template<typename This, class R, class I, class C>
  53. struct result<This(R&, I, C)>
  54. : result<This(R&, I const &, C)>
  55. {};
  56. template<typename This, class R, class I, class C>
  57. struct result<This(R&, I &, C)>
  58. {
  59. typedef I type;
  60. };
  61. template<class R, class I>
  62. I
  63. operator()(R& r, I i) const
  64. {
  65. return std::accumulate(detail::begin_(r), detail::end_(r), i);
  66. }
  67. template<class R, class I, class C>
  68. I
  69. operator()(R& r, I i, C c) const
  70. {
  71. return std::accumulate(detail::begin_(r), detail::end_(r), i, c);
  72. }
  73. };
  74. }
  75. BOOST_PHOENIX_ADAPT_CALLABLE(for_each, impl::for_each, 2)
  76. BOOST_PHOENIX_ADAPT_CALLABLE(accumulate, impl::accumulate, 2)
  77. BOOST_PHOENIX_ADAPT_CALLABLE(accumulate, impl::accumulate, 3)
  78. }}
  79. #endif