[Home]CPPTM Answers - Exercise 7-6

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

 // (by Ariel Badichi)
 #include <boost/static_assert.hpp>
 #include <boost/mpl/vector_c.hpp>
 #include <boost/mpl/equal_to.hpp>
 #include <boost/mpl/if.hpp>
 #include <boost/mpl/iterator_tags.hpp>
 #include <boost/mpl/iterator_category.hpp>
 #include <boost/mpl/iterator_range.hpp>
 #include <boost/mpl/advance.hpp>
 #include <boost/mpl/pop_front.hpp>
 #include <boost/mpl/front.hpp>
 #include <boost/mpl/next.hpp>
 #include <boost/mpl/prior.hpp>
 #include <boost/mpl/deref.hpp>
 #include <boost/mpl/begin.hpp>
 #include <boost/mpl/bool.hpp>
 #include <boost/mpl/and.hpp>
 #include <boost/mpl/size_t.hpp>
 #include <boost/mpl/at.hpp>
 #include <boost/mpl/list_c.hpp>
 #include <boost/mpl/equal.hpp>

 namespace mpl = boost::mpl;

 template<typename Iter, typename OrderIter?>
 struct permutation_iterator
 {
     typedef typename mpl::iterator_category<OrderIter?>::type category;

     typedef typename mpl::advance<
         Iter, 
         typename mpl::deref<OrderIter?>::type
     >::type base;

     typedef typename mpl::deref<base>::type type;
 };

 namespace boost
 {
     namespace mpl
     {
         template<typename Iter, typename OrderIter?>
         struct next<permutation_iterator<Iter, OrderIter?> >
         {
             typedef permutation_iterator<
                 Iter, 
                 typename mpl::next<OrderIter?>::type
             > type;
         };

         template<typename Iter, typename OrderIter?>
         struct prior<permutation_iterator<Iter, OrderIter?> >
         {
             typedef permutation_iterator<
                 Iter,
                 typename mpl::prior<OrderIter?>::type
             > type;
         };
     }
 }

 template<typename Indices, typename Elements>
 struct permutation_view
     : mpl::iterator_range<
           permutation_iterator<
               typename mpl::begin<Elements>::type,
               typename mpl::begin<Indices>::type
           >,
           permutation_iterator<
               typename mpl::begin<Elements>::type,
               typename mpl::end<Indices>::type
           >
       >
 {};

 int main()
 {
     typedef mpl::list_c<int, 2, 1, 3, 0, 2> indices;
     typedef mpl::vector_c<int, 11, 22, 33, 44> elements;
     typedef mpl::vector_c<int, 33, 22, 44, 11, 33> expected;
     BOOST_STATIC_ASSERT((mpl::equal<
                              permutation_view<indices, elements>, 
                              expected
                          >::value));
 }

BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited March 27, 2005 12:20 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers