[Home]CPPTM Answers - Exercise 7-4

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/advance.hpp>
 #include <boost/mpl/pop_front.hpp>
 #include <boost/mpl/front.hpp>
 #include <boost/mpl/next.hpp>
 #include <boost/mpl/deref.hpp>
 #include <boost/mpl/begin.hpp>
 #include <boost/mpl/bool.hpp>
 #include <boost/mpl/and.hpp>

 namespace mpl = boost::mpl;

 template<typename Iter, typename Order>
 struct permutation_iterator
 {
     typedef mpl::forward_iterator_tag category;

     typedef typename mpl::advance<
         Iter, 
         typename mpl::front<Order>::type
     >::type base;

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

     typedef permutation_iterator<
         Iter, 
         typename mpl::pop_front<Order>::type
     > next;
 };

 template<typename Iter1, typename Iter2, int N>
 struct compare
     : mpl::and_<
           mpl::equal_to<
               typename mpl::deref<Iter1>::type, 
               typename mpl::deref<Iter2>::type
           >,
           compare<
               typename mpl::next<Iter1>::type,
               typename mpl::next<Iter2>::type,
               N - 1
           >
       >
 {};

 template<typename Iter1, typename Iter2>
 struct compare<Iter1, Iter2, 0> : mpl::true_ {};

 int main()
 {
     typedef mpl::vector_c<int, 0, 1, 2, 3, 4> v;
     typedef mpl::vector_c<int, 3, 1, 4, 0, 2> order;
     typedef mpl::begin<v>::type first;
     typedef permutation_iterator<first, order> p;
     BOOST_STATIC_ASSERT((compare<p, mpl::begin<order>::type, 5>::value));
 }

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