group.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/comparing.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/group.hpp>
  8. #include <boost/hana/integral_constant.hpp>
  9. #include <boost/hana/length.hpp>
  10. #include <boost/hana/range.hpp>
  11. #include <boost/hana/tuple.hpp>
  12. #include <boost/hana/type.hpp>
  13. namespace hana = boost::hana;
  14. // group without a predicate
  15. BOOST_HANA_CONSTANT_CHECK(
  16. hana::group(hana::make_tuple(hana::int_c<1>, hana::long_c<1>, hana::type_c<int>, hana::char_c<'x'>, hana::char_c<'x'>))
  17. == hana::make_tuple(
  18. hana::make_tuple(hana::int_c<1>, hana::long_c<1>),
  19. hana::make_tuple(hana::type_c<int>),
  20. hana::make_tuple(hana::char_c<'x'>, hana::char_c<'x'>)
  21. )
  22. );
  23. // group with a predicate
  24. constexpr auto tuples = hana::make_tuple(
  25. hana::range_c<int, 0, 1>,
  26. hana::range_c<int, 0, 2>,
  27. hana::range_c<int, 1, 3>,
  28. hana::range_c<int, 2, 6>
  29. );
  30. BOOST_HANA_CONSTANT_CHECK(
  31. hana::group(tuples, hana::comparing(hana::length))
  32. == hana::make_tuple(
  33. hana::make_tuple(
  34. hana::range_c<int, 0, 1>
  35. ),
  36. hana::make_tuple(
  37. hana::range_c<int, 0, 2>,
  38. hana::range_c<int, 1, 3>
  39. ),
  40. hana::make_tuple(
  41. hana::range_c<int, 2, 6>
  42. )
  43. )
  44. );
  45. // group.by is syntactic sugar
  46. static_assert(
  47. hana::group.by(hana::comparing(hana::typeid_),
  48. hana::make_tuple(1, 2, 3, 'x', 'y', 4.4, 5.5))
  49. == hana::make_tuple(
  50. hana::make_tuple(1, 2, 3),
  51. hana::make_tuple('x', 'y'),
  52. hana::make_tuple(4.4, 5.5)
  53. )
  54. , "");
  55. int main() { }