Boost logo

Boost Users :

Subject: Re: [Boost-users] Preprocessor Sum Shift
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-10-28 01:48:54


AMDG

Mitch Besser wrote:
> I would like to take the following Sequence of Tuples:
>
> ((A, 1)) ((B, 1)) ((C, 1)) ((D, 2)) ((E, 4))
>
> and create a summation as follows:
>
> ((A, 0)) ((B, 1)) ((C, 2)) ((D, 3)) ((E, 5))
>

#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/seq/push_back.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>

#define SHIFTED_SUM_OP(s, state, seq) \
    (BOOST_PP_SEQ_PUSH_BACK( \
        BOOST_PP_TUPLE_ELEM(2, 0, state), \
        (BOOST_PP_TUPLE_ELEM(2, 0, seq), \
         BOOST_PP_TUPLE_ELEM(2, 1, state))),\
     BOOST_PP_ADD( \
        BOOST_PP_TUPLE_ELEM(2, 1, state), \
        BOOST_PP_TUPLE_ELEM(2, 1, seq)))

#define SHIFTED_SUM(seq) \
    BOOST_PP_TUPLE_ELEM(2, 0, \
        BOOST_PP_SEQ_FOLD_LEFT( \
            SHIFTED_SUM_OP, \
            (BOOST_PP_SEQ_NIL, 0), \
            seq))

SHIFTED_SUM(((A, 1)) ((B, 1)) ((C, 1)) ((D, 2)) ((E, 4)))

In Christ,
Steven Watanabe


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