Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-07-01 07:57:53


----- Original Message -----
From: "Vesa Karvonen" <vesa.karvonen_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, June 30, 2001 22:46
Subject: [boost] PREPROCESSOR library review

> Hi,
>
> A few hours ago, I invented a technique for implementing ADD(X,Y) and
SUB(X,Y)
> using linear amount of tokens.
[snip]
> It is highly likely that other interesting and useful operations could be
> implemented in the future.

I have now implemented also MUL(X,Y), MAX(X,Y) and MIN(X,Y).

A pint of beer is promised to the first person to implement DIV(X,Y) and
MOD(X,Y) using at most a linear amount of tokens (and the result must, of
course, be a single token).

MUL was implemented by changing the previously introduced REPEAT_CALL to
RECURSE that makes it possible to have an additional parameter (the same
technique was suggested by Aleksey Gurtovoy for REPEAT earlier) and the
introduction of 2ND_RECURSE making it possible to make recursive functions
that have quadratic complexity. I will also consider passing the recursion
level/depth to the macro.

  #define BOOST_PREPROCESSOR_RECURSE(N,M,C,P)\
    BOOST_PREPROCESSOR_RECURSE_DELAY(N,M,C,P)
  #define BOOST_PREPROCESSOR_RECURSE_DELAY(N,M,C,P)\
    BOOST_PREPROCESSOR_RECURSE##N(M,C,P)
  #define BOOST_PREPROCESSOR_RECURSE0(M,C,P) P
  #define BOOST_PREPROCESSOR_RECURSE1(M,C,P) M(C,P)
  #define BOOST_PREPROCESSOR_RECURSE2(M,C,P) M(C,M(C,P))
  #define BOOST_PREPROCESSOR_RECURSE3(M,C,P)\
    BOOST_PREPROCESSOR_RECURSE2(M,C,M(C,P))
  #define BOOST_PREPROCESSOR_RECURSE4(M,C,P)\
    BOOST_PREPROCESSOR_RECURSE3(M,C,M(C,P))
  #define BOOST_PREPROCESSOR_RECURSE5(M,C,P)\
    BOOST_PREPROCESSOR_RECURSE4(M,C,M(C,P))
  // ...

  #define BOOST_PREPROCESSOR_MUL(X,Y)\
    BOOST_PREPROCESSOR_2ND_RECURSE(X,BOOST_PREPROCESSOR_ADD,Y,0)

MAX and MIN use SUB. In addition, I changed DEC and INC to use saturation
arithmetic. In other words, and most importantly for MAX and MIN, DEC(0)
results in 0.

#define BOOST_PREPROCESSOR_MAX(X,Y)\
  BOOST_PREPROCESSOR_IF\
  ( BOOST_PREPROCESSOR_SUB(X,Y)\
  , X\
  , Y\
  )


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