Boost logo

Boost Users :

Subject: Re: [Boost-users] mpl programming task
From: Adam Merz (adammerz_at_[hidden])
Date: 2008-10-09 19:58:08


peter_foelsche writes:
> or even more clear:
>
> #include <map>
> #include <set>
>
> typedef std::map<size_t, size_t> CInt2IntMap;
> typedef std::set<size_t> CIntSet;
>
> static CInt2IntMap convert(const CIntSet &_r)
> { CInt2IntMap s;
> for (CIntSet::const_iterator p = _r.begin(); p != _r.end(); ++p)
> s.insert(std::make_pair(*p, s.size()));
> return s;
> }

//required headers
#include <cstddef>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/inserter.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/range_c.hpp>

//test headers
#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/size_t.hpp>
#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/vector.hpp>

namespace mpl = boost::mpl;

template<typename InputSeq>
struct convert : mpl::transform<
    InputSeq,
    mpl::range_c<std::size_t, 0, mpl::size<InputSeq>::value>,
    mpl::pair<mpl::_1, mpl::_2>,
    mpl::inserter<
        mpl::map<>,
        mpl::insert<mpl::_1, mpl::_2>
>
>
{ };

typedef mpl::vector<
    mpl::size_t<4>,
    mpl::size_t<8>,
    mpl::size_t<42>
> test_input_sequence;

typedef mpl::map<
    mpl::pair<mpl::size_t<4>, mpl::integral_c<std::size_t, 0> >,
    mpl::pair<mpl::size_t<8>, mpl::integral_c<std::size_t, 1> >,
    mpl::pair<mpl::size_t<42>, mpl::integral_c<std::size_t, 2> >
> test_expected_result;

BOOST_MPL_ASSERT((
    mpl::equal<convert<test_input_sequence>::type, test_expected_result>
));


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net