• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include <boost/hana/at.hpp>
6 #include <boost/hana/back.hpp>
7 #include <boost/hana/core/to.hpp>
8 #include <boost/hana/front.hpp>
9 #include <boost/hana/functional/on.hpp>
10 #include <boost/hana/integral_constant.hpp>
11 #include <boost/hana/length.hpp>
12 #include <boost/hana/less.hpp>
13 #include <boost/hana/pair.hpp>
14 #include <boost/hana/range.hpp>
15 #include <boost/hana/sort.hpp>
16 #include <boost/hana/transform.hpp>
17 #include <boost/hana/tuple.hpp>
18 #include <boost/hana/type.hpp>
19 #include <boost/hana/zip.hpp>
20 
21 #include <type_traits>
22 namespace hana = boost::hana;
23 using namespace hana::literals;
24 
25 
__anon87137ba80102(auto list, auto predicate) 26 auto indexed_sort = [](auto list, auto predicate) {
27     auto indexed_list = hana::zip(
28         list,
29         hana::to_tuple(hana::make_range(0_c, hana::length(list)))
30     );
31     auto sorted = hana::sort.by(predicate ^hana::on^ hana::front, indexed_list);
32     return hana::make_pair(hana::transform(sorted, hana::front),
33                            hana::transform(sorted, hana::back));
34 };
35 
main()36 int main() {
37     auto types = hana::tuple_t<char[4], char[2], char[1], char[5], char[3]>;
38     auto sorted = indexed_sort(types, [](auto t, auto u) {
39         return hana::sizeof_(t) < hana::sizeof_(u);
40     });
41     using Tup = decltype(
42         hana::unpack(hana::first(sorted), hana::template_<hana::tuple>)
43     )::type;
44     auto indices = hana::second(indexed_sort(hana::second(sorted), hana::less));
45 
46     // When accessed through the indices sequence, the tuple appears to be
47     // ordered as the `types` above. However, as can be seen in the
48     // static_assert below, the tuple is actually ordered differently.
49     Tup tup;
50     char const(&a)[4] = tup[indices[0_c]];
51     char const(&b)[2] = tup[indices[1_c]];
52     char const(&c)[1] = tup[indices[2_c]];
53     char const(&d)[5] = tup[indices[3_c]];
54     char const(&e)[3] = tup[indices[4_c]];
55 
56     static_assert(std::is_same<
57         Tup,
58         hana::tuple<char[1], char[2], char[3], char[4], char[5]>
59     >{}, "");
60 
61     (void)a; (void)b; (void)c; (void)d; (void)e;
62 }
63