Re: [Boost-users] mpl programming task

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; }

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> ));

#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; }
participants (2)
-
Adam Merz
-
peter_foelsche@agilent.com