1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(FUSION_FOR_EACH_05052005_1028)
8 #define FUSION_FOR_EACH_05052005_1028
10 #include <boost/fusion/support/config.hpp>
11 #include <boost/fusion/sequence/intrinsic/begin.hpp>
12 #include <boost/fusion/sequence/intrinsic/end.hpp>
13 #include <boost/fusion/iterator/equal_to.hpp>
14 #include <boost/fusion/iterator/next.hpp>
15 #include <boost/fusion/iterator/deref.hpp>
16 #include <boost/fusion/iterator/distance.hpp>
17 #include <boost/fusion/support/category_of.hpp>
18 #include <boost/mpl/bool.hpp>
20 namespace boost { namespace fusion {
23 template <typename First, typename Last, typename F>
24 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
26 for_each_linear(First const&, Last const&, F const&, mpl::true_)
30 template <typename First, typename Last, typename F>
31 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
33 for_each_linear(First const& first, Last const& last, F& f, mpl::false_)
36 detail::for_each_linear(fusion::next(first), last, f,
37 result_of::equal_to<typename result_of::next<First>::type, Last>());
41 template <typename Sequence, typename F, typename Tag>
42 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
44 for_each_dispatch(Sequence& seq, F& f, Tag)
46 detail::for_each_linear(
50 , result_of::equal_to<
51 typename result_of::begin<Sequence>::type
52 , typename result_of::end<Sequence>::type>());
56 struct for_each_unrolled
58 template<typename I0, typename F>
59 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
60 static void call(I0 const& i0, F& f)
63 typedef typename result_of::next<I0>::type I1;
64 I1 i1(fusion::next(i0));
66 typedef typename result_of::next<I1>::type I2;
67 I2 i2(fusion::next(i1));
69 typedef typename result_of::next<I2>::type I3;
70 I3 i3(fusion::next(i2));
72 for_each_unrolled<N-4>::call(fusion::next(i3), f);
77 struct for_each_unrolled<3>
79 template<typename I0, typename F>
80 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
81 static void call(I0 const& i0, F& f)
84 typedef typename result_of::next<I0>::type I1;
85 I1 i1(fusion::next(i0));
87 typedef typename result_of::next<I1>::type I2;
88 I2 i2(fusion::next(i1));
94 struct for_each_unrolled<2>
96 template<typename I0, typename F>
97 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
98 static void call(I0 const& i0, F& f)
101 typedef typename result_of::next<I0>::type I1;
102 I1 i1(fusion::next(i0));
108 struct for_each_unrolled<1>
110 template<typename I0, typename F>
111 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
112 static void call(I0 const& i0, F& f)
119 struct for_each_unrolled<0>
121 template<typename It, typename F>
122 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
123 static void call(It const&, F const&)
128 template <typename Sequence, typename F>
129 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
131 for_each_dispatch(Sequence& seq, F& f, random_access_traversal_tag)
133 typedef typename result_of::begin<Sequence>::type begin;
134 typedef typename result_of::end<Sequence>::type end;
135 for_each_unrolled<result_of::distance<begin, end>::type::value>::call(fusion::begin(seq), f);
138 template <typename Sequence, typename F>
139 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
141 for_each(Sequence& seq, F& f, mpl::false_) // unsegmented implementation
143 detail::for_each_dispatch(seq, f, typename traits::category_of<Sequence>::type());