1 /*=============================================================================
2 Copyright (c) 2011 Eric Niebler
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(BOOST_FUSION_SEGMENTED_FOLD_UNTIL_IMPL_HPP_INCLUDED)
8 #define BOOST_FUSION_SEGMENTED_FOLD_UNTIL_IMPL_HPP_INCLUDED
10 #include <boost/fusion/support/config.hpp>
11 #include <boost/mpl/bool.hpp>
12 #include <boost/mpl/eval_if.hpp>
13 #include <boost/mpl/identity.hpp>
14 #include <boost/utility/result_of.hpp>
15 #include <boost/type_traits/add_const.hpp>
16 #include <boost/type_traits/remove_reference.hpp>
18 #include <boost/fusion/support/void.hpp>
19 #include <boost/fusion/container/list/cons_fwd.hpp>
20 #include <boost/fusion/sequence/intrinsic_fwd.hpp>
21 #include <boost/fusion/iterator/equal_to.hpp>
22 #include <boost/fusion/iterator/deref.hpp>
23 #include <boost/fusion/iterator/next.hpp>
24 #include <boost/fusion/support/is_segmented.hpp>
25 #include <boost/fusion/sequence/intrinsic/segments.hpp>
27 // fun(seq, state, context)
28 // seq: a non-segmented range
29 // state: the state of the fold so far
30 // context: the path to the current range
32 // returns: (state', fcontinue)
34 namespace boost { namespace fusion
36 template <typename First, typename Last>
37 struct iterator_range;
39 template <typename Context>
40 struct segmented_iterator;
44 template <typename Cur, typename Context>
45 struct make_segmented_iterator
50 , typename result_of::end<
51 typename remove_reference<
53 typename result_of::deref<
54 typename Context::car_type::begin_type
63 segmented_iterator<cons<range_type, Context> >
68 template <typename Cur, typename Context>
69 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
70 inline typename result_of::make_segmented_iterator<Cur, Context>::type
71 make_segmented_iterator(Cur const& cur, Context const& context)
73 typedef result_of::make_segmented_iterator<Cur, Context> impl_type;
74 typedef typename impl_type::type type;
75 typedef typename impl_type::range_type range_type;
76 return type(cons<range_type, Context>(range_type(cur, fusion::end(*context.car.first)), context));
89 struct segmented_fold_until_iterate_skip_empty;
97 , bool IsDone = result_of::equal_to<Begin, End>::type::value
99 struct segmented_fold_until_iterate;
106 , bool IsSegmented = traits::is_segmented<Sequence>::type::value
108 struct segmented_fold_until_impl;
110 template <typename Segments, typename State, typename Context, typename Fun>
111 struct segmented_fold_until_on_segments;
113 //auto push_context(cur, end, context)
115 // return push_back(context, segment_sequence(iterator_range(cur, end)));
118 template <typename Cur, typename End, typename Context>
121 typedef iterator_range<Cur, End> range_type;
122 typedef cons<range_type, Context> type;
124 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
125 static type call(Cur const& cur, End const& end, Context const& context)
127 return cons<range_type, Context>(range_type(cur, end), context);
131 //auto make_segmented_iterator(cur, end, context)
133 // return segmented_iterator(push_context(cur, end, context));
136 //auto segmented_fold_until_impl(seq, state, context, fun)
138 // if (is_segmented(seq))
140 // segmented_fold_until_on_segments(segments(seq), state, context, fun);
144 // return fun(seq, state, context);
155 struct segmented_fold_until_impl
158 segmented_fold_until_on_segments<
159 typename remove_reference<
161 typename result_of::segments<Sequence>::type
170 typedef typename impl::type type;
171 typedef typename impl::continue_type continue_type;
173 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
174 static type call(Sequence& seq, State const& state, Context const& context, Fun const& fun)
176 return impl::call(fusion::segments(seq), state, context, fun);
186 struct segmented_fold_until_impl<Sequence, State, Context, Fun, false>
189 typename Fun::template apply<Sequence, State, Context>
192 typedef typename apply_type::type type;
193 typedef typename apply_type::continue_type continue_type;
195 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
196 static type call(Sequence& seq, State const& state, Context const& context, Fun const& fun)
198 return apply_type::call(seq, state, context, fun);
202 //auto segmented_fold_until_on_segments(segs, state, context, fun)
204 // auto cur = begin(segs), end = end(segs);
205 // for (; cur != end; ++cur)
209 // auto context` = push_context(cur, end, context);
210 // state = segmented_fold_until_impl(*cur, state, context`, fun);
211 // if (!second(state))
216 template <typename Apply>
219 typedef typename Apply::continue_type type;
222 template <typename Begin, typename End, typename State, typename Context, typename Fun, bool IsEmpty>
223 struct segmented_fold_until_iterate_skip_empty
225 // begin != end and !empty(*begin)
227 push_context<Begin, End, Context>
231 typename push_context_impl::type
235 segmented_fold_until_impl<
236 typename remove_reference<
238 typename result_of::deref<Begin>::type
248 typename fold_recurse_impl::type
252 segmented_fold_until_iterate<
253 typename result_of::next<Begin>::type
262 typename mpl::eval_if<
263 typename fold_recurse_impl::continue_type
264 , next_iteration_impl
265 , mpl::identity<next_state_type>
270 typename mpl::eval_if<
271 typename fold_recurse_impl::continue_type
272 , continue_wrap<next_iteration_impl>
273 , mpl::identity<mpl::false_>
277 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
278 static type call(Begin const& beg, End const& end, State const& state
279 , Context const& context, Fun const& fun)
281 return call(beg, end, state, context, fun, typename fold_recurse_impl::continue_type());
284 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
285 static type call(Begin const& beg, End const& end, State const& state
286 , Context const& context, Fun const& fun, mpl::true_) // continue
288 return next_iteration_impl::call(
291 , fold_recurse_impl::call(
294 , push_context_impl::call(beg, end, context)
300 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
301 static type call(Begin const& beg, End const& end, State const& state
302 , Context const& context, Fun const& fun, mpl::false_) // break
304 return fold_recurse_impl::call(
307 , push_context_impl::call(beg, end, context)
312 template <typename Begin, typename End, typename State, typename Context, typename Fun>
313 struct segmented_fold_until_iterate_skip_empty<Begin, End, State, Context, Fun, true>
316 segmented_fold_until_iterate<
317 typename result_of::next<Begin>::type
325 typedef typename impl::type type;
326 typedef typename impl::continue_type continue_type;
328 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
329 static type call(Begin const& beg, End const& end, State const& state
330 , Context const& context, Fun const& fun)
332 return impl::call(fusion::next(beg), end, state, context, fun);
336 template <typename Begin, typename End, typename State, typename Context, typename Fun, bool IsDone>
337 struct segmented_fold_until_iterate
340 typename result_of::empty<
341 typename remove_reference<
342 typename result_of::deref<Begin>::type
348 segmented_fold_until_iterate_skip_empty<Begin, End, State, Context, Fun, empty_type::value>
351 typedef typename impl::type type;
352 typedef typename impl::continue_type continue_type;
354 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
355 static type call(Begin const& beg, End const& end, State const& state
356 , Context const& context, Fun const& fun)
358 return impl::call(beg, end, state, context, fun);
362 template <typename Begin, typename End, typename State, typename Context, typename Fun>
363 struct segmented_fold_until_iterate<Begin, End, State, Context, Fun, true>
366 typedef mpl::true_ continue_type;
368 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
369 static type call(Begin const&, End const&, State const& state
370 , Context const&, Fun const&)
376 template <typename Segments, typename State, typename Context, typename Fun>
377 struct segmented_fold_until_on_segments
380 segmented_fold_until_iterate<
381 typename result_of::begin<Segments>::type
382 , typename result_of::end<Segments>::type
389 typedef typename impl::type type;
390 typedef typename impl::continue_type continue_type;
392 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
393 static type call(Segments& segs, State const& state, Context const& context, Fun const& fun)
395 return impl::call(fusion::begin(segs), fusion::end(segs), state, context, fun);