inc_binary_seq.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright Cromwell D. Enage 2013.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PARAMETER_AUX_PREPROCESSOR_INC_BINARY_SEQ_HPP
  6. #define BOOST_PARAMETER_AUX_PREPROCESSOR_INC_BINARY_SEQ_HPP
  7. #include <boost/preprocessor/seq/push_back.hpp>
  8. // This macro keeps the rest of the sequence if carry == 0.
  9. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_0(seq, element) \
  10. (BOOST_PP_SEQ_PUSH_BACK(seq, element), 0)
  11. /**/
  12. #include <boost/preprocessor/control/iif.hpp>
  13. // This macro updates the rest of the sequence if carry == 1.
  14. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_1(seq, element) \
  15. (BOOST_PP_SEQ_PUSH_BACK(seq, BOOST_PP_IIF(element, 0, 1)), element)
  16. /**/
  17. #include <boost/preprocessor/tuple/elem.hpp>
  18. #include <boost/preprocessor/cat.hpp>
  19. // This macro maintains a tuple (seq, carry), where seq is the intermediate
  20. // result and carry is a flag that will unset upon finding an element == 0.
  21. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_OP(s, result_tuple, element) \
  22. BOOST_PP_CAT( \
  23. BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_ \
  24. , BOOST_PP_TUPLE_ELEM(2, 1, result_tuple) \
  25. )(BOOST_PP_TUPLE_ELEM(2, 0, result_tuple), element)
  26. /**/
  27. // This macro keeps the sequence at its original length if carry == 0.
  28. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL_0(seq) seq
  29. /**/
  30. // This macro appends a zero to seq if carry == 1.
  31. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL_1(seq) \
  32. BOOST_PP_SEQ_PUSH_BACK(seq, 0)
  33. /**/
  34. // This macro takes in the tuple (seq, carry), with carry indicating whether
  35. // or not seq originally contained all 1s. If so, then seq now contains all
  36. // 0s, and this macro pushes an extra 0 before expanding to the new sequence.
  37. // Otherwise, this macro expands to seq as is.
  38. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL(seq_and_carry) \
  39. BOOST_PP_CAT( \
  40. BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL_ \
  41. , BOOST_PP_TUPLE_ELEM(2, 1, seq_and_carry) \
  42. )(BOOST_PP_TUPLE_ELEM(2, 0, seq_and_carry))
  43. /**/
  44. #include <boost/preprocessor/seq/seq.hpp>
  45. #include <boost/preprocessor/seq/fold_left.hpp>
  46. // This macro treats the specified sequence of 1s and 0s like a binary number
  47. // in reverse and expands to a sequence representing the next value up.
  48. // However, if the input sequence contains all 1s, then the output sequence
  49. // will contain one more element but all 0s.
  50. //
  51. // Examples:
  52. // seq = (1)(0)(1)(0) --> return (0)(1)(1)(0)
  53. // seq = (1)(1)(1)(0) --> return (0)(0)(0)(1)
  54. // seq = (1)(1)(1)(1) --> return (0)(0)(0)(0)(0)
  55. #define BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ(seq) \
  56. BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_IMPL( \
  57. BOOST_PP_SEQ_FOLD_LEFT( \
  58. BOOST_PARAMETER_AUX_PP_INC_BINARY_SEQ_OP \
  59. , (BOOST_PP_SEQ_NIL, 1) \
  60. , seq \
  61. ) \
  62. )
  63. /**/
  64. #endif // include guard