Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2008-07-19 20:52:43


David Abrahams wrote:

> Wow, looking at the tuples code it seems there's a lot of optimization
> we can do now!

So...

deref(advance(begin(tuple)), n)

where advance is implemented as

iterator advance(iterator x, int n)
{
    if (n == 0)
        return x;
    else
        return ++advance(x, n - 1); // corrected
}

Isn't this what we are already doing in fusion?

     template <>
     struct at_impl<cons_tag>
     {
         template <typename Sequence, typename N>
         struct apply
         {
             typedef typename
                 mpl::eval_if<
                     is_const<Sequence>
                   , add_const<typename Sequence::cdr_type>
                   , mpl::identity<typename Sequence::cdr_type>
>::type
             cdr_type;

             typedef typename
                 mpl::eval_if<
                     mpl::bool_<N::value == 0>
                   , mpl::identity<typename Sequence::car_type>
                   , apply<cdr_type, mpl::int_<N::value-1> >
>
             element;

             typedef typename
                 mpl::eval_if<
                     is_const<Sequence>
                   , detail::cref_result<element>
                   , detail::ref_result<element>
>::type
             type;

             template <typename Cons, int N2>
             static type
             call(Cons& s, mpl::int_<N2>)
             {
                 return call(s.cdr, mpl::int_<N2-1>());
             }

             template <typename Cons>
             static type
             call(Cons& s, mpl::int_<0>)
             {
                 return s.car;
             }

             static type
             call(Sequence& s)
             {
                 return call(s, mpl::int_<N::value>());
             }
         };
     };

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk