Boost logo

Boost :

Subject: Re: [boost] [TypeSort] Automatic type sorting
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2015-03-20 13:16:59


On 20/03/2015 17:49, costis glynos wrote:
>>
>> You also need to use Boost.Fusion to make it into a tuple at the end,
>> since this is what you want.
>>
>> Here is an example:
>>
>> #include <boost/mpl/sort.hpp>
>> #include <boost/mpl/vector.hpp>
>> #include <boost/mpl/sizeof.hpp>
>> #include <boost/fusion/include/as_vector.hpp>
>> #include <boost/fusion/adapted/mpl.hpp>
>> #include <iostream>
>>
>> int main()
>> {
>> namespace mpl = boost::mpl;
>> namespace fusion = boost::fusion;
>>
>> typedef mpl::vector<short, int, short> types;
>> typedef mpl::sort<types, mpl::less< mpl::sizeof_<mpl::_1>
>> , mpl::sizeof_<mpl::_2>
>> >
>> >::type sorted_types;
>> typedef fusion::result_of::as_vector<sorted_types>::type tuple;
>>
>> tuple t;
>> std::cout << sizeof(t) << std::endl; // 8 instead of 12
>> }
>>
>> This appears to work.

> That's fantastic! Thank you for your recommendation.

You probably need to be able to access the final tuple with the original
indices though, which is a bit more involved.

#include <boost/mpl/sort.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/zip_view.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/sizeof.hpp>
#include <boost/mpl/at.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <iostream>

int main()
{
   namespace mpl = boost::mpl;
   namespace fusion = boost::fusion;

   typedef mpl::vector<short, int, short> types;
   typedef mpl::copy<
     mpl::zip_view<
       mpl::vector< types
                  , mpl::range_c<long, 0L, mpl::size<types>::value>
>
>
   , mpl::back_inserter< mpl::vector0<> >
>::type indexes_types;
   typedef mpl::sort<
     indexes_types
   , mpl::less< mpl::sizeof_< mpl::front<mpl::_1> >
              , mpl::sizeof_< mpl::front<mpl::_2> >
>
>::type indexes_types_sorted;
   typedef mpl::transform<
     indexes_types_sorted
   , mpl::front<mpl::_1>
>::type sorted_types;
   typedef mpl::transform<
     indexes_types_sorted
   , mpl::back<mpl::_1>
>::type sorted_indexes;
   typedef fusion::result_of::as_vector<sorted_types>::type tuple;

   tuple t;
   std::cout << sizeof( fusion::at_c<mpl::at_c<sorted_indexes,
0>::type::value>(t) ) << std::endl;
   std::cout << sizeof( fusion::at_c<mpl::at_c<sorted_indexes,
1>::type::value>(t) ) << std::endl;
   std::cout << sizeof( fusion::at_c<mpl::at_c<sorted_indexes,
2>::type::value>(t) ) << std::endl;
}

This could make an interesting addition to Boost.Fusion.


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