sort.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/equal.hpp>
  6. #include <boost/hana/front.hpp>
  7. #include <boost/hana/greater.hpp>
  8. #include <boost/hana/integral_constant.hpp>
  9. #include <boost/hana/negate.hpp>
  10. #include <boost/hana/ordering.hpp>
  11. #include <boost/hana/sort.hpp>
  12. #include <boost/hana/tuple.hpp>
  13. #include <string>
  14. namespace hana = boost::hana;
  15. using namespace hana::literals;
  16. using namespace std::literals;
  17. // sort without a predicate
  18. BOOST_HANA_CONSTANT_CHECK(
  19. hana::sort(hana::make_tuple(1_c, -2_c, 3_c, 0_c)) ==
  20. hana::make_tuple(-2_c, 0_c, 1_c, 3_c)
  21. );
  22. // sort with a predicate
  23. BOOST_HANA_CONSTANT_CHECK(
  24. hana::sort(hana::make_tuple(1_c, -2_c, 3_c, 0_c), hana::greater) ==
  25. hana::make_tuple(3_c, 1_c, 0_c, -2_c)
  26. );
  27. int main() {
  28. // sort.by is syntactic sugar
  29. auto tuples = hana::make_tuple(
  30. hana::make_tuple(2_c, 'x', nullptr),
  31. hana::make_tuple(1_c, "foobar"s, hana::int_c<4>)
  32. );
  33. BOOST_HANA_RUNTIME_CHECK(
  34. hana::sort.by(hana::ordering(hana::front), tuples)
  35. == hana::make_tuple(
  36. hana::make_tuple(1_c, "foobar"s, hana::int_c<4>),
  37. hana::make_tuple(2_c, 'x', nullptr)
  38. )
  39. );
  40. }