sort_impl.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef BOOST_MPL_AUX_SORT_IMPL_HPP_INCLUDED
  2. #define BOOST_MPL_AUX_SORT_IMPL_HPP_INCLUDED
  3. // Copyright Eric Friedman 2002-2003
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/mpl for documentation.
  10. // $Id$
  11. // $Date$
  12. // $Revision$
  13. #include <boost/mpl/partition.hpp>
  14. #include <boost/mpl/copy.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #include <boost/mpl/back_inserter.hpp>
  17. #include <boost/mpl/front_inserter.hpp>
  18. #include <boost/mpl/iterator_range.hpp>
  19. #include <boost/mpl/joint_view.hpp>
  20. #include <boost/mpl/single_view.hpp>
  21. #include <boost/mpl/begin_end.hpp>
  22. #include <boost/mpl/empty.hpp>
  23. #include <boost/mpl/deref.hpp>
  24. #include <boost/mpl/eval_if.hpp>
  25. #include <boost/mpl/apply.hpp>
  26. #include <boost/mpl/identity.hpp>
  27. #include <boost/mpl/less.hpp>
  28. #include <boost/mpl/aux_/na.hpp>
  29. namespace boost { namespace mpl { namespace aux {
  30. template< typename Seq, typename Pred >
  31. struct quick_sort;
  32. // agurt, 10/nov/04: for the sake of deficeint compilers
  33. template< typename Pred, typename Pivot >
  34. struct quick_sort_pred
  35. {
  36. template< typename T > struct apply
  37. {
  38. typedef typename apply2<Pred,T,Pivot>::type type;
  39. };
  40. };
  41. template<
  42. typename Seq
  43. , typename Pred
  44. >
  45. struct quick_sort_impl
  46. {
  47. typedef typename begin<Seq>::type pivot;
  48. typedef typename partition<
  49. iterator_range<
  50. typename next<pivot>::type
  51. , typename end<Seq>::type
  52. >
  53. , protect< aux::quick_sort_pred< Pred, typename deref<pivot>::type > >
  54. , back_inserter< vector<> >
  55. , back_inserter< vector<> >
  56. >::type partitioned;
  57. typedef typename quick_sort< typename partitioned::first, Pred >::type part1;
  58. typedef typename quick_sort< typename partitioned::second, Pred >::type part2;
  59. typedef joint_view<
  60. joint_view< part1, single_view< typename deref<pivot>::type > >
  61. , part2
  62. > type;
  63. };
  64. template<
  65. typename Seq
  66. , typename Pred
  67. >
  68. struct quick_sort
  69. : eval_if<
  70. empty<Seq>
  71. , identity<Seq>
  72. , quick_sort_impl<Seq,Pred>
  73. >
  74. {
  75. };
  76. template <
  77. typename Sequence
  78. , typename Pred
  79. , typename In
  80. >
  81. struct sort_impl
  82. {
  83. typedef typename quick_sort<
  84. Sequence
  85. , typename if_na<Pred,less<> >::type
  86. >::type result_;
  87. typedef typename copy<result_,In>::type type;
  88. };
  89. template <
  90. typename Sequence
  91. , typename Pred
  92. , typename In
  93. >
  94. struct reverse_sort_impl
  95. {
  96. typedef typename quick_sort<
  97. Sequence
  98. , typename if_na<Pred,less<> >::type
  99. >::type result_;
  100. typedef typename reverse_copy<result_,In>::type type;
  101. };
  102. }}}
  103. #endif // BOOST_MPL_AUX_SORT_IMPL_HPP_INCLUDED