segmented_iterator_range.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 538 行 · 第 1/2 页

HPP
538
字号
/*============================================================================= Copyright (c) 2006 Eric Niebler Use, modification and distribution is subject to 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)==============================================================================*/#ifndef FUSION_SEGMENTED_ITERATOR_RANGE_EAN_05032006_1027#define FUSION_SEGMENTED_ITERATOR_RANGE_EAN_05032006_1027#include <boost/mpl/bool.hpp>#include <boost/mpl/minus.hpp>#include <boost/mpl/next_prior.hpp>#include <boost/mpl/and.hpp>#include <boost/type_traits/remove_cv.hpp>#include <boost/type_traits/remove_reference.hpp>#include <boost/fusion/iterator/mpl/convert_iterator.hpp>#include <boost/fusion/container/list/cons.hpp>#include <boost/fusion/view/joint_view.hpp>#include <boost/fusion/view/single_view.hpp>#include <boost/fusion/view/transform_view.hpp>#include <boost/fusion/view/iterator_range.hpp>#include <boost/fusion/view/ext_/multiple_view.hpp>#include <boost/fusion/view/ext_/segmented_iterator.hpp>#include <boost/fusion/adapted/mpl/mpl_iterator.hpp>namespace boost { namespace fusion{    namespace detail    {        ////////////////////////////////////////////////////////////////////////////        template<typename Cons, typename State = nil>        struct reverse_cons;        template<typename Car, typename Cdr, typename State>        struct reverse_cons<cons<Car, Cdr>, State>        {            typedef reverse_cons<Cdr, cons<Car, State> > reverse;            typedef typename reverse::type type;            static type call(cons<Car, Cdr> const &cons, State const &state = State())            {                return reverse::call(cons.cdr, fusion::make_cons(cons.car, state));            }        };        template<typename State>        struct reverse_cons<nil, State>        {            typedef State type;            static State const &call(nil const &, State const &state = State())            {                return state;            }        };        ////////////////////////////////////////////////////////////////////////////        // tags        struct full_view {};        struct left_view {};        struct right_view {};        struct center_view {};        template<typename Tag>        struct segmented_view_tag;        ////////////////////////////////////////////////////////////////////////////        // a segmented view of that includes all elements either to the        // right or the left of a segmented iterator.        template<typename Tag, typename Cons1, typename Cons2 = void_>        struct segmented_view          : sequence_base<segmented_view<Tag, Cons1, Cons2> >        {            typedef segmented_view_tag<Tag> fusion_tag;            typedef fusion_sequence_tag tag; // this gets picked up by MPL            typedef mpl::true_ is_view;            typedef forward_traversal_tag category;            explicit segmented_view(Cons1 const &cons)              : cons(cons)            {}            typedef Cons1 cons_type;            cons_type const &cons;        };        // a segmented view that contains all the elements in between        // two segmented iterators        template<typename Cons1, typename Cons2>        struct segmented_view<center_view, Cons1, Cons2>          : sequence_base<segmented_view<center_view, Cons1, Cons2> >        {            typedef segmented_view_tag<center_view> fusion_tag;            typedef fusion_sequence_tag tag; // this gets picked up by MPL            typedef mpl::true_ is_view;            typedef forward_traversal_tag category;            segmented_view(Cons1 const &lcons, Cons2 const &rcons)              : left_cons(lcons)              , right_cons(rcons)            {}            typedef Cons1 left_cons_type;            typedef Cons2 right_cons_type;            left_cons_type const &left_cons;            right_cons_type const &right_cons;        };        ////////////////////////////////////////////////////////////////////////////        // Used to transform a sequence of segments. The first segment is        // bounded by RightCons, and the last segment is bounded by LeftCons        // and all the others are passed through unchanged.        template<typename RightCons, typename LeftCons = RightCons>        struct segments_transform        {            explicit segments_transform(RightCons const &cons_)              : right_cons(cons_)              , left_cons(cons_)            {}            segments_transform(RightCons const &right_cons_, LeftCons const &left_cons_)              : right_cons(right_cons_)              , left_cons(left_cons_)            {}            template<typename First, typename Second>            struct result_;            template<typename Second>            struct result_<right_view, Second>            {                typedef segmented_view<right_view, RightCons> type;            };            template<typename Second>            struct result_<left_view, Second>            {                typedef segmented_view<left_view, LeftCons> type;            };            template<typename Second>            struct result_<full_view, Second>            {                typedef Second type;            };            template<typename Sig>            struct result;            template<typename This, typename First, typename Second>            struct result<This(First, Second)>              : result_<                    typename remove_cv<typename remove_reference<First>::type>::type                  , typename remove_cv<typename remove_reference<Second>::type>::type                >            {};            template<typename Second>            segmented_view<right_view, RightCons> operator ()(right_view, Second &second) const            {                return segmented_view<right_view, RightCons>(this->right_cons);            }            template<typename Second>            segmented_view<left_view, LeftCons> operator ()(left_view, Second &second) const            {                return segmented_view<left_view, LeftCons>(this->left_cons);            }            template<typename Second>            Second &operator ()(full_view, Second &second) const            {                return second;            }        private:            RightCons const &right_cons;            LeftCons const &left_cons;        };    } // namespace detail    namespace extension    {        ////////////////////////////////////////////////////////////////////////////        template<typename Tag>        struct is_segmented_impl<detail::segmented_view_tag<Tag> >        {            template<typename Sequence>            struct apply              : mpl::true_            {};        };        ////////////////////////////////////////////////////////////////////////////        template<>        struct segments_impl<detail::segmented_view_tag<detail::right_view> >        {            template<                typename Sequence              , typename Cdr = typename Sequence::cons_type::cdr_type            >            struct apply            {                typedef typename Sequence::cons_type::car_type segmented_range;                typedef typename result_of::size<segmented_range>::type size;                typedef typename mpl::prior<size>::type size_minus_1;                typedef detail::segments_transform<Cdr> tfx;                typedef joint_view<                    single_view<detail::right_view> const                  , multiple_view<size_minus_1, detail::full_view> const                > mask;                typedef transform_view<mask const, segmented_range const, tfx> type;                static type call(Sequence &seq)                {                    return type(                        mask(                            make_single_view(detail::right_view())                          , make_multiple_view<size_minus_1>(detail::full_view())                        )                      , seq.cons.car                      , tfx(seq.cons.cdr)                    );                }            };            template<typename Sequence>            struct apply<Sequence, nil>            {                typedef typename Sequence::cons_type::car_type segmented_range;                typedef typename segmented_range::iterator_type begin;                typedef typename segmented_range::sequence_non_ref_type sequence_type;                typedef typename result_of::end<sequence_type>::type end;                typedef iterator_range<begin, end> range;                typedef single_view<range> type;                static type call(Sequence &seq)                {                    return type(range(seq.cons.car.where_, fusion::end(seq.cons.car.sequence)));                }            };        };        ////////////////////////////////////////////////////////////////////////////        template<>        struct segments_impl<detail::segmented_view_tag<detail::left_view> >        {            template<                typename Sequence              , typename Cdr = typename Sequence::cons_type::cdr_type            >            struct apply            {                typedef typename Sequence::cons_type::car_type right_segmented_range;                typedef typename right_segmented_range::sequence_type sequence_type;                typedef typename right_segmented_range::iterator_type iterator_type;                typedef iterator_range<                    typename result_of::begin<sequence_type>::type                  , typename result_of::next<iterator_type>::type                > segmented_range;                typedef detail::segments_transform<Cdr> tfx;                typedef typename result_of::size<segmented_range>::type size;                typedef typename mpl::prior<size>::type size_minus_1;                typedef joint_view<

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?