Boost logo

Boost :

Subject: Re: [boost] [TypeSort] Automatic type sorting
From: Louis Dionne (ldionne.2_at_[hidden])
Date: 2015-03-21 22:12:22


Eric Niebler <eniebler <at> boost.org> writes:

>
> On 3/20/2015 11:44 AM, Louis Dionne wrote:
> > As a simple curiosity, here's how this could be implemented with Hana:
> <snip>
>
> And using Meta:
>
> [...]

Eric, abstracting the sort into an `indexed_sort` (meta)function
is a great idea. Here's an updated version:

------------------------------------------------------------------------------
#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>
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, 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[0_c]) < sizeof_(u[0_c]);
    });
    using Tup = decltype(unpack(first(sorted), template_<_tuple>))::type;
    auto indices = second(sorted);

    Tup tup;
    char(&a)[3] = tup[indices[0_c]];
    char(&b)[2] = tup[indices[1_c]];
    char(&c)[5] = tup[indices[2_c]];
}
------------------------------------------------------------------------------

> FWIW, these indices don't seem all that useful to me. What exactly was
> the desired behavior?

No idea :-)

Louis


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk