|
Boost : |
From: Joel de Guzman (joel_at_[hidden])
Date: 2008-07-20 09:49:52
David Abrahams wrote:
[snip]
>
> This instantiates the template on the cdr with N-1 iterations
> i.e., it's like advance(++x, n-1)
> You need ++advance(x, n-1) to avoid O(N^2)
>
>> >
>> element;
Nifty! I implemented this in fusion. Pretty straightforward (yeah,
I'm using partial specialization):
template <typename Cons>
struct cons_deref
{
typedef typename Cons::car_type type;
};
template <typename Cons, int I>
struct cons_advance
{
typedef typename
cons_advance<Cons, I-1>::type::cdr_type
type;
};
template <typename Cons>
struct cons_advance<Cons, 0>
{
typedef Cons type;
};
Then:
template <>
struct at_impl<cons_tag>
{
template <typename Sequence, typename N>
struct apply
{
typedef detail::cons_deref<
typename cons_advance<Sequence, N::value>::type>
element;
...
};
As an added bonus, I unrolled advance for up to N==5.
Thanks for the tips, Dave, Steven!
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