| 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 | |
| 11 | #ifndef BOOST_PHOENIX_ALGORITHM_ITERATION_HPP |
| 12 | #define BOOST_PHOENIX_ALGORITHM_ITERATION_HPP |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <numeric> |
| 16 | |
| 17 | #include <boost/phoenix/stl/algorithm/detail/begin.hpp> |
| 18 | #include <boost/phoenix/stl/algorithm/detail/end.hpp> |
| 19 | |
| 20 | #include <boost/phoenix/function/adapt_callable.hpp> |
| 21 | |
| 22 | namespace boost { namespace phoenix { |
| 23 | namespace impl |
| 24 | { |
| 25 | struct for_each |
| 26 | { |
| 27 | template <typename Sig> |
| 28 | struct result; |
| 29 | |
| 30 | template<typename This, class R, class F> |
| 31 | struct result<This(R&, F)> |
| 32 | : result<This(R&, F const &)> |
| 33 | {}; |
| 34 | |
| 35 | template<typename This, class R, class F> |
| 36 | struct result<This(R&, F &)> |
| 37 | { |
| 38 | typedef F type; |
| 39 | }; |
| 40 | |
| 41 | template<class R, class F> |
| 42 | F const operator()(R& r, F const& fn) const |
| 43 | { |
| 44 | return std::for_each(detail::begin_(r), detail::end_(r), fn); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | struct accumulate |
| 49 | { |
| 50 | template <typename Sig> |
| 51 | struct result; |
| 52 | |
| 53 | template<typename This, class R, class I> |
| 54 | struct result<This(R&, I)> |
| 55 | : result<This(R&, I const &)> |
| 56 | {}; |
| 57 | |
| 58 | template<typename This, class R, class I> |
| 59 | struct result<This(R&, I &)> |
| 60 | { |
| 61 | typedef I type; |
| 62 | }; |
| 63 | |
| 64 | template<typename This, class R, class I, class C> |
| 65 | struct result<This(R&, I, C)> |
| 66 | : result<This(R&, I const &, C)> |
| 67 | {}; |
| 68 | |
| 69 | template<typename This, class R, class I, class C> |
| 70 | struct result<This(R&, I &, C)> |
| 71 | { |
| 72 | typedef I type; |
| 73 | }; |
| 74 | |
| 75 | template<class R, class I> |
| 76 | I |
| 77 | operator()(R& r, I i) const |
| 78 | { |
| 79 | return std::accumulate(detail::begin_(r), detail::end_(r), i); |
| 80 | } |
| 81 | |
| 82 | template<class R, class I, class C> |
| 83 | I |
| 84 | operator()(R& r, I i, C c) const |
| 85 | { |
| 86 | return std::accumulate(detail::begin_(r), detail::end_(r), i, c); |
| 87 | } |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | BOOST_PHOENIX_ADAPT_CALLABLE(for_each, impl::for_each, 2) |
| 92 | BOOST_PHOENIX_ADAPT_CALLABLE(accumulate, impl::accumulate, 2) |
| 93 | BOOST_PHOENIX_ADAPT_CALLABLE(accumulate, impl::accumulate, 3) |
| 94 | |
| 95 | }} |
| 96 | |
| 97 | #endif |
| 98 | |