Boost logo

Boost Users :

Subject: Re: [Boost-users] [preprocessor] How to transform sequence into sequence of tuples.
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2013-04-11 14:11:04


On Thu, 11 Apr 2013 06:41:18 -0700, paul Fultz <pfultz2_at_[hidden]> wrote:
>> How would I go about doing the following transformation:
>>
>> (a)(b)(c)(d) -> (a, b)(c, d)
>>
> Although, you probably could use BOOST_PP_SEQ_FOR_EACH, the easiest way
> is to
> use sequence iteration instead:
>
> #define ADJACENT_SEQ_TO_TUPLE(seq)
> BOOST_PP_CAT(ADJACENT_SEQ_TO_TUPLE_1 seq, _END)
> #define ADJACENT_SEQ_TO_TUPLE_0(x) x ADJACENT_SEQ_TO_TUPLE_2
> #define ADJACENT_SEQ_TO_TUPLE_1(x) , x ADJACENT_SEQ_TO_TUPLE_2
> #define ADJACENT_SEQ_TO_TUPLE_2(x) x ADJACENT_SEQ_TO_TUPLE_1
> #define ADJACENT_SEQ_TO_TUPLE_0_END
> #define ADJACENT_SEQ_TO_TUPLE_1_END
> #define ADJACENT_SEQ_TO_TUPLE_2_END

Ah, thanks. I was going down the BOOST_PP_SEQ_FOR_EACH path. My algorithm
was going to be

#define SEQ (a)(b)(c)(d)
#define WRAP(r, data, elem) \
     LPAREN_IF(MOD(elem-index,2)) elem COMMA_IF(MOD(elem-index,2))
RPAREN_IF(NOT(MOD(elem-index,2)))
SEQ_FOR_EACH(WRAP, ~, SEQ)

But I couldn't figure out how to get the element index. BTW, what is the
"next available BOOST_PP_FOR repetition" in the ternary macro for
SEQ_FOR_EACH? For the example, why the is the output of:

#define SEQ (w)(x)(y)(z)(a)(b)(c)
#define MACRO(r, data, elem) r elem
BOOST_PP_SEQ_FOR_EACH(MACRO, 1, SEQ)

this:

2 w 3 x 4 y 5 z 6 a 7 b 8 c

It seems r = elem-index + 2. Is this true?

Thanks,

Mostafa


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net