[boost] BOOST_PP_RANGE?
I am going to use our wonderful Preprocessor library to generate a metafunction that basically looks like this: template int C0, int C1, ..., int Cn struct max_arity { static int const value = Cn 0 ? Cn : ( Cn-1 0 ? Cn-1 : ... ( C1 0 ? C1 : ( C0 0 ? C0 : -1 ) ) ) ; }; So here's what I came up with: template BOOST_PP_ENUM_PARAMS(BOOST_MPL_METAFUNCTION_MAX_ARITY, int C) struct max_arity { BOOST_STATIC_CONSTANT(int, value = BOOST_PP_LIST_FOLD_LEFT( AUX_MAX_ARITY_OP , -1 , (0, (1, (2, (3, (4, BOOST_PP_NIL) ) ); }; I love everything about it except for the (0, (1, (2, (3, (4, BOOST_PP_NIL) part. I would like the above to become something along these lines: value = BOOST_PP_RANGE_FOLD_LEFT( AUX_MAX_ARITY_OP , -1 , BOOST_PP_RANGE(0, 4) ) or, better yet, value = BOOST_PP_FOLD_LEFT( AUX_MAX_ARITY_OP , -1 , BOOST_PP_RANGE(0, 4) ) where 'BOOST_PP_FOLD_LEFT' is a generic algorithm that can be used on all PP sequences. How hard would it be to have something like this? I suppose it's possible to generate the code I need using BOOST_PP_WHILE, but IMO that solution wouldn't be as conceptually nice and intuitive as the above. Aleksey ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] BOOST_PP_RANGE?
- Original Message - From: Aleksey Gurtovoy [EMAIL PROTECTED] Hi Aleksey, I am going to use our wonderful Preprocessor library to generate a metafunction that basically looks like this: [...] I love everything about it except for the (0, (1, (2, (3, (4, BOOST_PP_NIL) part. I would like the above to become something along these lines: value = BOOST_PP_RANGE_FOLD_LEFT( AUX_MAX_ARITY_OP , -1 , BOOST_PP_RANGE(0, 4) ) This one is certainly possible. However, BOOST_PP_RANGE_FOLD_LEFT would just be a synonym for whatever folding macro operates on the type generated by BOOST_PP_RANGE. It would be to difficult to make a parametizable macro that yields a sequence of numbers: BOOST_PP_SEQ_FOLD_LEFT( AUX_MAX_ARITY_OP , -1 , BOOST_PP_SEQ_RANGE(0, 4) ) Which is close to what you want. In fact, it is almost already there: #include boost/preprocessor/seq/fold_left.hpp #include boost/preprocessor/seq/subseq.hpp #define NUMBERS \ (0)(1)(2)(3)(4)(5)(6)(7)(8)(9) \ (10)(11)(12)(13)(14)(15)(16)(17)(18)(19) \ /* ... */ #define RANGE(first, length) \ BOOST_PP_SEQ_SUBSEQ( NUMBERS, first, length ) \ /**/ BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5)) The only major difference is that BOOST_PP_SEQ_SUBSEQ takes first and length operands, rather than first and last. Which, for the zero-case is almost the same: first - BOOST_PP_INC(last). It wouldn't be too difficult to bang out a variant that goes from first - last though. or, better yet, value = BOOST_PP_FOLD_LEFT( AUX_MAX_ARITY_OP , -1 , BOOST_PP_RANGE(0, 4) ) where 'BOOST_PP_FOLD_LEFT' is a generic algorithm that can be used on all PP sequences. How hard would it be to have something like this? With C99's variadic macros, fairly easy with lists vs. sequences. Without them, impossible. The reason is simple, without variadics I have no way of telling the difference between (a) and (a, b), etc With variadics I can count the arguments. I suppose it's possible to generate the code I need using BOOST_PP_WHILE, but IMO that solution wouldn't be as conceptually nice and intuitive as the above. Paul Mensonides ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
RE: [boost] BOOST_PP_RANGE?
Paul Mensonides wrote: #include boost/preprocessor/seq/fold_left.hpp #include boost/preprocessor/seq/subseq.hpp #define NUMBERS \ (0)(1)(2)(3)(4)(5)(6)(7)(8)(9) \ (10)(11)(12)(13)(14)(15)(16)(17)(18)(19) \ /* ... */ #define RANGE(first, length) \ BOOST_PP_SEQ_SUBSEQ( NUMBERS, first, length ) \ /**/ BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5)) Hmm, it doesn't work on Metrowerks 8.3: the compiler chokes on preprocessing with the following diagnostics: Error : ',' expected fold_left.cpp line 19 BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5)) It seems like it doesn't like BOOST_PP_SEQ_SUBSEQ - the example from the docs fails as well: #include boost/preprocessor/seq/subseq.hpp #define SEQ (0)(1)(2)(3)(4)(5) BOOST_PP_SEQ_SUBSEQ(SEQ, 2, 3) // expands to (2)(3)(4) Error : ',' expected subseq.cpp line 5 BOOST_PP_SEQ_SUBSEQ(SEQ, 2, 3) // expands to (2)(3)(4) Is it a known failure? It would be nice to have some tests for the SEQ stuff :). Aleksey ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] BOOST_PP_RANGE?
- Original Message - From: Aleksey Gurtovoy [EMAIL PROTECTED] #define NUMBERS \ (0)(1)(2)(3)(4)(5)(6)(7)(8)(9) \ (10)(11)(12)(13)(14)(15)(16)(17)(18)(19) \ /* ... */ #define RANGE(first, length) \ BOOST_PP_SEQ_SUBSEQ( NUMBERS, first, length ) \ /**/ BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5)) Looks good! How efficient is it? For instance, if NUMBERS is a sequence from 0 to 255? It should be very efficient. The length of the input doesn't matter, the length of the output range matters slightly. Actually, if you only need a range starting from zero, you can use BOOST_PP_SEQ_FIRST_N instead of BOOST_PP_SEQ_SUBSEQ which will be more efficient yet. Paul Mensonides ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] BOOST_PP_RANGE?
- Original Message - From: Aleksey Gurtovoy [EMAIL PROTECTED] BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5)) Hmm, it doesn't work on Metrowerks 8.3: the compiler chokes on preprocessing [...] Is it a known failure? It would be nice to have some tests for the SEQ stuff :). For the time being, try BOOST_PP_SEQ_FIRST_N. If that doesn't work for now, I'll get on it immediately. I didn't know about it (which is exactly why I should have some regression tests for this). I thought I tested everything, but I must have missed this. Suffice to say, it *can* work. Paul Mensonides ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
RE: [boost] BOOST_PP_RANGE?
Paul Mensonides wrote: - Original Message - From: Aleksey Gurtovoy [EMAIL PROTECTED] #define NUMBERS \ (0)(1)(2)(3)(4)(5)(6)(7)(8)(9) \ (10)(11)(12)(13)(14)(15)(16)(17)(18)(19) \ /* ... */ #define RANGE(first, length) \ BOOST_PP_SEQ_SUBSEQ( NUMBERS, first, length ) \ /**/ BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5)) Looks good! How efficient is it? For instance, if NUMBERS is a sequence from 0 to 255? It should be very efficient. The length of the input doesn't matter, the length of the output range matters slightly. Oh, good! Actually, if you only need a range starting from zero, you can use BOOST_PP_SEQ_FIRST_N instead of BOOST_PP_SEQ_SUBSEQ which will be more efficient yet. OK, will have it in mind. Thanks, Aleksey ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost