/*============================================================================= Copyright (c) 2011 Eric Niebler Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ==============================================================================*/ #if !defined(BOOST_FUSION_SEGMENTED_ITERATOR_NEXT_IMPL_HPP_INCLUDED) #define BOOST_FUSION_SEGMENTED_ITERATOR_NEXT_IMPL_HPP_INCLUDED #include #include #include #include #include #include #include namespace boost { namespace fusion { template struct iterator_range; template struct segmented_iterator; namespace detail { template struct segmented_begin_impl; //bool is_invalid(stack) //{ // return empty(car(stack)); //} template struct is_invalid : result_of::equal_to< typename Stack::car_type::begin_type, typename Stack::car_type::end_type > {}; ////Advance the first iterator in the seq at the ////top of a stack of iterator ranges. Return the ////new stack. //auto pop_front_car(stack) //{ // return cons(iterator_range(next(begin(car(stack))), end(car(stack))), cdr(stack)); //} template struct pop_front_car { typedef iterator_range< typename result_of::next< typename Stack::car_type::begin_type >::type , typename Stack::car_type::end_type > car_type; typedef cons type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return type( car_type(fusion::next(stack.car.first), stack.car.last), stack.cdr); } }; template < typename Stack, typename Next = typename pop_front_car::type, bool IsInvalid = is_invalid::value, int StackSize = Stack::size::value> struct segmented_next_impl_recurse; // Handle the case where the top of the stack has no usable //auto segmented_next_impl_recurse3(stack) //{ // if (size(stack) == 1) // return cons(iterator_range(end(car(stack)), end(car(stack))), nil_); // else // return segmented_next_impl_recurse(stack.cdr); //} template < typename Stack, int StackSize = Stack::size::value> struct segmented_next_impl_recurse3 { typedef segmented_next_impl_recurse impl; typedef typename impl::type type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return impl::call(stack.cdr); } }; template struct segmented_next_impl_recurse3 { typedef typename Stack::car_type::end_type end_type; typedef iterator_range range_type; typedef cons type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return type(range_type(stack.car.last, stack.car.last)); } }; //auto segmented_next_impl_recurse2(stack) //{ // auto res = segmented_begin_impl(front(car(stack)), stack); // if (is_invalid(res)) // return segmented_next_impl_recurse3(stack); // else // return res; //} template < typename Stack, typename Sequence = typename remove_reference< typename add_const< typename result_of::deref< typename Stack::car_type::begin_type >::type >::type >::type, typename Result = typename segmented_begin_impl::type, bool IsInvalid = is_invalid::value> struct segmented_next_impl_recurse2 { typedef segmented_next_impl_recurse3 impl; typedef typename impl::type type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return impl::call(stack); } }; template struct segmented_next_impl_recurse2 { typedef Result type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return segmented_begin_impl::call(*stack.car.first, stack); } }; //auto segmented_next_impl_recurse(stack) //{ // auto next = pop_front_car(stack); // if (is_invalid(next)) // if (1 == size(stack)) // return next; // else // return segmented_next_impl_recurse(cdr(stack)); // else // return segmented_next_impl_recurse2(next) //} template struct segmented_next_impl_recurse { typedef typename segmented_next_impl_recurse::type type; BOOST_FUSION_GPU_ENABLED static type call(Stack const& stack) { return segmented_next_impl_recurse::call(stack.cdr); } }; template struct segmented_next_impl_recurse { typedef Next type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return pop_front_car::call(stack); } }; template struct segmented_next_impl_recurse { typedef segmented_next_impl_recurse2 impl; typedef typename impl::type type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return impl::call(pop_front_car::call(stack)); } }; //auto segmented_next_impl(stack) //{ // // car(stack) is a seq of values, not a seq of segments // auto next = pop_front_car(stack); // if (is_invalid(next)) // return segmented_next_impl_recurse(cdr(next)); // else // return next; //} template < typename Stack, typename Next = typename pop_front_car::type, bool IsInvalid = is_invalid::value> struct segmented_next_impl_aux { typedef segmented_next_impl_recurse impl; typedef typename impl::type type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return impl::call(stack.cdr); } }; template struct segmented_next_impl_aux { typedef Next type; BOOST_FUSION_GPU_ENABLED static type call(Stack const & stack) { return pop_front_car::call(stack); } }; template struct segmented_next_impl : segmented_next_impl_aux {}; } }} #endif