|
Boost Users : |
Subject: Re: [Boost-users] mpl programming task
From: peter_foelsche_at_[hidden]
Date: 2008-10-10 16:44:25
> #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;
> }
Thanks Adam.
I found my own solution meanwhile.
I tried both, yours and mine.
Very slowly I'm getting used to this programming style (grin).
The compiler error messages are not very helpful.
Kind Regards
Peter
#include <boost/mpl/map.hpp>
#include <boost/mpl/set.hpp>
#include <boost/mpl/vector.hpp>
#include <iostream>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/accumulate.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/for_each.hpp>
typedef boost::mpl::set<
boost::mpl::int_<1>,
boost::mpl::int_<3>
> CFirstSet;
template<typename CFirstSet>
struct CMyConvert:boost::mpl::accumulate<
CFirstSet,
boost::mpl::map<>,
boost::mpl::lambda<
boost::mpl::insert<
boost::mpl::_1,
boost::mpl::pair<
boost::mpl::_2,
boost::mpl::size<boost::mpl::_1>
>
>
>::type
>::type
{
};
template<typename InputSeq>
struct CConvert : boost::mpl::transform<
InputSeq,
boost::mpl::range_c<
long,
0,
boost::mpl::size<InputSeq>::value
>,
boost::mpl::pair<boost::mpl::_1, boost::mpl::_2>,
boost::mpl::inserter<
boost::mpl::map<>,
boost::mpl::insert<boost::mpl::_1, boost::mpl::_2>
>
>::type
{
};
typedef boost::mpl::map<
boost::mpl::pair<
boost::mpl::int_<3>,
boost::mpl::integral_c<long, 0>
>,
boost::mpl::pair<
boost::mpl::int_<1>,
boost::mpl::integral_c<long, 1>
>
> CMap1;
typedef boost::mpl::map<
boost::mpl::pair<
boost::mpl::int_<3>,
boost::mpl::long_<0>
>,
boost::mpl::pair<
boost::mpl::int_<1>,
boost::mpl::long_<1>
>
> CMap2;
template<typename T>
struct wrap
{
};
struct CPrintType
{ template<typename T>
void operator()(wrap<T>) const
{ std::cout << typeid(T).name() << "\n";
}
};
int main(int , char **)
{
BOOST_MPL_ASSERT((
boost::mpl::equal_to<
boost::mpl::at<
CConvert<CFirstSet>,
boost::mpl::int_<1>
>::type,
boost::mpl::integral_c<long, 1>
>
));
BOOST_MPL_ASSERT((
boost::mpl::equal_to<
boost::mpl::at<
CConvert<CFirstSet>,
boost::mpl::int_<3>
>::type,
boost::mpl::integral_c<long, 0>
>
));
BOOST_MPL_ASSERT((
boost::mpl::equal<
CConvert<CFirstSet>,
CMap1
>
));
BOOST_MPL_ASSERT((
boost::mpl::equal_to<
boost::mpl::at<
CMyConvert<CFirstSet>,
boost::mpl::int_<1>
>::type,
boost::mpl::long_<1>
>
));
BOOST_MPL_ASSERT((
boost::mpl::equal_to<
boost::mpl::at<
CMyConvert<CFirstSet>,
boost::mpl::int_<3>
>::type,
boost::mpl::long_<0>
>
));
BOOST_MPL_ASSERT((
boost::mpl::equal<
CMyConvert<CFirstSet>,
CMap2
>
));
boost::mpl::for_each<
CMyConvert<CFirstSet>,
wrap<boost::mpl::_1>
>(CPrintType());
return 0;
}
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