2
0
mirror of https://github.com/boostorg/hana.git synced 2026-02-01 08:32:11 +00:00
Files
hana/example/misc/indexed_sort.cpp
Louis Dionne 0660af6a8f [GCC] Initial port to GCC
- Fix ambiguous specialization in core/datatype
- [Config] Turn #errors into #warnings
- Qualify recursive calls to `apply` to make sure the right function is selected
- Remove usages of when_valid to workaround ambiguous specializations
- Use multiline comments to remove some GCC warnings with LaTeX's double backslash
- Specialize operators::of in the boost::hana::operators namespace
- Manually perform more conversions in constexpr-context
- Workaround the lack of support for constexpr-but-non-const member functions
- Disambiguate unqualified uses of size_t with hana::size_t and ::size_t
2015-05-20 16:56:34 -04:00

49 lines
1.6 KiB
C++
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
#include <type_traits>
using namespace boost::hana;
using namespace boost::hana::literals;
auto indexed_sort = [](auto list, auto predicate) {
auto indexed_list = zip(list, to<Tuple>(range(0_c, size(list))));
auto sorted = sort.by(predicate ^on^ head, indexed_list);
return make_pair(transform(sorted, head), transform(sorted, last));
};
int main() {
auto types = tuple_t<char[4], char[2], char[1], char[5], char[3]>;
auto sorted = indexed_sort(types, [](auto t, auto u) {
return sizeof_(t) < sizeof_(u);
});
using Tup = decltype(unpack(first(sorted), template_<_tuple>))::type;
auto indices = second(indexed_sort(second(sorted), less));
// When accessed through the indices sequence, the tuple appears to be
// ordered as the `types` above. However, as can be seen in the
// static_assert below, the tuple is actually ordered differently.
Tup tup;
char const(&a)[4] = tup[indices[0_c]];
char const(&b)[2] = tup[indices[1_c]];
char const(&c)[1] = tup[indices[2_c]];
char const(&d)[5] = tup[indices[3_c]];
char const(&e)[3] = tup[indices[4_c]];
static_assert(std::is_same<
Tup,
_tuple<char[1], char[2], char[3], char[4], char[5]>
>{}, "");
(void)a; (void)b; (void)c; (void)d; (void)e;
}