Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2008-07-19 19:35:42


on Sat Jul 19 2008, Steven Watanabe <watanabesj-AT-gmail.com> wrote:

> AMDG
>
> David Abrahams wrote:
>> How?
>
> In pseudocode using iterators:
>
> 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));
> }
> }

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

  // -- proof of concept --
  // doesn't even need partial specialization (in case we care)

  template <unsigned N>
  struct tuple_drop_front
  {
      template <class Tuple> // compute resultant tuple
      struct result_
      {
          typedef typename
             tuple_drop_front<N-1>::template
             result_<Tuple>::type::tail_type
          type;
      };

      template <class Tuple>
      typename result_<Tuple>::type const&
      operator()(Tuple const& x) const
      {
          return tuple_drop_front<N-1>()(x).tail;
      }

      template <class Tuple>
      typename result_<Tuple>::type&
      operator()(Tuple& x) const
      {
          return tuple_drop_front<N-1>()(x).tail;
      }
  };

  template <>
  struct tuple_drop_front<0>
  {
      template <class Tuple>
      struct result_
      {
          typedef Tuple type;
      };

      template <class Tuple>
      Tuple const&
      operator()(Tuple const& x) const
      {
          return x;
      }

      template <class Tuple>
      Tuple&
      operator()(Tuple& x) const
      {
          return x;
      }
  };

  template <unsigned N, class Tuple>
  typename tuple_drop_front<N>::template result_<Tuple>::type::head_type&
  get(Tuple& t)
  {
      return tuple_drop_front<N>()(t).head;
  }

  template <unsigned N, class Tuple>
  typename tuple_drop_front<N>::template result_<Tuple>::type::head_type const&
  get(Tuple const& t)
  {
      return tuple_drop_front<N>()(t).head;
  }

  struct null_type {};

  template <class HT, class TT = null_type>
  struct cons
  {
      cons() : head(), tail() {}

      template <class T, class U>
      cons(T const& x, U const& y) : head(x), tail(y) {}

      template <class T, class U>
      cons(T& x, U& y) : head(x), tail(y) {}

      template <class T, class U>
      cons(T const& x, U& y) : head(x), tail(y) {}

      template <class T, class U>
      cons(T& x, U const& y) : head(x), tail(y) {}

      template <class T>
      cons(T const& x) : head(x), tail() {}

      typedef HT head_type;
      HT head;

      typedef TT tail_type;
      TT tail;
  };

  int main()
  {

      assert( get<0>( cons<int>(42) ) == 42 );

      assert( get<0>( cons<int, cons<long> >(42, 10) ) == 42 );
      assert( get<1>( cons<int, cons<long> >(42, 10) ) == 10 );

      assert( get<0>( cons<int, cons<long, cons<char> > >(42, 3) ) == 42 );
      assert( get<1>( cons<int, cons<long, cons<char> > >(42, 3) ) == 3 );
      assert( get<2>( cons<int, cons<long, cons<char> > >(42, 3) ) == 0 );
  }

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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